LAB TASK 5 WORD

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

DATABASE LAB

EMAAN IMRAN

247055
A). Apply PK, FK and Not Null constraints in the Employee and

Department tables created in Assignment 1.

Copied code:

CREATE DATABASE COMPANY;

GO

USE COMPANY;

-- Create Employee table with PK, FK, and NOT NULL constraints

CREATE TABLE Employee (

FirstName NVARCHAR(50) NOT NULL,

MiddleInitial CHAR(1),

LastName NVARCHAR(50) NOT NULL,

SSN CHAR(9) PRIMARY KEY, -- Primary Key

BirthDate DATE NOT NULL,

Address NVARCHAR(100) NOT NULL,

Gender CHAR(1) NOT NULL,

Salary DECIMAL(10, 2) NOT NULL,

SupervisorSSN CHAR(9) NULL, -- Foreign Key for self-referencing (supervisor)

DepartmentNumber INT NOT NULL -- Foreign Key for Department table

);

GO
-- Create Department table with PK and NOT NULL constraints

CREATE TABLE Department (

DepartmentName NVARCHAR(50) NOT NULL,

DepartmentNumber INT PRIMARY KEY, -- Primary Key

ManagerSSN CHAR(9) NOT NULL, -- Foreign Key for Employee table

StartDate DATE NOT NULL

);

GO

-- Insert data into Employee table

INSERT INTO Employee(FirstName, MiddleInitial, LastName, SSN, BirthDate, Address,

Gender, Salary, SupervisorSSN, DepartmentNumber) VALUES

('John', 'B', 'Smith', '123456789', '1955-01-09', '731 Fondren, Houston, TX', 'M', 30000,

'987654321', 5),

('Franklin', 'T', 'Wong', '333445555', '1945-12-08', '638 Voss, Houston, TX', 'M', 40000,

'888665555', 5),

('Joyce', 'A', 'English', '453453453', '1962-07-31', '5631 Rice, Houston, TX', 'F', 25000,

'333445555', 5),

('Ramesh', 'K', 'Narayan', '666884444', '1952-09-15', '975 Fire Oak, Humble, TX', 'M', 38000,

'333445555', 5),

('James', 'E', 'Borg', '888665555', '1927-11-10', '450 Stone, Houston, TX', 'M', 55000, NULL, 1),
('Jennifer', 'S', 'Wallace', '987654321', '1931-06-20', '291 Berry, Bellaire, TX', 'F', 43000,

'888665555', 4),

('Ahmad', 'V', 'Jabbar', '987987987', '1959-03-29', '980 Dallas, Houston, TX', 'M', 25000,

'987654321', 4),

('Alicia', 'J', 'Zelaya', '999887777', '1958-07-19', '3321 Castle, Spring, TX', 'F', 25000,

'987654321', 4);

GO

-- Insert data into Department table

INSERT INTO Department (DepartmentName, DepartmentNumber, ManagerSSN, StartDate)

VALUES

('Headquarters', 1, '888665555', '1971-06-19'),

('Administration', 4, '987654321', '1985-01-01'),

('Research', 5, '333445555', '1978-05-22'),

('Automation', 7, '123456789', '2005-10-06');

GO

-- Add Foreign Key on Employee.SupervisorSSN referencing Employee.SSN (self-referencing

FK)

ALTER TABLE Employee

ADD CONSTRAINT FK_SupervisorSSN

FOREIGN KEY (SupervisorSSN) REFERENCES Employee(SSN);


-- Add Foreign Key on Employee.DepartmentNumber referencing

Department.DepartmentNumber

ALTER TABLE Employee

ADD CONSTRAINT FK_DepartmentNumber

FOREIGN KEY (DepartmentNumber) REFERENCES Department(DepartmentNumber);

-- Add Foreign Key on Department.ManagerSSN referencing Employee.SSN

ALTER TABLE Department

ADD CONSTRAINT FK_ManagerSSN

FOREIGN KEY (ManagerSSN) REFERENCES Employee(SSN);

GO

--Retrieve all employees in department 5 whose salary is between $30,000 and

$40,000.

SELECT *

FROM Employee

WHERE DepartmentNumber = 5

AND Salary BETWEEN 30000 AND 40000;

--Retrieve SSN and department name for all employees

SELECT E.SSN, D.DepartmentName

FROM Employee E

JOIN Department D ON E.DepartmentNumber = D.DepartmentNumber;

--Retrieve the names of all employees who do not have supervisors.

SELECT FirstName, LastName


FROM Employee

WHERE SupervisorSSN IS NULL;

--For each employee, retrieve the employee’s first and last name and the first and last

name of his or her immediate supervisor.

SELECT E.FirstName AS EmployeeFirstName, E.LastName AS EmployeeLastName,

S.FirstName AS SupervisorFirstName, S.LastName AS SupervisorLastName

FROM Employee E

LEFT JOIN Employee S ON E.SupervisorSSN = S.SSN;

--Retrieve the name and address of all employees who work for the ‘Research’

department.

SELECT E.FirstName, E.LastName, E.Address

FROM Employee E

JOIN Department D ON E.DepartmentNumber = D.DepartmentNumber

WHERE D.DepartmentName = 'Research';

Screenshot:
RESULT:

1.

2.
3.

4.

5.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy