Assignment
The purpose of this lab is to introduce you to the DDL set of statements in SQL. By writing SQL to create tables, constraints, and views, you will have the tools needed to implement database designs that you will create later in the course. By finishing this lab, the student will be able to:
1) create, modify, and drop tables based on design specifications provided,
2) inserting new data into tables, update data in tables, and delete data from tables while considering referential integrity,
3) enforce constraints on tables to ensure data integrity and consistency,
4) create a table using the structure and data from an existing table,
5) import data into a table from other tables.
Submission:
Your submission will be a single text-based .SQL file with the solutions provided.
Your submission needs to include a comment header block and be commented to include the question and the solutions. Make sure every SQL statement terminates with a semicolon.
Task:
Add
SET AUTOCOMMIT ON;
under the comment header and execute it
Consider the following table specifications:
Part I: (DDL)
• Create table the following tables and their given constraints:
DBS211NDD_MOVIES (movieid:int, title:varchar(35), releaseYear:int, directorid:int, score:decimal(3,2))
Column Name
|
Column DataType
|
PK
|
Not Null
|
Unique
|
FK
|
Default Value
|
Validation
|
movieid
|
Int
|
?
|
|
|
|
|
|
title
|
varchar(35)
|
|
? |
|
|
|
|
releaseYear
|
Int
|
|
?
|
|
|
|
|
directorid
|
Int
|
|
?
|
|
|
|
|
score
|
decimal(3,2)
|
|
|
|
|
|
< 5 and > 0
|
DBS211NDD_ACTORS (actorid:int, firstName:varchar(20), lastname:varchar(30))
Column Name
|
Column DataType
|
PK
|
Not Null
|
Unique
|
FK
|
Default Value
|
Validation
|
actorid
|
Int
|
?
|
|
|
|
|
|
firstName
|
varchar(20)
|
|
?
|
|
|
|
|
lastName
|
Varchar(30)
|
|
?
|
|
|
|
|
DBS211NDD_CASTINGS (movieid:int, actorid:int)
Column Name
|
Column DataType
|
PK
|
Not Null
|
Unique
|
FK
|
Default Value
|
Validation
|
movieid
|
Int
|
?
|
|
|
? (movies)
|
|
|
actorid
|
int
|
? |
|
|
? (actors)
|
|
|
DBS211NDD_DIRECTORS(directorid:int, firstname:varchar(20), lastname:varchar(30))
Column Name
|
Column DataType
|
PK
|
Not Null
|
Unique
|
FK
|
Default Value
|
Validation
|
directorid
|
Int
|
?
|
|
|
|
|
|
firstname
|
varchar(20)
|
|
?
|
|
|
|
|
lastname
|
varchar(30)
|
|
?
|
|
|
|
|
• Modify the dbs211ndd_movies table to create foreign key constraint that refers to table dbs211ndd_directors.
• Modify the dbs211ndd_movies table to create new constraint so the uniqueness of the movie title is guaranteed.
• Write insert statements to add the following data to table dbs211ndd_directors and dbs211ndd_movies.
Director
directorid
|
First name
|
Last name
|
1010
|
Rob
|
Minkoff
|
1020
|
Bill
|
Condon
|
1050
|
Josh
|
Cooley
|
2010
|
Brad
|
Bird
|
3020
|
Lake
|
Bell
|
Movies
movieid
|
title
|
releaseyear
|
directorid
|
score
|
100
|
The Lion King
|
2019
|
3020
|
3.50
|
200
|
Beauty and the Beast
|
2017
|
1050
|
4.20
|
300
|
Toy Story 4
|
2019
|
1020
|
4.50
|
400
|
Mission Impossible
|
2018
|
2010
|
5.00
|
500
|
The Secret Life of Pets
|
2016
|
1010
|
3.90
|
• Write SQL statements to remove all above tables. Is the order of tables important when removing? Why?
Part II: More DML
A. Create a new empty table dbs211ndd_employee2 the same as table employees. Use a single statement to create the table and insert the data at the same time.
B. Modify table dbs211ndd_employee2 and add a new column username to this table. The value of this column is not required and does not have to be unique.
C. Delete all the data in the dbs211ndd_employee2 table
D. Re-insert all data from the employees table into your new table dbs211ndd_employee2 using a single statement.
E. In table dbs211ndd_employee2, write a SQL statement to change the first name and the last name of employee with ID 1002 to your name.
F. In table dbs211ndd_employee2, generate the email address for column username for each student by concatenating the first character of employee's first name and the employee's last name. For instance, the username of employee Peter Stone will be pstone. NOTE: the username is in all lower case letters.
G. In table dbs211ndd_employee2, remove all employees with office code 4.
H. Drop table dbs211ndd_employee2.