文章目录
1. 约束(not null、unique、primary key、 foreign key)
非空约束(not null):约束的字段不能为NULL
唯一约束(unique):约束的字段不能重复,但可以为NULL
主键约束(primary key):约束的字段既不能为NULL,也不能重复(简称PK)
外键约束(foreign key):…可以为NULL(简称FK)
检查约束(check):注意Oracle数据库有check约束,但是mysql没有,目前mysql不支持该约束。
1.1 非空约束 (not null)
not null约束只有列级约束
- drop table if exists t_user;
create table t_user(
id int,
username varchar(255) not null,
password varchar(255)
);
insert into t_user(id,password) values(1,‘123’); // 报错,username不能为空
insert into t_user(id,username,password) values(1,‘lisi’,‘123’);
1.2 唯一约束(unique)
约束的字段不能重复,但可以为NULL;
有列级约束和表级约束。
- 案例:给某一列添加unique
drop table if exists t_user;
create table t_user(
id int,
username varchar(255) unique // 列级约束
);
insert into t_user values(1,‘zhangsan’);
insert into t_user values(2,'zhangsan’); // 报错,username不能重复
insert into t_user(id) values(2);
insert into t_user(id) values(3); // username可以为NULL
- 案例:给两个列或者多个列添加unique
drop table if exists t_user;
create table t_user(
id int,
usercode varchar(255),
username varchar(255),
unique(usercode,username) // 多个字段联合起来添加1个约束unique ,当2个都相同时,才报错。【表级约束】
);
insert into t_user values(1,‘111’,‘zs’);
insert into t_user values(4,‘111’,‘zs’); // 报错, usercode,username2个都相同
1.3 主键约束(primary key)
约束的字段既不能为NULL,也不能重复;
一张表的主键约束只能有1个。(必须记住);
有列级约束和表级约束。
-
drop table if exists t_user;
create table t_user(
id int primary key, // 列级约束
username varchar(255),
email varchar(255) ); -
使用表级约束方式定义主键:
drop table if exists t_user;
create table t_user(
id int,
username varchar(255),
primary key(id)
); -
mysql提供主键值自增:(非常重要。)
drop table if exists t_user;
create table t_user(
id int primary key auto_increment, // id字段自动维护一个自增的数字,从1开始,以1递增。
username varchar(255)
);
//id自增开始
insert into t_user(username) values(‘a’);
insert into t_user(username) values(‘b’);
insert into t_user(username) values(‘c’);
1.4 外键约束(foreign key)
外键可以为NULL;
被引用的字段不一定是主键,但至少具有unique约束。
-
删除数据的时候,先删除子表,再删除父表。
添加数据的时候,先添加父表,在添加子表。
创建表的时候,先创建父表,再创建子表。
删除表的时候,先删除子表,在删除父表。
drop table if exists t_student;
drop table if exists t_class; -
create table t_class(
cno int,
cname varchar(255),
primary key(cno)
);create table t_student(
sno int,
sname varchar(255),
classno int,
primary key(sno),
foreign key(classno) references t_class(cno)
);
2. 事务(TCL)
commit提交事务,rollback回滚事务
四种特性之隔离性:
-
第一级别:读未提交(read uncommitted)
set global transaction isolation level read uncommitted;设置第一级别
select @@global.tx_isolation; 查看级别
对方事务还没有提交,我们当前事务可以读取到对方未提交的数据。
读未提交存在脏读(Dirty Read)现象:表示读到了脏的数。 -
第二级别:读已提交(read committed)
set global transaction isolation level read committed;第二
select @@global.tx_isolation; 查看级别
对方事务提交之后的数据我方可以读取到。
这种隔离级别解决了: 脏读现象没有了。
读已提交存在的问题是:不可重复读。 -
第三级别:可重复读(repeatable read)
set global transaction isolation level repeatable read;第三
这种隔离级别解决了:不可重复读问题。
这种隔离级别存在的问题是:读取到的数据是幻象。 -
第四级别:序列化读/串行化读(serializable)
解决了所有问题。
效率低。需要事务排队。
start transaction; 关闭自动提交事务
3. 索引(原理面试问)
主键和具有unique约束的字段自动会添加索引。
创建索引对象:
create index 索引名称 on 表名(字段名);
删除索引对象:
drop index 索引名称 on 表名;
- create index emp_sal_index on emp(sal); // 给薪资sal字段添加索引
- explain select ename,sal from emp where sal = 5000; // 查看sql语句的执行计划
模糊查询的时候,第一个通配符使用的是%,这个时候索引是失效的
select ename from emp where ename like ‘%A%’;
4. 视图(view)
怎么创建视图?怎么删除视图?(同创建表、删除表一样,table/view )
create view myview as select * from emp;
create view myview as select empno,ename from emp;
drop view myview;
注意:只有DQL语句才能以视图对象的方式创建出来。
- update myview1 set ename=‘hehe’,sal=1 where empno = 7369; // 通过视图修改原表数据。
- delete from myview1 where empno = 7369; // 通过视图删除原表数据。
5. 范式(重点内容,面试经常问)
第一范式:任何一张表都应该有主键,并且每一个字段原子性不可再分。
第二范式:建立在第一范式的基础之上,所有非主键字段完全依赖主键,不能产生部分依赖。
多对多?三张表,关系表两个外键。
t_student学生表
sno(pk) sname
-------------------
1 张三
2 李四
3 王五
t_teacher 讲师表
tno(pk) tname
---------------------
1 王老师
2 张老师
3 李老师
t_student_teacher_relation 学生讲师关系表
id(pk) sno(fk) tno(fk)
----------------------------------
1 1 3
2 1 1
3 2 2
4 2 3
5 3 1
6 3 3
第三范式:建立在第二范式的基础之上,所有非主键字段直接依赖主键,不能产生传递依赖。
一对多?两张表,多的表加外键。
班级t_class
cno(pk) cname
--------------------------
1 班级1
2 班级2
学生t_student
sno(pk) sname classno(fk)
---------------------------------------------
101 张1 1
102 张2 1
103 张3 2
104 张4 2
105 张5 2
一对一设计有两种方案:主键共享、外键唯一
主键共享:
t_user_login 用户登录表
id(pk) username password
--------------------------------------
1 zs 123
2 ls 456
t_user_detail 用户详细信息表
id(pk+fk) realname tel ....
------------------------------------------------
1 张三 1111111111
2 李四 1111415621
外键唯一:
t_user_login 用户登录表
id(pk) username password
--------------------------------------
1 zs 123
2 ls 456
t_user_detail 用户详细信息表
id(pk) realname tel userid(fk+unique)....
-----------------------------------------------------------
1 张三 1111111111 2
2 李四 1111415621 1