
- •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
- •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
- •Practicei Overviewi

10
Creating and Managing
Tables

Objectivesj i
After completing this lesson, you should be able to do the following:
•• Describeri thet mainin databaset objectsj ts
•• Creater te tablest l
•• Describeri thet datatypest t thatt t can be used when specifyingif i columnl definitionfi iti
•• Alterlt r tablet le definitionsfi iti
•• Drop,r , rename,r , and truncatetr te tablest l
10-2

Database Objectsj
Object |
Description |
|
|
|
|
Table |
Basic unit of storage; composed of rows |
|
and columns |
|
|
View |
Logically represents subsets of data from |
|
one or more tables |
Sequence |
Generates primary key values |
Index |
Improves the performance of some queries |
Synonym |
Gives alternative names to objects |
|
|
10-3

Namingi Conventionsi
•• Mustt beginin withith a letterl tt r
•• Can be 1–30 charactersr t rs longl
•• Mustt containt in onlyly A–Z,, a–z,, 0–9,, _,, $,, and #
•• Mustt nott duplicateli te thet name off anothert r objectj t owned by thet same userr
•• Mustt nott be an Oracler le Serverr r reservedr r wordrd
10-4

The CREATE TABLE Statement
•• You mustt have ::
–CREATE TABLE privilegeri il
–A storaget r arear
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]);
•• You specifyify::
–Tablele name
–Columnl name,, columnl datatype,t t , and columnl sizei
10-5

Referencingi Another User’s’s
Tablesl
•• Tablesl belongingl i toto othert r usersrs arere nott inin thet user’sr’s schema..
•• You shouldld use thet owner’sr’s name as a prefixr fix toto thet tablet le..
10-6

The DEFAULT Optioni
•• Specifyify a defaultf lt valuel forf r a columnl duringri an inserti rt..
… hiredate DATE DEFAULT SYSDATE, …
•• Legall valuesl arere literallit r l value,l , expression,r i , orr SQL functionf ti ..
•• IllegalIll l valuesl arere anothert r column’sl ’s name orr pseudocolumnl ..
•• The defaultf lt datatypet t mustt matcht thet columnl datatypet t ..
10-7

|
Creatingi Tablesl |
|
•• Create the table. |
||
r |
te t t |
le. |
SQL> CREATE TABLE dept |
||
2 |
(deptno |
NUMBER(2), |
3 |
dname |
VARCHAR2(14), |
4 |
loc |
VARCHAR2(13)); |
Table created.
|
•• Confirm table creation. |
|
|
|
|
firm t le r |
ti . |
|
|
|
SQL> DESCRIBE dept |
|
|
|
|
|
|
|
|
|
Name |
Null? |
Type |
|
|
--------------------------- |
-------- |
--------- |
|
|
DEPTNO |
|
NUMBER(2) |
|
|
DNAME |
|
VARCHAR2(14) |
|
|
LOC |
|
VARCHAR2(13) |
|
|
|
|
|
|
10-8

Queryingi the Data Dictionaryi i
•• Describeri tablest l owned by thet userr..
SQL> SELECT * |
|
|
2 |
FROM |
user_tables; |
•• Viewi distincti ti t objectj t typest owned by thet userr..
|
|
|
|
|
|
SQL> SELECT |
DISTINCT object_type |
|
|
|
2 |
FROM |
user_objects; |
|
|
|
|
|
|
•• Viewi tables,t l , views,i , synonyms,, and sequences owned by thet userr..
SQL> SELECT *
2FROM user_catalog;
10-9

|
|
|
Datatypes |
|
|
|
|
|
|
|
Datatype |
Description |
|
|
|
|
|
||
|
VARCHAR2(size)Variable-length character data |
|
||
|
CHAR(size) |
Fixed-length character data |
|
|
|
NUMBER(p,s) Variable-length numeric data |
|
||
|
DATE |
Date and time values |
|
|
|
LONG |
Variable-length character data |
|
|
|
|
up to 2 gigabytes |
|
|
|
CLOB |
Single-byte character data up to 4 |
gigabytes |
|
|
RAW and LONG RAW Raw binary data |
|
||
|
BLOB |
Binary data up to 4 gigabytes |
|
|
|
BFILE |
Binary data stored in an external |
|
file; up to 4 gigabytes
10-10

Creatingi a Tablele by Usingi a Subquery
•• Creater te a tablet le and inserti rt rowsr by combiningi i thet CREATE TABLE statementt t t and AS subqueryry optionti ..
CREATE TABLE table
[column(, column...)]
AS subquery;
•• Matcht thet numberr off specifiedifi columnsl toto thet numberr off subqueryry columnsl ..
•• Definefi columnsl withith columnl names and defaultf lt valuesl ..
10-11