This lab focuses on the SQL DDL.
Configure an Oracle user for yourself.
As you did last week, start the OracleXE server, move to your network drive and start
sqlplus
as the system
user.
Create a database user for yourself by saying
SQL> CREATE USER yourId IDENTIFIED BY yourPassword; SQL> GRANT CREATE SESSION, CREATE TABLE, CREATE VIEW, CREATE MATERIALIZED VIEW, UNLIMITED TABLESPACE, CREATE SEQUENCE TO yourID;
You can now logout ( exit
) and log back in as yourself ( sqlplus
yourId/yourPassword
).
You now will consider a sample command file that defines a simple movies database with some
sample data. Pull this (and future) sample code from this (public) repo: https://github.com/cs342/code
.
Study the movies database code in 02modeling/movies.sql
so that you know how
command file works. In particular, consider the purpose of the StatusValue table.
Try the following, noting what happens and why. Store your DDL/DML commands along with
explanations of how/why they work in lab02_1.sql
.
Try adding records to the movie relation that cause these intra-relation issues:
Try adding records that cause these inter-relation issues:
Try deleting/modifying records as follows:
Note that though
the text discusses it, Oracle doesn’t support ON UPDATE CASCADE
.
Would supporting such a feature be a good idea? See Ask Tom’s discussion of this.
Save your command file for submission.
We now address two more challenging questions.
Give answers to these more challenging questions in lab02_2.txt
.
The original IMDB Movie table included fields for both score and votes. Can you add a constraint (using the mechanisms we’ve studied) the requires that all movies having a non-NULL score value must also have more than 1000 votes? If so, explain what constraint you’d specify; if not, include an explanation of why it’s not possible.
Database systems, including Oracle, allow DBAs to create separate constraints that are set on given tables, which allows DBAs to turn constraints off and on during database operations, e.g.:
ALTER TABLE tableName ADD CONSTRAINT constraintName someConstraint;
Can you imagine when, if ever, you might want to separate a constraint from the table definition it modifies? If so, describe the circumstances; if not, explain why not.
Save your text file for submission.
Note also that Oracle treats empty strings (i.e., ''
) as NULL
values
while other DBMSs, e.g., SQL Server, distinguish these two values.
If time allows, complete the following exercises. They are optional for the lab but required for the homework.
The movies database hard-codes enumerated types for the performer status using a CHECK constraint.
Can you modify the movies database to support the enumerated status type using the relational model itself rather than hard-coding the values in a CHECK constraint? If so, show how and explain the benefits and/or costs of doing it this way. If not, explain why not.
Store your modified version of the movies database or your explanation in
lab02_3.sql
.
DBMSs provide automated support for generating sequences of unique Id numbers; see Oracle’s Overview of Sequences.
Build and use a sequence for the primary key values of the Movie relation, then consider these questions.
Store your modified command file and your answers to the questions in
lab02_4.sql
.
Remember to stop the OracleXE service as described in lab 1.
We will grade your work according to the following criteria:
Exercises 2.3–4 are graded with the homework.