数据库中有三个完整性: 域、实体、参照完整性
域(列)完整性:
域完整性是对数据表中字段属性的约束 实体完整性在MySQL中实现:
通过主键约束和候选键约束实现的 参照完整性:
也就是说是MySQL的外键
- 通过alter语句添加: alter .... modify/change .... / alter .... add unique
- alter table t3 modify id int unique;
- alter table t3 add unique(name);
- alter table t3 add constraint un_id unique(id);
- 通过alter语句添加: alter .... modify/change .... / alter .... add primary key ....
- alter table t4 modify id int primary key;
- alter table t3 add constraint un_primary primary key(id, name);
员工表(从表) 部门表(主表)
员工号 员工姓名 部门名称 部门号 部门名称
1 张三 1 1 人力
2 李四 2 2 销售
3 王五 3
- create table emp(
empno int promary key auto_increment,
ename varchar(32) not null,
deptno int,
[constraint fk_name] foreign key(deptno) references dept(deptno) -- 添加外键约束);
- create table dept(
deptno int primary key auto_increment,
dname varchar(32),
loc varchar(32) );
- 使用alter add constraint ....
- alter table emp add constraint fk_name foreign key(deptno) references dept (deptno);
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。