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

Advanced Database Lab

The document describes creating several database tables to model a normal form database for projects, employees, customers, and related entities. It shows the creation of tables for projects, employees, project employees, job orders, customers, project feedback, and contact persons. It then describes applying first, second, and third normal form rules by adding primary keys, splitting columns into new tables, and removing functional dependencies to normalize the database schema.

Uploaded by

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

Advanced Database Lab

The document describes creating several database tables to model a normal form database for projects, employees, customers, and related entities. It shows the creation of tables for projects, employees, project employees, job orders, customers, project feedback, and contact persons. It then describes applying first, second, and third normal form rules by adding primary keys, splitting columns into new tables, and removing functional dependencies to normalize the database schema.

Uploaded by

Mercy Dega
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

--create database NormalForm

use master
CREATE TABLE Projects(
[ID] INT PRIMARY KEY IDENTITY,
[Name] VARCHAR(100),
[Value] DECIMAL(5,2),
StartDate DATETime,
EndDate DATETime)

---
use NormalForm
CREATE TABLE Employees(
[ID] INT PRIMARY KEY IDENTITY,
[FirstName] VARCHAR(50),
[LastName] VARCHAR(50),
[HourlyWage] DECIMAL(5,2),
[HireDate] DATE)
--
use NormalForm
CREATE TABLE ProjectEmployees(
[ID] INT PRIMARY KEY IDENTITY,
[ProjectID] INT,
[EmployeeID] INT,
[Hours] DECIMAL(5,2),
CONSTRAINT FK_ProjectEmployees_Projects FOREIGN KEY ([ProjectID]) REFERENCES [Projects]
([ID]),
CONSTRAINT FK_ProjectEmployees_Employees FOREIGN KEY ([EmployeeID]) REFERENCES
[Employees] ([ID])
)
--
CREATE TABLE JobOrders(
[ID] INT PRIMARY KEY IDENTITY,
[EmployeeID] INT,
[ProjectID] INT,
[Description] TEXT,
[OrderDateTime] DATETIME,
[Quantity] INT,
[Price] DECIMAL(5,2),
CONSTRAINT FK_JobOrders_Projects FOREIGN KEY ([ProjectID]) REFERENCES [Projects] ([ID]),
CONSTRAINT FK_JobOrders_Employees FOREIGN KEY ([EmployeeID]) REFERENCES [Employees]
([ID])
)
--
CREATE TABLE Customers (
[Name] VARCHAR(100),
[Industry] VARCHAR(100),
[Project1_ID] INT,
[Project1_Feedback] TEXT,
[Project2_ID] INT,
[Project2_Feedback] TEXT,
[ContactPersonID] INT,
[ContactPersonAndRole] VARCHAR(255),
[PhoneNumber] VARCHAR(12),
[Address] VARCHAR(255),
[City] VARCHAR(255),
[Zip] VARCHAR(5)
)
--Apply 1NF Rule 1
--Here we need to apply first normal form by adding primary key into Customer table
ALTER TABLE [Customers]
ADD [ID] INT IDENTITY PRIMARY KEY
--Apply 1NF rule 2
--to rename the exsting column use the ff query
sp_rename 'Customers.[ContactPersonAndRole]', 'ContactPerson', 'COLUMN'
GO
ALTER TABLE [Customers]
ADD [ContactPersonRole] VARCHAR(20)
GO
--Apply 1NF Rule 3
ALTER TABLE [Customers]
DROP COLUMN Project1_ID
ALTER TABLE [Customers]
DROP COLUMN Project1_Feedback
ALTER TABLE [Customers]
DROP COLUMN Project2_ID
ALTER TABLE [Customers]
DROP COLUMN Project2_Feedback
GO
--Now create a new tabele
use NormalForm
CREATE TABLE ProjectFeedback1(
[ID] INT PRIMARY KEY IDENTITY,
[ProjectID] INT,
[CustomerID] INT,
[Feedback] TEXT,
CONSTRAINT FK_ProjectFeedbacks_Projects FOREIGN KEY ([ProjectID]) REFERENCES [Projects]
([ID]),
CONSTRAINT FK_ProjectFeedbacks_Customers FOREIGN KEY ([CustomerID]) REFERENCES
[Customers] ([ID])
)
--Now apply 2NF by Dropping the three columns and by creating a new table
ALTER TABLE [Customers]
DROP COLUMN ContactPerson
ALTER TABLE [Customers]
DROP COLUMN ContactPersonRole
ALTER TABLE [Customers]
DROP COLUMN PhoneNumber
--In order to apply or to satisfy 2NF creat a new table ContactPersons
CREATE TABLE ContactPersons(
[ID] INT PRIMARY KEY IDENTITY,
[ContactPerson] VARCHAR(100),
[ContactPersonRole] VARCHAR(20),
[PhoneNumber] VARCHAR(12)
)
--Now create a relathionship b/n customer table and ContactPersons
ALTER TABLE [Customers]
ADD CONSTRAINT FK_Customers_ContactPersons FOREIGN KEY ([ContactPersonID])
REFERENCES ContactPersons([ID])
--Apply 3NF
--In order to apply 3NF first drop the column and create a new table
--Drop
ALTER TABLE [Customers]
DROP COLUMN City
GO
--Create a new table
CREATE TABLE ZipCodes(
[ZipID] VARCHAR(5) PRIMARY KEY,
[City] VARCHAR(255)
)

select * from Customers

You might also like