数据库的基本概念:
1、数据库是什么? 数据库本质就是一个C/S的套接字软件 常见的数据库: 关系型: mysql mariadb oracle db2 sqlserver 非关系: 存取数据都是以key:value mongodb redis memcache 2、数据库相关概念 数据库服务器:运行有数据库管理软件的计算机 数据库管理软件mysql:就是一个套接字服务端 库:就是一个文件夹 表:就是一个文件 记录:就相当于文件中的一行内容(抽取事物一系列典型的特征拼到一起,) 数据:用于记录现实世界中的某种状态
数据库安装步骤
#1、下载:MySQL Community Server 5.7.16 http://dev.mysql.com/downloads/mysql/ #2、解压 如果想要让MySQL安装在指定目录,那么就将解压后的文件夹移动到指定目录,如:C:mysql-5.7.16-winx64 #3、添加环境变量 【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【将MySQL的bin目录路径追加到变值值中,用 ; 分割】 #4、初始化 mysqld --initialize-insecure #5、启动MySQL服务 mysqld # 启动MySQL服务 #6、启动MySQL客户端并连接MySQL服务 mysql -u root -p # 连接MySQL服务器 安装 my.ini配置文件 mysqld服务端 client 客户端全局性配置 mysql 客户端
配置文件管理
pass
破解密码
mysql(客户端) mysqld(服务端)
客户端登录
mysql -uroot -p
管理员设置密码相关密码(视频有相关介绍)
破解密码的两种方法
方法一: 暴力破解法 将(E:mysql56datamysql)路径下的所有文件删除,适用于刚刚安装没有多少内容的数据库
net start mysql 正常启动mysql
net stop mysql 把服务端给停掉
quit是退出客户端
方法二:mysqld --skip-grant-tables (跳过授权表)
updata mysql.user set password="123"
但凡权限修改,flush privileges
mysql的相关破解步骤
#1、关闭mysql net stop mysql #2、重新启动 mysqld --skip-grant-tables #3 mysql -uroot -p update mysql.user set password=password("egon123") where user="root" and host="localhost"; flush privileges; #password(代表表当中的一个字段,password()代表mysql的一个功能)
#where是条件的意思userhost="localhost"代表本地账号 #flush privileges 刷新权限
#4、关闭mysql,正常启动 net start mysql
数据类型1的上课相关笔记
数据类型1 mysql> create table t3(x tinyint); Query OK, 0 rows affected (0.02 sec) mysql> desc t3; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | x | tinyint(4) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.02 sec) mysql> show create table t3; +-------+----------------------------------------------------------------------- -----------------+ | Table | Create Table | +-------+----------------------------------------------------------------------- -----------------+ | t3 | CREATE TABLE `t3` ( `x` tinyint(4) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+----------------------------------------------------------------------- -----------------+ 1 row in set (0.00 sec) mysql> insert into t3 values(-1); Query OK, 1 row affected (0.00 sec) mysql> select*from t3; +------+ | x | +------+ | -1 | +------+ 1 row in set (0.00 sec) mysql> insert into t3 values(130); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select*from t3; +------+ | x | +------+ | -1 | | 127 | +------+ 2 rows in set (0.00 sec) mysql> insert into t1 values('128'); ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> insert into t3 values('136'); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select*from t3; +------+ | x | +------+ | -1 | | 127 | | 127 | +------+ 3 rows in set (0.00 sec) mysql> select @@sql_mode; +------------------------+ | @@sql_mode | +------------------------+ | NO_ENGINE_SUBSTITUTION | +------------------------+ 1 row in set (0.00 sec) mysql> set global sql_mode="strict_trans_table"; ERROR 1231 (42000): Variable 'sql_mode' can't be set to the value of 'strict_tra ns_table' mysql> set global sql_mode="srtict_trans_tables"; ERROR 1231 (42000): Variable 'sql_mode' can't be set to the value of 'srtict_tra ns_tables' mysql> set global sql_mode="strict_trans_tables"; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye C:UsersAdministrator>mysql Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2 Server version: 5.6.24 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> select @@sql_mode; +---------------------+ | @@sql_mode | +---------------------+ | STRICT_TRANS_TABLES | +---------------------+ 1 row in set (0.00 sec) mysql> use sb1; ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'sb1' mysql> use db1; ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db1' mysql> use sb1; ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'sb1' mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec) mysql> use sb1; ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'sb1' mysql> mysql -uroot -p; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -uroot -p' at line 1 mysql> mysql -uroot -p; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -uroot -p' at line 1 mysql> mysql -uroot -p -> c; ERROR: No query specified mysql> c mysql> quit Bye C:UsersAdministrator>mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3 Server version: 5.6.24 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> use sb1; Database changed mysql> des t3; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'des t 3' at line 1 mysql> desc t3; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | x | tinyint(4) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.01 sec) mysql> select*from t3; +------+ | x | +------+ | -1 | | 127 | | 127 | +------+ 3 rows in set (0.00 sec) mysql> insert into t3 values(128); ERROR 1264 (22003): Out of range value for column 'x' at row 1 mysql> create table t4(x tinyint unsigned); Query OK, 0 rows affected (0.02 sec) mysql> desc t4; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | x | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 1 row in set (0.04 sec) mysql> insert into t4 values(255); Query OK, 1 row affected (0.00 sec) mysql> insert into t4 values(-1); ERROR 1264 (22003): Out of range value for column 'x' at row 1 mysql> create table t5(id int(1)); Query OK, 0 rows affected (0.03 sec) mysql> desc t5; +-------+--------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------+------+-----+---------+-------+ | id | int(1) | YES | | NULL | | +-------+--------+------+-----+---------+-------+ 1 row in set (0.04 sec) mysql> insert t5 values(300000); Query OK, 1 row affected (0.00 sec) mysql> insert t5 values(9876543210123); ERROR 1264 (22003): Out of range value for column 'id' at row 1 mysql> insert t5 values(9876543210); ERROR 1264 (22003): Out of range value for column 'id' at row 1 mysql> insert t5 values(987654321); Query OK, 1 row affected (0.00 sec) mysql> select*from t5; +-----------+ | id | +-----------+ | 300000 | | 987654321 | +-----------+ 2 rows in set (0.00 sec) mysql> create table t6(id int(5)); Query OK, 0 rows affected (0.02 sec) mysql> insert into t6 values(2147483648); ERROR 1264 (22003): Out of range value for column 'id' at row 1 mysql> select*from t6; Empty set (0.00 sec) mysql> insert into t6 values(2147483647); Query OK, 1 row affected (0.00 sec) mysql> insert into t6 values(13); Query OK, 1 row affected (0.00 sec) mysql> select*from t6; +------------+ | id | +------------+ | 2147483647 | | 13 | +------------+ 2 rows in set (0.00 sec) mysql> drop table t6; Query OK, 0 rows affected (0.02 sec) mysql> create table t6(id int(5) zerofill); Query OK, 0 rows affected (0.01 sec) mysql> insert into t6 values(1); Query OK, 1 row affected (0.00 sec) mysql> insert into t6 values(13); Query OK, 1 row affected (0.00 sec) mysql> select*from t6; +-------+ | id | +-------+ | 00001 | | 00013 | +-------+ 2 rows in set (0.00 sec) mysql> insert into t6 values(13132122); Query OK, 1 row affected (0.00 sec) mysql> select*from t6; +----------+ | id | +----------+ | 00001 | | 00013 | | 13132122 | +----------+ 3 rows in set (0.00 sec) mysql> create table t7(id int); Query OK, 0 rows affected (0.02 sec) mysql> desc t7; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.03 sec) 数据类型2 mysql> create table t8 (x float(255,30)); Query OK, 0 rows affected (0.01 sec) mysql> create table t9 (x double(255,30)); Query OK, 0 rows affected (0.01 sec) mysql> create table t10(x decimal(65,30)); Query OK, 0 rows affected (0.03 sec) mysql> desc t8; +-------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+-------+ | x | float(255,30) | YES | | NULL | | #精度最低 +-------+---------------+------+-----+---------+-------+ 1 row in set (0.03 sec) mysql> desc t9; +-------+----------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------+------+-----+---------+-------+ | x | double(255,30) | YES | | NULL | | #精度第二 +-------+----------------+------+-----+---------+-------+ 1 row in set (0.02 sec) mysql> desc t10; +-------+----------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------+------+-----+---------+-------+ | x | decimal(65,30) | YES | | NULL | | #精度最高 +-------+----------------+------+-----+---------+-------+ 1 row in set (0.01 sec) mysql> insert into t8 values(1.1111111111111111111111111111111); Query OK, 1 row affected (0.00 sec) mysql> insert into t9 values(1.1111111111111111111111111111111); Query OK, 1 row affected (0.00 sec) mysql> insert into t10 values(1.1111111111111111111111111111111); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select*from t8; +----------------------------------+ | x | +----------------------------------+ | 1.111111164093017600000000000000 | +----------------------------------+ 1 row in set (0.00 sec) mysql> select*from t9; +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111200000000000000 | +----------------------------------+ 1 row in set (0.00 sec) mysql> select*from t10; +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111111111111111111 | +----------------------------------+ 1 row in set (0.00 sec) 数据类型3 mysql> create table student( -> id int primary key auto_increment, -> name char(16), -> born_year year, -> birth date, -> class_time time, -> reg_time datetime -> ); Query OK, 0 rows affected (0.02 sec) mysql> desc student; +------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+----------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(16) | YES | | NULL | | | born_year | year(4) | YES | | NULL | | | birth | date | YES | | NULL | | | class_time | time | YES | | NULL | | | reg_time | datetime | YES | | NULL | | +------------+----------+------+-----+---------+----------------+ 6 rows in set (0.03 sec) mysql> create table student( -> id int primary key auto_increment, -> name char(16), -> born_year year, -> birth date, -> class_time time, -> reg_time datetime -> ); ERROR 1050 (42S01): Table 'student' already exists mysql> insert into student(name,born_year,birth,class_time,reg_time) values -> ('egon1',now(),now(),now(),now());#now截取当前的时间 Query OK, 1 row affected, 1 warning (0.00 sec) mysql> mysql> insert into student(name,born_year,birth,class_time,reg_time) values -> ('egon1',2000,20001111,now(),now()); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into student(name,born_year,birth,class_time,reg_time) values -> ('egon1',2000,'2000-11-11',083000,now()); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into student(name,born_year,birth,class_time,reg_time) values -> ('egon1',2000,'2000-11-11',"08:30:00",20171111111111); Query OK, 1 row affected (0.00 sec) mysql> mysql> insert into student(name,born_year,birth,class_time,reg_time) values -> ('egon1',2000,'2000-11-11',"08:30:00","2017-11-11 11:11:11"); Query OK, 1 row affected (0.00 sec) mysql> select*from student; +----+-------+-----------+------------+------------+---------------------+ | id | name | born_year | birth | class_time | reg_time | +----+-------+-----------+------------+------------+---------------------+ | 1 | egon1 | 2018 | 2018-07-24 | 09:35:13 | 2018-07-24 09:35:13 | | 2 | egon1 | 2000 | 2000-11-11 | 09:35:13 | 2018-07-24 09:35:13 | | 3 | egon1 | 2000 | 2000-11-11 | 08:30:00 | 2018-07-24 09:35:13 | | 4 | egon1 | 2000 | 2000-11-11 | 08:30:00 | 2017-11-11 11:11:11 | | 5 | egon1 | 2000 | 2000-11-11 | 08:30:00 | 2017-11-11 11:11:11 | +----+-------+-----------+------------+------------+---------------------+ 5 rows in set (0.00 sec) mysql> create table t11(x timestamp); Query OK, 0 rows affected (0.02 sec) mysql> desc t11; +-------+-----------+------+-----+-------------------+-------------------------- ---+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-------------------------- ---+ | x | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTA MP | +-------+-----------+------+-----+-------------------+-------------------------- ---+ 1 row in set (0.03 sec) mysql> insert into t11 values(); Query OK, 1 row affected (0.00 sec) mysql> select*from t11; +---------------------+ | x | +---------------------+ | 2018-07-24 09:40:14 | +---------------------+ 1 row in set (0.00 sec) mysql> create table t12(x datetime); Query OK, 0 rows affected (0.02 sec) mysql> desc t12; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | x | datetime | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 1 row in set (0.01 sec) mysql> insert into t12 values(); Query OK, 1 row affected (0.00 sec) mysql> select*from t12; +------+ | x | +------+ | NULL | +------+ 1 row in set (0.00 sec) mysql> drop table t12; Query OK, 0 rows affected (0.01 sec) mysql> create table t12(x datetime not null default now()); Query OK, 0 rows affected (0.02 sec) mysql> insert into t12 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into t12 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into t12 values(); Query OK, 1 row affected (0.00 sec) mysql> select*from t; ERROR 1146 (42S02): Table 'sb2.t' doesn't exist mysql> select*from t12; +---------------------+ | x | +---------------------+ | 2018-07-24 09:45:50 | | 2018-07-24 09:45:52 | | 2018-07-24 09:45:53 | +---------------------+ 3 rows in set (0.00 sec)
文件:表 切换文件夹 use db1; select database(); 查看当前所在的库 增 create table t1(id int,name char); #char 就是字符串 mysql称为字符 create table db1.t1(id int,name char); 删 drop table t1; 改 alter table t1 add age int; alter table t1 modify name char(15);#仅只能改数据类型 最大传15个字符 alter table t1 change name NAME char(15);#数据名字和类型都可以改 alter table t1 drop age; 查 show tables; show create table t1; desc t1; 查看表结构
文件内的一行行内容:记录 增 insert into t1(id,name) values (1,'egon'), (2,'lxx'), (3,'alex'); 删 delete from db1.t1 where id >= 2; create table t2(id int primary key auto_incremnt,name char(15)); insert into t2(name) values ('egon'), ('lxx'), ('wxx'), ('axx'); 清空表应该使用: truncate t2; 改 update db1.t1 set name='lxx_dsb' where id=2; 查 select id from db1.t1; select id,name from t1; select name,id from t1; select * from t1; select * from t1 where id >= 2;
SQL语言主要用于存取数据、查询数据、更新数据和管理关系数据库系统,SQL语言由IBM开发。SQL语言分为3种类型: #1、DDL语句 数据库定义语言: 数据库、表、视图、索引、存储过程,例如CREATE DROP ALTER #2、DML语句 数据库操纵语言: 插入数据INSERT、删除数据DELETE、更新数据UPDATE、查询数据SELECT #3、DCL语句 数据库控制语言: 例如控制用户的访问权限GRANT、REVOKE
库相关操作
查看数据库 show databases; show create database db1; select database(); 选择数据库 USE 数据库名 删除数据库 DROP DATABASE 数据库名; 修改数据库 alter database db1 charset utf8;
数据类型
一、创建表的完整语法 #语法: create table 库名.表名( 字段名1 类型[(宽度) 约束条件],#中括号里面的代表可有可无 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); 约束条件:是在数据类型之外对字段附加的额外的限制 #注意: 1、最后一个字段之后不能加逗号 2. 在同一张表中,字段名是不能相同 3. 宽度和约束条件可选,字段名和类型是必须的 二、数据类型 #1、整型:默认是有符号的 create table t3(x tinyint); ps:修改sql_mode为严格模式,必须重启客户端才能生效 set global sql_mode="strict_trans_tables"; select @@sql_mode; create table t4(x tinyint unsigned); # 强调:整型类型后面的宽度限制的根本不是存储宽度,限制的是显示宽度 create table t5(id int(1)); create table t6(id int(5)); #2、浮点型: float(255,30) double(255,30) decimal(65,30) create table t8(x float(255,30)); create table t9(x double(255,30)); create table t10(x decimal(65,30)); insert into t8 values(1.111111111111111111111111111111); insert into t9 values(1.111111111111111111111111111111); insert into t10 values(1.111111111111111111111111111111); 3、日期类型 year 1999 date 1999-11-11 time 08:30:00 datetime/timestamp 1999-11-11 08:30:00 create table student( id int primary key auto_increment, name char(16), born_year year, birth date, class_time time, reg_time datetime ); insert into student(name,born_year,birth,class_time,reg_time) values ('egon1',now(),now(),now(),now());#now截取当前的时间 insert into student(name,born_year,birth,class_time,reg_time) values ('egon1',2000,20001111,now(),now()); insert into student(name,born_year,birth,class_time,reg_time) values ('egon1',2000,'2000-11-11',083000,now()); insert into student(name,born_year,birth,class_time,reg_time) values ('egon1',2000,'2000-11-11',"08:30:00",20171111111111); insert into student(name,born_year,birth,class_time,reg_time) values ('egon1',2000,'2000-11-11',"08:30:00","2017-11-11 11:11:11"); create table t11(x timestamp); create table t12(x datetime not null default now()); 4、字符类型 # 注意:宽度指限制的是字符个数 char:定长 char(5) varchar:变长 varchar(5) 相同点:宽度指的都是最大存储的字符个数,超过了都无法正常存储 不同点: char(5): 'm'--->'m '5个字符 前4个是空格 varchar(5) 'm'--->'m'1个字符 set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH";#填充字符串到最完整的长度 注意:mysql在查询时针对where 字段="值 "会忽略掉右面的空格,即where 字段="值" 如果时like模糊匹配就不会忽略右面的空格了 char(5) egon |axx |lxx |fm | varchar(5) 1bytes+egon|1bytes+axx|1bytes+lxx|1bytes+fm| # 宽度相关练习 mysql> create table t13(x char(5)); Query OK, 0 rows affected (0.20 sec) mysql> create table t14(x varchar(5)); Query OK, 0 rows affected (0.27 sec) mysql> mysql> mysql> insert into t13 values('xxxxxx'); ERROR 1406 (22001): Data too long for column 'x' at row 1 mysql> insert into t14 values('xxxxxx'); ERROR 1406 (22001): Data too long for column 'x' at row 1 5、枚举与集合类型 枚举enum('a','b','c'):多选一 集合set('a','b','c'):多选多 create table emp( name varchar(15), sex enum('male','female','unkown'), hobbies set('read','music','yinshi','play') ); insert into emp values ('zhangming','xxx','xxxx'); mysql> insert into emp values('zhangming','female','read,play'); Query OK, 1 row affected (0.03 sec) mysql> select * from emp; +-----------+--------+-----------+ | name | sex | hobbies | +-----------+--------+-----------+ | zhangming | female | read,play | +-----------+--------+-----------+ 1 row in set (0.00 sec) 三、约束条件
为什么用vachar呢?
大多数情况下用varchar更省空间,但是用varchar不是因为更省空间的问题,因为现在的存储
越来越便宜,是因为数据存在硬盘,查询从硬盘经过IO读到内存里面去的,如果数据量小
就能经过更小的IO将数据读到内存,所以基于这点考虑应高用varchar