DBS211_Lab04
DBS211_Lab04
Lab 04 - DDL
Objective:
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:
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.
Tasks:
Add
SET AUTOCOMMIT ON;
under the comment header and execute it
Part A (DDL) :
1. Create table the following tables and their given constraints:
MOVIES (movieid:int, title:varchar(35), year:int,
director:int,score:decimal(3,2))
Constraint SCORE
};
id int,
Constraint Actors_id_PK
);
CASTINGS (movieid:int, actorid:int)
};
DIRECTORS(id:int, name:varchar(20), lastname:varchar(30))
id int null,
};
2. Modify the movies table to create a foreign key constraint that refers to table directors.
ALTER TABLE Movies ADD CONSTRAINT FK_D FOREIGN KEY(Director) REFERENCES
Directors(id);
3. Modify the movies table to create a new constraint so the uniqueness of the movie title is
guaranteed.
ALTER TABLE movies ADD CONSTRAINT U_TITLE UNIQUE (title);
4. Write insert statements to add the following data to table directors and movies.
Director
directorid First name Last name
1010 Rob Minkoff
1020 Bill Condon
1050 Josh Cooley
2010 Brad Bird
3020 Lake Bell
DBS211 – Introduction to Database Systems
DBS211 – Introduction to Database Systems
(200, 'Beauty and the Beast', 2017, 1050, 4.20), (300, 'Toy Story 4', 2019, 1020, 4.50), (400, 'Mission
Impossible', 2018, 2010, 5.00),
Movies
id title year director 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
Due to the necessity of deleting all child tables before deleting a parent table, the sequence is
important. Foreign keys used in child tables prevent the deletion of the parent table. It is
necessary to remove the child table first as a result.
DROP TABLES Castings, Movies, Actors, Directors;
7. Modify table 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.
ALTER TABLE employee2 ADD username varchar(40)NULL;
9. Re-insert all data from the employees table into your new table employee2 using a single
statement.
10. In table employee2, write a SQL statement to change the first name and the last name of
employee with ID 1002 to your name.
UPDATE employee2 SET name = 'George, lastname = 'Smith WHERE employeeNumber = 1002;
DBS211 – Introduction to Database Systems
11. In table 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.
SET SQL_SAFE_UPDATES = 0; #turns off safe mode UPDATE employee2 SET username =
CONCAT(SUBSTRING(employee2.name, 1, 1), employee2.lastname);