Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2) In SQL I am trying to Create a view. I will give you my script first then the

ID: 3737627 • Letter: 2

Question

2) In SQL I am trying to Create a view. I will give you my script first then the instructions.

CREATE DATABASE Computer_Inventory_System

CREATE TABLE Brands
(
   BrandKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   Brand varchar(40) NOT NULL,
   Active bit DEFAULT(1) NOT NULL
)

CREATE TABLE ComputerTypes
(
   ComputerTypeKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   ComputerType varchar(25) NOT NULL
)

CREATE TABLE ComputerStatuses
(
   ComputerStatusKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   ComputerStatus varchar(50) NOT NULL,
   ActiveStatus bit NOT NULL --an indicator of if this status means the computer is available or not
)

CREATE TABLE CPUTypes
(
   CPUTypeKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   CPUType varchar(40) NOT NULL
)

CREATE TABLE Computers
(
   ComputerKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   ComputerTypeKey int NOT NULL,
   BrandKey int NOT NULL,  
   ComputerStatusKey int NOT NULL DEFAULT(0),
   PurchaseDate date NOT NULL,
   PurchaseCost money NOT NULL,
   MemoryCapacityInGB int NOT NULL,
   HardDriveCapacityinGB int NOT NULL,
   VideoCardDescription varchar (255),
   CPUTypeKey int NOT NULL,
   CPUClockRateInGHZ decimal (6, 4)
)

SET IDENTITY_INSERT Computers ON
INSERT Computers (ComputerKey, ComputerTypeKey, BrandKey, ComputerStatusKey, PurchaseDate, PurchaseCost, MemoryCapacityInGB,
   HardDriveCapacityinGB, VideoCardDescription, CPUTypeKey, CPUClockRateInGHZ) VALUES
   (1, 1, 1, 0, '1/1/2017', 1999.99, 32, 1024, 'Nvidia 1080', 1, 3.5),
   (2, 2, 4, 0, '1/1/2017', 2399.99, 16, 512, 'Nvidia GeForce GT 650M', 1, 2.5)
SET IDENTITY_INSERT Computers OFF

CREATE TABLE Departments
(
   DepartmentKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   Department varchar(255)
)

SET IDENTITY_INSERT Departments ON
INSERT Departments (DepartmentKey, Department) VALUES
   (1, 'CEO'),
   (2, 'Human Resources'),
   (3, 'Information Technology'),
   (4, 'Accounting')
SET IDENTITY_INSERT Departments OFF

CREATE TABLE Employees
(
   EmployeeKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   LastName varchar(25) NOT NULL,
   FirstName varchar(25) NOT NULL,
   Email varchar(50) NOT NULL,
   Hired date NOT NULL,
   Terminated date NULL,
   DepartmentKey int NOT NULL,
   SupervisorEmployeeKey int NOT NULL --CEO/Top of hierarchy should have their own EmployeeKey
)

SET IDENTITY_INSERT Employees ON
INSERT Employees (EmployeeKey, LastName, FirstName, Email, Hired, DepartmentKey, SupervisorEmployeeKey) VALUES
   (1, 'Ceo', 'John The', 'JCeo@thiscompany.com', '1/1/2017', 1, 1),
   (2, 'Brother', 'Big', 'BBrother@thiscompany.com', '1/1/2017', 2, 1),
   (3, 'Geek', 'Major', 'MGeek@thiscompany.com', '1/1/2017', 3, 1)
SET IDENTITY_INSERT Employees OFF


CREATE TABLE EmployeeComputers
(
   EmployeeComputerKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   EmployeeKey int NOT NULL,
   ComputerKey int NOT NULL,
   Assigned date NOT NULL,
   Returned date NULL
)

SET IDENTITY_INSERT ComputerStatuses ON
   INSERT ComputerStatuses (ComputerStatusKey, ComputerStatus, ActiveStatus) VALUES
       (0, 'New', 1),
       (1, 'Assigned', 1),
       (2, 'Available', 1),
       (3, 'Lost', 0),
       (4, 'In for Repairs', 0),
       (5, 'Retired', 1)
SET IDENTITY_INSERT ComputerStatuses OFF

SET IDENTITY_INSERT CPUTypes ON
INSERT CPUTypes (CPUTypeKey, CPUType) VALUES
   (1, 'AMD'),
   (2, 'Intel'),
   (3, 'Samsung'),
   (4, 'Apple'),
   (5, 'Qualcomm')
SET IDENTITY_INSERT CPUTypes OFF

SET IDENTITY_INSERT ComputerTypes ON
INSERT ComputerTypes (ComputerTypeKey, ComputerType) VALUES
   (1, 'Desktop'),
   (2, 'Laptop'),
   (3, 'Tablet'),
   (4, 'Phone')
SET IDENTITY_INSERT ComputerTypes OFF

