常见的约束类型包括主键约束(PRIMARY KEY)、唯一约束(UNIQUE)、外键约束(FOREIGN KEY)、检查约束(CHECK)和非空约束(NOT NULL)。 3. 如何在ALTER TABLE语句中添加约束 在ALTER TABLE语句中,可以使用ADD CONSTRAINT子句来添加约束。以下是添加不同类型约束的示例:...
4. 删除列:ALTER TABLE table_name DROP COLUMN column_name; 5. 添加主键约束:ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column_name); 6. 添加外键约束:ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES other_table (other_column); 7...
alter table dirk_emp add constraint emp_fk_dept foreign key(deptid) references dirk_dept; -- 可以添加 on delte cascade -- foreign key引用行为 delete cascade/delete set null/ (默认)delete on action -- uk add constraint unique alter table dirk_emp add constraint dirk_emp_num_uk unique(num)...
altertablePersonsaddprimarykey(id_p);altertablePersonsaddconstraintuc_PersonIDprimarykey(id_p, lastname); 3、FOREIGN KEY (两种写法) altertableOrdersaddFOREIGNKEY(Id_P)REFERENCESPerson(Id_P);altertableOrdersaddCONSTRAINTfk_PerOrdersFOREIGNKEY(Id_P)REFERENCESPersons(Id_P); 4、CHECK (两种写法) altert...
主键:ALTER TABLE tablename ADD CONSTRAINT constraint_name PRIMARY KEY(colname)。 外键:ALTER TABLE talename ADD CONSTRAINT constraint_name FOREIGN KEY(colname1) REFERENCES tablename (colname2) 后面跟着: 1.ON DELETE NO ACTION:当删除colname2的时候,colname1不做任何操作 ...
ADD FOREIGN KEY(manager_id) references s_emp(id) 四、删除约束 ALTER TABLE 1、根据约束名来删除表中的指定约束 ALTER TABLE s_emp DROP CONSTRAINT s_emp_manager_id_fk; 2、在删除主键的过程中,通过CASCADE关键字级联删除引用此主键的外键约束
add constraint myemp_empno_fk foreign key(deptno) references dept(deptno);5、删除约束--- alter table myemp drop constraint myemp_ename_nn;优先级:Where > Group BY > Having > Order by select e.empno, e.ename, (e.sal+nvl(e.comm, 0))"总收⼊" from emp e where to_char(e.hiredat...
//0.2列:altertabledeptrenamecolumnloctolocation; altertabledeptrenamecolumnlocationtoloc; //1.添加约束 //1.1primarykey altertableemployee_infoaddconstraintpk_emp_infoprimarykey(empno); //1.2foreignkey altertableemployee_infoaddconstraintfk_emp_infoforeignkey(deptno) referencesdept(deptno); //1.3...
FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] 并且 table_constraint_...