-- 1.	Create a table to store information about books, including columns for book ID, title, author, genre, publication year, and price.
-- Ensure that the book ID is an auto-incrementing primary key.


CREATE TABLE books (
    book_id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255),
    author VARCHAR(255),
    genre VARCHAR(50),
    publication_year INT,
    price DECIMAL(10, 2)
);

INSERT INTO books (title, author, genre, publication_year, price) VALUES 
('To Kill a Mockingbird', 'Harper Lee', 'Fiction', 1960, 12.99),
('1984', 'George Orwell', 'Dystopian', 1949, 9.99),
('The Great Gatsby', 'F. Scott Fitzgerald', 'Classic', 1925, 8.99),
('Pride and Prejudice', 'Jane Austen', 'Romance', 1813, 7.99),
('The Catcher in the Rye', 'J.D. Salinger', 'Coming-of-age', 1951, 10.99),
('Brave New World', 'Aldous Huxley', 'Science Fiction', 1932, 11.99),
('The Lord of the Rings', 'J.R.R. Tolkien', 'Fantasy', 1954, 15.99),
('Harry Potter and the Philosopher\'s Stone', 'J.K. Rowling', 'Fantasy', 1997, 14.99),
('Crime and Punishment', 'Fyodor Dostoevsky', 'Psychological Fiction', 1866, 1.99),
('Moby-*****', 'Herman Melville', 'Adventure', 1851, 11.49);


-- 	2.	Add a CHECK constraint to ensure that the publication year of a book is between 1800 and the current year, and the price is greater than 0.


ALTER TABLE books
ADD CONSTRAINT chk_publication_year CHECK (publication_year BETWEEN 1800 AND 2024 AND price > 0);


-- 3.	Update the price of all books in the 'Science Fiction' genre to increase it by 10%


UPDATE books
SET price = price * 1.1
WHERE genre = 'Science Fiction';

-- 4.	Display the books in descending order of their publication years

SELECT * FROM books
ORDER BY publication_year DESC;

-- 5.	Delete all books with a price less than 10.00.
DELETE FROM books
WHERE price < 10.00;



SELECT * FROM books
ORDER BY publication_year DESC;


-- 6.	Add a unique constraint on the combination of the "title" and "author" columns to ensure that each book has a unique title within an author.

ALTER TABLE books
ADD CONSTRAINT unique_title_author UNIQUE (title, author);


-- 7.	Add a new column ISBN to books table with unique key constraint

ALTER TABLE books
ADD COLUMN isbn VARCHAR(20) UNIQUE;

-- 8.	update existing record in books table with ISBN number
UPDATE books
SET isbn = 
    CASE 
        WHEN title = 'To Kill a Mockingbird' AND author = 'Harper Lee' THEN 'ISBN123456'
        WHEN title = '1984' AND author = 'George Orwell' THEN 'ISBN789012'
        WHEN title = 'The Great Gatsby' AND author = 'F. Scott Fitzgerald' THEN 'ISBN345678'
        WHEN title = 'Pride and Prejudice' AND author = 'Jane Austen' THEN 'ISBN987654'
        WHEN title = 'The Catcher in the Rye' AND author = 'J.D. Salinger' THEN 'ISBN456789'
        WHEN title = 'Brave New World' AND author = 'Aldous Huxley' THEN 'ISBN654321'
        WHEN title = 'The Lord of the Rings' AND author = 'J.R.R. Tolkien' THEN 'ISBN789456'
        WHEN title = 'Harry Potter and the Philosopher\'s Stone' AND author = 'J.K. Rowling' THEN 'ISBN123789'
        WHEN title = 'Crime and Punishment' AND author = 'Fyodor Dostoevsky' THEN 'ISBN987321'
        WHEN title = 'Moby-*****' AND author = 'Herman Melville' THEN 'ISBN456123'
        ELSE isbn
    END;

SELECT * FROM books
ORDER BY publication_year DESC;



-- 9.	Create a table named "transactions" with the following columns: transaction_id, book_id, member_id, transaction_date, due_date.
CREATE TABLE transactions (
    transaction_id INT AUTO_INCREMENT PRIMARY KEY,
    book_id INT,
    member_id INT,
    transaction_date DATE,
    due_date DATE,
    FOREIGN KEY (book_id) REFERENCES books(book_id)
);
INSERT INTO transactions (book_id, member_id, transaction_date, due_date)
VALUES
(1, 101, '2024-03-12', '2024-03-19'),
(3, 102, '2024-03-13', '2024-03-20'),
(5, 103, '2024-03-14', '2024-03-21'),
(2, 104, '2024-03-15', '2024-03-22'),
(4, 105, '2024-03-16', '2024-03-23');
-- 10.	Add a foreign key constraint to the "transactions" table referencing the "books" table.
ALTER TABLE transactions
ADD CONSTRAINT fk_book_id FOREIGN KEY (book_id) REFERENCES books(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;