CREATE TABLE Books (
    book_id INT PRIMARY KEY,
    title VARCHAR(255),
    author VARCHAR(255),
    publication_date DATE,
    genre VARCHAR(255),
    publisher VARCHAR(255),
    ISBN VARCHAR(255),
    quantity INT
);


CREATE TABLE Members (
    member_id INT PRIMARY KEY,
    first_name VARCHAR(255),
    last_name VARCHAR(255),
    address VARCHAR(255),
    email VARCHAR(255),
    phone_number VARCHAR(255)
);


CREATE TABLE Transactions (
    transaction_id INT PRIMARY KEY,
    book_id INT,
    member_id INT,
    checkout_date DATE,
    due_date DATE,
    return_date DATE,
    FOREIGN KEY (book_id) REFERENCES Books(book_id),
    FOREIGN KEY (member_id) REFERENCES Members(member_id)
);

CREATE TABLE Categories (
    category_id INT PRIMARY KEY,
    category_name VARCHAR(255)
);


CREATE TABLE Authors (
    author_id INT PRIMARY KEY,
    author_name VARCHAR(255)
);


CREATE TABLE Publishers (
    publisher_id INT PRIMARY KEY,
    publisher_name VARCHAR(255)
);

CREATE TABLE Book_Categories (
    book_id INT,
    category_id INT,
    FOREIGN KEY (book_id) REFERENCES Books(book_id),
    FOREIGN KEY (category_id) REFERENCES Categories(category_id)
);

CREATE TABLE Book_Authors (
    book_id INT,
    author_id INT,
    FOREIGN KEY (book_id) REFERENCES Books(book_id),
    FOREIGN KEY (author_id) REFERENCES Authors(author_id)
);

CREATE TABLE Reservations (
    reservation_id INT PRIMARY KEY,
    book_id INT,
    member_id INT,
    reservation_date DATE,
    status VARCHAR(255),
    FOREIGN KEY (book_id) REFERENCES Books(book_id),
    FOREIGN KEY (member_id) REFERENCES Members(member_id)
);


CREATE TABLE Library_Staff (
    staff_id INT PRIMARY KEY,
    first_name VARCHAR(255),
    last_name VARCHAR(255),
    email VARCHAR(255),
    phone_number VARCHAR(255),
    role VARCHAR(255)
);


CREATE TABLE Reviews (
    review_id INT PRIMARY KEY,
    book_id INT,
    member_id INT,
    rating INT,
    comments VARCHAR(255),
    FOREIGN KEY (book_id) REFERENCES Books(book_id),
    FOREIGN KEY (member_id) REFERENCES Members(member_id)
);

CREATE TABLE Fine_Records (
    fine_id INT PRIMARY KEY,
    member_id INT,
    transaction_id INT,
    fine_amount DECIMAL(10, 2),
    payment_status VARCHAR(255),
    FOREIGN KEY (member_id) REFERENCES Members(member_id),
    FOREIGN KEY (transaction_id) REFERENCES Transactions(transaction_id)
);

CREATE TABLE Library_Locations (
    location_id INT PRIMARY KEY,
    location_name VARCHAR(255),
    address VARCHAR(255),
    phone_number VARCHAR(255)
);

CREATE TABLE Library_Section (
    section_id INT PRIMARY KEY,
    section_name VARCHAR(255),
    location_id INT,
    FOREIGN KEY (location_id) REFERENCES Library_Locations(location_id)
);

INSERT INTO Books (book_id, title, author, publication_date, genre, publisher, ISBN, quantity)
VALUES
    (1, 'rich dad poor dad',                       'Robert Kiyosaki',  '1997-04-25', 'motivational', 'Warner Books', 'ISBN-001', 5),
    (2, 'the silent patient',                      'Alex Michaelides', '2019-02-05', 'Mystery',      'Celadon Books', 'ISBN-002', 3),
    (3, 'the alchemist',                           'Paulo Coelho',     '1988-03-01', 'Fiction',      'Harper Torch', 'ISBN-003', 7),
    (4, 'The Forty Rules of Love',                 'Elif Shafak',      '2009-03-01', 'novel',        'penguin books', 'ISBN-004', 2),
    (5, 'Harry Potter and the Philosophers Stone', 'J. K. Rowling',    '1997-06-26', 'Fantasy',      'Scholastic Corporation ', 'ISBN-005', 4),
    (6, 'Harry Potter and the Chamber of Secrets', 'J. K. Rowling',    '1998-06-02', 'Fantasy',      'Scholastic Corporation', 'ISBN-006', 6);


INSERT INTO Members (member_id, first_name, last_name, address, email, phone_number)
VALUES
    (1, 'mozalfa', 'ilyas', 'gujrat', '[email protected]', '123-456-7890'),
    (2, 'ume', 'arbab', 'city gujrat', '[email protected]', '987-654-3210'),
    (3, 'zuha', 'eman', 'gujrat', '[email protected]', '555-123-4567'),
    (4, 'sadia', 'amir', 'gujrat main', '[email protected]', '777-888-9999'),
    (5, 'maryam', 'mustafa', 'sargodha road gujrat', '[email protected]', '111-222-3333'),
    (6, 'rida', 'yahya', 'GT road gujrat', '[email protected]', '444-555-6666');


