0% found this document useful (0 votes)
50 views17 pages

Tugas DML SQL Credit Earning Satria Data 2023

The document describes creating and populating database tables in MySQL to store customer, product, order and inventory data for a sporting goods store. Tables are created for customers, employees, branches, inventory, products, orders and order details. Data is then inserted into these tables to populate them with sample records. A daily report table and stored procedure are also created to generate daily revenue reports from the order data.

Uploaded by

Habiburrohman
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)
50 views17 pages

Tugas DML SQL Credit Earning Satria Data 2023

The document describes creating and populating database tables in MySQL to store customer, product, order and inventory data for a sporting goods store. Tables are created for customers, employees, branches, inventory, products, orders and order details. Data is then inserted into these tables to populate them with sample records. A daily report table and stored procedure are also created to generate daily revenue reports from the order data.

Uploaded by

Habiburrohman
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/ 17

Credit Earning Satria Data 2023

Tugas Pertemuan 4
Data Manipulation Language (DML)
Nama : Habiburrohman
NIM : 10121089
Perguruan Tinggi : Institut Teknologi Bandung
Nomor Peserta : 90
N Jawaban
o
1 CREATE DATABASE satria_data_2023;
Use satria_data_2023;

CREATE TABLE Clients (


IdCustomers CHAR(36) NOT NULL PRIMARY KEY DEFAULT (UUID()),
CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UpdatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP,
Email VARCHAR(255) NOT NULL,
PhoneNumber VARCHAR(20) NOT NULL,
FirstName VARCHAR(255) NOT NULL,
LastName VARCHAR(255) NOT NULL,
Country VARCHAR(255) NOT NULL,
Province VARCHAR(255),
District VARCHAR(255),
City VARCHAR(255),
ZipCode VARCHAR(10)
);

1
2 RENAME TABLE Clients TO Customers;

3 CREATE TABLE Clients (


ClientId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UpdatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP,
Email VARCHAR(255) NOT NULL,
PhoneNumber VARCHAR(20) NOT NULL,
FirstName VARCHAR(255) NOT NULL,
LastName VARCHAR(255) NOT NULL,
Country VARCHAR(255) NOT NULL,
Province VARCHAR(255),
District VARCHAR(255),
City VARCHAR(255),
ZipCode VARCHAR(10)
);

2
CREATE TABLE Employees (
EmployeeId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(100),
ManagerId INT,
Salary DECIMAL(10, 2),
BranchId INT
);

CREATE TABLE Branch (


BranchId INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
BranchName VARCHAR(100),
BranchLocation VARCHAR(100)
);

CREATE TABLE Inventory (


InventoryId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ProductId INT,
QuantityInStock INT,
BranchId INT
);

3
CREATE TABLE Products (
ProductId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ProductName VARCHAR(100),
ProductPrice DECIMAL(10, 2)
);

CREATE TABLE Orders (


OrderId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
OrderDate DATE,
ClientId CHAR
);

CREATE Table OrderDetails (


OrderDetailId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
OrderId INT,
ProductId INT,
Quantity INT,
TotalAmount DECIMAL (10, 2)
);

4
4 INSERT INTO Customers
(Email, PhoneNumber, FirstName, LastName, Country, Province, District,
City, ZipCode)
VALUES
('[email protected]', '081234567890', 'Budi', 'Setiawan',
'Indonesia', 'DKI Jakarta', 'Jakarta Pusat', 'Menteng', '10310'),
('[email protected]', '081298765432', 'Dewi', 'Susanti',
'Indonesia', 'Jawa Barat', 'Bandung Barat', 'Bandung', '40116'),
('[email protected]', '081211122233', 'Ahmad', 'Santoso',
'Indonesia', 'Jawa Timur', 'Surabaya Utara', 'Surabaya', '60175'),
('[email protected]', '081244455566', 'Siti', 'Hartati',
'Indonesia', 'Bali', 'Denpasar Selatan', 'Denpasar', '80232');

INSERT INTO Clients


