-- Creating Categories Table CREATE TABLE Categories ( category_id INT PRIMARY KEY AUTO_INCREMENT, category_name VARCHAR(255) NOT NULL UNIQUE ); -- Creating Products Table CREATE TABLE Products ( product_id INT PRIMARY KEY AUTO_INCREMENT, product_name VARCHAR(255) NOT NULL, category_id INT NOT NULL, price DECIMAL(10,2) NOT NULL CHECK (price > 0), FOREIGN KEY (category_id) REFERENCES Categories(category_id), INDEX idx_category_id (category_id) ); -- Creating Transactions Table CREATE TABLE Transactions ( transaction_id INT PRIMARY KEY AUTO_INCREMENT, product_id INT NOT NULL, transaction_date DATE NOT NULL, quantity INT NOT NULL CHECK (quantity > 0), price DECIMAL(10,2) NOT NULL CHECK (price > 0), FOREIGN KEY (product_id) REFERENCES Products(product_id), INDEX idx_product_id (product_id), INDEX idx_transaction_date (transaction_date) ); -- Inserting Categories (including missing ones) INSERT INTO Categories (category_name) VALUES ('Electronics'), ('Clothing'), ('Home Appliances'), ('Groceries'), ('Books'); -- Inserting Products (Removed product_id since it's auto-incremented) INSERT INTO Products (product_name, category_id, price) VALUES -- Electronics ('Laptop', 1, 50000), ('Smartphone', 1, 30000), ('Headphones', 1, 2000), ('Tablet', 1, 25000), ('Smartwatch', 1, 15000), -- Clothing ('T-shirt', 2, 500), ('Jeans', 2, 2000), ('Jacket', 2, 4000), ('Sneakers', 2, 6000), ('Hoodie', 2, 3000), -- Home Appliances ('Microwave', 3, 8000), ('Vacuum Cleaner', 3, 10000), ('Refrigerator', 3, 40000), ('Washing Machine', 3, 50000), ('Air Conditioner', 3, 60000), -- Groceries (Now referencing correct category_id) ('Rice', 4, 50), ('Wheat', 4, 45), ('Milk', 4, 30), ('Eggs', 4, 10), ('Coffee', 4, 200), -- Books (Now referencing correct category_id) ('Fiction', 5, 500), ('Non-fiction', 5, 600), ('Science', 5, 800), ('Mystery', 5, 450), ('History', 5, 700); -- Inserting Transactions INSERT INTO Transactions (product_id, transaction_date, quantity, price) VALUES -- Electronics (1, '2024-02-01', 10, 50000), (2, '2024-02-02', 15, 30000), (3, '2024-02-03', 12, 2000), (4, '2024-02-04', 8, 25000), (5, '2024-02-05', 14, 15000), (1, '2024-02-06', 5, 50000), (2, '2024-02-07', 8, 30000), (3, '2024-02-08', 6, 2000), -- Clothing (6, '2024-02-01', 20, 500), (7, '2024-02-02', 25, 2000), (8, '2024-02-03', 10, 4000), (9, '2024-02-04', 18, 6000), (10, '2024-02-05', 12, 3000), (6, '2024-02-06', 15, 500), (7, '2024-02-07', 22, 2000), (8, '2024-02-08', 7, 4000), -- Home Appliances (11, '2024-02-01', 5, 8000), (12, '2024-02-02', 7, 10000), (13, '2024-02-03', 10, 40000), (14, '2024-02-04', 6, 50000), (15, '2024-02-05', 4, 60000), (11, '2024-02-06', 9, 8000), (12, '2024-02-07', 6, 10000), (13, '2024-02-08', 12, 40000), -- Groceries (16, '2024-02-01', 30, 50), (17, '2024-02-02', 28, 45), (18, '2024-02-03', 25, 30), (19, '2024-02-04', 40, 10), (20, '2024-02-05', 15, 200), (16, '2024-02-06', 35, 50), (17, '2024-02-07', 22, 45), (18, '2024-02-08', 27, 30), -- Books (21, '2024-02-01', 8, 500), (22, '2024-02-02', 10, 600), (23, '2024-02-03', 5, 800), (24, '2024-02-04', 12, 450), (25, '2024-02-05', 7, 700), (21, '2024-02-06', 6, 500), (22, '2024-02-07', 9, 600), (23, '2024-02-08', 8, 800);
Write, Run & Share MySQL queries online using OneCompiler's MySQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MySQL. Getting started with the OneCompiler's MySQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MySQL' and start writing queries to learn and test online without worrying about tedious process of installation.
MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);
ALTER TABLE Table_name ADD column_name datatype;
INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');
TRUNCATE table table_name;
DROP TABLE table_name;
RENAME TABLE table_name1 to new_table_name1;
--Line1;
/* Line1,
Line2 */
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Note: Column names are optional.
INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');
SELECT column1, column2, ...
FROM table_name
[where condition];
SELECT * FROM EMPLOYEE where dept ='sales';
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001';
DELETE FROM table_name where condition;
DELETE from EMPLOYEE where empId='0001';
CREATE INDEX index_name on table_name(column_name);
CREATE UNIQUE INDEX index_name on table_name(column_name);
DROP INDEX index_name ON table_name;
Creating a View:
CREATE VIEW View_name AS
Query;
SELECT * FROM View_name;
ALTER View View_name AS
Query;
DROP VIEW View_name;
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW [trigger_order] trigger_body
/* where
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } */
DROP TRIGGER [IF EXISTS] trigger_name;
CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;
CALL sp_name;
DROP PROCEDURE sp_name;
SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;
SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;
SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;
SELECT select_list from TABLE1 CROSS JOIN TABLE2;