0% found this document useful (0 votes)
19 views6 pages

DELIMITER

The document defines database tables for products, suppliers, customers, orders and order items using SQL statements. It inserts sample data and writes queries to retrieve and join data from the tables.

Uploaded by

ester.xhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

DELIMITER

The document defines database tables for products, suppliers, customers, orders and order items using SQL statements. It inserts sample data and writes queries to retrieve and join data from the tables.

Uploaded by

ester.xhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DELIMITER //

DELIMITER //
CREATE TRIGGER increment_num_of_tracks
CREATE TRIGGER Before_Insert_Oferte CREATE TRIGGER kontrollo_oferte_trigger
AFTER INSERT ON Has_Song
BEFORE INSERT ON Oferte BEFORE INSERT ON tabela_oferte
FOR EACH ROW
FOR EACH ROW FOR EACH ROW
BEGIN
BEGIN BEGIN
DECLARE cd_count INT;
DECLARE product_count INT; DECLARE oferte_ekzistuese INT;

SELECT COUNT(*) SET oferte_ekzistuese = (SELECT COUNT(*)


SELECT num_of_tracks INTO cd_count
INTO product_count FROM tabela_oferte
FROM CD
FROM Oferte WHERE produkt_id = NEW.produkt_id
WHERE id = NEW.cd_id;
WHERE product_id = NEW.product_id AND data_fillimi <= NEW.data_mbarimi

AND data_mbarimit >= NEW.data_fillimit AND data_mbarimi >= NEW.data_fillimi);


-- Increment num_of_tracks by 1
AND data_fillimit <= NEW.data_mbarimit; IF oferte_ekzistuese > 0 THEN
UPDATE CD
IF product_count > 0 THEN SIGNAL SQLSTATE '45000' SET
MESSAGE_TEXT = 'Ekziston një ofertë tjetër për këtë
SET num_of_tracks = cd_count + 1
SIGNAL SQLSTATE '45000' produkt në kohën e caktuar.';
WHERE id = NEW.cd_id;
SET MESSAGE_TEXT = 'Cannot insert offer. Overlapping END IF;
offer already exists for the same product.';
END;
END;
END IF;
//DELIMITER ;
END//

DELIMITER ;

CREATE TRIGGER kontrollo_oferte_trigger

BEFORE INSERT ON tabela_oferte

FOR EACH ROW

BEGIN

DECLARE oferte_ekzistuese INT;


SET oferte_ekzistuese = (SELECT COUNT(*) FROM tabela_oferte WHERE produkt_id = NEW.produkt_id AND data_fillimi <= NEW.data_mbarimi AND
data_mbarimi >= NEW.data_fillimi);

IF oferte_ekzistuese > 0 THEN

SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Ekziston një ofertë tjetër për këtë produkt në kohën e caktuar.';

END IF;

END;

-- Define the Product table

CREATE TABLE Products (

ProductID INT PRIMARY KEY IDENTITY,

ProductName NVARCHAR(255),

SupplierID INT,

UnitPrice INT,

Package NVARCHAR(255),

IsDiscontinued SMALLINT

);

-- Define the Supplier table

CREATE TABLE Suppliers (

SupplierID INT PRIMARY KEY,

CompanyName NVARCHAR(255),

ContactName NVARCHAR(255),

City NVARCHAR(255),
Country NVARCHAR(255),

Phone NVARCHAR(255),

Fax NVARCHAR(255)

);

-- Insert data into the Products table

INSERT INTO Products (ProductName, SupplierID, UnitPrice, Package, IsDiscontinued)

VALUES

('Product 1', 11, 25, 'Package 1', 0),

('Product 2', 17, 20, 'Package 2', 0),

('Product 3', 10, 25, 'Package 3', 1);

-- Define the Customer table

CREATE TABLE Customers (

CustomerID INT PRIMARY KEY IDENTITY,

FirstName NVARCHAR(255),

LastName NVARCHAR(255),

City NVARCHAR(255),

Country NVARCHAR(255),

Phone NVARCHAR(255)

);

-- Insert data into the Customers table

INSERT INTO Customers (FirstName, LastName, City, Country, Phone)

VALUES

('Alan', 'Doe', 'Tirane', 'Albania', '125222312412'),

('John', 'Tavernier', 'Durres', 'Albania', '124445554'),

('Smith', 'Foden', 'Athens', 'Greece', '1251243113331');

-- Retrieve products with specified unit prices

SELECT *

FROM Products

WHERE UnitPrice IN (10, 20, 30, 40, 50);

-- Retrieve suppliers from specified countries

SELECT *

FROM Suppliers

WHERE Country IN ('Albania', 'UK', 'Japan');


-- Retrieve customers from countries with at least 2 customers

SELECT Country, COUNT(*) AS NumCustomers

FROM Customers

GROUP BY Country

HAVING COUNT(*) >= 2;

-- Define the Orders table

CREATE TABLE Orders (

OrderID INT PRIMARY KEY IDENTITY,

OrderDate DATE,

OrderNumber INT,

CustomerID INT,

TotalAmount INT

);

-- Insert data into the Orders table

INSERT INTO Orders (OrderDate, OrderNumber, CustomerID, TotalAmount)

VALUES

('2023-10-29', 111, 1, 5),

('2023-04-02', 112, 2, 15),

('2023-12-11', 113, 3, 23);

-- Retrieve orders with customer details (Method 1)

SELECT *

FROM Orders, Customers

WHERE Orders.CustomerID = Customers.CustomerID;

-- Retrieve orders with customer details (Method 2, using INNER JOIN)

SELECT *

FROM Orders

INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

-- Define the OrderItem table

CREATE TABLE OrderItems (

OrderItemID INT PRIMARY KEY IDENTITY,

OrderID INT,

ProductID INT,
UnitPrice INT,

Quantity INT

);

-- Insert data into the OrderItems table

INSERT INTO OrderItems (OrderID, ProductID, UnitPrice, Quantity)

VALUES

(1, 2, 10, 5),

(2, 3, 14, 3),

(3, 1, 40, 1);

-- Retrieve order details with product information

SELECT Orders.OrderID, Orders.OrderDate, Orders.TotalAmount,

OrderItems.UnitPrice, OrderItems.Quantity,

Products.ProductName, Products.UnitPrice AS ProductUnitPrice

FROM Orders

INNER JOIN OrderItems ON Orders.OrderID = OrderItems.OrderID

INNER JOIN Products ON OrderItems.ProductID = Products.ProductID;

-- Retrieve suppliers who have supplied products

SELECT *

FROM Suppliers

WHERE EXISTS (SELECT 1 FROM Products WHERE Products.SupplierID = Suppliers.SupplierID);

-- Create a new table for suppliers from Albania

SELECT *

INTO AlbaniaSuppliers

FROM Suppliers

WHERE Country = 'Albania';

You might also like