0% found this document useful (0 votes)
66 views6 pages

Database Lab Assignment 4

This document contains the details of an assignment to create tables for an employee database. It includes: 1. SQL queries to create tables for Employees, Departments, Jobs, and Locations with the relevant columns and constraints. 2. SQL queries to insert sample data into the tables. 3. Additional tasks to modify the table structures, such as setting default values and making certain columns mandatory. 4. A task to delete a location and update any associated departments.

Uploaded by

Sameer Khowaja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
66 views6 pages

Database Lab Assignment 4

This document contains the details of an assignment to create tables for an employee database. It includes: 1. SQL queries to create tables for Employees, Departments, Jobs, and Locations with the relevant columns and constraints. 2. SQL queries to insert sample data into the tables. 3. Additional tasks to modify the table structures, such as setting default values and making certain columns mandatory. 4. A task to delete a location and update any associated departments.

Uploaded by

Sameer Khowaja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

DATABASE SYSTEMS LAB

(BS-DS-2A)

NAME : SAMEER KHOWAJA


REG- NO : DS-221039
YEAR : SPRING 2022
ASSIGNMENT NO 4
CREATE THE FOLLOWING TABLES AND SHOW THEIR STRUCTURES:

Employee(employeeID, firstName, lastName, hiringDate, deptID, jobID),


Department(deptID, deptName, locationID),
Job(jobID, jobTitle, salary),
Location(locationID, city, country).
Job IDs should have the format “ENG123” for an engineer, “MAN456” for a manager etc.
It is not necessary that all employees may be assigned a department or that a department may have employees.
It is also not necessary that each designation may be filled.
SQL queries:

CREATE TABLE Location (


locationID INT PRIMARY KEY AUTO_INCREMENT,
city VARCHAR(50) DEFAULT 'Karachi',
country VARCHAR(50) DEFAULT 'Pakistan'
);
CREATE TABLE Department (
deptID INT PRIMARY KEY AUTO_INCREMENT,
deptName VARCHAR(50) NOT NULL,
locationID INT,
FOREIGN KEY (locationID) REFERENCES Location(locationID)
);

CREATE TABLE Job (


jobID VARCHAR(10) PRIMARY KEY,
jobTitle VARCHAR(50) NOT NULL,
salary DECIMAL(10, 2)
);
CREATE TABLE Employee (
employeeID INT PRIMARY KEY AUTO_INCREMENT,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(50) NOT NULL,
hiringDate DATE,
deptID INT,
jobID VARCHAR(10),
FOREIGN KEY (deptID) REFERENCES Department(deptID),
FOREIGN KEY (jobID) REFERENCES Job(jobID)
);
Output:

2. ENSURE THAT ALL NUMERICAL IDS INCREMENT AUTOMATICALLY. SHOW THE RELEVANT TABLE
STRUCTURES.

NOTE: TASK 2 for auto increment has been done already in task 1.
SQL Queries:

INSERT INTO Employee (firstName, lastName, hiringDate, deptID, jobID)


VALUES ('Hannan', 'Ali', '2022-01-01', 1, 'ENG123'),
('Sameer', 'Rizvi', '2022-02-01', 2, 'MAN456'),
('Yousuf', 'Shehzad', '2022-03-01', 3, 'ADM789'),
('Faizan', 'Khan', '2022-04-01', 4, NULL);

INSERT INTO Department (deptName, locationID)


VALUES ('Engineering', 1),
('Management', 2),
('Administration', NULL),
('Marketing', 1);

INSERT INTO Job (jobID, jobTitle, salary)


VALUES ('ENG123', 'Engineer', 50000),
('MAN456', 'Manager', 80000),
('ADM789', 'Administrator', NULL);
INSERT INTO Location (city, country)
VALUES ('Karachi', 'Pakistan'),
('Lahore', 'Pakistan'),
('Islamabad', 'Pakistan');

3. SET THE LOCATION FIELD TO HOLD THE VALUE “KARACHI, PAKISTAN” UNLESS OTHERWISE
SPECIFIED. SHOW THE RELEVANT TABLE STRUCTURES.

SQL QUERY:

ALTER TABLE Location


ALTER COLUMN city SET DEFAULT 'Karachi';

4. INSERT AT LEAST 4 RELEVANT RECORDS IN EACH TABLE. DISPLAY ALL THE DATA.

ALREADY ENTERED DATA IN FIRST STEP

Table No 1: EMPLOYEES
TABLE NO 2: Department

TABLE NO 3: JOB
TABLE NO 4:

5. ENSURE THAT EMPLOYEE NAMES, DEPARTMENT NAMES AND JOB TITLES ARE NEVER EMPTY.
SHOW THE RELEVANT TABLE STRUCTURES.

ALTER TABLE Employee


MODIFY firstName VARCHAR(50) NOT NULL,
MODIFY lastName VARCHAR(50) NOT NULL;
ALTER TABLE Department
MODIFY deptName VARCHAR(50) NOT NULL;
ALTER TABLE Job
MODIFY jobTitle VARCHAR(50) NOT NULL;

6. DELETE ANY LOCATION AND ENSURE THAT ALL DEPARTMENTS AT THAT LOCATION HAVE THEIR
LOCATIONS SET TO NULL. SHOW ALL RELEVANT QUERIES, TABLE STRUCTURES, AND DATA.

DELETE FROM Location


WHERE locationID = 2;
UPDATE Department
SET locationID = NULL
WHERE locationID = 2;

You might also like