MySQL Practical 2
MySQL Practical 2
CREATE DATABASE database_name;
Activity 1
CREATE DATABASE EXERCISE1;
Type CREATE DATABASE EXERCISE1;
Click Go button
Page 1|6
M. Chinyuku Database Systems 2022
Create Table
2. Create Table syntax
Activity 2
Example: create a table called PET with the following structure. PET (NAME, OWNER, SPECIES, SEX,
BIRTH, DEATH)
Table 1
Pet
Name Owner Specie Sex Birth Death
s
Foxy John Dog M 15 October 2013 1 January 2020
Kwaipa John Cat F 21 October 2015 31 October 2020
Delphi Mary Cat F 14 December 2019 5 September 2020
Page 2|6
M. Chinyuku Database Systems 2022
Exercise 1
The following business rules describe the TEACH database:
ERD is a conceptual model that shows all the data requirements for a organization.
Use SQL script to create database TEACH using the Relational Schemas below: refer to lecture 8
LECTURER(LNum, LName)
Solution
Page 3|6
M. Chinyuku Database Systems 2022
Restrict
• RESTRICT option prevents the removal (i.e. using delete) or modification (i..e using an update) of
rows from the parent table.
• means that any attempt to delete and/or update the parent will fail throwing an error.
NO Action
• There will not be any change in the referencing rows when the referenced ones are deleted or
updated.
• Same as restrict
Cascade
• Delete: the referencing rows will be deleted automatically along with the referenced ones.
• IF WE DELETE row 1(a1) FROM parent it will delete rows 1 and 2 entries from child.
• CASCADE if the parent primary key is changed, the child value will also change to reflect that.
Page 4|6
M. Chinyuku Database Systems 2022
• IF WE Update in row 1(a1) FROM parent it will update rows 1 and 2 entries from child.
Set Null
• On Delete/Update the value of referencing record will be set to NULL automatically along with
the referenced ones.
Activity 3
Develop a database with the following business requirements
“A departments have several lecturers. A department should have at least one
lecturer. A department is defined by a unique name. Each lecturer should belong
to only one department and can teach at least one course. A lecturer is defined by
a unique lecturer number and name. Each course has a unique ID and name. A
lecturer teaches a course during specific times in various venues.”
Solution
ERD
Page 5|6
M. Chinyuku Database Systems 2022
Relational Schemas
Department(DeptName)
Course(CourseID, CourseName, )
Use your own data to populate the tables you created above
Page 6|6