Lecture 03 S1 2023
Lecture 03 S1 2023
INF 312
DATABASE DESIGN AND
DEVELOPMENT
Semester 1, 2023
Week 3 – Lecture 3
Lecture 3 Objectives
After completing this chapter, you should be able to do the
following:
• Identify the table name and structure
• Create a new table with 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
• Mark a column as unused and then delete it later
• Rename, truncate (shorten), and drop a table.
16 February 2023 3
READINGS
• Casteel, Chapter 3
pp. 57 - 98
16 February 2023 4
TABLE CREATION
• A part of Data Definition Language (DDL) in SQL
• Textbook focuses on creating tables in isolation from
creating constraints on tables
• The next few slides present a more pragmatic approach:
creating a table and constraints on it together
• You may use either approach:
Create table and then constraints on the table
Create table and constraints on the table together
5
SUMMARY
• You create a table with the CREATE TABLE command.
Each column to be included in the table must be defined
in terms of the column name, datatype, and the width
(for certain datatypes).
• A table can contain up to 1000 columns.
• Each column name in a table must be unique.
• Table and column names can contain as many as 30
characters. The names must begin with a letter and
can’t contain blank spaces.
• A DEFAULT setting assigns a column value if no value
is provided for the column when a new row is added.
37
SUMMARY
• A virtual column is a column defined by an expression
that generates a value based on other column values in
the table when queried.
• A query on the data dictionary object
USER_TAB_COLUMNS enables you to verify DEFAULT
and virtual column settings.
• To create a table based on existing tables’ structure and
data, use the CREATE TABLE ... AS command to use a
subquery that extracts the necessary data from the
existing table.
• You can change a table’s structure with the ALTER
TABLE command. Columns can be added, resized, and
even deleted with the ALTER TABLE command.
38
SUMMARY
• When using the ALTER TABLE command with the
DROP COLUMN clause, only one column can be
specified for deletion.
• You can use the SET UNUSED clause to mark a column
so that its storage space can be freed up later.
• Tables can be renamed with the RENAME ... TO
command.
• 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.
39
SUMMARY
• A dropped table is moved to the recycle bin and can be
recovered by using the FLASHBACK TABLE command.
• Using the PURGE option in a DROP TABLE command
removes the table permanently, meaning you can’t
recover it from the recycle bin.
16 February 2023 40