首页 > 代码库 > create table as 复制not null

create table as 复制not null

创建一张表包含非空约束,唯一约束和check约束

SQL> create table dept(
2 ename varchar2(20) constraint mep_ename_nn not null,
3 email varchar2(30) constraint mep_email_uk unique,
4 constraint emp_email_ck check ((instr(email,‘@‘)>0)));

Table created.

 

查看约束情况:
SQL> select constraint_name,constraint_type,column_name
2 from user_constraints natural join user_cons_columns
3 where table_name=‘DEPT‘;

CONSTRAINT_NAME C COLUMN_NAME
------------------------------ - ---------------
MEP_ENAME_NN       C ENAME
EMP_EMAIL_CK         C EMAIL
MEP_EMAIL_UK         U EMAIL

 

使用create table as语句创建另外一张表:

SQL> create table dept_test as select * from dept;

Table created.

 

检查约束情况,只有ename上的nut null约束被复制了过来

SQL> select constraint_name,constraint_type,column_name
2 from user_constraints natural join user_cons_columns
3 where table_name=‘DEPT_TEST‘;

CONSTRAINT_NAME C COLUMN_NAME
------------------------------ - ---------------
SYS_C0011591           C ENAME

 

尝试插入数据报错

SQL> insert into dept_test(email) values(‘cc@‘);
insert into dept_test(email) values(‘cc@‘)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYS"."DEPT_TEST"."ENAME")

create table as 复制not null