数据库的备份和还原
约束
主键
默认值
非空
唯一
外键
MySQL
Navicat使用MySQL注释
创建表
插入数据
insert into 表名 values(...),(...)...
insert into student(name) values('老夫子')
直接插入多条数据
insert into students(name) values('老夫子5'),('老夫子6')
数据之间用英文逗号隔开
更新数据
uptate 表名 set 更新的内容(字段名=更新的数据) where 条件
uptate 表名 set 字段名name='火灵邪神',age=25 where id=5;
删除表数据
delete from 表名 where 条件
delete from students where id=6;
查数据
查询表(>,<,=,<>,!=,>=,and,or,in,between,not)
select * from 表名;
模糊查询
模糊查询
开头的话就把%放结尾,需要结尾的话就把%放前面,理解为占位符,替代这个空间的位置。
开头
select * from students where name like '孙%';
结尾
select * from students where name like '%巧';
包含
select * from students where name like '%巧%';
NOT LIKE | Case insensitive exact string inequality comparison 没有用通配符等价于 != | col_name NOT LIKE "ABCD" |
% | Used anywhere in a string to match a sequence of zero or more characters (only with LIKE or NOT LIKE) 通配符,代表匹配0个以上的字符 | col_name LIKE "%AT%" (matches "AT", "ATTIC", "CAT" or even "BATS") "%AT%" 代表AT 前后可以有任意字符 |
_ | Used anywhere in a string to match a single character (only with LIKE or NOT LIKE) 和% 相似,代表1个字符 | col_name LIKE "AN_" (matches "AND", but not "AN") |
IN (…) | String exists in a list 在列表 | col_name IN ("A", "B", "C") |
NOT IN (…) | String does not exist in a list 不在列表 | col_name NOT IN ("D", "E", "F") |
排序数据结果
排序(默认的话就是从小到大,升序)
select * from 表名 order by 字段a;
asc升序(从小到大)
select * from 表名 order by 字段a asc;
desc降序(从大到小)
select * from 表名 order by 字段a desc;
显示数据
(数据量很大的时候)
select * from 表名 limit start,count
显示前三行的数据
select * from students limit 0,3;
显示4到6行的数据
select * from students limit 4,6;
显示前十行的
select * from student limit 10;
数据结果过滤
起别名as
select 别名.字段1,别名.字段2,... from 表名 as 别名
给学生表起别名
select s.name,s.sex,s.age from students as s;
select * from student as s where s.id=100;
一切都是为了最后的那个多表操作铺垫的
MySQL的高级应用
数据库设计
ER模型
常见
sql的高级操作
(多表查询)
外连接
左连接
select * from 表1 left join 表2 on 表1.id=表2.id
右连接
select * from 表1 right join 表2 on 表1.id=表2.id
内连接(这个用的多吧)
select * from 表1 inner join 表2 on 表1.id=表2.id
显示公共部分的数据
sql函数
avg(字段名)
平均值
count(*)
查看数据共多少条
max(字段名)
查看最大值
min(字段名)
查看最小值
sum()
求每一列数值的和
分组操作
Python的flask经常用这些sql
今天突然想到了排序
order by(排序)
1.asc(升序,从小到大)
select * from 表名 order by 字段1 asc;
排序是不需要where,desc是降序,默认的话就是升序也就是asc
select * from 表名 order by 字段1;
2.desc(降序,从大到小)
select * from 表名 order by 字段1 desc;
真的很卑微,一个胖子