概念
- 创建表是添加约束条件
- 通过修改来添加约束条件
分类
primary key (主键)
非空且唯一
- 创建表时添加
//方式一:
create table(userid number,
constraint pk_user primary key(userid),
username varchar2(10));
//方式二:
create table(userid number primary key,
username varchar2(10));
- 创建表后添加主键
alter table tablename add constraint pk_user primary key(userid);
- 删除主键约束
alter table student drop constraint pk_user;
alter table student drop primary key;
foreign key (外键)
一个表中非主键字段的值是另外一个表中主键的值
- 创建表时添加
//方式一
create table student(username number constraint fk_user references user(userid));
//方式二
create table student(username number, constraint fk_user foreign key(username) references user(userid));
- 添加约束条件
alter table tablename add constraint fk_user foreign key(usercon) references dept(dept_id(主键));
- 删除约束条件
alter table tablename drop constraint fk_user;
not null(非空)
- 创建字段时添加
alter table student add stu_sex number not null;
- 给已有字段添加
alter table student modify student_id not null;
unique(唯一)
- 创建时添加约束
alter table student add stu_no number unique;
- 给已有字段添加约束
alter table student modify stu_no unique;
注意点:以上都可以在创建表的时候就设置好
check(强制满足条件)
假定在sal列上定义了check约束,并要求sal列值在1000~2000之间,如果不在1000~2000之间就会提示出错。
- 添加字段时添加约束条件
alter table staff add staff_sal number check(staff_sal between a and b);
- 给已有字段添加约束
alter table staff modify staff_sal check(staff_sal between 1000 and 4000);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/117397.html