当前位置:Gxlcms > mysql > Oracle中表的建立与修改-五种约束

Oracle中表的建立与修改-五种约束

时间:2021-07-01 10:21:17 帮助过:27人阅读

oracle中包括五种约束,包括主键约束(primary key,该属性的值不能为空,不可重复,默认该列自动建立索引),外键约束(foreign k

表的建立

Oracle中包括五种约束,包括主键约束(primary key,该属性的值不能为空,不可重复,默认该列自动建立索引),外键约束(foreign key() references rname,参照完整性约束,该属性的值必须在所对应的关系中存在),非空约束(not null),唯一约束(unique,不可重复,但可以为空,因为在数据库中,该表达式 null=null 的返回结果为unknown),check约束(check(a in(a1,a2,a3))),,如下例

create table r1
( id numeric(15) primary key, //主键约束
name varchar2(15) not null, //非空约束
gender varchar2(2) check(gender in ('M','F','O')), //模拟枚举约束,Oracle中不支持枚举类型
stu_number unique, //禁止重复,但可以为空
foreign key(stu_number) references r0); //外键约束

表的修改

1.增加一个新的属性

alter table r add id varchar2(10);

2.减少一个已经存在的属性

alter table r drop name;

3.修改一个已经存在的属性的限制

alter table test1 modify grade number(5,2) not null;

4.修改一个已经存在的属性的属性名称

alter table test1 rename column id to stu_number;

linux

人气教程排行