Professional Documents
Culture Documents
2. Create the tables as per the relationship diagram, ensuring minimum disk space utilization.
1
CREATE DATABASE
ON PRIMARY
MAXSIZE=50mb,SIZE=20mb,
FILEGROWTH=2mb
LOG ON
NAME= ShopeHere_Log,
MAXSIZE=10mb,SIZE=4mb,
FILEGROWTH =2kb
2
CREATE SCHEMAS
3
CREATE TABLES
TABLE Items.ItemDetails
CREATE TABLE Items.ItemDetails
(
ItemID INT PRIMARY KEY IDENTITY(1,1),
CategoryID INT ,
SupplierID INT
ADD
[CategoryID]
) REFERENCES Items.ProductCategory
ADD
SupplierID
) REFERENCES Supplier.SupplierDetails
4
TABLE Items.ProductCategory
CREATE TABLE Items.ProductCategory
(CategoryName IN ('HouseHold','Sports','Accessories','Clothing')),
TALBLE Supplier.SupplierDetails
CREATE TABLE Supplier.SupplierDetails
('[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]')),
TABLE HumanResources.Employee
CREATE TABLE HumanResources.Employee
('[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]'))
5
)
TABLE Transactions.OrderDetails
CREATE TABLE Transactions.OrderDetails
DEFAULT GETDATE(),
ItemsID INT,
ShipMethod NVARCHAR(200) ,
'Received','Cancelled'))
ADD
CONSTRAINT [FK_Transactions.OrderDetails_Items.ItemDetails]
FOREIGN KEY
ADD
CONSTRAINT [FK_Transactions.OrderDetails_HumanResources.Employee]
FOREIGN KEY
6
PERFORM VALIDATION ON TABLES
ON Transactions.OrderDetails
FOR INSERT,UPDATE
AS
BEGIN
SELECT @QuantityOrder=QuantityOrder,@QuantityReceived=QuantityReceived
FROM INSERTED
IF @QuantityReceived>=@QuantityOrder
BEGIN
ROLLBACK TRAN
END
END
7
QuantityReceived should be added to QuantityInHand in the Items table
CREATE TRIGGER TRG_addquantity
ON Transactions.OrderDetails
FOR INSERT,UPDATE
AS
BEGIN
SELECT @QuantityReceived=QuantityReceived,
@QantityInHand=QantityInHand,
@ItemID=ItemID
SET @add=@QuantityReceived+@QantityInHand
UPDATE Items.ItemDetails
SET QantityInHand=@add
WHERE ItemID=@ItemID
END
8
RecievingDate should be greater than OrderDate
CREATE TRIGGER TRG_checkdate
ON Transactions.OrderDetails
FOR INSERT,UPDATE
AS
BEGIN
SELECT @RecievingDate=RecievingDate,
@OrderDate=OrderDate
FROM inserted
IF @RecievingDate<=@OrderDate
BEGIN
ROLLBACK TRAN
END
END
9
DATABASE DIAGRAM
10
CREATE INDEX
1. Extract the Order Details for all the Purchase Orders in the current
month:
2. Extract the details of all the Orders placed more than two years
back:
3. Extract the details of the TOP FIVE Suppliers to whom the aximum
number of order have been placed in the current month:
11
TASK SIMPLIFYING
AS
BEGIN
SELECT@PurchaseOrderID=PurchaseOrderID,
@QuantityReceived=QuantityReceived,
@UnitPrice=UnitPrice
FROM Transactions.OrderDetails
WHERE PurchaseOrderID=@PurchaseOrderID
SET @Totalcost=@UnitPrice*@QuantityReceived
PRINT @Totalcost
END
12
Calculation of the total of all the orders placed by a particular employee in
a particular month
CREATE PROC T OLTALORDER @EMPLOYEE ID INT,@MONTH INT
AS
BEGIN
END
13