7/22/2010
Database and Information Retrieval
ICT118 Lecture 4 SQL syntax for Creating and Managing tables
22 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 1
Oracle 11g: SQL
Chapter 3 Table Creation and Management
22 July, 2010 ICT118 - Database and Information Retrieval, Sem 2, 2010 4. 2
Objectives
Identify the table name and structure Create a new table using the CREATE TABLE command Use a subquery to create a new table Add a column to an existing table Modify the definition of a column in an existing table Delete a column from an existing table
22 July, 2010 ICT118 - Database and Information Retrieval, Sem 2, 2010 4. 3
7/22/2010
Objectives (continued)
Mark a column as unused and then delete it at a later time Rename a table Truncate a table Drop a table
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 4
Database Table
A database object Stores data for the database Consists of columns and rows Created and modified through data definition language (DDL) commands
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 5
Table Design
Table and column names:
Can contain a maximum 30 characters no blank spaces Must begin with a letter Can contain numbers, underscore (_), and number sign (#) Must be unique No reserved words are allowed
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 6
7/22/2010
Table Design (continued)
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 7
Table Creation
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 8
Defining Columns
Column definition list must be enclosed in parentheses Datatype must be specified for each column Maximum of 1,000 columns
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 9
7/22/2010
CREATE TABLE Command
Virtual Column
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 10
Viewing List of Tables: USER_TABLES
A data dictionary is a typical component of a DBMS that maintains information about database objects You can query the data dictionary to verify all the tables that exist in your schema The USER_TABLES data dictionary object maintains information regarding all your tables
22 July, 2010 July, 2010 ICT118 - Database and Information Retrieval, Sem 2, 2010 4. 11
Viewing Table Structures: DESCRIBE
DESCRIBE displays the structure of a specified table DESCRIBE
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 12
7/22/2010
Table Creation through Subqueries
You can use subqueries to retrieve data from an existing table Requires use of AS keyword New column names can be assigned
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 13
CREATE TABLEAS
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 14
CREATE TABLEAS
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 15
7/22/2010
Modifying Existing Tables
Accomplished through the ALTER TABLE command Use an ADD clause to add a column Use a MODIFY clause to change a column Use a DROP COLUMN to drop a column
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 16
ALTER TABLE Command Syntax
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 17
ALTER TABLEADD
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 18
7/22/2010
ALTER TABLEMODIFY
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 19
Modification Guidelines
Column must be as wide as the data it already contains If a NUMBER column already contains data, size cannot be decreased Adding or changing default data does not affect existing data
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 20
ALTER TABLEDROP COLUMN
Can only reference one column per execution Deletion is permanent Cannot delete last remaining column in a table
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 21
7/22/2010
ALTER TABLESET UNUSED Command
Once marked for deletion, a column cannot be restored Storage space is freed at a later time
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 22
ALTER TABLEDROP UNUSED Command
Frees up storage space from columns Frees previously marked as unused
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 23
Renaming a Table
RENAMETO is used to rename a table the RENAMETO old name is no longer valid
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 24
7/22/2010
Truncating a Table
TRUNCATE TABLE command rows are TRUNCATE deleted Structure of table remains Structure
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 25
Deleting a Table
DROP TABLE command table DROP structure and contents are deleted
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 26
without DROP TABLE without Purge Option
Oracle 10g introduced a recycle bin Dropped tables can be recovered from the recycle bin
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 27
7/22/2010
FLASHBACK Command
The FLASHBACK command recovers a The table from the recycle bin
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 28
Use PURGE to Remove a Table from the Recycle Bin
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 29
PURGE Option Available for DROP TABLE Command
Using the PURGE option will permanently remove a table from the database The table will not be copied into the recycle bin
22 July, 2010 July, 2010
ICT118 - Database and Information Retrieval, Sem 2, 2010
4. 30
10
7/22/2010
Summary
You create a table with the CREATE TABLE command Each column to be contained in the table must be defined in terms of the column name, data type, and for certain data types, the width A table can contain up to 1000 columns Each column name within a table must be unique You can change the structure of a table with the ALTER TABLE command Columns can be added, resized, and even deleted with the ALTER TABLE command Tables can be renamed with the RENAME...TO command
22 July, 2010 July, 2010 ICT118 - Database and Information Retrieval, Sem 2, 2010 4. 31
Summary (continued)
To delete all the rows in a table, use the TRUNCATE TABLE command To remove both the structure of a table and all its contents, use the DROP TABLE command A dropped table is moved to the recycle bin and can be recovered using the FLASHBACK TABLE command Using the PURGE option in a DROP TABLE command permanently removes the table, meaning you cannot recover it from the recycle bin
22 July, 2010 July, 2010 ICT118 - Database and Information Retrieval, Sem 2, 2010 4. 32
11