Mega Code Archive

 
Categories / Oracle PLSQL / Table
 

Create as select, then add primary key

SQL> SQL> create table employee(   2        employee_id number,   3        break_reason varchar2(100),   4        break_time interval day(1) to second(2)); Table created. SQL> SQL> insert into employee ( employee_id, break_reason, break_time )   2      values ( 101, 'C',   3               TIMESTAMP '2001-09-03 12:47:00.000000' -   4               TIMESTAMP '2001-09-03 13:13:00.000000' ); 1 row created. SQL> SQL> insert into employee ( employee_id, break_reason, break_time )   2      values ( 102, 'B',   3               TIMESTAMP '2001-09-03 13:35:00.000000' -   4               TIMESTAMP '2001-09-03 13:39:00.000000' ); 1 row created. SQL> SQL> insert into employee ( employee_id, break_reason, break_time )   2      values ( 103, 'P',   3               TIMESTAMP '2001-09-03 16:30:00.000000' -   4               TIMESTAMP '2001-09-03 17:00:00.000000' ); 1 row created. SQL> SQL> insert into employee ( employee_id, break_reason, break_time )   2      values ( 104, 'F',   3               TIMESTAMP '2001-09-03 17:00:00.000000' -   4               TIMESTAMP '2001-09-03 17:30:00.000000' ); 1 row created. SQL> SQL> select * from employee; EMPLOYEE_ID BREAK_REASON                   BREAK_TIME ----------- ------------------------------ ------------------------------         101 C                              -0 00:26:00.00         102 B                              -0 00:04:00.00         103 P                              -0 00:30:00.00         104 F                              -0 00:30:00.00 SQL> SQL> SQL> create table another_dept as select * from employee; Table created. SQL> SQL> alter table another_dept   2  add constraint another_dept_pk   3  primary key(employee_id); Table altered. SQL> SQL> SQL> select * from another_dept; EMPLOYEE_ID BREAK_REASON                   BREAK_TIME ----------- ------------------------------ ------------------------------         101 C                              -0 00:26:00.00         102 B                              -0 00:04:00.00         103 P                              -0 00:30:00.00         104 F                              -0 00:30:00.00 SQL> SQL> drop table another_dept cascade constraints; Table dropped. SQL> drop table employee cascade constraints; Table dropped. SQL>