INSERT INTO Transactions (transaction_id, book_id, member_id, checkout_date, due_date, return_date)
VALUES
    (1, 1, 1, '2022-01-05', '2022-02-05', '2022-02-12'),
    (2, 2, 2, '2022-01-05', '2022-02-05','2022-01-13'),
    (3, 3, 3, '2022-01-05', '2022-02-05', '2022-01-14'),
    (4, 4, 4, '2022-02-05', '2022-03-05', '2022-02-12'),
    (5, 5, 5, '2022-02-05', '2022-03-05', '2022-03-06'),
    (6, 6, 6, '2022-02-05', '2022-03-05', '2022-02-14');


INSERT INTO Categories (category_id, category_name)
VALUES
    (1, 'Motivational'),
    (2, 'Mystery'),
    (3, 'Fiction'),
    (4, 'Novel'),
    (5, 'Fantasy');


INSERT INTO Authors (author_id, author_name)
VALUES
    (1, 'Robert Kiyosaki'),
    (2, 'Alex Michaelides'),
    (3, 'Paulo Coelho'),
    (4, 'Elif Shafak'),
    (5, 'J. K. Rowling');

INSERT INTO Publishers (publisher_id, publisher_name)
VALUES
    (1, 'Warner Books'),
    (2, 'Celadon Books'),
    (3, 'Harper Torch'),
    (4, 'penguin books'),
    (5, 'Scholastic Corporation');


INSERT INTO Book_Categories (book_id, category_id)
VALUES
    (1, 1),
    (2, 2),
    (3, 3),
    (4, 4),
    (5, 5),
    (6, 5);


INSERT INTO Book_Authors (book_id, author_id)
VALUES
    (1, 1),
    (2, 2),
    (3, 3),
    (4, 4),
    (5, 5),
    (6, 5);


INSERT INTO Reservations (reservation_id, book_id, member_id, reservation_date, status)
VALUES
    (1, 1, 1, '2022-01-05', 'pending'),
    (2, 2, 2, '2022-01-05', 'active'),
    (3, 3, 3, '2022-01-05', 'cancelled'),
    (4, 4, 4, '2022-02-05', 'active'),
    (5, 5, 5, '2022-02-05', 'pending'),
    (6, 6, 6, '2022-02-05', 'active');


INSERT INTO Library_Staff (staff_id, first_name, last_name, email, phone_number, role)
VALUES
    (1, 'izzah', 'asghar', '[email protected]', '111-222-3333', 'librarian'),
    (2, 'mishaim', 'fatima', '[email protected]', '444-555-6666', 'administrator'),
    (3, 'esha', 'sohail', '[email protected]', '777-888-9999', 'manger'),
    (4, 'zara', 'shehbaz', '[email protected]', '123-456-7890', 'assitant and technician'),
    (5, 'hadia', 'ijaz', '[email protected]', '987-654-3210', 'librarian'),
    (6, 'fatima', 'khalid', '[email protected]', '555-123-4567', 'administrator');


INSERT INTO Reviews (review_id, book_id, member_id, rating, comments)
VALUES
    (1, 1, 1, 4, 'Great book!'),
    (2, 2, 2, 5, 'Highly recommended.'),
    (3, 3, 3, 3, 'Average read.'),
    (4, 4, 4, 2, 'Disappointed.'),
    (5, 5, 5, 4, 'Enjoyed it.'),
    (6, 6, 6, 5, 'Excellent!');


INSERT INTO Fine_Records (fine_id, member_id, transaction_id, fine_amount, payment_status)
VALUES
    (1, 1, 1, 0.00, 'paid'),
    (2, 2, 2, 0.00, 'no fine'),
    (3, 3, 3, 0.00, 'no fine'),
    (4, 4, 4, 0.00, 'no fine'),
    (5, 5, 5, 3.50, 'pending'),
    (6, 6, 6, 0.00, 'no fine');


INSERT INTO Library_Locations (location_id, location_name, address, phone_number)
VALUES
    (1, 'Main Library', 'jail chok gujrat','555-123-4567'),
    (2, 'Branch Library', 'gt road gujrat', '555-456-7890');
    


INSERT INTO Library_Section (section_id, section_name, location_id)
VALUES
    (1, 'Motivational', 1),
    (2, 'Mystery', 1),
    (3, 'Novel', 2),
    (4, 'Fiction', 2),
    (5, 'Fantasy', 2);

SELECT * FROM Books;

SELECT * FROM Members;

SELECT * FROM Transactions;

SELECT * FROM Categories;

SELECT * FROM Authors;

SELECT * FROM Publishers;

SELECT * FROM Book_Categories;

SELECT * FROM Book_Authors;

SELECT * FROM Reservations;

SELECT * FROM Library_Staff;

SELECT * FROM Reviews;

SELECT * FROM Fine_Records;

SELECT * FROM Library_Locations;

SELECT * FROM Library_Section;

SELECT Books.book_id, Books.author,Transactions.member_id from books inner join Transactions ON books.book_id= Transactions.book_id inner join Book_Categories ON Books.book_id=Book_Categories.book_id; 

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;