1、数值类型
TINYINT --极小整数
SMALLINT --较小整数
MEDIUMINT --中型整数
INT --常规整数
BIGINT --较大整数
FLOAT --小型单精度
DOUBLE --常规双精度
DECIMAL --精确值数值(含整数和小数)
DIB --位字段值
2、字符类型
CHAR --固定长字符
VARCHAR --可变长
TINYTEXT --
TEXT
MEDIUMTEXT
LONGTEXT
ENUM -枚举值
SET --集合
3、时间类型
DATE --YYYY-MM-DD
TIME --hh:mm:ss[.uuuuuu]
DATETIME
TIMESTAMP
YEAR ---YYYY
4、创建数据库
create database school; create schema sch; show charset; show collation; CREATE DATABASE test CHARSET utf8; create database xyz charset utf8mb4_bin;
4.1建库规范:
1.库名不能有大写字母
2.建库要加字符集
3.库名不能有数字开头
4. 库名要和业务相关
4.2建库标准语句:
mysql> create database db charset utf8mb4;
mysql> show create database xuexiao;
4.3删库语句(生产禁用)
mysql> drop database oldboy;
4.4修改字符集
SHOW CREATE DATABASE school;
ALTER DATABASE school CHARSET utf8;
注意:修改字符集,修改后的字符集一定是原字符集的严格超集
4.5查库相关信息
show databases;
show create database oldboy;
5、创建表
USE school; CREATE TABLE stu( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学号', sname VARCHAR(255) NOT NULL COMMENT '姓名', sage TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '年龄', sgender ENUM('m','f','n') NOT NULL DEFAULT 'n' COMMENT '性别' , sfz CHAR(18) NOT NULL UNIQUE COMMENT '身份证', intime TIMESTAMP NOT NULL DEFAULT NOW() COMMENT '入学时间' ) ENGINE=INNODB CHARSET=utf8 COMMENT '学生表';
5.1建表规范
1. 表名小写
2. 不能是数字开头
3. 注意字符集和存储引擎
4. 表名和业务有关
5. 选择合适的数据类型
6. 每个列都要有注释
7. 每个列设置为非空,无法保证非空,用0来填充。
5.2删除表 drop table stu; 5.3修改 在表中增加qq列 desc stu; ALTER TABLE stu ADD qq varchar(20) NOT NULL COMMENT 'QQ号'; 在表中sname列后增加wechat列 ALTER TABLE stu ADD wechat VARCHAR(20) UNIQUE NOT NULL COMMENT '微信' AFTER sname; 在id列前增加新列num ALTER TABLE stu ADD num INT NOT NULL COMMENT '数字' FIRST; 5.4删除列 ALTER TABLE stu DROP qq; ALTER TABLE stu DROP wechat; ALTER TABLE stu DROP num; 修改列类型属性 ALTER TABLE stu MODIFY sname varchar(120) NOT NULL; 修改列名及属性 ALTER TABLE stu CHANGE sgender sg CHAR(1) NOT NULL DEFAULT 'n'; 表属性查询(DQL) use school show tables; desc stu; show create table stu; 5.5创建一样的表ceshi; CREATE TABLE ceshi LIKE stu; 5.6DCL(应用) grant revoke 6、DML应用 作用:对表中的数据行进行增、删、改 6.1 insert ---最标准的inset语句 INSERT INTO stu(id,sname,sage,sg,sfz,intime) VALUES (1,'zhangsan',18,'m','123456',now()); select * from stu; ----省事的写法 INSERT INTO stu VALUES (2,'lisi',19,'f','1234567',now()); ----针对性的录入数据 INSERT INTO stu(sname,sfz) VALUES ('wangwu','12345678'); ---针对性同时录入多行数据 INSERT INTO stu(sname,sfz) VALUES ('zhanglong','112345678'), ('zhaohu','123452678'), ('wangchao','123453678'), ('mahan','123445678'); 6.2 update DESC stu; SELECT * FROM stu; UPDATE stu SET sname='lisier' WHERE id=2; 注意:update语句必须要加where。 6.3 delete 记录删除 DELETE FROM stu WHERE id=3; 全表删除 DELETE FROM stu truncate table stu; 区别: delete: DML操作, 是逻辑性质删除,逐行进行删除,速度慢. truncate: DDL操作,对与表段中的数据页进行清空,速度快. 6.4 --伪删除:用update来替代delete,最终保证业务中查不到(select)即可 1.添加状态列 ALTER TABLE stu ADD state TINYINT NOT NULL DEFAULT 1 ; SELECT * FROM stu; 2. UPDATE 替代 DELETE UPDATE stu SET state=0 WHERE id=6; 3. 业务语句查询 SELECT * FROM stu WHERE state=1;
7、DQL应用(select)
7.1 单独使用 -- select @@xxx 查看系统参数 SELECT @@port; SELECT @@basedir; SELECT @@datadir; SELECT @@socket; SELECT @@server_id; -- select 函数(); SELECT NOW(); SELECT DATABASE(); SELECT USER(); SELECT CONCAT("hello world"); SELECT CONCAT(USER,"@",HOST) FROM mysql.user; SELECT GROUP_CONCAT(USER,"@",HOST) FROM mysql.user; -- select 函数(); SELECT NOW(); SELECT DATABASE(); SELECT USER(); SELECT CONCAT("hello world"); SELECT CONCAT(USER,"@",HOST) FROM mysql.user; SELECT GROUP_CONCAT(USER,"@",HOST) FROM mysql.user; 7.2 单表子句-from SELECT 列1,列2 FROM 表 SELECT * FROM 表 例子: -- 查询stu中所有的数据(不要对大表进行操作) SELECT * FROM stu ; -- 查询stu表中,学生姓名和入学时间 SELECT sname , intime FROM stu; 7.3 where模糊查询 SELECT * FROM stu where SNAME LIKE 'zhang%'; 注意:%不能放在前面,因为不走索引. 7.4 group by ,order by 7.5 having where |group|having SELECT sname,SUM(sage) from stu WHERE sfz like '12%' GROUP BY sname HAVING SUM(sage)>'18'; 7.5 统计中国,每个省的总人口,找出总人口大于500w的,并按总人口从大到小排序,只显示前三名 SELECT district, SUM(population) FROM city WHERE countrycode='CHN' GROUP BY district HAVING SUM(population)>5000000 ORDER BY SUM(population) DESC LIMIT 3 ; LIMIT N ,M --->跳过N,显示一共M行 LIMIT 5,5 SELECT district, SUM(population) FROM city WHERE countrycode='CHN' GROUP BY district HAVING SUM(population)>5000000 ORDER BY SUM(population) DESC LIMIT 5,5; 7.6 DISTINCT :去重复 SELECT countrycode FROM city ; SELECT DISTINCT(countrycode) FROM city ; 7.7 UNION ALL 联合查询 -- 中国或美国城市信息 SELECT * FROM city WHERE countrycode IN ('CHN' ,'USA'); SELECT * FROM city WHERE countrycode='CHN' UNION ALL SELECT * FROM city WHERE countrycode='USA' 说明:一般情况下,我们会将 IN 或者 OR 语句 改写成 UNION ALL,来提高性能 UNION 去重复 UNION ALL 不去重复 8、 information_schema.tables视图 DESC information_schema.TABLES TABLE_SCHEMA ---->库名 TABLE_NAME ---->表名 ENGINE ---->引擎 TABLE_ROWS ---->表的行数 AVG_ROW_LENGTH ---->表中行的平均行(字节) INDEX_LENGTH ---->索引的占用空间大小(字节) 查询整个数据库中所有库和所对应的表信息 SELECT table_schema,GROUP_CONCAT(table_name) FROM information_schema.tables GROUP BY table_schema; 统计所有库下的表个数 SELECT table_schema,COUNT(table_name) FROM information_schema.TABLES GROUP BY table_schema 查询所有innodb引擎的表及所在的库 SELECT table_schema,table_name,ENGINE FROM information_schema.`TABLES` WHERE ENGINE='innodb'; 统计world数据库下每张表的磁盘空间占用 SELECT table_name,CONCAT((TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") AS size_KB FROM information_schema.tables WHERE TABLE_SCHEMA='world'; 统计所有数据库的总的磁盘空间占用 SELECT TABLE_SCHEMA, CONCAT(SUM(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024," KB") AS Total_KB FROM information_schema.tables GROUP BY table_schema; mysql -uroot -p123 -e "SELECT TABLE_SCHEMA,CONCAT(SUM(TABLE_ROWS*AVG_ROW_LENGTH+INDEX_LENGTH)/1024,' KB') AS Total_KB FROM information_schema.tables GROUP BY table_schema;"
生成整个数据库下的所有表的单独备份语句
模板语句:
mysqldump -uroot -p123 world city >/tmp/world_city.sql SELECT CONCAT("mysqldump -uroot -p123 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql" ) FROM information_schema.tables WHERE table_schema NOT IN('information_schema','performance_schema','sys') INTO OUTFILE '/tmp/bak.sh' ; CONCAT("mysqldump -uroot -p123 ",table_schema," ",table_name," >/tmp/",table_schema,"_",table_name,".sql" ) 107张表,都需要执行以下2条语句 ALTER TABLE world.city DISCARD TABLESPACE; ALTER TABLE world.city IMPORT TABLESPACE; SELECT CONCAT("alter table ",table_schema,".",table_name," discard tablespace") FROM information_schema.tables WHERE table_schema='world' INTO OUTFILE '/tmp/dis.sql';
9. show 命令
show databases; #查看所有数据库 show tables; #查看当前库的所有表 SHOW TABLES FROM #查看某个指定库下的表 show create database world #查看建库语句 show create table world.city #查看建表语句 show grants for root@'localhost' #查看用户的权限信息 show charset; #查看字符集 show collation #查看校对规则 show processlist; #查看数据库连接情况 show index from #表的索引情况 show status #数据库状态查看 SHOW STATUS LIKE '%lock%'; #模糊查询数据库某些状态 SHOW VARIABLES #查看所有配置信息 SHOW variables LIKE '%lock%'; #查看部分配置信息 show engines #查看支持的所有的存储引擎 show engine innodb statusG #查看InnoDB引擎相关的状态信息 show binary logs #列举所有的二进制日志 show master status #查看数据库的日志位置信息 show binlog evnets in #查看二进制日志事件 show slave status G #查看从库状态 SHOW RELAYLOG EVENTS #查看从库relaylog事件信息 desc (show colums from city) #查看表的列定义信息
作者:wwwoldguocom
链接:https://www.jianshu.com/p/08c4b78402ff
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。