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

SQL Code

The document contains SQL queries and commands for managing a database related to publications, customers, items, and transactions. It includes tasks for selecting data, creating tables, inserting records, and defining stored procedures and triggers. The queries focus on retrieving sales information, author details, and managing customer and item records in a structured manner.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL Code

The document contains SQL queries and commands for managing a database related to publications, customers, items, and transactions. It includes tasks for selecting data, creating tables, inserting records, and defining stored procedures and triggers. The queries focus on retrieving sales information, author details, and managing customer and item records in a structured manner.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

#Discussion 1 Task 1

USE PUBS;
SELECT * FROM titles;
SELECT title,ytd_sales FROM titles WHERE ytd_sales > 8000;

#Discussion 1 Task 2
USE PUBS;
SELECT * FROM titles;
SELECT title,royalty FROM titles WHERE royalty>12 AND royalty<24;

#Discussion 1 Task 3
USE PUBS;
SELECT * FROM titles;
SELECT type,AVG(price) AveragePrice,SUM(ytd_sales) TotalYearlySales
FROM titles GROUP BY type;

#Discussion 2 Task 1(i)


USE PUBS;
SELECT title,CONCAT(au_fname,' ',au_lname) AS AuthorName FROM titles
JOIN
titleauthor ON titles.title_id = titleauthor.title_id JOIN authors ON
titleauthor.au_id = authors.au_id;
#Discussion 2 Task 1(ii)
USE PUBS;
SELECT title,CONCAT(au_fname,' ',au_lname) AS AuthorName,pub_name
FROM titles JOIN
titleauthor ON titles.title_id = titleauthor.title_id JOIN authors ON
titleauthor.au_id = authors.au_id JOIN publishers ON titles.pub_id =
publishers.pub_id;

#Discussion 2 Task 2
USE PUBS;
SELECT CONCAT(au_fname,' ',au_lname) AS
AuthorName,authors.city,pub_name FROM
authors,publishers WHERE authors.city = publishers.city;

Another method
USE PUBS;
SELECT CONCAT(au_fname,' ',au_lname) AS
AuthorName,authors.city,pub_name FROM
authors JOIN publishers ON authors.city = publishers.city;
#Discussion 2 Task 3
USE PUBS;
SELECT title,CONCAT(au_fname,' ',au_lname)
AS AuthorName,royalty FROM authors JOIN titleauthor
ON authors.au_id = titleauthor.au_id JOIN titles
ON titleauthor.title_id =titles.title_id
WHERE royalty = (SELECT MAX(royalty) FROM titles);

#Create Table CustomerAndSuppliers


