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