Oracle PL/SQL Tutorial/Table/Alter Table
Содержание
- 1 Adding a Column
- 2 ADD initially_created DATE DEFAULT SYSDATE NOT NULL;
- 3 Altering a Table
- 4 alter table nologging
- 5 Alter table to add new constraint for new added column
- 6 Alter table to add primary key
- 7 Alter table to add primary key across more than one columns
- 8 Alter table to add primary key with tablespace and storage setting
- 9 Alter table to add unique with tablespace and storage setting
- 10 ALTER TABLE to DISABLE a CONSTRAINT
- 11 Changing the Data Type of a Column
- 12 Changing the Default Value of a Column
- 13 Changing the Precision of a Numeric Column
- 14 Changing the Size of a Column
- 15 Decrease the length of a column
- 16 Decrease the precision of a numeric column
- 17 Dropping a Column
- 18 Fill data into new added table column
- 19 New added columns are empty
- 20 Some of the aspects of a column you can modify using ALTER TABLE
Adding a Column
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL> ALTER TABLE employee
2 ADD modified_by INTEGER;
Table altered.
SQL>
SQL> desc employee;
Name Null? Type
------------------------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
MODIFIED_BY NUMBER(38)
SQL>
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION MODIFIED_BY
---- ---------- ---------- --------- --------- ---------- ---------- --------------- -----------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
ADD initially_created DATE DEFAULT SYSDATE NOT NULL;
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL> ALTER TABLE employee
2 ADD initially_created DATE DEFAULT SYSDATE NOT NULL;
Table altered.
SQL>
SQL> desc employee;
Name Null? Type
---------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
INITIALLY_CREATED NOT NULL DATE
SQL>
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION INITIALLY
---- ---------- ---------- --------- --------- ---------- ---------- --------------- ---------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer 02-JUN-07
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester 02-JUN-07
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester 02-JUN-07
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager 02-JUN-07
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester 02-JUN-07
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester 02-JUN-07
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager 02-JUN-07
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester 02-JUN-07
8 rows selected.
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
Altering a Table
You alter a table using the ALTER TABLE statement.
You can use ALTER TABLE to perform tasks such as:
- Add, modify, or drop a column
- Add or drop a constraint
- Enable or disable a constraint
6. 3. Alter Table 6. 3. 1. Altering a Table 6. 3. 2. <A href="/Tutorial/Oracle/0120__Table/SomeoftheaspectsofacolumnyoucanmodifyusingALTERTABLE.htm">Some of the aspects of a column you can modify using ALTER TABLE</a> 6. 3. 3. <A href="/Tutorial/Oracle/0120__Table/AddingaColumn.htm">Adding a Column</a> 6. 3. 4. <A href="/Tutorial/Oracle/0120__Table/ADDinitiallycreatedDATEDEFAULTSYSDATENOTNULL.htm">ADD initially_created DATE DEFAULT SYSDATE NOT NULL;</a> 6. 3. 5. <A href="/Tutorial/Oracle/0120__Table/ChangingtheSizeofaColumn.htm">Changing the Size of a Column</a> 6. 3. 6. <A href="/Tutorial/Oracle/0120__Table/Decreasethelengthofacolumn.htm">Decrease the length of a column</a> 6. 3. 7. <A href="/Tutorial/Oracle/0120__Table/ChangingthePrecisionofaNumericColumn.htm">Changing the Precision of a Numeric Column</a> 6. 3. 8. <A href="/Tutorial/Oracle/0120__Table/Decreasetheprecisionofanumericcolumn.htm">Decrease the precision of a numeric column</a> 6. 3. 9. <A href="/Tutorial/Oracle/0120__Table/ChangingtheDataTypeofaColumn.htm">Changing the Data Type of a Column</a> 6. 3. 10. <A href="/Tutorial/Oracle/0120__Table/Filldataintonewaddedtablecolumn.htm">Fill data into new added table column</a> 6. 3. 11. <A href="/Tutorial/Oracle/0120__Table/ChangingtheDefaultValueofaColumn.htm">Changing the Default Value of a Column</a> 6. 3. 12. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddnewconstraintfornewaddedcolumn.htm">Alter table to add new constraint for new added column</a> 6. 3. 13. <A href="/Tutorial/Oracle/0120__Table/ALTERTABLEtoDISABLEaCONSTRAINT.htm">ALTER TABLE to DISABLE a CONSTRAINT</a> 6. 3. 14. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddprimarykey.htm">Alter table to add primary key</a> 6. 3. 15. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddprimarykeyacrossmorethanonecolumns.htm">Alter table to add primary key across more than one columns</a> 6. 3. 16. <A href="/Tutorial/Oracle/0120__Table/DroppingaColumn.htm">Dropping a Column</a> 6. 3. 17. <A href="/Tutorial/Oracle/0120__Table/Newaddedcolumnsareempty.htm">New added columns are empty</a> 6. 3. 18. <A href="/Tutorial/Oracle/0120__Table/Altertabletoadduniquewithtablespaceandstoragesetting.htm">Alter table to add unique with tablespace and storage setting</a> 6. 3. 19. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddprimarykeywithtablespaceandstoragesetting.htm">Alter table to add primary key with tablespace and storage setting</a> 6. 3. 20. <A href="/Tutorial/Oracle/0120__Table/altertablenologging.htm">alter table nologging</a>
alter table nologging
SQL>
SQL>
SQL> create table big_table as select * from all_objects where 1=0;
Table created.
SQL>
SQL> set autotrace on statistics;
SQL>
SQL> insert into big_table select * from all_objects where rownum < 50;
SQL> insert /*+ APPEND */ into big_table select * from all_objects where rownum < 50;
SQL> commit;
Commit complete.
SQL> alter table big_table nologging;
Table altered.
SQL> insert into big_table
2 select * from all_objects where rownum < 50;
49 rows created.
SQL> insert /*+ APPEND */ into big_table
2 select * from all_objects where rownum < 50;
49 rows created.
SQL> commit;
Commit complete.
SQL>
SQL> drop table big_table;
Table dropped.
SQL>
SQL>
Alter table to add new constraint for new added column
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL primary key,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- -------------------- -------------------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
desc employee;
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- -------------------- -------------------- --------- --------- ---------- ---------- ---------------
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL> alter table employee
2 add (
3 gender varchar(10)
4 );
Table altered.
SQL>
SQL> alter table employee
2 add constraint ck_gender
3 check (gender in ("MALE", "FEMALE"));
Table altered.
SQL>
SQL>
SQL>
SQL> desc employee;
Name Null? Type
--------------------------------------------------------------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
GENDER VARCHAR2(10)
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
Alter table to add primary key
SQL>
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL> ALTER TABLE employee
2 ADD PRIMARY KEY (id);
Table altered.
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
Alter table to add primary key across more than one columns
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL>
SQL> ALTER TABLE employee
2 ADD PRIMARY KEY (id,
3 first_name,
4 last_name
5 );
Table altered.
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
Alter table to add primary key with tablespace and storage setting
SQL>
SQL> create table employee_job (
2 id number not null,
3 worker_id number not null,
4 logical_workplace_id number not null,
5 active_date date default SYSDATE not null,
6 inactive_date date )
7 tablespace USERS pctfree 20
8 storage (initial 10K next 10K pctincrease 0);
Table created.
SQL>
SQL>
SQL> alter table employee_job add
2 constraint employee_job_PK
3 primary key ( id )
4 using index
5 tablespace USERS pctfree 20
6 storage (initial 10K next 10K pctincrease 0);
Table altered.
SQL>
SQL> drop table employee_job;
Table dropped.
SQL>
Alter table to add unique with tablespace and storage setting
SQL>
SQL> create table employee_job (
2 id number not null,
3 worker_id number not null,
4 logical_workplace_id number not null,
5 active_date date default SYSDATE not null,
6 inactive_date date )
7 tablespace USERS pctfree 20
8 storage (initial 10K next 10K pctincrease 0);
Table created.
SQL>
SQL>
SQL> alter table employee_job add
2 constraint employee_job_UK
3 unique (
4 worker_id,
5 active_date )
6 using index
7 tablespace USERS pctfree 20
8 storage (initial 10K next 10K pctincrease 0);
Table altered.
SQL>
SQL>
SQL>
SQL> drop table employee_job;
Table dropped.
SQL>
SQL>
ALTER TABLE to DISABLE a CONSTRAINT
SQL>
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL primary key,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL>
SQL> ALTER TABLE employee
2 ADD CONSTRAINT reasonable_stock_date CHECK(
3 start_date >= to_date("19000917","YYYYMMDD")
4 );
Table altered.
SQL>
SQL>
SQL> ALTER TABLE employee DISABLE CONSTRAINT reasonable_stock_date;
Table altered.
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
Changing the Data Type of a Column
If the table is empty or the column contains null values, you can change the column to any data type.
Otherwise you can only change the data type of a column to a compatible data type.
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL>
SQL> desc employee;
Name Null? Type
-----------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> ALTER TABLE employee
2 MODIFY first_Name CHAR(15);
Table altered.
SQL>
SQL> desc employee;
Name Null? Type
------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME CHAR(15)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
Changing the Default Value of a Column
The default value only applies to new rows added to the table.
SQL> CREATE TABLE myTable (
2 id INTEGER,
3 status VARCHAR2(20) DEFAULT "Order placed" NOT NULL,
4 last_modified DATE DEFAULT SYSDATE
5 );
Table created.
SQL>
SQL> desc myTable;
Name Null? Type
-------------------
ID NUMBER(38)
STATUS NOT NULL VARCHAR2(20)
LAST_MODIFIED DATE
SQL>
SQL>
SQL> ALTER TABLE myTable
2 MODIFY last_modified DEFAULT SYSDATE - 1;
Table altered.
SQL>
SQL> desc myTable;
Name Null? Type
--------------------
ID NUMBER(38)
STATUS NOT NULL VARCHAR2(20)
LAST_MODIFIED DATE
SQL>
SQL> drop table myTable;
Table dropped.
SQL>
Changing the Precision of a Numeric Column
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE),
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> desc employee;
Name Type
--------------------
ID VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> ALTER TABLE employee
2 MODIFY salary NUMBER(8,4);
Table altered.
SQL>
SQL> desc employee;
Name Type
--------------
ID VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,4)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
Changing the Size of a Column
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL> ALTER TABLE employee
2 MODIFY first_name VARCHAR2(30);
Table altered.
SQL>
SQL>
SQL> desc employee;
Name Null? Type
----------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(30)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ------------------------------ ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
Decrease the length of a column
You can only decrease the length of a column if there are no rows in the table or all the columns contain null values.
create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
no rows selected
SQL>
SQL>
SQL> desc employee;
Name Null? Type
--------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL>
SQL> ALTER TABLE employee
2 MODIFY first_name VARCHAR2(3);
Table altered.
SQL>
SQL>
SQL> desc employee;
Name Null? Type
-----------------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(3)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> select * from Employee
2 /
no rows selected
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
Decrease the precision of a numeric column
You can only decrease the precision of a numeric column if there are no rows in the table or the column contains null values.
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
no rows selected
SQL>
SQL>
SQL> desc employee;
Name Null? Type
--------------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> ALTER TABLE employee
2 MODIFY salary NUMBER(2,2);
Table altered.
SQL>
SQL> desc employee;
Name Null? Type
------------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(2,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> select * from employee;
no rows selected
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
Dropping a Column
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL> desc employee;
Name Null? Type
-----------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
LAST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> ALTER TABLE employee
2 DROP COLUMN last_name;
SQL>
SQL> desc employee;
Name Null? Type
-----------------
ID NOT NULL VARCHAR2(4)
FIRST_NAME VARCHAR2(10)
START_DATE DATE
END_DATE DATE
SALARY NUMBER(8,2)
CITY VARCHAR2(10)
DESCRIPTION VARCHAR2(15)
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>
Fill data into new added table column
SQL>
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE),
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- -------------------- -------------------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- -------------------- -------------------- --------- --------- ---------- ---------- ---------------
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL> alter table employee
2 add (
3 gender varchar(10)
4 );
Table altered.
SQL>
SQL> alter table employee
2 add constraint ck_gender
3 check (gender in ("MALE", "FEMALE"));
Table altered.
SQL>
SQL>
SQL> update employee
2 set gender = "MALE"
3 where mod(id,2) = 0
4 /
4 rows updated.
SQL>
SQL> select * from employee;
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION GENDER
---- -------------------- -------------------- --------- --------- ---------- ---------- --------------- ----------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester MALE
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager MALE
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester MALE
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION GENDER
---- -------------------- -------------------- --------- --------- ---------- ---------- --------------- ----------
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester MALE
8 rows selected.
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
New added columns are empty
SQL> create table employee(
2 employee_id number(9),
3 first_name varchar2(15),
4 last_name varchar2(20),
5 email varchar2(25),
6 constraint pk_employee primary key (employee_id)
7 );
Table created.
SQL>
SQL> insert into employee
2 values (1, "Tom", "Kyte", "t@aaa.ru");
1 row created.
SQL>
SQL> insert into employee
2 values (2, "Sean", "Dillon", "s@aaa.ru");
1 row created.
SQL>
SQL> insert into employee
2 values (3, "Christopher", "Beck", "c@aaa.ru");
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> select *
2 from employee;
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL
----------- -------------------- -------------------- -------------------------
1 Tom Kyte t@aaa.ru
2 Sean Dillon s@aaa.ru
3 Christopher Beck c@aaa.ru
3 rows selected.
SQL> alter table employee
2 add (
3 phone_number varchar2(10)
4 )
5 /
Table altered.
SQL>
SQL> select *
2 from employee;
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMB
----------- -------------------- -------------------- ------------------------- ----------
1 Tom Kyte t@aaa.ru
2 Sean Dillon s@aaa.ru
3 Christopher Beck c@aaa.ru
3 rows selected.
SQL> drop table employee;
Table dropped.
SQL>
SQL>
Some of the aspects of a column you can modify using ALTER TABLE
The following list shows some of the aspects of a column you can modify using ALTER TABLE:
- Change the size of a sizable column, such as CHAR or VARCHAR2.
- Change the precision of a numeric column.
- Change the data type of a column.
- Change the default value of a column.
6. 3. Alter Table 6. 3. 1. <A href="/Tutorial/Oracle/0120__Table/AlteringaTable.htm">Altering a Table</a> 6. 3. 2. Some of the aspects of a column you can modify using ALTER TABLE 6. 3. 3. <A href="/Tutorial/Oracle/0120__Table/AddingaColumn.htm">Adding a Column</a> 6. 3. 4. <A href="/Tutorial/Oracle/0120__Table/ADDinitiallycreatedDATEDEFAULTSYSDATENOTNULL.htm">ADD initially_created DATE DEFAULT SYSDATE NOT NULL;</a> 6. 3. 5. <A href="/Tutorial/Oracle/0120__Table/ChangingtheSizeofaColumn.htm">Changing the Size of a Column</a> 6. 3. 6. <A href="/Tutorial/Oracle/0120__Table/Decreasethelengthofacolumn.htm">Decrease the length of a column</a> 6. 3. 7. <A href="/Tutorial/Oracle/0120__Table/ChangingthePrecisionofaNumericColumn.htm">Changing the Precision of a Numeric Column</a> 6. 3. 8. <A href="/Tutorial/Oracle/0120__Table/Decreasetheprecisionofanumericcolumn.htm">Decrease the precision of a numeric column</a> 6. 3. 9. <A href="/Tutorial/Oracle/0120__Table/ChangingtheDataTypeofaColumn.htm">Changing the Data Type of a Column</a> 6. 3. 10. <A href="/Tutorial/Oracle/0120__Table/Filldataintonewaddedtablecolumn.htm">Fill data into new added table column</a> 6. 3. 11. <A href="/Tutorial/Oracle/0120__Table/ChangingtheDefaultValueofaColumn.htm">Changing the Default Value of a Column</a> 6. 3. 12. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddnewconstraintfornewaddedcolumn.htm">Alter table to add new constraint for new added column</a> 6. 3. 13. <A href="/Tutorial/Oracle/0120__Table/ALTERTABLEtoDISABLEaCONSTRAINT.htm">ALTER TABLE to DISABLE a CONSTRAINT</a> 6. 3. 14. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddprimarykey.htm">Alter table to add primary key</a> 6. 3. 15. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddprimarykeyacrossmorethanonecolumns.htm">Alter table to add primary key across more than one columns</a> 6. 3. 16. <A href="/Tutorial/Oracle/0120__Table/DroppingaColumn.htm">Dropping a Column</a> 6. 3. 17. <A href="/Tutorial/Oracle/0120__Table/Newaddedcolumnsareempty.htm">New added columns are empty</a> 6. 3. 18. <A href="/Tutorial/Oracle/0120__Table/Altertabletoadduniquewithtablespaceandstoragesetting.htm">Alter table to add unique with tablespace and storage setting</a> 6. 3. 19. <A href="/Tutorial/Oracle/0120__Table/Altertabletoaddprimarykeywithtablespaceandstoragesetting.htm">Alter table to add primary key with tablespace and storage setting</a> 6. 3. 20. <A href="/Tutorial/Oracle/0120__Table/altertablenologging.htm">alter table nologging</a>