CCS0014 - Lab Exercise 5
CCS0014 - Lab Exercise 5
EXERCISE
5
DDL Queries
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr] [, …]);
Constraints
The Oracle server uses constraints to prevent invalid data entry into tables.
Constraint Description
NOT NULL Specifies that the column cannot contain a null value
Defining Constraints
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
…
[table_constraint] [, …]);
Syntax:
column [CONSTRAINT constraint_name] constraint_type,
Syntax:
column, …
[CONSTRAINT constraint_name] constraint_type
(column, …),
Syntax:
CREATE TABLE table
[(column, column…)]
AS subquery;
Read-Only Tables
You can use the ALTER TABLE syntax to:
Put a table into read-only mode, which prevents DDL or DML changes during table
maintenance
Put the table back into read/write mode
Syntax:
ALTER TABLE table READ ONLY;
ALTER TABLE table READ WRITE;
Dropping a Table
Moves a table to the recycle bin
Removes the table and all its data entirely if the PURGE clause is specified
Syntax:
DROP TABLE table [PURGE];
Inserting Data into the Table
With this syntax, only one row is inserted at a time
Syntax:
INSERT INTO table [(column [, column…])]
VALUES (value [, value…]);
Syntax:
INSERT INTO table [(column [, column…])]
subquery;
Syntax:
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]…);
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];
Syntax:
FLASHBACK TABLE [schema.] table [, [schema.]table]…
TO {TIMESTAMP | SCN} expr;
III. EXPERIMENTAL PROCEDURE
INSTRUCTIONS:
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.
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.
Step 3: Populate the DEPT table with data from the DEPARTMENTS table. Include only
columns that you need.
Step 6: Modify the EMP2 table to allow for longer employee last names.
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:
ALTER TABLE EMPLOYEES2 READ WRITE
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.
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.
________________________________________________________________________
________________________________________________________________________
1. Explain why you encountered an error message in Step 10 after you inserted a row into
the EMPLOYEES2 table.
YOU WILL NOT BE ALLOWED TO INSERT ANY ROW INTO THE TABLE
BECAUSE IT IS ASSIGNED A READ-ONLY STATUS.
2. What is the purpose of modifying the status of your table to READ ONLY or
READ/WRITE mode?
THE PRIMARY PURPOSE OF READ-ONLY TABLESPACES IS TO ELIMINATE
THE NEED TO PERFORM BACKUP AND RECOVERY OF LARGE, STATIC
PORTIONS OF A DATABASE. READ-ONLY TABLESPACES ALSO PROVIDE A
WAY TO PROTECTING HISTORICAL DATA SO THAT USERS CANNOT
MODIFY IT. MAKING A TABLESPACE READ-ONLY PREVENTS UPDATES ON
ALL TABLES IN THE TABLESPACE, REGARDLESS OF A USER'S UPDATE
PRIVILEGE LEVEL.
Criteria Descriptions Points
IV. ASSESSMENT
Note: The following rubrics/metrics will be used to grade students’ output in the lab
exercise.
Step 1 and step 2 Created the DEPT table based on instance 10
chart given.
The table DEPT was created
Step 3 Used the correct SQL syntax to populate 5
the DEPT table with data from the
DEPARTMENTS table with the
appropriate attributes stated
Step 4 and step 5 Use the correct SQL syntax for creating a 10
new table EMP2 based on the instance
chart given.
Verify that EMP2 table is created.