一、语句分类
DML(数据库操作语言):insert delete update
DDL(数据库定义语言):create drop alter
DCL(数据库控制语言):grant revoke
DQL(数据库查询语言):select
sql语句中是不区分大小写的(linux里面是区分大小写的)
二、操作
1、库操作
show databases; //列出所有的库 create database 库名; //创建库 show create database 库名; //查看所创建库的属性 use 库名; //切换库 drop database 库名; //删除库
2、表操作
show tables; //列出当前库中所有的表 create table 表名 (字段1 属性1,字段2 属性2...); //创建表,至少要有一个字段,多个字段用","隔开 desc 表名; //查看表结构 show create table 表名\G //查看表的创建属性 alter table t1 表名1 to 表名2; //修改[表名1]为[表名2] alter table t1 add 字段 属性; //添加字段在末位 alter table t1 add 字段1 属性 after 字段2; //添加[字段1]在指定[字段2]的后面 alter table t1 add 字段 属性 first; //添加字段在首位 alter table t1 drop 字段; //删除字段 alter table t1 change 字段1 字段2 属性; //修改[字段1]的名称及属性 alter table t1 modify 字段 属性; //修改字段的属性 truncate 表名; //清空表 drop table 表名; //删除表
3、记录操作
insert into 表名 set 字段=值,...; //添加一条记录 insert into 表名 (字段1,...) values (值1,...);//添加多条记录 insert into 表名 values (值1,...); //注意:列数和值数一定要一样 update 表名 set 字段1='值1' where 条件; //修改记录,注意定位 update 表名 set 字段1=值1 where 条件1 and 条件2;//多条件定位 and ↔ && or ↔ || not ↔ ! delete from 表名 where 条件; //删除记录,注意定位
4、单表查询
select * from 表名; //查询表中所有字段的所有记录.如果显示很乱,用\G结尾 select 字段1,字段2,... from 表名; //查询部分字段的所有记录 select 字段1,字段2,... from 表名 where 条件; //查询部分字段的部分记录 select 字段1,字段2,... from 表名 where 字段 between 值1 and 值2; //两边都是闭合 select 字段1,字段2,... from 表名 where 字段3 in('值1','值2'); //用in代替or select * from 表名 order by 字段; //以指定字段值升序排列 select * from 表名 order by 字段 desc; //desc: 逆序 select * from 表名 order by 字段 desc limit 2; //只显示前几条记录 select * from 表名1 where 字段1=(select max(字段1) from 表名1); //子查询 select * from 表名 where 字段 like 't%'; //like通配符 %: 所有 _: 任意单个字符 select * from 表名 where 字段 regexp '^t'; //regexp正则表达式 select 字段1,字段2+字段3+字段4 as 别名 from 表名; //as别名 select count(字段) from 表名; //统计字段有多少记录, null不会被统计 select count(1) from 表名; //统计有多少条记录 select 字段1,count(字段1) from 表名 group by 字段1; //统计指定字段中相同的值的个数 select sum(字段) from 表名; //分数聚合 select avg(字段) from 表名; //平均值 select max(字段) from 表名; //最大值 select min(字段) from 表名; //最小值 select count(字段) from 表名; //统计 select count(1) from 表名 where 字段=100; //统计一百分有几个人 select sex,count(sex) from t1 group by sex; //分类统计 select sex,count(sex) as total from t1 group by sex; //改列名 select name,chinese+math+english as total from t1 order by total desc //总分排序 select name,score from t1 order by score desc limit 3; //分数前三名 select name,english from t1 where english=(select max(english) from t1);//最高分
5、多表联合查询
内连接
select t1.name,t1.sex,t2.math from t1 join t2 on t1.name=t2.name; select a.id,a.name,a.age,a.sex,b.linux,b.shell,b.mysql from stu_info a join stu_score b on a.id=b.stu_id; select t1.name,t1.sex,t2.math from t1,t2 where t1.name=t2.name;
外连接
select t1.name,t1.sex,t2.math from t1 left join t2 on t1.name=t2.name; //左连接 select t2.name,t1.sex,t2.math from t1 right join t2 on t1.name=t2.name; //右连接
三、函数
database() //查看当前所在的库 user() //查看当前登陆的用户 password() //给字符串加密,一般用于修改密码 sum() //显示age字段的和 avg() //求age字段的平均值 max() //最大值 min() //最小值 count() //统计sex字段每种有多少条记录 power() //幂运算 curtime() //查看当前时间 curdate() //查看当前日期 now() sysdate() //查看系统时间 concat() //连接函数 lower() //小写 upper() //大写 length() //长度
四、其他命令
status:查看服务器运行状态。
show processlist:查看连接数。
show variables:查看服务其设置。
set autocommit=0:临时关闭自动事务提交。
可以通过variables查看log、pid、conn。
五、运算符
算术运算符:+ - * / % () power()
比较运算符:> < >= <= = !=
逻辑运算符:and or not
六、数据类型
整型
tinyint : -128~127 0~255(2^8-1)
smallint : -32768~32767 0~65535
mediumint : -8388608~8388607 0~16777215
int : -2147483648~2147483647 0~4294967295
bigint : -9223372036854775808~9223372036854775807 0~18446744073709551615
unsigned 无符号
浮点型
float(M,D) :M是总长度,D小数的位数 存储空间4B
double(M,D) :存储空间8B
字符串
char: 定长字符串 char(10) 占用10个字节 括号中的长度范围1~255
varchar: 变长字符串 varchar(10) 变长字符串需要1个或2个字节存储字符串的长度
注:对于char型,存储时候长度不够,默认会在字符串后面补空格,但是查询时,空格会被脱掉
enum:枚举类型 多个给定值中选择一个
set:集合类型 多个给定值中选者一个或多个
日期类
DATE: 'YYYY-MM-DD'
TIME: 'HH:MM:SS'
DATETIME 'YYYY-MM-DD HH:MM:SS'
TIMESTAMP 'YYYY-MM-DD HH:MM:SS' //时间戳
YEAR 'YYYY'
七、约束
1.默认值:default
2.非空:not null
3.唯一:unique
4.自增:auto_increment (1.该字段必须是数值型,2.字段要唯一性索引或者主键)
总结:
(1).当自增字段发生断档时,值会从最大值继续自增
(2).当delete删除最大值时,下一个值仍然从删除之前的最大值继续自增
(3).当truncate表时,值从1开始重新计算
5.主键:primary key
主键是表中的特殊字段,这个字段能够唯一标识表中的每一条记录。,一张表只能有一个主键,主键的用途:快速定位数据,主键要满足的条件:非空且唯一。
primary key == not null + unique
6.外键:foreign key
一个表的数据依赖另一张表的主键列的数据,如果在主键列没有出现的值,是不能够出现在外键字段的。
创建外键的条件:
1)存储引擎必须是innodb
2)相关联字段数据类型要一致
3)最好在外键列上建索引(目的是为了减少扫描范围,不创建也可以,值是影响性能)
杂七杂八
drop table if exists 表名; //sql文件开头
show variables like ‘%char%’;
show processlist; //查看登录mysql的人数。
从数据库导出sql文件。
mysqldump -u root -p db1 > namebak.sql //导出
mysql -uroot -D qfedu < /root/qfedu.sql //导入