1 约束Constraints
强制加在表上的一些规定,为了保证数据的完整性, 准确性,可靠性。
- 实体完整性:记录和记录之间因为是可以区分的,不能完全一样。
- 域完整性:字段范围约束,比如给年龄设置合法范围。
- 引用完整性:跟其他表有引用关系时,对应数据必须存在。
- 自定义约束:比如用户名唯一,密码不能为空等约束
分类
约束名 | 作用 |
primary key | 非空且唯一 |
unique | 唯一键约束 |
foreign key | 外键约束 |
not null | 非空约束 |
check | 检查约束 |
default | 默认值约束 |
auto_increment | 自动增长列 |
约束的使用
- 非空约束
- 创建时添加约束:
create table stu(id int, name varchar(10) not null)
- 创建表后再添加:
alter table stu modify id int not null;
- 唯一键约束
- 创建时添加:
create table stu(id int, name varchar(10) unique);
- 联合唯一键:
create table stu(cid int, sid int, name varchar(10), unique(cid, sid))
- 创建表后再添加唯一键:
alter table stu add unique(name);
- 删除唯一键:
alter table stu drop index name;
- 创建唯一键是通过创建唯一索引来实现的,要删除唯一键就要通过删除索引的方式来删除,唯一键索引的名字默认是字段的名字。
- 主键约束
- 非空且唯一,每个表只能有一个主键,用来保证实体完整性。
- 联合主键:
primary key (id, name);
- 创建表后添加主键:
alter table stu add primary key(id);
- 删除主键:
alter table stu drop primary key;
- 自动增长列auto_increment:一般主键会添加到主键上
- 默认值约束default:一般配合not null使用
- 检查约束check: 指定约束条件
- 约束分类
- 列级约束:
a int check(a>0)
- 表级约束:
c int, check(c>a && c < b)
check(age between 0 and 100);
check(gender in (‘男’, '女'));
- 外键约束foreign key: 保证引用完整性
-
create table dept(did int primary key, dname varchar(10));
主表 -
create table emp(eid int primary key, ename varchar(10), did int , foreign key (did) references dept(did));
从表 - 外键可以为空,要添加时必须添加主表的对应值
- 从表还在使用主表的记录时,主表无法删除该记录
2 多表查询
内连接inner join
写法1:select * from emp, dept;
写法2:select * from emp join dept;
两个表直接将所有行连接,类似与乘法操作。但是没有按照主键和外键进行连接,得到的是两个表的笛卡尔积。
写法3 : select * from emp inner join dept on emp.did = dept.did;
这个是内连接,inner join 的inner可以省略。根据主键和外键的引用关系进行连接。
外连接left join/ right join
写法4: select * from emp left join dept using (did);
根据左表中的主键去关联右边从表中的记录,查询结果会保留左表中的所有记录。内连接的查询结果是主键和从键关联的记录。如果左表中的主键为空,外连接也会保留该记录。
同理,右外连接是拿右表的关联字段去管理左表的记录,会保留所有右表的记录。
自然连接
写法5:select * from emp natural (inner) join dept;
自动在左表和右表中寻找字段名称一致的进行连接。查询结果会将字段名称相同的融合成一列。
查询技巧–自连接
问题:查询自己的经理是谁,可以将表进行自连接操作。
查询:select e2.* from emp e1 join emp e2 on e1.mid = e2.eid where ename = '王冰冰';
合并结果集
使用union可以将两段查询结果合并到一起,要求查询的结果集的字段名称和字段的数量一样。
3 函数
分类:多行函数 和 单行函数
多行函数
分组函数 / 聚合函数:max, min, count, avg,sum
单行函数
- 数值函数:abs, ceil, floor, rand,
- round(12.3, 2) : 四舍五入
-
truncate(12.34,1)
:截断小数,保留1位
字符串函数: length, char_length, concat, - concat_ws(‘#’, 可变参数):使用#连接字符串
- char_length: 字符个数
- left(“hello”, 2): 返回左边2个字符
-
lpad('abc', 5, '#')
: 字符串长度不足使用#填充 - trim(both ‘#’ from ‘###hello###’): 去除#
- strcmp(‘abc’, ‘abc’): 比较字符大小
- 日期函数
- curdate(), curtime(), now():当前日期时间
- year() , month(), day(): 将日期分割为年月日
-
datediff(end, start)
: 返回日期之间的间隔天数
- 流程函数
-
IFNULL(字段名, 默认值)
: 如果是null,就设置默认值 -
IF(语句1, 语句2, 语句3)
: 0、false、null表示假,其余为真,不能使用字符串,字符串默认都为假 - 条件语句case when
case
when salary > 20000 then '大神'
when salary > 15000 then '中神'
when salary > 10000 then '小神'
when salary > 8000 then '新手'
else ‘菜鸟’
end as 等级
- switch语句
case dept_id
when 1 then '大神'
when 2 then '中神'
else ‘菜鸟’
end as 称号
- 窗口函数
select
salary,
did,
row_number () over ww,
rank over ww,
dense_rank() over ww
from
t_employee
window ww as (pation by did order by salary);
用来对数据进行实时处理分析的。
- 其他函数
- database():显式当前数据库
- user():显式当前用户
- md5(str): 摘要算法,类似于加密