MySQL学习笔记--day5

Day5

约束(非常重要)

什么是约束:constraint

在创建表的时候,我们可以给表中的字段加上一些约束,来保证表中的数据的完整性有效性

常见的约束
  • 非空约束 not null
  • 唯一性约束 unique
  • 主键约束 primary key (简称PK)
  • 外键约束 foreign key (简称FK)
  • 检查约束 cheack (mysql不支持,oracle支持)
非空约束:not null

非空约束 not null 约束的字段不能为 NULL

not null只允许列级约束,不允许表级约束

建立一个表

drop table if exists t_vip;
create table t_vip (
	id int ,
	name varchar(10) not null
);
//插入值
insert into t_vip(id,name) values(1,'闫'),(2,'李');
insert into t_vip(id) value (3);
ERROR 1364 (HY000): Field 'name' doesn't have a default value
//没写时的报错
唯一性约束:unique

约束的字段不可以重复,但可以为null

drop table if exists t_vip;
create table t_vip (
	id int unique,
	name varchar(10) not null
);
//插入值
insert into t_vip(id,name) values(1,'闫'),(2,'李');
insert into t_vip(id,name) values(1,'刘');
ERROR 1062 (23000): Duplicate entry '1' for key 't_vip.id'

两个字段联合起来唯一:

表级约束:多个字段同时约束

drop table if exists t_vip;
create table t_vip (
	id int unique,
	name varchar(10),
	email varchar(255),
	unique(name,email)
);
insert into t_vip(id,name,email) values(1,'闫','yan@11.com'),(2,'刘','liu@11.com');
insert into t_vip(id,name,email) values(3,'liu','liu2@11.com');
insert into t_vip(id,name,email) values(4,'liu','liu3@11.com');

mysql> select * from t_vip;
+------+------+-------------+
| id   | name | email       |
+------+------+-------------+
|    1 | 闫   | yan@11.com  |
|    2 | 刘   | liu@11.com  |
|    3 | liu  | liu2@11.com |
|    4 | liu  | liu3@11.com |
+------+------+-------------+
4 rows in set (0.00 sec)

mysql> insert into t_vip(id,name,email) values(5,'liu','liu3@11.com'); 
ERROR 1062 (23000): Duplicate entry 'liu-liu3@11.com' for key 't_vip.name'

在MySQL中 一个字段同时被 not null 和 unique 约束时,则自动变为主键约束

主键约束(primary key)
主键约束的相关术语:
  • 主键约束:就是一种约束。
  • 主键字段
  • 主键值:主键字段中的每一个值,

什么是主键?有什么用?

主键值是每一行记录的唯一标识

主键值是每一行记录的身份证号

如何一张表都应该有主键,没有主 键表无效

主键的特征

not null + unique (主键值不能为null,也不能重复)

主键约束只能添加一个

主键值建议使用:

  • int
  • bigint
  • char

等类型,不建议使用varchar,主键一般都是数字,是定长的

添加主键约束
drop table if exists t_vip;
create table t_vip(
	id int primary key,
	name varchar(255) not null
);
insert into t_vip(id,name) values(1,'yan');
insert into t_vip(id,name) values(2,'liu');

insert into t_vip(id,name) values(2,'aaa');
ERROR 1062 (23000): Duplicate entry '2' for key 't_vip.PRIMARY'
复合主键

两个或多个字段 联合表级约束 做主键 叫做复合主键。

但实际开发中不建议使用,复合主键比较复杂

外键约束 foreign key
相关术语
  • 外键约束
  • 外键字段
  • 外键值
案例:

业务背景:

​ 请设计数据库表,来描述 “班级和学生”的信息?

第一种方案:班级和学生存储在一张表中 . 会导致数据冗余,浪费空间

**第二种方案:**班级表和学生表分开存

t_stu是子表,t_class是父表

删表先删子表,

创表先创父表,

插入先插入父表

删除数据先删子表

drop table if exists t_stu;
drop table if exists t_class;

create table t_class(
cno int primary key,
cname varchar(255)
);
create table t_stu(
	sno int primary key auto_increment,
    name varchar(255),
    cno int,
    foreign key(cno) references t_class(cno)
);

insert into t_class(cno ,cname) values(1,'2034'),(2,'2035'),(3,'2036');
insert into t_stu(name,cno) values('闫',1),('王',2),('暴',3);

mysql> select * from t_stu;   
+-----+------+------+
| sno | name | cno  |
+-----+------+------+
|   1 | 闫   |    1 |
|   2 | 王   |    2 |
|   3 | 暴   |    3 |
+-----+------+------+

mysql> select * from t_class;
+-----+-------+
| cno | cname |
+-----+-------+
|   1 | 2034  |
|   2 | 2035  |
|   3 | 2036  |
+-----+-------+


//外键不匹配时报错
insert into t_stu(name,cno) values('wang',4);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`running_potato`.`t_stu`, CONSTRAINT `t_stu_ibfk_1` FOREIGN KEY (`cno`) REFERENCES `t_class` (`cno`))
思考:

Q:外键引用的父表中的某个字段,必须都是主键吗?

A:不一定是主键,但必须具有unique约束,唯一

Q:外键可以为空吗?

A:可以为空 null

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值