0% found this document useful (0 votes)
35 views40 pages

Lecture 03 S1 2023

The document discusses creating and modifying database tables in Oracle SQL. It covers using the CREATE TABLE, ALTER TABLE, and DROP TABLE commands to define table structure and manage columns. It also discusses using constraints, subqueries, and the data dictionary to view metadata about tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views40 pages

Lecture 03 S1 2023

The document discusses creating and modifying database tables in Oracle SQL. It covers using the CREATE TABLE, ALTER TABLE, and DROP TABLE commands to define table structure and manage columns. It also discusses using constraints, subqueries, and the data dictionary to view metadata about tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

16 February 2023 1

INF 312
DATABASE DESIGN AND
DEVELOPMENT

Semester 1, 2023
Week 3 – Lecture 3

Table Creation and Management


16 February 2023 2

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

CREATE TABLE & CONSTRAINT


• Example:

CREATE TABLE department2


(deptid NUMBER(3) PRIMARY KEY);

• Table name is: department2


• It has one column: deptid and its datatype: NUMBER(3)
• deptid column has a constraint: PRIMARY KEY.
6

CREATE TABLE & CONSTRAINT


• Example 2:
CREATE TABLE employee2
(
empid NUMBER(4) PRIMARY KEY,
name VARCHAR2(20) NOT NULL,
address VARCHAR2(50),
dob DATE,
deptno NUMBER(3) REFERENCES department2(deptid)
);
• Table name is: employee2
• It has five columns:
• empid and its datatype: NUMBER(4) has a constraint: PRIMARY KEY
• name and its datatype: VARCHAR2(20) has a constraint: NOT NULL
• address and its datatype: VARCHAR2(50)
• dob and its datatype: DATE
• deptno and its datatype: NUMBER(3) has a constraint: FOREIGN KEY referencing deptid
column from department2 table.
7

CREATE TABLE & CONSTRAINT


• Example 3:
CREATE TABLE employee3
(
name VARCHAR2(20),
address VARCHAR2(50),
dob DATE,
CONSTRAINT pk_employee3 PRIMARY KEY(name, address)
);
• Table name is: employee3
• It has three columns:
• name and its datatype: VARCHAR2(20)
• address and its datatype: VARCHAR2(50)
• dob and its datatype: DATE

• Also a named constraint: pk_employee3 to represent PRIMARY KEY


constraint for name and address columns together (composite key).
Database Table
• A database object
• Stores data for the database
• Consists of columns and rows
• Created and modified through data definition language
(DDL) commands

Oracle 11g: SQL 8


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, e.g. a table cannot be named as
“select”.

Oracle 11g: SQL 9


Table column datatypes

Oracle 11g: SQL 10


Table Creation Syntax

Oracle 11g: SQL 11


Defining Columns
• Column definition list must be enclosed in parentheses ( )
• Data type must be specified for each column
• Maximum of 1,000 columns in a table

Oracle 11g: SQL 12


CREATE TABLE Example

Virtual Column: appear to


be normal columns, but
their values are derived
rather than being stored in
the table.

Oracle 11g: SQL 13


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

Oracle 11g: SQL 14


Viewing Table Structures: DESCRIBE
• DESCRIBE displays the structure of a specified table

Oracle 11g: SQL 15


Table Creation through Subqueries
• You can use subqueries to retrieve data from an existing
table and create a new table
• Requires use of AS keyword
• New column names can be assigned

Oracle 11g: SQL 16


CREATE TABLE…AS

Oracle 11g: SQL 17


CREATE TABLE…AS Example

Oracle 11g: SQL 18


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

Oracle 11g: SQL 19


ALTER TABLE Syntax

Oracle 11g: SQL 20


ALTER TABLE…ADD Example

Oracle 11g: SQL 21


ALTER TABLE…MODIFY Example

Oracle 11g: SQL 22


Modification Guidelines
• Adding or changing default data or modifying columns
does not affect existing data
• Column size must be as wide as the data it already
contains (it cannot be shorter than the data it holds since
it will imply that the data will be lost)
• If a NUMBER column already contains data, size cannot
be decreased

Oracle 11g: SQL 23


ALTER TABLE…DROP COLUMN Example

• Can only drop one column per execution


• Deletion is permanent (CAREFUL!)
• Cannot delete the last remaining column in a table!

Oracle 11g: SQL 24


ALTER TABLE…SET UNUSED
• Marking a column for deletion
• Once marked for deletion, a column cannot be
restored
• Storage space can be freed at a later time (to
permanently drop the column)

Oracle 11g: SQL 25


ALTER TABLE…DROP UNUSED
• Frees up storage space from columns
previously marked as UNUSED

Oracle 11g: SQL 26


Renaming a Table
• RENAME…TO is used to rename a table – the old
table name will no longer be valid after a rename.

Oracle 11g: SQL 27


Truncating a Table
• TRUNCATE TABLE command
• all rows of a table are deleted
• However, the structure of the table remains

Oracle 11g: SQL 28


Deleting/ Dropping a Table
• DROP TABLE command
• Table structure and all rows are deleted

Oracle 11g: SQL 29


30

Deleting/ Dropping a Table


• If there are foreign keys pointing to the table to be
dropped:
DROP TABLE <tablename> CASCADE CONSTRAINTS;

• This will remove the referring constraints and then drops


the table.
DROP TABLE (without PURGE option)

• Oracle 10g introduced a “recycle bin” concept


• Dropped tables can be recovered from the
recycle bin

Oracle 11g: SQL 31


FLASHBACK
• The FLASHBACK command recovers a
dropped table from the recycle bin

Oracle 11g: SQL 32


Use PURGE to remove a table from the
Recycle Bin (permanently)

Oracle 11g: SQL 33


DROP TABLE (with PURGE option)

• Using the PURGE option will permanently


remove a table from the database
• The table will not be copied into the recycle
Deleting/ Dropping a Table
bin

Oracle 11g: SQL 34


35

DROP, TRUNCATE & DELETE ?


• DROP: removes a table from the database – all table
rows, indexes and privileges will also be removed.
DROP can be rolled back using FLASHBACK.
• TRUNCATE: removes ALL ROWS from a table but
keeps the table. TRUNCATE is faster and cannot be
rolled back.
• DELETE: DELETE is a Data Manipulation Language
(DML) unlike DROP and TRUNCATE which are DDL.
DELETE can remove selective rows from a table but
can be rolled back using ROLLBACK.
• More next week on DELETE…
36

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

Tasks for this week…..


• Read Chapter 3 – Table Creation and
Management.
• Do Multiple Choice Questions – on pp.129
– 132.
• Do Review Questions – on p.128.
• Do Online Quiz #3 – available on Moodle.
• Hands-on Practical Assignment #1

You might also like