Вы находитесь на странице: 1из 3

College of Computing Education

3rd Floor, DPT Building


Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Name :_______________________________ Date :__________________________________


Time :_______________________________ Prof :_______________________________

[ Laboratory No. 3.5: SQL Constraints]


Objectives:
1. To know more about MySQL constraints and its underlying concepts
2. To create a database that enforce referential integrity between each table

Materials:
1. PC or Laptop
2. WAMP/XAMPP Installer
3. Web Browser or CLI

Background

MySQL CONSTRAINT is used to define rules to allow or restrict what values can be stored in columns. The
purpose of inducing constraints is to enforce the integrity of a database. In addition, these are used to limit
the type of data that can be inserted into a table. This can be classified into two types - column level and
table level.

The column level constraints can apply only to one column whereas table level constraints are applied to the
entire table.

MySQL CONSTRAINT is declared at the time of creating a table. Below are as follows:

 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
 FOREIGN KEY - Uniquely identifies a row/record in another table
 NOT NULL - Ensures that a column cannot have a NULL value
 UNIQUE - Ensures that all values in a column are different
 CHECK - Ensures that all values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column when no value is specified
 INDEX - Use to create and retrieve data from the database very quickly

Look at the following two tables:

"tbl_Gender" table:
Gender_ID Gender_Title
1 Male
2 Female

"tbl_owner" table:
Owner_ID Owner_name Owner_age Gender_ID
1 Rob Stark 23 1
2 The Hound 32 2
3 Cersei 12 1
4 Lucifer 18 1

"tbl_pet" table:
Pet_ID Pet Name Pet_type Pet_Age Owner_ID Gender_ID
1 Mart Lannister Dragon 30 1 1
2 Sansa Stark Wolf 23 2 2
3 Jon Snow Dog 20 1 1

IT105/L Martzel P. Baste|Page 1


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

The following SQL creates a PRIMARY KEY on the "gender_id" column when the "tbl_gender"
table is created:

CREATE TABLE tbl_Gender (  PRIMARY KEY ensures that a field must NOT contain
gender_id int(3) PRIMARY KEY AUTO_INCREMENT, any NULL value and is UNIQUE.
gender_title varchar(6) UNUQUE,  AUTO_INCREMENT means gender_id field will generate
); value by incrementing automatically. See, first
value is 1, then next will be 2, 3 and so on and so
forth.

 UNIQUE ensures that a field value must NOT contain


any duplication. Hence, it is UNIQUE.

The following SQL creates a PRIMARY KEY on the "owner_id" column when the "tbl_owner"
table is created:

CREATE TABLE tbl_Owner ( NOT NULL is a constraint which ensures that a field must
owner_id int(8) PRIMARY KEY, not contain any NULL value.
owner_name varchar(50) NOT NULL,
owner_age int(3) DEFAULT 0, DEFAULT is a constraint which ensures that a field
should have values when not being assigned. In this case
);
age will be assigned 0 as value when not entered any
during taking of inputs.

The following SQL creates a PRIMARY KEY on the "pet_id" column when the "tbl_pet" table is
created:

CREATE TABLE tbl_Pet ( CHECK is used to limit the value range that can be
pet_id int(8) PRIMARY KEY, placed in a column. As stated pet_price must have
pet_name varchar(50) NOT NULL, assigned value more than to 100.
pet_type varchar(30) NOT NULL DEFAULT ‘Dog’,
pet_age int DEFAULT 0, GENDER_ID and OWNER_ID are a Foreign Keys of this table.
pet_price float(10,2) , CHECK(pet_price>100), Gender_ID is Primary Key of the tbl_gender and Owner_Id
gender_id int(3), is a primary key of tbl_owner. We need to establish PK and
owner_id int(8), FK relationship between these tables so it will prevent actions
that would destroy links between them. The FK constraint
); also prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values
contained in the table it points to.

This statement does the linking or establishing of


ALTER TABLE tbl_pet add foreign key(gender_id)
connections to the tbl_pet and tbl_gender. Also, with
references tbl_gender(gender_id); the tbl_pet and tbl_owner. Invoke them once you are done
creating tbl_pet. Or unless you will choose to invoke
ALTER TABLE tbl_pet add foreign key(owner_id) the next DDL or command below.
references tbl_owner(owner_id);

CREATE TABLE tbl_Pet (


pet_id int(8) PRIMARY KEY,
pet_name varchar(50) NOT NULL,
pet_type varchar(30) NOT NULL DEFAULT ‘Dog’,
pet_age int DEFAULT 0,
pet_price float(10,2) , CHECK(pet_price>100),
gender_id int(3), foreign key(gender_id) references tbl_gender(gender_id),
owner_id int(8), foreign key(owner_id) references tbl_owner(owner_id),

);

IT105/L Martzel P. Baste|Page 2


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Instructions:

Based on the ERD given, convert it into Physical database by creating a table for each entity:

NOTE: Provide a screenshot (or paste in a notepad) for each action you performed.

1. Creation of each table and defining constraints


2. Print and show the data dictionary of all tables.
3. Store atleast 5 records for all of the tables, EXCEPT tbl_gender which must contain only 2 rows.

IT105/L Martzel P. Baste|Page 3

Вам также может понравиться