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

Sesa SQL Query

This document contains SQL code to create a database called WasteManagement and associated tables for a waste management system. It first checks if the database exists and creates it if not. It then creates tables for clients, collections, depots, drivers, dustbins, requests, and trucks. Finally it adds foreign key constraints between the tables.

Uploaded by

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

Sesa SQL Query

This document contains SQL code to create a database called WasteManagement and associated tables for a waste management system. It first checks if the database exists and creates it if not. It then creates tables for clients, collections, depots, drivers, dustbins, requests, and trucks. Finally it adds foreign key constraints between the tables.

Uploaded by

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

/*

* ER/Studio Data Architect SQL Code Generation


* Project : Waste Management Model 3.DM1
*
* Date Created: Friday, October 20, 2023 22:01:31
* Target DBMS: Microsoft SQL Server 2017
*/

-- Use the master database to create the new database


USE master;
GO

-- Check if the database already exists; if not, create it


IF NOT EXISTS (SELECT 1 FROM sys.databases WHERE name = 'WasteManagement')
BEGIN
CREATE DATABASE WasteManagement;
PRINT '<<< CREATED DATABASE WasteManagement >>>';
END
ELSE
PRINT '<<< DATABASE WasteManagement ALREADY EXISTS >>>';
GO

-- Use the WasteManagement database for the following operations


USE WasteManagement;
GO

-- Create the Client table


CREATE TABLE Client (
ClientID INT IDENTITY(1, 1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
OtherNames NVARCHAR(100),
LastName NVARCHAR(50) NOT NULL,
Gender NCHAR(10) NOT NULL,
PhoneNo NVARCHAR(15) NOT NULL,
Email NVARCHAR(100),
GPSAddress NVARCHAR(20) NOT NULL
);
GO

-- Create the Collection table


CREATE TABLE Collection (
CollectionID INT IDENTITY(1, 1) PRIMARY KEY,
CollectionDate DATETIME NOT NULL,
CollectionTime TIME(7),
DriverID INT NOT NULL,
TruckID INT NOT NULL,
DustbinID INT NOT NULL
);
GO

-- Create the Depot table


CREATE TABLE Depot (
DepotID INT IDENTITY(1, 1) PRIMARY KEY,
DepotName NVARCHAR(50) NOT NULL,
Location NVARCHAR(20) NOT NULL
);
GO

-- Create the Driver table


CREATE TABLE Driver (
DriverID INT IDENTITY(1, 1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
OtherNames NVARCHAR(100),
LastName NVARCHAR(50) NOT NULL,
PhoneNo NVARCHAR(15) NOT NULL,
Email NVARCHAR(50),
LicenseNo NVARCHAR(20),
DepotID INT NOT NULL
);
GO

-- Create the Dustbin table


CREATE TABLE Dustbin (
DustbinID INT IDENTITY(1, 1) PRIMARY KEY,
IsDamaged NVARCHAR(10) NOT NULL,
ClientID INT NOT NULL
);
GO

-- Create the Request table


-- Create the Request table with a BIT data type for IsCollected
CREATE TABLE Request (
RequestID INT IDENTITY(1, 1) PRIMARY KEY,
RequestDate DATETIME NOT NULL,
IsCollected BIT, -- Change the data type to BIT
RequestStatement NVARCHAR(300) NOT NULL,
RequestTime TIME(7),
ClientID INT NOT NULL,
DriverID INT NOT NULL,
);
GO

-- Create the Truck table


CREATE TABLE Truck (
TruckID INT IDENTITY(1, 1) PRIMARY KEY,
TruckType NVARCHAR(10) NOT NULL,
LicensePlateNo NVARCHAR(15) NOT NULL,
IsDrive NVARCHAR(10) NOT NULL,
DepotID INT NOT NULL
);
GO

-- Add foreign key constraints to the Collection table


ALTER TABLE Collection
ADD CONSTRAINT FK_Driver_Collection FOREIGN KEY (DriverID) REFERENCES Driver
(DriverID);

ALTER TABLE Collection


ADD CONSTRAINT FK_Truck_Collection FOREIGN KEY (TruckID) REFERENCES Truck
(TruckID);

ALTER TABLE Collection


ADD CONSTRAINT FK_Dustbin_Collection FOREIGN KEY (DustbinID) REFERENCES Dustbin
(DustbinID);
GO

-- Add foreign key constraint to the Driver table


ALTER TABLE Driver
ADD CONSTRAINT FK_Depot_Driver FOREIGN KEY (DepotID) REFERENCES Depot (DepotID);
GO

-- Add foreign key constraint to the Dustbin table


ALTER TABLE Dustbin
ADD CONSTRAINT FK_Client_Dustbin FOREIGN KEY (ClientID) REFERENCES Client
(ClientID);
GO

-- Add foreign key constraints to the Request table


ALTER TABLE Request
ADD CONSTRAINT FK_Client_Request FOREIGN KEY (ClientID) REFERENCES Client
(ClientID);

ALTER TABLE Request


ADD CONSTRAINT FK_Driver_Request FOREIGN KEY (DriverID) REFERENCES Driver
(DriverID);
GO

-- Add foreign key constraint to the Truck table


ALTER TABLE Truck
ADD CONSTRAINT FK_Depot_Truck FOREIGN KEY (DepotID) REFERENCES Depot (DepotID);
GO

You might also like