一、约束:
主键约束(key primary):每个表只设置一个主键,设置后:值非空且唯一。主键约束可设置主键自增;(auto_increment),设置后不输入值时、或者输入0或者输入null时将直接引起主键自增(即主键自己按顺序增长);
唯一约束(unique):设置后值唯一,不可重复;
非空约束(not null):设置后值非空,必须有;
外键约束【construct [约束名] foreign key (字段名) references 主表(主键名)】:用于关联两个表,关联表叫子表,被关联表叫主表,此处只简单介绍,后面分享多表查询时会详细介绍(下面代码里也还未涉及);
默认约束(default):设置默认值,不输入值时默认;
注意:1.约束建议都是在建表时添加,简单方便;2.主键约束每个表只有一个,其它约束可以有多个。
二、单表查询
基础查询:select */字段名 from 表名
条件查询:where (比较/逻辑/范围/模糊/非空判断)
分组/聚合/统计函数:sum()、avg()、count()、max()、min()
分组查询:select 分组字段名,聚合函数(字段名)from 表名 group by 分组字段名1,分组字段名2。。。
排序查询:select 字段名 from 表名 order by 排序字段名1 (asc:升序;desc:降序),排序字段名2(asc:升序;desc:降序)。。。
Limit查询:select 字段名 from 表名 limit x,y(x为起始索引,y为一次查询数量)。
注意:
1.书写顺序:select-distinct-聚合函数-from-where-group by-having-order by-limit x,y;
特别地,Having也能跟聚合条件,但不建议,会造成效率低下;
2.执行顺序:from(从硬盘中扫描真实表文件加载到内存形成伪表)—where—group by(分组,切成运算区)—聚合函数—having—select distinct—order by—limit
3.delete from 与 truncate的区别:前者删除数据会重置主键自增,truncate不会。
例:
create database database1;#创建database1(数据库名)
use database1; #使用数据库
create table student( #创建student(表名)
id int primary key auto_increment, #主键自增
name varchar(100) not null, #非空约束
weight double unique, #唯一约束
height double default 1.9 #默认约束
);
show databases; #查看目前所有的数据库
show tables; #查看目前所有的表
desc student; # 查看表结构(desc 表名)
#插入数据
insert into student values(1,'john',50.6,1.78),
(2,'july',51,1.67),(3,'huahua',53.7,1.89);
#改数据
update student set name='alice' where name='huahua'; 3]
update student set name='alice' where id=3;
#删除了单列数据
delete from student where id=3;
delete from student where name='july';
#删除了所有数据
delete from student;
#另一种删除所有数据方法:truncate
truncate student;
#删除表
drop table student;
#再次插入数据
insert into student values(9,'john',50,1.78),(null,'july',55,1.78),(0,'huahua',53.8,1.89);
#height默认值已设置,不输入值时会默认1.9
insert into student(id, name, weight) values(4,'john',59);
#单表查询
#(1)单表查询之基础查询
select * from student;
select distinct * from student;
select distinct name as n,weight as w from student;#as可省略;
#(2)单表查询之条件查询
select * from student where name like 'j%';
select * from student where name like '%j%';#查询name带有j的
select * from student where name like 'ju__';
select * from student where weight <>51 and weight <>55;#查询weight不为51也不为55的数据
select * from student where weight not in(51,55);#查询weight不为51也不为55的数据
select * from student where weight!=51 and weight!=55;#查询weight不为51也不为55的数据
select * from student where weight not between 51 and 55;#查询weight不在51和55之间的数据
select * from student where weight is not null;#查询有weight的数据
select count(*) from student where weight is not null;#个数查询,推荐
#个数查询,利用其自动忽略none值,推荐
select count(weight) from student;
#(3)聚合函数查询
select count(weight) c,max(weight) ma,min(weight) mi,
round(avg(weight),2) av,sum(weight) s from student;
#(4)分组查询(分组字段名height必须跟在select后面,可多个分组字段)
select height,count(*) from student group by height ;
#(5)排序查询(可多个字段排序)
select height,name from student order by weight,height ;
#(6)limit 查询
select height,name from student limit 0,2;
备注:被查询的字段名都可以通过as 起别名(as也可省略,字段名后面可以直接跟别名),但应避免是中文名。
今天的分享到此为止,欢迎多交流沟通,共同进步!