-- Creating the Restaurants table CREATE TABLE Restaurants ( restaurant_id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, location VARCHAR(255) ); -- Creating the Customers table CREATE TABLE Customers ( customer_id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE ); -- Creating the Orders table CREATE TABLE Orders ( order_id INT PRIMARY KEY, customer_id INT, restaurant_id INT, order_date DATE NOT NULL, total_amount DECIMAL(10,2) NOT NULL, FOREIGN KEY (customer_id) REFERENCES Customers(customer_id) ON DELETE CASCADE, FOREIGN KEY (restaurant_id) REFERENCES Restaurants(restaurant_id) ON DELETE CASCADE ); -- Creating the Reviews table CREATE TABLE Reviews ( review_id INT PRIMARY KEY, customer_id INT, restaurant_id INT, review_date DATE NOT NULL, rating INT CHECK (rating BETWEEN 1 AND 5), comment TEXT, FOREIGN KEY (customer_id) REFERENCES Customers(customer_id) ON DELETE CASCADE, FOREIGN KEY (restaurant_id) REFERENCES Restaurants(restaurant_id) ON DELETE CASCADE ); -- Inserting data into Restaurants table INSERT INTO Restaurants (restaurant_id, name, location) VALUES (1, 'Spice Delight', 'New York'), (2, 'Ocean Breeze', 'Los Angeles'), (3, 'The Italian Place', 'Chicago'); -- Inserting data into Customers table INSERT INTO Customers (customer_id, name, email) VALUES (1, 'Alice Johnson', '[email protected]'), (2, 'Bob Smith', '[email protected]'), (3, 'Charlie Brown', '[email protected]'); -- Inserting data into Orders table INSERT INTO Orders (order_id, customer_id, restaurant_id, order_date, total_amount) VALUES (101, 1, 1, '2024-01-15', 50.00), (102, 2, 2, '2024-01-20', 75.00), (103, 3, 3, '2024-02-05', 60.00), (104, 1, 2, '2024-02-10', 80.00), (105, 2, 3, '2024-03-12', 90.00); -- Inserting data into Reviews table INSERT INTO Reviews (review_id, customer_id, restaurant_id, review_date, rating, comment) VALUES -- January 2024 (201, 1, 1, '2024-01-05', 5, 'Amazing food!'), (202, 2, 1, '2024-01-15', 4, 'Great service.'), (203, 3, 2, '2024-01-10', 3, 'Decent seafood.'), (204, 1, 2, '2024-01-18', 4, 'Loved the seafood.'), (205, 2, 3, '2024-01-20', 5, 'Best pasta ever!'), (206, 3, 3, '2024-01-25', 4, 'Authentic Italian flavors!'), -- February 2024 (207, 1, 1, '2024-02-05', 4, 'Nice atmosphere.'), (208, 2, 1, '2024-02-15', 3, 'Slow service.'), (209, 3, 2, '2024-02-10', 5, 'Excellent seafood!'), (210, 1, 2, '2024-02-18', 3, 'Not as fresh as before.'), (211, 2, 3, '2024-02-20', 4, 'Good ambiance.'), (212, 3, 3, '2024-02-25', 5, 'Delicious and well-presented!'), -- March 2024 (213, 1, 1, '2024-03-05', 5, 'Loved the desserts!'), (214, 2, 1, '2024-03-15', 4, 'A bit overpriced.'), (215, 3, 2, '2024-03-10', 5, 'Superb seafood dishes.'), (216, 1, 2, '2024-03-18', 4, 'Good variety.'), (217, 2, 3, '2024-03-20', 3, 'Average experience.'), (218, 3, 3, '2024-03-25', 5, 'Best Italian food ever!'), -- April 2024 (219, 1, 1, '2024-04-05', 4, 'Great food, slow service.'), (220, 2, 1, '2024-04-15', 3, 'Average experience.'), (221, 3, 2, '2024-04-10', 5, 'Fresh and tasty seafood.'), (222, 1, 2, '2024-04-18', 4, 'Enjoyed the ambiance.'), (223, 2, 3, '2024-04-20', 4, 'Good service, okay food.'), (224, 3, 3, '2024-04-25', 5, 'Excellent pasta!'), -- May 2024 (225, 1, 1, '2024-05-05', 5, 'Outstanding flavors!'), (226, 2, 1, '2024-05-15', 4, 'Friendly staff.'), (227, 3, 2, '2024-05-10', 3, 'Food was okay.'), (228, 1, 2, '2024-05-18', 2, 'Could be better.'), (229, 2, 3, '2024-05-20', 5, 'Highly recommended!'), (230, 3, 3, '2024-05-25', 4, 'Would visit again.');
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;