-
列约束
(1) 唯一约束——unique
声明了唯一约束的列上,不允许出现重复的值,
(2) 默认值约束——default
可以使用default关键字来设置默认值,具体应用方式有两种
Insert into laptop values(1,‘小米’, default,…);
Insert into laptop(lid,title) values(2,‘小米2’);
没有出现的列会应用默认值
(3) 检查约束
也称为自定义约束,但MySQL不支持,会影响数据插入速度,可能会对服务器造成比较大的压力
Create table student(
Score tinyine check(score>=0 and score<=100)
);
(4) 外键约束
声明了外键约束的列,取值范围到另一个表的主键列中去取,允许插入null。
外键列和对应的主键列两者列类型要保持一致
Foreign key(外键列)references 另一个表的名(主键列)
外键约束通常用来建立两个表之间的关联 -
自增列
Auto_increment 自动增长,如果设置了自增列,再插入数据的时候,只需要设置为null,就会获取当前的最大值然后加1插入
注意:自增列只能添加在整数类型的主键列上,自增列允许手动赋值 -
简单查询
(1) 查询特定的列
示例:查询出所有学员的编号及其姓名
Select eid,ename from emp;
练习 查询所有员工的姓名,性别,生日,工资
select ename,sex,birthday,salary from emp;
(2) 查询所有的列
Select * from emp;
Select eid ,ename,sex,birthday,salary,deptid from emp;
(3) 给列起别名
示例:查询出所有员工的编号和姓名,使用中文别名
Select eid as 编号,eneme as 姓名 from emp;
练习:查询所有员工的姓名生日工资使用中文别名
select ename 姓名,birthday 生日,salary 工资 from emp;
as可用 空格 代替 即可起别名
练习:查询出所有员工的姓名和工资 只用一个字母
select ename e,salary s from emp;
(4) 显示不同的记录
示例:查询出所有员工的性别有哪些
Select distinct sex from emp;
练习:显示出员工分布在哪些部门
Select distinct deptid from emp;
(5) 查询时执行计算
示例:计算2+547.8+361.5=
Select 2+547.8+361.5;
练习:查询所有员工的姓名及其年薪
select ename,salary*12 from emp;
练习:假设每个员工的工资增长1000,年终奖20000,查询所有员工的姓名及其年薪,使用汉字别名
select ename as 姓名,(salary+1000)*12+20000 as 年薪 from emp;
(6) 查询的结果及排序
示例:查询出所有部门的数据,结果集按照部门编号从小到大排列
Select * from dept order by did asc; #asc升序的 ascendant
查询出所有部门的数据,结果集按照部门编号从大到小排列
Select * from dept order by did desc; #desc降序的 descendant
练习:查询所有员工,工资按照降序排列
Select * from emp order by salary desc;
练习:查询出所有员工,结果集按照年龄从大到小排列
Select * from emp order by birthday asc;
练习:查询出所有员工,按照姓名的升序排列
Select * from emp order by ename asc;
练习:查询出所有员工,结果集按照工资的降序排列,如果工资相同按照年龄从大到小排列
Select * from emp order by salary desc,birthday
练习:查询出所有员工,结果集中女员工在前,性别相同按姓名升序
Select * from emp order by sex ,ename
不加排序规则,默认按照升序排列 ASC
(7) 条件查询
示例:查询出编号为5的员工的所有列
Select * from emp where eid=5;
练习:查询出姓名为king的员工的所有列
Select * from emp where ename=‘king‘;
练习:查询出所有的女员工有哪些
select * from emp where sex=1
练习:查询出工资在5000以上的员工有哪些
Select * from emp where salary>5000;
< >= <= = !=
练习:查询不在20号部门的员工
Select * from emp where deptid!=20;
练习:查询出没有明确部门的员工
Select * from emp where deptid is null;
练习:查询出有明确部门的员工
Select * from emp where deptid is not null;
练习:查询工资7000以上的男员工
Select * from emp where salary>7000 and sex=1;
练习:查询工资7000~10000之间的
Select * from emp where salary>=7000 and salary<=10000;
Select * from emp where salary between 7000 and 1000;
between A and B 在A和B之间 包含AB
练习:查询工资7000以下10000以上的员工
Select * from emp where salary<=7000 or salary>=10000;
Select * from emp where salary not between 7000 and 1000;
练习:查询出1993年出生的员工有哪些
Select * from emp where birthday between‘1993-01-01’ and ‘1993-12-31’;
Select * from emp where birthday >=‘1993-01-01’ and birthday<=‘1993-12-31’;
练习:查询出20号部门和30号部门的员工有哪些
Select * from emp where deptid=20 or deptid=30;
Select * from emp where deptid in(20,30);
in(A,B) 满足A 或者B
练习:查询出不在20号部门和30号部门的员工有哪些
Select * from emp where deptid!=20 and deptid!=30;
Select * from emp where deptid not in(20,30);
is null / is not null
and / or
between…and… / not between…and…
in() / not in()
(8) 模糊条件查询
通常用于搜索
示例:查询出姓名中含有o的员工有哪些
select * from emp where ename like ‘%o%’;
练习:查询出姓名中以o结尾的的员工有哪些
select * from emp where ename like ‘%o’;
练习:查询出姓名中第二个字符是o的的员工有哪些
select * from emp where ename like ‘_o%’