Oracle 约束详解

(1).约束的分类
1.主键约束:唯一、非空性
2.唯一约束:可以为空,但只能一个
3.检查约束:对数据的格式、范围等加以限制
4.默认约束:当数据不填时,为默认值
5.外键约束:需要建立两表间关系并引用主表的列
(2).在建表后添加约束
a).主键约束 primary key
alter table 表名 add constraint 主键名 primary key 列名
b).唯一约束 unique
alter table 表名 add constraint 约束名 unique(列名)
c).默认约束 default
alter table 表名 add constraint 约束名 default(值) for 列名
alter table stuInfo add constraint DF_stuAddress default (‘地址不详’) for stuAddress
d).检查约束 check
alter table 表名 add constraint 约束名 check(条件表达式)
alter table student add constraint chk_age check(age BETWEEN 1 and 100)
e).外键约束 foreign key
alter table 表名 add constraint 约束名 foreign key(列名) references 表名(列名)
(3).建表时添加约束

 create table stuInfo(
    			id NUMBER(10) primary key,	--主键
    			--stuId NUMBER(10) references study(stuId), --外键
    			name VARCHAR2(8) NOT NULL,	--姓名,非空
    			sex char(4) CHECK(sex='男' or sex='女'),	--检查约束1
    			identityId NUMBER(18) UNIQUE,	--唯一约束
    			age NUMBER(3) CHECK(age>=18 AND age<=22),--检查约束2
    			address VARCHAR2(30) DEFAULT '地址不详'	--默认值约束
    		)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值