You are on page 1of 7

SEU/IS/18/ICT/005

Practical Recording.

Date: 03rd September 2020.


Practical Number: 07.
Title: MySQL Constraints.
Aims:
• Getting basic knowledge on MySQL constraints.
• Getting practice to define constraints in MySQL.
Task:
1. Defining NOT NULL.
 By default, a column can hold NULL values. The NOT NULL constraint enforces
a column to NOT accept NULL values. This enforces a field to always contain a
value, which means that you cannot insert new record, or update a record without
adding a value to this field.

2. Defining UNIQUE.
 The UNIQUE constraint ensures that all values in a column are different. Both the
UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for
a column or set of columns.

3. Defining PRIMARY KEY.


 A PRIMARY KEY constraint automatically has a UNIQUE constraint. However,
you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.

4. Defining FOREIGN KEY.


 A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY
is a field (or collection of fields) in one table that refers to the PRIMARY KEY in
another table. The table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent table.

5. Defining DEFAULT.
 The DEFAULT constraint is used to provide a default value for a column. The
default value will be added to all new records IF no other value is specified.

Page 1 of 7
SEU/IS/18/ICT/005

Exercise 01.

The following table named Car from the Database Vehicle shows the information of available
cars. Each car can be uniquely identified by its License Number (PRIMARY KEY). However,
the Engine Serial number is also unique (UNIQUE KEY) to each of the cars.

Create the following table accordingly.

Page 2 of 7
SEU/IS/18/ICT/005

Exercise 02.
The database named Trip keeps track of business trips of salespersons in a sales office. The
following tables (relations) are available in the database Trip.
 SALESPERSON (NIC, Name, Start_year)

 TRIP (NIC, From_city, To_city, Departure_date, Return_date, Trip_id)

Page 3 of 7
SEU/IS/18/ICT/005

 EXPENSE (Trip_id, Account_Num, Amount)

Exercise 03.
1. Create a database called Company.

2. Create the following tables as shown below. (Add the necessary keys as suitable)

Page 4 of 7
SEU/IS/18/ICT/005

Page 5 of 7
SEU/IS/18/ICT/005

3. Populate the data given below to their respective tables.

Page 6 of 7
SEU/IS/18/ICT/005

Page 7 of 7

You might also like