USE Assignment;
CREATE TABLE CustomerAndSuppliers(
cust_id CHAR(6) PRIMARY KEY CHECK (cust_id LIKE '[CS][0-9][0-9][0-9][0-
9][0-9]'),
cust_fname CHAR(15) NOT NULL,
cust_lname VARCHAR(15),
cust_address TEXT,
cust_telno CHAR(12) CHECK (cust_telno LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-
9][0-9][0-9][0-9][0-9][0-9]'),
cust_city CHAR(12) DEFAULT 'Rajshahi',
sales_amnt MONEY CHECK (sales_amnt > = 0),
proc_amnt MONEY CHECK (proc_amnt > = 0)
);
INSERT INTO
CustomerAndSuppliers(cust_id,cust_fname,cust_lname,cust_address,cust_telno,cu
st_city,sales_amnt,proc_amnt)
VALUES('C00001','Hasem','Ali','Lebubagan,Binodpur','017-
95125465','Rajshahi',0,0);
INSERT INTO
CustomerAndSuppliers(cust_id,cust_fname,cust_lname,cust_address,cust_telno,cu
st_city,sales_amnt,proc_amnt)
VALUES('C00002','Nurnobi','Mia','Lebubagan,Binodpur','017-
60340575','Rajshahi',0,0);
INSERT INTO
CustomerAndSuppliers(cust_id,cust_fname,cust_lname,cust_address,cust_telno,cu
st_city,sales_amnt,proc_amnt)
VALUES('C00003','Mehedi','Hasan','Lebubagan,Binodpur','017-
74490826','Rajshahi',0,0);

SELECT * FROM CustomerAndSuppliers;

#Create Table Item


USE Assignment;
CREATE TABLE Item(
item_id CHAR(6) PRIMARY KEY CHECK (item_id LIKE '[P][0-9][0-9][0-9][0-
9][0-9]'),
item_name CHAR(12) NOT NULL,
item_category CHAR(10),
item_price FLOAT CHECK (item_price > = 0),
item_qoh INT CHECK (item_qoh > = 0),
item_last_sold DATE DEFAULT GETDATE()
);
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00001','Phone','Electrical','25000','50','2025-01-17');
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00002','Laptop','Electrical','50000','30','2025-01-17');
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00003','Gears','Mechanical','2000','100','2025-01-17');
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00004','Bearings','Mechanical','500','150','2025-01-17');
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00005','Antivirus','Software','700','20','2025-01-17');
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00006','Browser','Software','400','30','2025-01-17');
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00007','Comics','Books','1200','50','2025-01-17');
INSERT INTO
item(item_id,item_name,item_category,item_price,item_qoh,item_last_sold)
VALUES('P00008','Novels','Books','1500','50','2025-01-17');

SELECT * FROM Item;


#Create Table Transactions
USE Assignment;
CREATE TABLE Transactions(
tran_id CHAR(10) PRIMARY KEY CHECK (tran_id LIKE '[T][0-9][0-9][0-9][0-
9][0-9][0-9][0-9][0-9][0-9]'),
item_id CHAR(6) REFERENCES Item(item_id),
cust_id CHAR(6) REFERENCES CustomerAndSuppliers(cust_id),
tran_type CHAR(1) CHECK (tran_type IN ('S','O')),
tran_quantity INT CHECK (tran_quantity > = 0),
tran_date DATETIME DEFAULT GETDATE()
);

SELECT * FROM Transactions;

#Discussion 3 Task 1
USE Assignment;
GO
CREATE PROCEDURE printDetails
AS
BEGIN
SELECT item_category,SUM(item_qoh) AS TotalItem,AVG(item_price) AS
newAvgPrice FROM Item GROUP BY item_category
END

EXEC printDetails;
#Discussion 3 Task 2
USE Assignment;
GO
CREATE PROCEDURE printDetails1 @itemCategory CHAR(10),@itemPrice
INT
AS
BEGIN
SELECT * FROM Item WHERE item_category = @itemCategory AND
item_price<@itemPrice
END

EXEC printDetails1 @itemCategory = 'Electrical',@itemPrice = 50000 ;

#Discussion 3 Task 3
USE Assignment;
GO
CREATE PROCEDURE printDetails2 @itemCategory
CHAR(10),@desiredAvgValue FLOAT
AS
BEGIN
DECLARE @totalPrice DECIMAL(10,2),@totalItem INT,@currentAvgPrice
FLOAT
SELECT @totalPrice = SUM(item_price),@totalItem = COUNT(*) FROM Item
WHERE item_category = @itemCategory
SET @currentAvgPrice = @totalPrice/@totalItem
WHILE @currentAvgPrice<@desiredAvgValue
BEGIN
UPDATE Item
SET item_price = item_price + 0.1*(item_price) WHERE item_category =
@itemCategory
SELECT @totalPrice = SUM(item_price) FROM Item WHERE item_category =
@itemCategory
SET @currentAvgPrice = @totalPrice/@totalItem
END
SELECT item_category,AVG(item_price) newAvgPrice FROM Item WHERE
item_category = @itemCategory
GROUP BY item_category
END

EXEC printDetails2 @itemCategory = 'Books',@desiredAvgValue = 1500;

#Discussion 4 Task 1
USE Assignment;
GO
CREATE TRIGGER updateAmounts ON Transactions AFTER INSERT
AS
BEGIN
DECLARE @tranType CHAR(1)
DECLARE @tranQuantity INT
DECLARE @itemPrice DECIMAL(10,2)
DECLARE @custID CHAR(6)
SELECT @tranType = tran_type,@tranQuantity = tran_quantity,@custID =
cust_id FROM INSERTED
IF (@tranType = 'S')
BEGIN
SELECT @itemPrice = item_price FROM Item WHERE item_id IN (SELECT
item_id FROM INSERTED)
UPDATE CustomerAndSuppliers
SET sales_amnt = sales_amnt + (@itemPrice*@tranQuantity) WHERE cust_id =
@custID
END
IF (@tranType = 'O')
BEGIN
SELECT @itemPrice = item_price FROM Item WHERE item_id IN (SELECT
item_id FROM INSERTED)
UPDATE CustomerAndSuppliers
SET proc_amnt = proc_amnt + (@itemPrice*@tranQuantity) WHERE cust_id =
@custID
END
END

You might also like