Lab 5 - DDL Queries
Lab 5 - DDL Queries
EXERCISE
5
DDL Queries
Name of Student
Name of Professor
Data Performed
Date Submitted
I. OBJECTIVES
At the end of this exercise, students must be able to:
a) Create, alter and drop tables
b) Verify that tables exist
c) Set a table to read-only and read/write status
d) Manage constraints
e) Add, modify and drop columns
f) Perform flashback operations
II. BACKGROUND INFORMATION
CREATE TABLE Statement
This statement is one of the DDL statements that are a subset of the SQL statements
used to create, modify, or remove Oracle database structures.
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr] [, ]);
Constraints
The Oracle server uses constraints to prevent invalid data entry into tables.
You can use constraints to do the following:
Prevent the deletion of a table if there are dependencies from other tables.
Defining Constraints
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
[table_constraint] [, ]);
Syntax:
INSERT INTO table [(column [, column])]
VALUES (value [, value]);
Syntax:
INSERT INTO table [(column [, column])]
subquery;
ALTER TABLE Statement
Syntax:
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]);
ALTER TABLE table
MODIFY (column datatype [DEFAULT expr]
[, column datatype]);
ALTER TABLE table
DROP (column);
Adding a Constraint
Syntax:
ALTER TABLE table
ADD (CONSTRAINT constraint_name] type column_name;
Dropping a Constraint
Syntax:
ALTER TABLE table
DROP PRIMARY KEY | UNIQUE (column) |
CONSTRAINT constraint [CASCADE];
FLASHBACK TABLE Statement
Syntax:
FLASHBACK TABLE [schema.]table [, [schema.]table]
TO {TIMESTAMP | SCN} expr;
III. PROCEDURES
Overview
In this exercise, you are to create new tables by using the CREATE TABLE statement,
ALTER TABLE command to modify columns and add constraints, and confirm that the new
table was added to the database. You will also set the status of a table as READ ONLY and
then revert to READ/WRITE.
Note: You will use the DEPARTMENTS and EMPLOYEES table in the HR schema. Use
a qualifier to query data from the table in the HR schema.
Syntax:
<schema>.table
Example:
HR.DEPARTMENTS
Task
Write the equivalent SQL statements for the steps that follow.
Step 1: Create a new table DEPT table based on the following table instance chart below.
Enter the syntax in the SQL Worksheet. Then, execute the statement to create the table.
Column name
Data type
Length
ID
NUMBER
7
NAME
VARCHAR2
25
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_______________________________________________________________________
Step 2: Confirm that the table is created. Sample output is as follows:
________________________________________________________________________
________________________________________________________________________
Step 3: Populate the DEPT table with data from the DEPARTMENTS table. Include only
columns that you need.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_______________________________________________________________________
Step 4: Create the EMP2 table based on the following table instance chart. Enter the
syntax in the SQL Worksheet. Then, execute the statement to create the table.
Column name
Data type
Length
ID
NUMBER
7
LAST_NAME
VARCHAR2
25
FIRST_NAME
VARCHAR2
25
DEPT_ID
NUMBER
7
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Step 5: Confirm that the table is created. Sample output is as follows.
________________________________________________________________________
________________________________________________________________________
Step 6: Modify the EMP2 table to allow for longer employee last names.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Step 8: Create the EMPLOYEES2 table based on the structure of the EMPLOYEES
table. Include only the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and
DEPARTMENT_ID columns. Name the columns in your new table ID, FIRST_NAME,
LAST_NAME, SALARY, and DEPT_ID, respectively.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
Step 9: Alter the EMPLOYEES2 table status to read-only.
________________________________________________________________________
________________________________________________________________________
Step 10: Try to insert the following row in the EMPLOYEES2 table:
________________________________________________________________________
________________________________________________________________________
Step 11: Revert the EMPLOYEES2 table to the read/write status. Now, try to insert the
same row again. You should get the following messages:
________________________________________________________________________
________________________________________________________________________
Step 12: Drop the EMP2 table.
________________________________________________________________________
________________________________________________________________________
Step 13: Query the recycle bin to see whether the table is present. Sample output is as
follows.
________________________________________________________________________
________________________________________________________________________
Step 14: Restore the EMP2 table to a state before the DROP statement.
________________________________________________________________________
________________________________________________________________________
Step 15: Drop the first name column from the EMPLOYEES2 table.
________________________________________________________________________
________________________________________________________________________
Step 16: Confirm your modification by checking the description of the table. Sample
output is as follows.
________________________________________________________________________
________________________________________________________________________
HINT: DROP YOUR EMP2 AND DEPT TABLE PERMANENTLY BEFORE
PROCEEDING WITH STEP 17 AND 18
Step 17: Add a table-level PRIMARY KEY constraint to the EMP2 table on the ID
column. The constraint should be named at creation. Name the constraint my_emp_id_pk
________________________________________________________________________
________________________________________________________________________
Step 18: Create a PRIMARY KEY constraint to the DEPT table using the ID column. The
constraint should be named at creation. Name the constraint my_dept_id_pk.
________________________________________________________________________
________________________________________________________________________
Step 19: Add a foreign key reference on the EMP2 table that ensures that the employee is
not assigned to a nonexistent department. Name the constraint my_emp_dept_id_fk.
________________________________________________________________________
________________________________________________________________________
Step 20: Modify the EMP2 table. Add a COMMISSION column of the NUMBER data
type, precision 2, scale 2. Add a constraint to the COMMISSION column that ensures
that a commission value is greater than zero.
________________________________________________________________________
________________________________________________________________________
Step 21: Drop the EMP2 and DEPT tables so that they cannot be restored.
________________________________________________________________________
________________________________________________________________________
Information Technology
ITEDBASE1
Database Management Systems 1
1 SY 20162017
Topic
Data Definition Language
Lab Activity No 5
DDL Queries
Lab Activity
CLO
4
Note: The following rubrics/metrics will be used to grade students output in the lab
exercise 2.
Criteria
Step 1 and step 2
Step 3
Step 6
Step 7
Step 8
Step 9, 10 and 11
Step 15 and 16
Step 17
Descriptions
Created the DEPT table based on instance
chart given.
The table DEPT was created
Used the correct SQL syntax to populate
the DEPT table with data from the
DEPARTMENTS
table
with
the
appropriate attributes stated
Use the correct SQL syntax for creating a
new table EMP2 based on the instance
chart given.
Verify that EMP2 table is created.
Points
10
10
1
5
15
15
10
Criteria
Step 18:
Descriptions
Points
Correct SQL statement for creating a 5
primary key constraint to the DEPT
table using the ID column. The
constraint should be named at creation.
The constraint should be named
my_dept_id_pk.
Step 19:
Step 20:
Step 21:
Step 22:
Total
100
V. REFERENCES
Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2007). Modern Database Management 8th
Edition. New Jersey: Pearson Prentice Hall.