- •Objectivesj i
- •Database Objectsj
- •Namingi Conventionsi
- •The CREATE TABLE Statement
- •Referencingi Another User’s’s
- •The DEFAULT Optioni
- •Queryingi the Data Dictionaryi i
- •Creatingi a Tablele by Usingi a Subquery
- •The ALTER TABLE Statement
- •Addingi a Columnl
- •Addingi a Columnl
- •Modifyingi i a Columnl
- •Droppingi a Tablele
- •Changingi the Name of an Objectj
- •Truncatingi a Tablele
- •Addingi Comments to a Tablele
- •Summary
- •Creatingi a Tablele by Usingi a Subquery
- •Practicei Overviewi
Creatingi a Tablele by Usingi a Subquery
|
|
|
|
|
|
|
|
|
|
SQL> CREATE TABLE |
dept30 |
|
|
|
|
||
|
2 |
|
AS |
|
|
|
|
|
|
3 |
|
SELECT |
empno, ename, sal*12 ANNSAL, |
hiredat |
|||
|
4 |
|
FROM |
emp |
|
|
|
|
|
5 |
|
WHERE |
deptno = 30; |
|
|
|
|
|
Table created. |
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
SQL> DESCRIBE dept30 |
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
Name |
|
Null? |
Type |
|
|
||
|
---------------------------- |
|
|
-------- |
----- |
|
|
|
|
EMPNO |
|
NOT NULL NUMBER(4) |
|
|
|||
|
ENAME |
|
|
VARCHAR2(10) |
|
|
||
|
ANNSAL |
|
|
NUMBER |
|
|
||
|
HIREDATE |
|
|
DATE |
|
|
||
|
|
|
|
|
|
|
|
|
10-12
The ALTER TABLE Statement
Use the ALTER TABLE statement to:
•• Add a new columnl
•• Modifyify an existingi ti columnl
•• Definefi a defaultf lt valuel forf r thet new columnl
ALTER TABLE table
|
ADD |
(column datatype [DEFAULT expr] |
|
|
|
[, column datatype]...); |
|
|
|
|
|
|
|
|
|
|
ALTER TABLE table |
|
|
|
MODIFY |
(column datatype [DEFAULT expr] |
|
|
|
[, column datatype]...); |
|
|
|
|
|
10-13
Addingi a Columnl
DEPT30 |
|
|
New column |
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
EMPNO |
ENAME |
ANNSAL |
HIREDATE |
|
|
|
|
JOB |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
------ |
---------- |
-------- |
|
|
|
|
|
|
|
|
7698 |
BLAKE |
34200 |
01-MAY-81 |
|
|
|
|
|
|
|
7654 |
MARTIN |
15000 |
28-SEP-81 |
|
|
|
|
|
|
|
7499 |
ALLEN |
19200 |
20-FEB-81 |
|
|
|
|
|
|
|
7844 |
TURNER |
18000 |
08-SEP-81 |
|
|
|
|
|
|
|
... |
|
|
|
|
|
|
|
|
|
“…add a new column into DEPT30 table…”
DEPT30 |
|
|
|
|
EMPNO |
ENAME |
ANNSAL |
HIREDATE |
JOB |
------ |
---------- -------- |
|
|
|
7698 |
BLAKE |
34200 |
01-MAY-81 |
|
7654 |
MARTIN |
15000 |
28-SEP-81 |
|
7499 |
ALLEN |
19200 |
20-FEB-81 |
|
7844 |
TURNER |
18000 |
08-SEP-81 |
|
... |
|
|
|
|
10-14
Addingi a Columnl
•• You use thet ADD clausel toto add columnsl ..
|
|
|
|
|
|
SQL> |
ALTER TABLE dept30 |
|
|
|
2 |
ADD |
(job VARCHAR2(9)); |
|
|
Table altered. |
|
|
|
|
|
|
|
|
•• The new columnl becomes thet lastl t columnl ..
EMPNO |
ENAME |
ANNSAL |
HIREDATE JOB |
--------- |
---------- --------- |
--------- ---- |
|
7698 |
BLAKE |
34200 |
01-MAY-81 |
7654 |
MARTIN |
15000 |
28-SEP-81 |
7499 |
ALLEN |
19200 |
20-FEB-81 |
7844 |
TURNER |
18000 |
08-SEP-81 |
... |
|
|
|
6 rows selected.
10-15
Modifyingi i a Columnl
•• You can change a column'sl 's datatype,t t , size,i , and defaultf lt valuel ..
ALTER TABLE |
dept30 |
|
MODIFY |
(ename |
VARCHAR2(15)); |
Table altered.
•• A change toto thet defaultf lt valuel affectsff ts onlyly subsequentt insertionsi rti toto thet tablet le..
10-16
Droppingi a Tablele
•• Allll datata and structuretr t re inin thet tablet le isis deletedl t ..
•• Any pendingi transactionstr ti arere committeditt ..
•• Allll indexesi arere droppedr ..
•• You cannott rollr ll back thist is statementt t t..
SQL> DROP TABLE dept30;
Table dropped.
10-17
Changingi the Name of an Objectj
•• To change thet name off a table,t l , view,i , sequence,, orr synonym,, you executete thet RENAME statementt t t..
SQL> RENAME dept TO department;
Table renamed.
•• You mustt be thet ownerr off thet objectj t..
10-18
Truncatingi a Tablele
•• The TRUNCATE TABLE statementt t t::
–Removes allll rowsr fromfr a tablet le
–Releasesl thet storaget r space used by thatt t tablet le
SQL> TRUNCATE TABLE department;
Table truncated.
•• Cannott rollr ll back rowr removalr l when usingi TRUNCATE
•• Alternatively,lt r ti l , remover rowsr by usingi thet
DELETE statementt t t
10-19
Addingi Comments to a Tablele
•• You can add commentsts toto a tablet le orr columnl by usingi thet COMMENT statementt t t..
SQL> COMMENT ON TABLE emp
2 IS 'Employee Information';
Comment created.
•• Commentsts can be viewedi throught r thet datata dictionaryi ti ry viewsi ..
–ALL_COL_COMMENTS
–USER_COL_COMMENTS
–ALL_TAB_COMMENTS
–USER_TAB_COMMENTS
10-20
Summary
Statement Description
CREATE TABLE Creates a table |
|
ALTER TABLE |
Modifies table structures |
DROP TABLE |
Removes the rows and table structure |
RENAME Changes the name of a table, view, |
|
sequence, or synonym |
|
TRUNCATE |
Removes all rows from a table and |
releases the storage space |
|
COMMENT |
Adds comments to a table or view |
10-21