SET IDENTITY_INSERT Brands ON
INSERT Brands (BrandKey, Brand) VALUES
   (1, 'Apple'),
   (2, 'Samsung'),
   (3, 'Sony'),
   (4, 'HP'),
   (5, 'Acer'),
   (6, 'NVidia')
SET IDENTITY_INSERT Brands OFF

ALTER TABLE Computers
   ADD CONSTRAINT FK_ComputerComputerTypes
   FOREIGN KEY (ComputerTypeKey)
   REFERENCES ComputerTypes (ComputerTypeKey)

ALTER TABLE Computers
   ADD CONSTRAINT FK_ComputerBrands
   FOREIGN KEY (BrandKey)
   REFERENCES Brands (BrandKey)

ALTER TABLE Computers
   ADD CONSTRAINT FK_ComputerComputerStatus
   FOREIGN KEY (ComputerStatusKey)
   REFERENCES ComputerStatuses (ComputerStatusKey)

ALTER TABLE Computers
   ADD CONSTRAINT FK_ComputerCPUType
   FOREIGN KEY (CPUTypeKey)
   REFERENCES CPUTypes (CPUTypeKey)

ALTER TABLE Employees
   ADD CONSTRAINT FK_EmployeeDepartment
   FOREIGN KEY (DepartmentKey)
   REFERENCES Departments (DepartmentKey)

ALTER TABLE Employees
   ADD CONSTRAINT FK_EmployeeSupervisor
   FOREIGN KEY (SupervisorEmployeeKey)
   REFERENCES Employees (EmployeeKey)

ALTER TABLE EmployeeComputers
   ADD CONSTRAINT FK_EmployeeComputerEmployee
   FOREIGN KEY (EmployeeKey)
   REFERENCES Employees (EmployeeKey)

ALTER TABLE EmployeeComputers
   ADD CONSTRAINT FK_EmployeeComputerComputer
   FOREIGN KEY (ComputerKey)
   REFERENCES Computers (ComputerKey);

CREATE TABLE ComputerAssignments
(
   ComputerAssignmentKey int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   ComputerStatusKey int NOT NULL,
   Added bit DEFAULT(1) NOT NULL,
   Assigned bit DEFAULT(1) NOT NULL,
   Reassigned bit DEFAULT(1) NOT NULL,
   Lost bit DEFAULT(1) NOT NULL,
   StartDate datetime NOT NULL
);
SET IDENTITY_INSERT ComputerAssignments ON
INSERT ComputerAssignments (ComputerAssignmentKey, ComputerStatusKey,
   Added, Assigned, Reassigned, Lost, StartDate) VALUES
   (0, 0, 1, 1, 0, 0, '1/1/2017'),
   (1, 1, 0, 1, 0, 0, '1/13/2017'),
   (2, 2, 1, 0, 0, 0, '2/1/2017'),
   (3, 3, 0, 0, 0, 1, '1/13/2017'),
   (4, 4, 0, 1, 0, 0, '5/8/2017'),
   (5, 5, 0, 0, 0, 0, '6/10/2017')

SET IDENTITY_INSERT ComputerAssignments OFF


ALTER TABLE ComputerAssignments
ADD CONSTRAINT FK_ComputerStatusKeyAssignment
FOREIGN KEY (ComputerStatusKey)
REFERENCES ComputerStatuses (ComputerStatusKey)

Create a view that shows all computers that are in for repair. Include who the computer belongs to, their email address, how long it has been in for repairs, and the associated specs, brand, type, etc. This list will be used to update those users that are waiting on repairs. Try not to use subqueries.

Explanation / Answer

ANS:-

  

PROGRAM:-

SQL Query:-

--Create a view named Computers4Repair,which joins the tables --Brands,ComputerTypes,ComputerStatuses,EmployeeComputers and Employee
--according to ComputerTypekey of Computer types
CREATE VIEW Computers4Repair
AS

   Select Brands.Brand, ComputerTypes.ComputerType, ComputerStatuses.ComputerStatus,
   (Employees.LastName + ', ' + Employees.FirstName) AS Employee, Employees.Email,
   Datediff(dd,EmployeeComputers.Assigned,getdate()) as DaysforRepair,
   Computers.MemoryCapacityInGB, Computers.HardDriveCapacityinGB
   FROM Computers
   INNER JOIN Brands ON Computers.BrandKey = Brands.BrandKey
   INNER JOIN ComputerTypes ON ComputerTypes.ComputerTypeKey = Computers.ComputerTypeKey
   INNER JOIN ComputerStatuses ON ComputerStatuses.ComputerStatusKey = Computers.ComputerStatusKey
   INNER JOIN EmployeeComputers ON EmployeeComputers.ComputerKey = Computers.ComputerKey
   INNER JOIN Employees ON Employees.EmployeeKey = EmployeeComputers.EmployeeKey
   Where ComputerTypes.ComputerTypeKey = '4'
GO

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote