Professional Documents
Culture Documents
Copyright
Objectives
After completing this lesson, you should
be able to do the following:
Describe the main database objects
Create tables
Describe the datatypes that can be used
when specifying column definition
Alter table definitions
Drop, rename, and truncate tables
10-2
Copyright
Database Objects
Object
Description
Table
View
10-3
Sequence
Index
Synonym
Copyright
Naming Conventions
Must begin with a letter
Can be 130 characters long
Must contain only AZ, az, 09, _, $,
and #
Must not duplicate the name of another
object owned by the same user
Must not be an Oracle Server reserved
word
10-4
Copyright
You specify:
Table name
Column name, column datatype, and
column size
10-5
Copyright
10-6
Copyright
Copyright
Creating Tables
Create the table.
SQL> CREATE TABLE dept
2
(deptno NUMBER(2),
3
dname VARCHAR2(14),
4
loc
VARCHAR2(13));
Table created.
Copyright
Type
--------NUMBER(2)
VARCHAR2(14)
VARCHAR2(13)
Data Dictionary
Collection of tables created and
Copyright
*
user_tables;
DISTINCT object_type
user_objects;
*
user_catalog;
Copyright
Datatypes
Datatype
Description
VARCHAR2(size)
CHAR(size)
NUMBER(p,s)
DATE
LONG
CLOB
BLOB
BFILE
10-11
Copyright
Creating a Table
by Using a Subquery
Create a table and insert rows by
combining the CREATE TABLE statement
and AS subquery option.
CREATE TABLE table
[(column, column...)]
AS subquery;
Copyright
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
------------------------------------------------------EMPNO
EMPNO
ENAME
ENAME
ANNSAL
ANNSAL
HIREDATE
HIREDATE
10-13
Copyright
Null?
Null?
--------------NOT
NOT NULL
NULL
Type
Type
--------NUMBER(4)
NUMBER(4)
VARCHAR2(10)
VARCHAR2(10)
NUMBER
NUMBER
DATE
DATE
10-14
Copyright
Adding a Column
DEPT30
New column
EMPNO ENAME
-----7698
7654
7499
7844
...
ANNSAL
HIREDATE
---------- -------BLAKE
34200
MARTIN
15000
ALLEN
19200
TURNER
18000
01-MAY-81
28-SEP-81
20-FEB-81
08-SEP-81
JOB
add a
new
column
into
DEPT30
table
DEPT30
EMPNO ENAME
-----7698
7654
7499
7844
...
10-15
Copyright
ANNSAL
HIREDATE
---------- -------BLAKE
34200
MARTIN
15000
ALLEN
19200
TURNER
18000
01-MAY-81
28-SEP-81
20-FEB-81
08-SEP-81
JOB
Adding a Column
You use the ADD clause to add columns.
SQL> ALTER TABLE dept30
2 ADD
(job VARCHAR2(9));
Table altered.
Copyright
JOB
JOB
-------
Modifying a Column
You can change a columns datatype,
size, and default value.
ALTER TABLE dept30
MODIFY
(ename VARCHAR2(15));
Table altered.
10-17
Copyright
Dropping a Table
All data and structure in the table is
deleted.
Any pending transactions are
committed.
All indexes are dropped.
You cannot roll back this statement.
SQL> DROP TABLE dept30;
Table dropped.
10-18
Copyright
10-19
Copyright
Truncating a Table
The TRUNCATE TABLE statement:
Removes all rows from a table
Releases the storage space used by
that table
SQL> TRUNCATE TABLE department;
Table truncated.
Copyright
Summary
Statement
Description
CREATE TABLE
Creates a table
ALTER TABLE
DROP TABLE
RENAME
TRUNCATE
COMMENT
10-24
Copyright
Practice Overview
Creating new tables
Creating a new table by using the CREATE TABLE AS syntax
Modifying column definitions
Verifying that the tables exist
Adding comments to a tables
Dropping tables
Altering tables
10-25
Copyright
10-26
Copyright