Mega Code Archive

 
Categories / Oracle PLSQL / Constraints
 

Add primary key and try to insert default value to it

SQL> SQL> CREATE TABLE DEPT (DEPTNO NUMBER(2),DNAME VARCHAR2(14),LOC VARCHAR2(13) ); Table created. SQL> SQL> INSERT INTO DEPT VALUES (10, 'ACCOUNTING', 'NEW YORK'); 1 row created. SQL> INSERT INTO DEPT VALUES (20, 'RESEARCH', 'DALLAS'); 1 row created. SQL> INSERT INTO DEPT VALUES (30, 'SALES', 'CHICAGO'); 1 row created. SQL> INSERT INTO DEPT VALUES (40, 'OPERATIONS', 'BOSTON'); 1 row created. SQL> SQL> SQL> create table another_dept as select * from dept; Table created. SQL> SQL> alter table another_dept add constraint another_dept_pk primary key( deptno ); Table altered. SQL> SQL> insert into another_dept  values( 40, 'OPERATIONS', 'BOSTON' ); insert into another_dept  values( 40, 'OPERATIONS', 'BOSTON' ) * ERROR at line 1: ORA-00001: unique constraint (SYS.ANOTHER_DEPT_PK) violated SQL> insert into another_dept (loc)values( 'RESTON' ); insert into another_dept (loc)values( 'RESTON' ) * ERROR at line 1: ORA-01400: cannot insert NULL into ("SYS"."ANOTHER_DEPT"."DEPTNO") SQL> SQL> drop table another_dept; Table dropped. SQL> drop table dept; Table dropped. SQL>