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,[email protected])>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([email protected]
insert into dept_test(email) values([email protected])
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYS"."DEPT_TEST"."ENAME")
create table as 復制not null