Assignment# 3 - Creation of Tables and Constraints
Assignment# 3 - Creation of Tables and Constraints
2Exercises 16 - 20
HARJOT SINGH C0822257
(Total marks: 100, Weight: 5%)
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
1002 Administration
1003 Men's Clothing
1004 Women's Clothing
1005 Kids
1006 Toys
1
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 If unknown, use '1000'
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
Table 1:
Create table assignment.locations(
locationid int NOT NULL,
city char(20) NOT NULL,
storemanager int NULL,
primary key (locationid)
);
Table 2:
Create table assignment.locations_departments(
locationid int NOT NULL,
department_id int NOT NULL,
department_manager int NULL,
CONSTRAINT pk_locationsanddepartments PRIMARY KEY (locationid,department_id)
);
Table 3:
CREATE TABLE assignment.department(
department_id int NOT NULL,
department_name char(50) NOT NULL,
PRIMARY KEY (department_id)
);
Table 4:
CREATE TABLE assignment.employee(
employee_id int NOT NULL,
firstname char(15) NOT NULL,
middleinitial char(1) NULL,
lastname char(15) NOT NULL,
Database Design & SQL CSD 2206
departments data
INSERT INTO assignment.department(department_id,department_name)
VALUES (1001,'IT');
INSERT INTO assignment.department(department_id,department_name)
VALUES (1002,'Administration');
INSERT INTO assignment.department(department_id,department_name)
VALUES (1003,'Mens Clothing');
INSERT INTO assignment.department(department_id,department_name)
VALUES (1004,'Womens Clothing');
INSERT INTO assignment.department(department_id,department_name)
VALUES (1005,'Kids');
INSERT INTO assignment.department(department_id,department_name)
VALUES (1006,'Toys');
select * from assignment.department;
Database Design & SQL CSD 2206
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
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