Creating and Managing Tables
Creating and Managing Tables
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Describe
Describe the
the main
main database
database objects
objects
•• Create
Create tables
tables
•• Describe
Describe the
the data
data types
types that
that can
can be
be
used
used when
when specifying
specifying column
column definition
definition
•• Alter
Alter table
table definitions
definitions
•• Drop,
Drop, rename,
rename, and
and truncate
truncate tables
tables
10-2 Copyright Oracle Corporation, 1998. All rights reserved.
Database Objects
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
•• Must
Must begin
begin with
with aa letter
letter
•• Can
Can be
be 1–30
1–30 characters
characters long
long
•• Must
Must contain
contain only
only A–Z,
A–Z, a–z,
a–z, 0–9,
0–9, _,
_, $,
$,
and
and ##
•• Must
Must not
not duplicate
duplicate the
the name
name ofof another
another
object
object owned
owned byby the
the same
same user
user
•• Must
Must not
not be
be an
an Oracle
Oracle Server
Server reserved
reserved
word
word
•• You
You specify:
specify:
–– Table
Table name
name
–– Column
Column name,
name, column
column data
data type,
type, and
and
column
column size
size
10-5 Copyright Oracle Corporation, 1998. All rights reserved.
Referencing Another User’s
Tables
•• Tables
Tables belonging
belonging toto other
other users
users are
are not
not
in
in the
the user’s
user’s schema.
schema.
•• You
You should
should use
use the
the owner’s
owner’s name
name as
as aa
prefix
prefix to
to the
the table.
table.
•• Legal
Legal values
values are
are literal
literal value,
value, expression,
expression,
or
or SQL
SQL function.
function.
•• Illegal
Illegal values
values are
are another
another column’s
column’s name
name oror
pseudocolumn.
pseudocolumn.
•• The
The default
default datatype
datatype must
must match
match the
the
column
column datatype.
datatype.
10-7 Copyright Oracle Corporation, 1998. All rights reserved.
Creating Tables
•• Create
Create the
the table.
table.
SQL> CREATE TABLE dept
2 (deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13));
Table created.
•• Confirm
Confirm table
table creation.
creation.
SQL> DESCRIBE dept
•• User
User Tables
Tables
–– Collection
Collection of
of tables
tables created
created and
and
maintained
maintained by
by the
the user
user
–– Contain
Contain user
user information
information
•• Data
Data Dictionary
Dictionary
–– Collection
Collection of
of tables
tables created
created and
and
maintained
maintained by
by the
the Oracle
Oracle server
server
–– Contain
Contain database
database information
information
10-9 Copyright Oracle Corporation, 1998. All rights reserved.
Querying the Data Dictionary
•• Describe
Describe tables
tables owned
owned by
by the
the user.
user.
SQL> SELECT *
2 FROM user_tables;
•• View
View distinct
distinct object
object types
types owned
owned by
by the
the
user.
user.
SQL> SELECT DISTINCT object_type
2 FROM user_objects;
•• View
View tables,
tables, views,
views, synonyms,
synonyms, and
and
sequences
sequences owned
owned byby the
the user.
user.
SQL> SELECT *
2 FROM user_catalog;
•• Match
Match the
the number
number ofof specified
specified columns
columns
to
to the
the number
number ofof subquery
subquery columns.
columns.
•• Define
Define columns
columns with
with column
column names
names and
and
default
default values.
values.
10-12 Copyright Oracle Corporation, 1998. All rights reserved.
Creating a Table
by Using a Subquery
SQL> CREATE TABLE dept30
2 AS
3 SELECT empno, ename, sal*12 ANNSAL, hiredate
4 FROM emp
5 WHERE deptno = 30;
Table created.
Name
Name Null?
Null? Type
Type
----------------------------
---------------------------- --------
-------- -----
-----
EMPNO
EMPNO NOT
NOT NULL
NULL NUMBER(4)
NUMBER(4)
ENAME
ENAME VARCHAR2(10)
VARCHAR2(10)
ANNSAL
ANNSAL NUMBER
NUMBER
HIREDATE
HIREDATE DATE
DATE
•• The
The new
new column
column becomes
becomes the
the last
last column.
column.
EMPNO
EMPNO ENAME
ENAME ANNSAL
ANNSAL HIREDATE
HIREDATE JOB
JOB
---------
--------- ----------
---------- ---------
--------- ---------
--------- ----
----
7698
7698 BLAKE
BLAKE 34200
34200 01-MAY-81
01-MAY-81
7654
7654 MARTIN
MARTIN 15000
15000 28-SEP-81
28-SEP-81
7499
7499 ALLEN
ALLEN 19200
19200 20-FEB-81
20-FEB-81
7844
7844 TURNER
TURNER 18000
18000 08-SEP-81
08-SEP-81
...
...
66 rows
rows selected.
selected.
•• A
A change
change to
to the
the default
default value
value affects
affects
only
only subsequent
subsequent insertions
insertions to
to the
the table.
table.
•• All
All data
data and
and structure
structure in
in the
the table
table is
is
deleted.
deleted.
•• Any
Any pending
pending transactions
transactions are
are
committed.
committed.
•• All
All indexes
indexes are
are dropped.
dropped.
•• You
You cannot
cannot roll
roll back
back this
this statement.
statement.
SQL> DROP TABLE dept30;
Table dropped.
•• To
To change
change the
the name
name of
of aa table,
table, view,
view,
sequence,
sequence, or
or synonym,
synonym, you
you execute
execute the
the
RENAME
RENAME statement.
statement.
SQL> RENAME dept TO department;
Table renamed.
•• You
You must
must be
be the
the owner
owner of
of the
the object.
object.
•• You
You cannot
cannot roll
roll back
back row
row removal
removal when
when
using
using TRUNCATE.
TRUNCATE.
•• Alternatively,
Alternatively, you
you can
can remove
remove rows
rows by
by
using
using the
the DELETE
DELETE statement.
statement.
10-20 Copyright Oracle Corporation, 1998. All rights reserved.
Adding Comments to a Table
•• You
You can
can add
add comments
comments to
to aa table
table or
or
column
column by
by using
using the
the COMMENT
COMMENT
statement.
statement.
SQL> COMMENT ON TABLE emp
2 IS 'Employee Information';
Comment created.
•• Comments
Comments can can be be viewed
viewed through through the the
data
data dictionary
dictionary views. views.
–– ALL_COL_COMMENTS
ALL_COL_COMMENTS
–– USER_COL_COMMENTS
USER_COL_COMMENTS
–– ALL_TAB_COMMENTS
ALL_TAB_COMMENTS
–– USER_TAB_COMMENTS
USER_TAB_COMMENTS
10-21 Copyright Oracle Corporation, 1998. All rights reserved.
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
•• Creating
Creating new
new tables
tables
•• Creating
Creating aa new
new table
table by
by using
using the
the
CREATE
CREATE TABLE
TABLE ASAS syntax
syntax
•• Modifying
Modifying column
column definitions
definitions
•• Verifying
Verifying that
that the
the tables
tables exist
exist
•• Adding
Adding comments
comments to to aa tables
tables
•• Dropping
Dropping tables
tables
•• Altering
Altering tables
tables
10-23 Copyright Oracle Corporation, 1998. All rights reserved.