The constraints (five kinds (primary key, foreign key, non-empty, the only check)):
Restrictions | Explanation |
---|---|
primary key | The primary key constraint |
foreign key | Foreign key constraint |
not null/ null | Non-empty constraint / null constraint |
check | Check constraints |
unique default 'Default' | The only constraint |
-
primary key
is a table in one or more fields, its value is used to uniquely identify a record in the table
features: unique, not empty, easy to distinguish-
Add constraint to build the table
--注释 constraint 约束名 primary key(约束列) create table student( id number(8,0), name varchar2(20), constraint pk_id primary key(id) ); --或者 create table student( id number(8,0) primary key, --如果此处这样定义主键,则主键名称系统自己定义设置 --id number(8,0) constraint pk_id primary key; name varchar2(20) );
-
After adding constraints to build the table
--alter table 表名 add constraint 主键名 primary key(要设为主键的列名); create table student( id number(8,0), name varchar2(20) ); alter table student add constraint pk_id primary key(id);
-
Drop Primary
--1. alter table 表名 drop constraint 主键名 alter table student drop constrait pk_id; --2. alter table 表名 drop primary key alter table student drop primary key;
-
-
foreign key
to the table is a table join between the other fields, the use of the foreign key is to ensure data integrity.
Note:
1. The need for the primary key of another table;
2. General foreign key name beginning with "fK_";-
Add constraint to build the table
--constraint 外键名(一般外键名称为”fK_”开头) foreign key (要设为外键的列名) references 主表名(主表中该列列名) create table dept( deptid number(8,0) primary key, deptname varchar2(20), constraint fk_deptid foreign key(deptid) references dept(deptid) );
-
After adding constraints to build the table
--alter table 从表名 add constraint 外键名称 foreign key(要设为外键的列名) references 主表名(主表中该列列名); alter table student add constraint fk_deptid foreign key(deptid) references dept(deptid);
-
Remove the foreign key
--alter table "表名" drop constraint "外键名" alter table dept drop constraint fk_deptid;
-
-
not null
-
Add constraint to build the table
create table student( id number(8,0), name varchar2(20) not null, constraint pk_id primary key(id) --注释 constraint 约束名 primary key(约束列) );
-
After adding constraints to build the table
--alter table 表名 modify 列名 not null/null; --如果表中已经存在null,就不能更改其为not null约束 alter table student modify name not null;
-
Delete-null constraint
--alter table 表名 modify 列名 null; --删除非空,就是将其设为空 alter table student modify name null;
-
-
check
-
Add constraint to build the table
--constraint 列名 check(检查约束的条件); create table student( id number(8,0) primary key, name varchar2(20), age number(3,0), constraint name check (age>=15 and age<=25) );
-
After adding constraints to build the table
--alter table 表名 add constraint 列名 check(检查条件); alter table student add constraint age check(age>=15 and age<=25);
-
Delete Check Constraints
--alter table 表名 drop constraint 列名; alter table student drop constraint age;
-
-
unique
-
Add constraint to build the table
--constraint unique_列名 unique(列名) create table student( id number(8,0) primary key, name varchar2(20), age number(3,0), constraint unique_name unique(name) );
-
After adding constraints to build the table
--alter table 表明 add constraint unique_列名 unique(列名); alter table student add constraint unique_name unique(name);
-
Drop the unique constraints
--alter table 表明 drop constraint unique_列名; alter table student drop constraint unique_name;
-