-- 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.');

 
by

MySQL online editor

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.

About MySQL

MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.

Key Features:

  • Open-source relational database management systems.
  • Reliable, very fast and easy to use database server.
  • Works on client-server model.
  • Highly Secure and Scalable
  • High Performance
  • High productivity as it uses stored procedures, triggers, views to write a highly productive code.
  • Supports large databases efficiently.
  • Supports many operating systems like Linux*,CentOS*, Solaris*,Ubuntu*,Windows*, MacOS*,FreeBSD* and others.

Syntax help

Commands

1. CREATE

CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

Example

CREATE TABLE EMPLOYEE (
  empId INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL
);

2. ALTER

ALTER TABLE Table_name ADD column_name datatype;

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');

3. TRUNCATE

TRUNCATE table table_name;

4. DROP

DROP TABLE table_name;

5. RENAME

RENAME TABLE table_name1 to new_table_name1; 

6. COMMENT

Single-Line Comments:

 --Line1;

Multi-Line comments:

   /* Line1,
   Line2 */

DML Commands

1. INSERT

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Note: Column names are optional.

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');

2. SELECT

SELECT column1, column2, ...
FROM table_name
[where condition]; 

Example

SELECT * FROM EMPLOYEE where dept ='sales';

3. UPDATE

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

Example

UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001'; 

4. DELETE

DELETE FROM table_name where condition;

Example

DELETE from EMPLOYEE where empId='0001'; 

Indexes

1. CREATE INDEX

  CREATE INDEX index_name on table_name(column_name);
  • To Create Unique index:
  CREATE UNIQUE INDEX index_name on table_name(column_name);

2. DROP INDEX

DROP INDEX index_name ON table_name;

Views

1. Create a View

Creating a View:
CREATE VIEW View_name AS 
Query;

2. How to call view

SELECT * FROM View_name;

3. Altering a View

ALTER View View_name AS 
Query;

4. Deleting a View

DROP VIEW View_name;

Triggers

1. Create a Trigger

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 } */

2. Drop a Trigger

DROP TRIGGER [IF EXISTS] trigger_name;

Stored Procedures

1. Create a Stored Procedure

CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;

2. How to call Stored procedure

CALL sp_name;

3. How to delete stored procedure

DROP PROCEDURE sp_name;

Joins

1. INNER JOIN

SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;

2. LEFT JOIN

SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;

3. RIGHT JOIN

SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;

4. CROSS JOIN

SELECT select_list from TABLE1 CROSS JOIN TABLE2;