You are on page 1of 23

10

Creating and Managing


Tables
Objectives

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 datatypes
datatypes 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
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

10-3
Naming Conventions

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

10-4
The CREATE TABLE Statement
•• You
You must
must have
have ::
–– CREATE
CREATE TABLE
TABLE privilege
privilege
–– A
A storage
storage area
area
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);

•• You
You specify:
specify:
–– Table
Table name
name
–– Column
Column name,
name, column
column datatype,
datatype, and
and
column
column size
size
10-5
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.

10-6
The DEFAULT Option
•• Specify
Specify aa default
default value
value for
for aa column
column during
during
an
an insert.
insert.
… hiredate DATE DEFAULT SYSDATE, …

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

Name Null? Type


--------------------------- -------- ---------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)

10-8
Tables in the Oracle Database

•• 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
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;

10-10
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-11
Creating a Table
by Using a Subquery
•• Create
Create aa table
table and
and insert
insert rows
rows by
by
combining
combining thethe CREATE
CREATE TABLE
TABLE statement
statement
and
and AS
AS subquery
subquery option.
option.
CREATE TABLE table
[(column, column...)]
AS subquery;

•• 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
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.

SQL> DESCRIBE dept30

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

10-13
The ALTER TABLE Statement
Use
Use the
the ALTER
ALTER TABLE
TABLE statement
statement to:
to:
•• Add
Add aa new
new column
column
•• Modify
Modify an
an existing
existing column
column
•• Define
Define aa default
default value
value for
for the
the new
new column
column
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]...);

ALTER TABLE table


MODIFY (column datatype [DEFAULT expr]
[, column datatype]...);

10-14
Adding a Column
“…add a
DEPT30 New column
new
EMPNO ENAME ANNSAL HIREDATE JOB column
------ ---------- -------- into
7698 BLAKE 34200 01-MAY-81 DEPT30
7654 MARTIN 15000 28-SEP-81 table…”
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
...
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-15
Adding a Column
•• You
You use
use the
the ADD
ADD clause
clause to
to add
add columns.
columns.
SQL> ALTER TABLE dept30
2 ADD (job VARCHAR2(9));
Table altered.

•• 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.

10-16
Modifying a Column
•• You
You can
can change
change aa column’s
column’s datatype,
datatype,
size,
size, and
and default
default value.
value.
ALTER TABLE dept30
MODIFY (ename VARCHAR2(15));
Table altered.

•• 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.

10-17
Dropping a 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.

10-18
Changing the Name of an Object

•• 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.

10-19
Truncating a Table
•• The
The TRUNCATE
TRUNCATE TABLE
TABLE statement:
statement:
–– Removes
Removes all
all rows
rows from
from aa table
table
–– Releases
Releases the
the storage
storage space
space used
used by
by
that
that table
table
SQL> TRUNCATE TABLE department;
Table truncated.

•• 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
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
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-22
Practice Overview
• Creating new tables
• Creating
• Creating newtable
a new tables
by using the CREATE TABLE AS syntax
• Creating a new table by using the CREATE TABLE AS syntax
• Modifying column definitions
• Modifying column definitions
• Verifying that the tables exist
• Verifying that the tables
• Adding comments to a tables exist
• Adding comments to a tables
• Dropping tables
• Dropping tables
• Altering tables
• Altering tables

10-23

You might also like