0% found this document useful (0 votes)
4 views6 pages

DBS211_Lab04

Lab 04 of DBS211 focuses on DDL statements in SQL, teaching students to create, modify, and manage database tables while ensuring data integrity. Students will create tables for Movies, Actors, Castings, and Directors, establish constraints, and perform data manipulation tasks. The lab culminates in the removal of created tables and emphasizes the importance of order in table deletion due to foreign key dependencies.

Uploaded by

48ph4fpyhz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

DBS211_Lab04

Lab 04 of DBS211 focuses on DDL statements in SQL, teaching students to create, modify, and manage database tables while ensuring data integrity. Students will create tables for Movies, Actors, Castings, and Directors, establish constraints, and perform data manipulation tasks. The lab culminates in the removal of created tables and emphasizes the importance of order in table deletion due to foreign key dependencies.

Uploaded by

48ph4fpyhz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DBS211 – Introduction to Database Systems

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:

 create, modify, and drop tables based on design specifications provided,


 inserting new data into tables, update data in tables, and delete data from tables while
considering referential integrity,
 enforce constraints on tables to ensure data integrity and consistency,
 create a table using the structure and data from an existing table,
 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.

Tasks:
Add
SET AUTOCOMMIT ON;
under the comment header and execute it

Consider the following table specifications:

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))

Column Column PK Not Unique FK Default Validation


Name DataType Null Value
mid Int ✓
title varchar(35) ✓
releaseYear Int ✓
director Int ✓
score decimal(3,2) between 0
and 5
DBS211 – Introduction to Database Systems

CREATE TABLE Movies (

id int not null,

title varchar (35) not null,

year int not null,

Director int not null,

score decimal (3.2) null,

Primary Key (id),

Constraint SCORE

CHECK (score < 5 AND score > 0)

};

ACTORS (actorid:int, name:varchar(20), lastname:varchar(30))

Column Column PK Not Unique FK Default Validation


Name DataType Null Value
aid Int ✓
firstName varchar(20) ✓
lastName Varchar(30) ✓
CREATE TABLE Actors (

id int,

name varchar (20) not null,

Lastname varchar (30) not null,

Constraint Actors_id_PK

Primary Key (id)

);
CASTINGS (movieid:int, actorid:int)

Column Column PK Not Unique FK Default Validation


Name DataType Null Value
movieid Int ✓ ✓
(movies
)
actorid int ✓ ✓
(actors)
DBS211 – Introduction to Database Systems

CREATE TABLE Castings (

movieid int not null, actorid int not null,

Primary Key (movieid, actorid)

Foreign Key (movieid) REFERENCES Movies (id)

Foreign Key (movieid) REFERENCES Actors (id)

};
DIRECTORS(id:int, name:varchar(20), lastname:varchar(30))

Column Column PK Not Unique FK Default Validation


Name DataType Null Value
directorid Int ✓
firstname varchar(20) ✓
lastname varchar(30) ✓

CREATE TABLE Directors (

id int null,

name varchar (20) not null,

Lastname varchar (30) not null,

Primary Key (id)

};

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

INSERT INTO Movies (id, title, year, director, score)

VALUES (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);

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

5. Write SQL statements to remove all above tables.


Is the order of tables important when removing? Why?

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;

Part B (More DML):


6. Create a new empty table employee2 the same as table employees. Use a single statement to
create the table and insert the data at the same time.
CREATE TABLE employee2 ( employeeNumber int not null PRIMARY KEY, FOREIGN KEY
(officeCode) REFERENCES employees(officeCode)) AS (SELECT * FROM employees);

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;

8. Delete all the data in the employee2 table

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);

12. In table employee2, remove all employees with office code 4.


DELETE FROM employee2 WHERE officeCode=4;

13. Drop table employee2.

You might also like