-- Create tables CREATE TABLE Categories ( CategoryID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL ); desc Categories; -- Describes Categories table CREATE TABLE Products ( ProductID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, CategoryID INT, Price DECIMAL(10, 2) NOT NULL, StockQuantity INT NOT NULL, FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID) ); desc Products; -- Describes Products table CREATE TABLE Suppliers ( SupplierID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, ContactNumber VARCHAR(20), Email VARCHAR(100) ); desc Suppliers; CREATE TABLE Customers ( CustomerID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, Address VARCHAR(255), Email VARCHAR(100), Phone VARCHAR(20) ); desc Customers; CREATE TABLE Orders ( OrderID INT PRIMARY KEY, CustomerID INT, OrderDate DATE NOT NULL, TotalAmount DECIMAL(10, 2) NOT NULL, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ); desc Orders; CREATE TABLE OrderDetails ( OrderDetailID INT PRIMARY KEY, OrderID INT, ProductID INT, Quantity INT NOT NULL, UnitPrice DECIMAL(10, 2) NOT NULL, TotalPrice DECIMAL(10, 2) NOT NULL, FOREIGN KEY (OrderID) REFERENCES Orders(OrderID), FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ); desc OrderDetails; CREATE TABLE Transactions ( TransactionID INT PRIMARY KEY, TransactionDate DATE NOT NULL, OrderID INT, Amount DECIMAL(10, 2) NOT NULL, Type ENUM('Sale', 'Refund') NOT NULL, FOREIGN KEY (OrderID) REFERENCES Orders(OrderID) ); desc Transactions; -- Inserting data INSERT INTO Categories (CategoryID, Name) VALUES (1, 'Fruits'), (2, 'Vegetables'), (3, 'Dairy'), (4, 'Meat'), (5, 'Beverages'); SELECT * from Categories; -- showing values in Categories INSERT INTO Products (ProductID, Name, CategoryID, Price, StockQuantity) VALUES (1, 'Apple', 1, 1.99, 100), (2, 'Banana', 1, 0.99, 150), (3, 'Tomato', 2, 0.79, 200), (4, 'Potato', 2, 0.49, 250), (5, 'Milk', 3, 2.49, 50), (6, 'Cheese', 3, 3.99, 30), (7, 'Chicken', 4, 5.99, 20), (8, 'Beef', 4, 9.99, 15), (9, 'Water', 5, 0.99, 100), (10, 'Soda', 5, 1.49, 80); SELECT * from Products; INSERT INTO Suppliers (SupplierID, Name, ContactNumber, Email) VALUES (1, 'Fresh Farms', '123-456-7890', '[email protected]'), (2, 'Green Grocers', '987-654-3210', '[email protected]'); SELECT * from Suppliers; INSERT INTO Customers (CustomerID, Name, Address, Email, Phone) VALUES (1, 'John Doe', '123 Main St, Cityville', '[email protected]', '555-123-4567'), (2, 'Jane Smith', '456 Elm St, Townsville', '[email protected]', '555-987-6543'); SELECT * from Customers; INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount) VALUES (1, 1, '2024-03-21', 25.45), (2, 2, '2024-03-20', 15.75); SELECT * from Orders; INSERT INTO OrderDetails (OrderDetailID, OrderID, ProductID, Quantity, UnitPrice, TotalPrice) VALUES (1, 1, 1, 5, 1.99, 9.95), (2, 1, 3, 3, 0.79, 2.37), (3, 2, 5, 2, 2.49, 4.98), (4, 2, 10, 3, 1.49, 4.47); SELECT * from OrderDetails; INSERT INTO Transactions (TransactionID, TransactionDate, OrderID, Amount, Type) VALUES (1, '2024-03-21', 1, 25.45, 'Sale'), (2, '2024-03-20', 2, 15.75, 'Sale'); SELECT * from Transactions;
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;