0% found this document useful (0 votes)
275 views

Assignment# 3 - Creation of Tables and Constraints

The document describes the design of four database tables - locations, locations_departments, departments, and employees - including the attributes, data types, constraints, and sample data for each table. It provides SQL commands to create the tables with constraints and insert the sample data. It then lists tests for each constraint by attempting invalid insert statements and verifying error messages are returned.

Uploaded by

Harjot Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
275 views

Assignment# 3 - Creation of Tables and Constraints

The document describes the design of four database tables - locations, locations_departments, departments, and employees - including the attributes, data types, constraints, and sample data for each table. It provides SQL commands to create the tables with constraints and insert the sample data. It then lists tests for each constraint by attempting invalid insert statements and verifying error messages are returned.

Uploaded by

Harjot Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Database Design & SQL CSD 2206

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

1. Create the 4 tables and include the constraints


Solution:
create schema assignment;

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

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.
INSERT INTO assignment.locations(locationid,city)
VALUES (11,'sarnia');
INSERT INTO assignment.locations(locationid,city)
VALUES (22,'london');
INSERT INTO assignment.locations(locationid,city)
VALUES (33,'ontario');
select * from assignment.locations;

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

> PRIMARY KEY (locationid) :


Insert into assignment.locations (locationid, city) values (11, 'Jalandhar')

> CONSTRAINT pk_locationsanddepartments PRIMARY KEY (locationid,department_id) :


insert into assignment.locations_departments (locationid, department_id) values (110, 211)

> PRIMARY KEY (department_id):


insert into assignment.department (department_id,department_name) values (1004, 'Customer
Relation')
Database Design & SQL CSD 2206

> PRIMARY KEY (employee_id)

insert into assignment.employee(employee_id,firstname, middleinitial,


lastname,hiredate,storelocation, salary) values (1101,'Har','K', 'Singh','07/02/2021',
'Ludhiana', 10000.88)

You might also like