(Email, PhoneNumber, FirstName, LastName, Country, Province, District,
City, ZipCode)
VALUES
('[email protected]', '081234567890', 'Budi', 'Setiawan', 'Indonesia', 'DKI
Jakarta', 'Jakarta Pusat', 'Menteng', '10310'),
('[email protected]', '081298765432', 'Dewi', 'Susanti', 'Indonesia', 'Jawa
Barat', 'Bandung Barat', 'Bandung', '40116'),
('[email protected]', '081211122233', 'Ahmad', 'Santoso', 'Indonesia', 'Jawa
Timur', 'Surabaya Utara', 'Surabaya', '60175'),
('[email protected]', '081244455566', 'Siti', 'Hartati', 'Indonesia', 'Bali',
'Denpasar Selatan', 'Denpasar', '80232');

5
INSERT INTO Branch
(BranchName, BranchLocation)
VALUES
('Cabang Lamongan', 'Lamongan'),
('Cabang Sidoarjo', 'Sidoarjo'),
('Cabang Surabaya', 'Surabaya');

INSERT INTO Products


(ProductName, ProductPrice)
VALUES
('Papan Karambol', 150000),
('Biji Karambol', 15000),
('Raket Buku Tangkis', 90000),
('Kok Bulu Tangkis', 7000),
('Bed Tenis Meja', 35000),
('Bola Tenis Meja', 3000),
('Catur', 50000),
('Bola Sepak', 135000),
('Bola Voli', 125000),
('Bola Basket', 150000),
('Bola Kasti', 12000);

6
INSERT INTO Employees
(FirstName, ManagerId, Salary, BranchId)
VALUES
('Bayu', 1, 5000000, 1),
('Doni', 1, 4750000, 1),
('Nando', 1, 4500000, 2),
('Yayan', 1, 4500000, 3);

INSERT INTO Inventory


(ProductId, QuantityInStock, BranchId)
VALUES
(1, 30, 1), (1, 25, 2), (1, 25, 3),
(2, 100, 1), (2, 90, 2), (2, 90, 3),
(3, 50, 1), (3, 60, 2), (3, 45, 3),
(4, 250, 1), (4, 225, 2), (4, 275, 3),
(5, 70, 1), (5, 60, 2), (5, 50, 3),
(6, 310, 1), (6, 330, 2), (6, 300, 3),
(7, 100, 1), (7, 100, 2), (7, 100, 3),
(8, 80, 1), (8, 75, 2), (8, 85, 3),
(9, 90, 1), (9, 95, 2), (9, 100, 3),
(10, 60, 1), (10, 55, 2), (10, 50, 3),
(11, 200, 1), (11, 210, 2), (11, 250, 3);

7
INSERT INTO Orders
(OrderDate, ClientId)
VALUES
('2023-01-01', 1), ('2023-01-02', 2), ('2023-01-03', 3), ('2023-01-04', 4),
('2023-01-11', 1), ('2023-01-12', 2), ('2023-01-13', 3), ('2023-01-14', 4),
('2023-02-21', 1), ('2023-02-22', 2), ('2023-02-23', 3), ('2023-02-24', 4),
('2023-02-01', 1), ('2023-02-02', 2), ('2023-02-03', 3), ('2023-02-04', 4),
('2023-03-11', 1), ('2023-03-12', 2), ('2023-03-13', 3), ('2023-03-14', 4),
('2023-03-21', 1), ('2023-03-22', 2), ('2023-03-23', 3), ('2023-03-24', 4),
('2023-04-01', 1), ('2023-04-02', 2), ('2023-04-03', 3), ('2023-04-04', 4),
('2023-04-11', 1), ('2023-04-12', 2), ('2023-04-13', 3), ('2023-04-14', 4),
('2023-05-21', 1), ('2023-05-22', 2), ('2023-05-23', 3), ('2023-05-24', 4),
('2023-05-01', 1), ('2023-05-02', 2), ('2023-05-03', 3), ('2023-05-04', 4),
('2023-06-11', 1), ('2023-06-12', 2), ('2023-06-13', 3), ('2023-06-14', 4),
('2023-06-21', 1), ('2023-06-22', 2), ('2023-06-23', 3), ('2023-06-24', 4);

8
9
INSERT INTO OrderDetails
(OrderId, ProductId, Quantity, TotalAmount)
VALUES
(1, 1, 1, 150000), (1, 11, 4, 48000), (2, 2, 1, 15000), (2, 10, 1, 150000),
(3, 3, 1, 90000), (3, 9, 1, 125000), (4, 4, 2, 14000), (4, 8, 1, 135000),
(5, 5, 2, 70000), (5, 7, 1, 50000), (6, 6, 3, 9000), (6, 6, 1, 3000),
(7, 7, 1, 50000), (7, 5, 1, 35000), (8, 8, 1, 135000), (8, 4, 1, 7000),
(9, 9, 1, 125000), (9, 3, 1, 90000), (10, 10, 1, 150000), (10, 2, 1,
15000),
(11, 11, 1, 12000), (11, 1, 1, 150000), (12, 1, 1, 150000), (12, 2, 1,
15000),
(13, 2, 2, 30000), (13, 3, 1, 90000), (14, 3, 2, 180000), (14, 4, 8,
56000),
(15, 4, 5, 28000), (15, 5, 1, 35000), (16, 5, 2, 70000), (16, 6, 7, 21000),
(17, 6, 4, 12000), (17, 7, 2, 100000), (18, 7, 2, 100000), (18, 8, 1,
135000),
(19, 8, 1, 135000), (19, 9, 1, 125000), (20, 9, 2, 250000), (20, 10, 1,
150000),
(21, 10, 1, 150000), (21, 11, 1, 12000), (22, 11, 3, 36000), (22, 1, 1,
150000),
(23, 1, 2, 300000), (23, 2, 2, 30000), (24, 2, 2, 30000), (24, 3, 1,
90000),
(25, 3, 1, 90000), (25, 4, 3, 21000), (26, 4, 2, 14000), (26, 5, 1, 35000),
(27, 5, 1, 35000), (27, 6, 4, 12000), (28, 6, 2, 6000), (28, 7, 1, 50000),
(29, 7, 1, 50000), (29, 8, 1, 135000), (30, 8, 2, 270000), (30, 9, 1,
125000),
(31, 9, 1, 125000), (31, 10, 1, 150000), (32, 10, 1, 150000), (32, 11, 1,
12000),
(33, 11, 2, 24000), (33, 11, 1, 12000), (34, 1, 1, 150000), (34, 10, 1,
150000),
(35, 2, 1, 15000), (35, 9, 2, 250000), (36, 3, 1, 90000), (36, 8, 2,
270000),
(37, 4, 1, 7000), (37, 7, 1, 50000), (38, 5, 1, 35000), (38, 6, 3, 9000),
(39, 6, 1, 3000), (39, 5, 1, 35000), (40, 7, 1, 50000), (40, 4, 2, 14000),
(41, 8, 1, 135000), (41, 3, 1, 90000), (42, 9, 1, 125000), (42, 2, 3,
45000),
(43, 10, 1, 150000), (43, 1, 2, 300000), (44, 11, 1, 12000), (44, 2, 1,
15000),
(45, 1, 1, 150000), (45, 3, 1, 90000), (46, 2, 3, 45000), (46, 4, 5,
35000),
(47, 3, 2, 180000), (47, 5, 1, 35000), (48, 4, 10, 70000), (48, 6, 12,
36000);

10
11
5 CREATE TABLE DailyReport (
ReportDate DATE,
ClientName VARCHAR(100),
ProductName VARCHAR(100),
CityName VARCHAR(100),
ZipCode INT,
TotalRevenue DECIMAL(10, 2),
PRIMARY KEY (ReportDate, ClientName, ProductName)
);

12
DELIMITER //
CREATE PROCEDURE DailyReportProcedure(IN report_date DATE)
BEGIN
START TRANSACTION;

INSERT INTO DailyReport (ReportDate, ClientName, ProductName, CityName,


ZipCode, TotalRevenue)
SELECT
report_date,
C.FirstName ClientName,
P.ProductName,
C.City CityName,
C.ZipCode,
SUM(OD.TotalAmount)
FROM
Clients C
JOIN
Orders O ON C.ClientId = O.ClientId
JOIN
OrderDetails OD ON O.OrderId = OD.OrderId
JOIN
Products P ON OD.ProductId = P.ProductId
WHERE
DATE(O.OrderDate) = report_date
GROUP BY
C.FirstName, P.ProductName
ON DUPLICATE KEY UPDATE
CityName = VALUES(CityName),
ZipCode = VALUES(ZipCode),
TotalRevenue = VALUES(TotalRevenue);

COMMIT;
END //
DELIMITER ;

13
CREATE TABLE MonthlyReport (
ReportYear INT,
ReportMonth INT,
ClientName VARCHAR(100),
ProductName VARCHAR(100),
CityName VARCHAR(100),
Zipcode INT,
TotalRevenue DECIMAL(10, 2),
PRIMARY KEY (ReportYear, ReportMonth, ClientName, ProductName)
);

14
DELIMITER //
CREATE PROCEDURE MonthlyReportProcedure(IN report_month INT, IN report_year INT)
BEGIN
START TRANSACTION;

INSERT INTO MonthlyReport (ReportYear, ReportMonth, ClientName,


ProductName, CityName, ZipCode, TotalRevenue)
SELECT
report_year,
report_month,
C.FirstName ClientName,
P.ProductName,
C.City CityName,
C.ZipCode,
SUM(OD.TotalAmount)
FROM
Clients C
JOIN
Orders O ON C.ClientId = O.ClientId
JOIN
OrderDetails OD ON O.OrderId = OD.OrderId
JOIN
Products P ON OD.ProductId = P.ProductId
WHERE
YEAR(O.OrderDate) = report_year AND MONTH(O.OrderDate) = report_month
GROUP BY
C.FirstName, P.ProductName
ON DUPLICATE KEY UPDATE
CityName = VALUES(CityName),
ZipCode = VALUES(ZipCode),
TotalRevenue = VALUES(TotalRevenue);

COMMIT;
END //
DELIMITER ;

15
16
17

You might also like