You are on page 1of 6

Database Design & SQL CSD 2206

Assignment 4
Submitted by:
Protsan Karki
Aashim Salhotra
Jargraj Singh
Saubhagya Rai
Apil Kumal
Sugam Subedi

After further analysis, the following constraints (business rules) have been determined:
1 locations
Attribute Data Type Length NULL Capable Constraints
Location ID (PK) Numeric 2,0 N
City Character 20 N
Store Manager Numeric 5,0 Y Must be a valid employee

locations data:
location_id city store_manager
11 Sarnia null
22 London null
33 Toronto null

2 locations_departments
Attribute Data Type Length NULL Capable Constraints
location ID (PK) Numeric 2,0 N Must be a valid location
department_id (PK) Numeric 4,0 N Must be a valid department
department_manager Numeric 5,0 Y Must be a valid employee

3 departments
Attribute Data Type Length NULL Capable
Department ID (PK) Numeric 4,0 N
Department name Character 50 N

departments data:
department_id department_name
1001 IT
1
Database Design & SQL CSD 2206

1002 Administration
1003 Men's Clothing
1004 Women's Clothing
1005 Kids
1006 Toys

2
Database Design & SQL CSD 2206

4 employees
Attribute Data Type Length NULL Constraints
Capable
Employee ID (PK) Numeric 5,0 N • Unique identifier
First name Character 15 N
Middle Initial Character 1 Y
Last Name Character 15 N
Hire Date Date 10 N • If unknown, use current date
• Must be greater than birth date
Store Location Numeric 2,0 N • Must be a valid location
Work Department Numeric 4,0 Y • Must be a valid department

Job Class Character 1 Y • If unknown, use 'T'


• Must be 'T', 'J', 'C', or 'M'
Coach ID Numeric 5,0 Y • Must be a valid employee
Salary Numeric 9,2 N • Must be less than 92000.00
• Must be greater than commission
Bonus Numeric 7,2 Y • Must have commission or bonus
Commission Numeric 7,2 Y • Must have commission or bonus

1. Create the 4 tables and include the constraints


Answer :
create schema Locations_1;

use Locations_1;

Create table Locations_1.locations(


locationid int NOT NULL,
city char(20) NOT NULL,
storemanager int NULL,
primary key (locationid)
);

Create table Locations_1.locations_departments(


locationid int NOT NULL,
department_id int NOT NULL,
department_manager int NULL,
CONSTRAINT pk_locationsanddepartments PRIMARY KEY (locationid,department_id)
);

CREATE TABLE Locations_1.department(


department_id int NOT NULL,
3
Database Design & SQL CSD 2206

department_name char(50) NOT NULL,


PRIMARY KEY (department_id)
);

CREATE TABLE Locations_1.employee(


employee_id int NOT NULL,
firstname char(15) NOT NULL,
middleinitial char(1) NULL,
lastname char(15) NOT NULL,
hiredate date NOT NULL,
storelocation int NOT NULL,
workdepartment int NULL,
jobclass char(1) NULL,
coachid int NULL,
salary dec(9,2) NOT NULL,
bonus dec(9,2) NULL,
commission dec(7,2) NULL,
PRIMARY KEY (employee_id)
);

2. Insert the data provided in the tables: locations data and departments data.
Answer :

-- locations data --
INSERT INTO Locations_1.locations(locationid,city)
VALUES (11,'sarnia');
INSERT INTO Locations_1.locations(locationid,city)
VALUES (22,'london');
INSERT INTO Locations_1.locations(locationid,city)
VALUES (33,'ontario');
select * from Locations_1.locations;

4
Database Design & SQL CSD 2206

-- departments data --
INSERT INTO Locations_1.department(department_id,department_name)
VALUES (1001,'IT');
INSERT INTO Locations_1.department(department_id,department_name)
VALUES (1002,'Administration');
INSERT INTO Locations_1.department(department_id,department_name)
VALUES (1003,'Mens Clothing');
INSERT INTO Locations_1.department(department_id,department_name)
VALUES (1004,'Womens Clothing');
INSERT INTO Locations_1.department(department_id,department_name)
VALUES (1005,'Kids');
INSERT INTO Locations_1.department(department_id,department_name)
VALUES (1006,'Toys');
select * from Locations_1.department;

3: Constraint Testing –
Test each constraint by listing each constraint and providing a test to validate that each constraint is
working:
a. Include an INSERT statement to "force" a constraint error. For example, modify the
employee_id so that it is the same as the employee_id in the previous row.
This will force a primary key error when the INSERT statement is run
5
Database Design & SQL CSD 2206

b. Run the INSERT statement and verify that the error occurred
c. Take a screen shot of the error and insert into the constraint testing document
d. Move to the next constraint and perform a constraint test

For Primary Key :


insert into Locations_1.locations (locationid, city) values (11, 'Pune');

For Constraint :
insert into Locations_1.locations_departments (locationid, department_id) values (110,
211);

For Primary Key :


insert into Locations_1.department (department_id,department_name) values (1004,
'Customer Relation');

For Primary Key :


insert into Locations_1.employee(employee_id, firstname, middleinitial, lastname,
hiredate, storelocation, salary) values (1101,'Aashi','m' 'Salhotra','05/11/2023',
'Mumbai', 51000.99);

You might also like