以下操作是在已经创建了表之后,进行的约束条件的增加
1、增加check约束条件
alter table 表名 add constraint 约束条件名称 check(约束条件);
如:
alter table test add constraint ch_salary check(salary>0) ;
2、增加主键约束条件
alter table 表名 add constraint 约束条件名称 primary key (主键列);
如:
alter table test add constraint pk_id primary key (test_id);
3、增加外键约束条件
alter table 表1 add constraint 约束条件名称 foreign key( 表名1外键列) references 表2(表2 的主键列)
如:
alter table employees add constraint fk_dept_id foreign key(department_id) departments (department_id)
4、增加NOT NULL约束条件
ALTER TABLE 表名 ADD CONSTRAINT 约束条件名称 UNIQUE (column,column,...) ;