0
点赞
收藏
分享

微信扫一扫

MySQL -约束

木匠0819 2022-02-19 阅读 84

约束的概念:对表中的数据进行限定,保证数据的正确性、有效性和完整性。

分类:1.主键约束:primary key
2.非空约束:not null
3.唯一约束:unique
4.外键约束:foreign key

1.主键约束:pirimary key

	注意:
	1.含义:非空且唯一。
	2.一张表只能有一个字段为主键。
	3.主键就是表中记录的唯一标识。
  创建表时,添加主键约束。
create table stu(
    id int primary key ,   -- 给id添加主键
    phone_number varchar(20)
);
 创建表后,添加主键约束
alter table stu modify id int primary key ;
   删除主键
alter table stu drop primary key ;

自动增长:如果某一列是数值类型的,使用auto_increment来实现自动增长。

  删除自动增长
alter table stu modify id int;
  创建表后添加自动增长
alter table stu modify id int auto_increment;

2.非空约束: not null

		1.创建表时添加约束
create  table stu(
    id int,
    name varchar(20) not null  --创建非空约束
);

	   2.创建表后,添加非空约束

```sql
alter table stu modify name varchar(20) not null ;
 删除name的非空约束
 alter table stu modify name varchar(20); 

3.唯一约束 unique

 添加了唯一约束
create table stu(
    id int,
    phone_number varchar(20) unique  
);

 在创建表后,添加唯一约束
alter table stu modify phone_number varchar(20) unique ;
 删除唯一约束
alter table stu drop index phone_number ;

4.外键约束:foreign key:让表与表产生关系,从而保证数据的正确性。
在创建表时,可以添加外键。

		语法:
		create table 表名(
		...
		外键列
		constraint  外键名称  foreign key  (外键列名称)  references  主表名称(主表的列名称)
		);

我们先来创建一个表:

create table emp(
    id int primary key  auto_increment,
    name varchar(20),
    age int,
    dep_name varchar(30), -- 部门名称
    dep_location varchar(30)   -- 部门位置
);
insert into emp(name, age, dep_name, dep_location) values ('张三',20,'研发部','广州');
insert into emp(name, age, dep_name, dep_location) values ('李四',20,'研发部','广州');
insert into emp(name, age, dep_name, dep_location) values ('王五',18,'研发部','广州');
insert into emp(name, age, dep_name, dep_location) values ('赵六',19,'销售部','深圳');
insert into emp(name, age, dep_name, dep_location) values ('陈七',22,'销售部','深圳');
insert into emp(name, age, dep_name, dep_location) values ('周八',20,'销售部','北京');

生成的表:
图1

仔细观看,发现其中6个人的部门有重复数据,有数据冗余现象,修改起来不方便。
接下来做一个表的拆分。拆成两张表:1.部门表(主表) ; 2.员工表(从表)

-- 创建部门表
create table department(
     id int primary key auto_increment,
     dep_name varchar(20),
     dep_location varchar(20)
 );

-- 创建员工表
create table employee(
    id int primary key auto_increment,
    name varchar(20),
    age int,
    dep_id int,  -- 外键对应主表的主键
    constraint emp_dept_fk foreign key (dep_id) references department(id)
);

-- 添加两个部门
insert into department values (null,'研发部','广州'),(null,'销售部','深圳');
select * from department;

-- 添加员工,dep_id 表示员工所在的部门
insert into employee (name,age,dep_id) values ('张三',20,1);
insert into employee (name,age,dep_id) values ('李四',20,1);
insert into employee (name,age,dep_id) values ('王五',18,1);
insert into employee (name,age,dep_id) values ('赵六',19,2);
insert into employee (name,age,dep_id) values ('陈七',22,2);
insert into employee (name,age,dep_id) values ('周八',20,2);

select * from employee;

图1图2在这里图插入图片描述

在这里插入图片描述
此时删除dapartment表中的研发部,由于存在外键约束,所有研发部这一行不能删除。

删除外键约束:alter table 表名 drop foreign key 外键名称;

删除外键约束:
alter table  employee drop foreign key emp_dept_fk; 

创建表后,添加外键:alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名称(主表列名称)

创建表后,添加外键约束
alter table employee add constraint emp_dept_fk foreign key (dep_id) references department(id);
  1. 级联操作:
 添加级联操作:级联更新on update cascade,级联删除on  delete cascade
alter table 表名 add constraint 外键名称 foreign key (外键字段) references  主表名称(主表列名称) on update cascade  on delete cascade;

举报

相关推荐

MySql约束

Mysql -- 约束

Mysql约束

MySQL约束

MySQL 约束

MySQL约束&外键约束

0 条评论