约束
约束的作用
-
面临的问题
-某列必须有值且唯一
-某列的取值受到另一列取值的限制 -
数据库提供的解决方法
-限制无效的数据进入到表中
-数据库层面的"安检"
约束的类型
约束名 | 意思 |
---|---|
primary key |
主键约束 |
unique key |
唯一键约束 |
not null |
非空约束 |
references foreign key |
外键约束 |
check |
检查约束 |
create table test(
c1 number constraint test_c1_pk primary key ,c2 number
);
insert into test values (1,1);
主键约束(表级约束):不能为空
,唯一
alter table test add constraint test_c3_pk
primary key (c2);
给存在的表增加约束,语法形式跟表级约束一样
非空约束(not null)
- 不允许将该列的值置为空
- 只有列级约束的形式
colname datetype not null
唯一键约束 - 列级约束
create table test (
c1 number(3) constraint c1pk primary key ,
c2 number