-- Create Employee table CREATE TABLE Employee ( EMPLOYEE_ID INT PRIMARY KEY, name VARCHAR(100), age INT, department VARCHAR(100), salary DECIMAL(10, 2) ); -- Create Student table CREATE TABLE Student ( STUDENT_ID INT PRIMARY KEY, name VARCHAR(100), age INT, major VARCHAR(100), GPA DECIMAL(3, 2) ); -- Create Product table CREATE TABLE Product ( PRODUCT_ID INT PRIMARY KEY, name VARCHAR(100), price DECIMAL(10, 2), category VARCHAR(100) ); -- Create Customer table CREATE TABLE Customer ( CUSTOMER_ID INT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100), phone VARCHAR(20) ); -- Create Account table CREATE TABLE Account ( ACCOUNT_ID INT PRIMARY KEY, account_number VARCHAR(20), balance DECIMAL(15, 2), type VARCHAR(100) ); -- Create Person table CREATE TABLE Person ( PERSON_ID INT PRIMARY KEY, name VARCHAR(100), age INT, gender VARCHAR(10) ); -- Insert data into Employee table INSERT INTO Employee (EMPLOYEE_ID, name, age, department, salary) VALUES (1, 'John Doe', 30, 'IT', 50000.00), (2, 'Jane Smith', 35, 'HR', 55000.00), (3, 'Michael Johnson', 28, 'Finance', 60000.00), (4, 'Emily Davis', 32, 'Marketing', 52000.00), (5, 'David Wilson', 40, 'Sales', 58000.00); -- Insert data into Student table INSERT INTO Student (STUDENT_ID, name, age, major, GPA) VALUES (1, 'Alice Johnson', 20, 'Computer Science', 3.75), (2, 'Bob Smith', 22, 'Engineering', 3.50), (3, 'Carol Brown', 21, 'Mathematics', 3.90), (4, 'Dan White', 23, 'Physics', 3.80), (5, 'Eve Taylor', 19, 'Biology', 3.85); -- Insert data into Product table INSERT INTO Product (PRODUCT_ID, name, price, category) VALUES (1, 'Laptop', 1000.00, 'Electronics'), (2, 'Shirt', 25.00, 'Clothing'), (3, 'Book', 15.00, 'Stationery'), (4, 'Headphones', 50.00, 'Electronics'), (5, 'Watch', 200.00, 'Accessories'); -- Insert data into Customer table INSERT INTO Customer (CUSTOMER_ID, name, email, phone) VALUES (1, 'John Smith', '[email protected]', '123-456-7890'), (2, 'Alice Johnson', '[email protected]', '987-654-3210'), (3, 'Bob Brown', '[email protected]', '456-789-0123'), (4, 'Emily Davis', '[email protected]', '789-012-3456'), (5, 'David Wilson', '[email protected]', '321-654-9870'); -- Insert data into Account table INSERT INTO Account (ACCOUNT_ID, account_number, balance, type) VALUES (1, '1234567890', 10000.00, 'Savings'), (2, '9876543210', 5000.00, 'Checking'), (3, '4567890123', 7500.00, 'Savings'), (4, '7890123456', 12000.00, 'Checking'), (5, '3216549870', 9000.00, 'Savings'); -- Insert data into Person table INSERT INTO Person (PERSON_ID, name, age, gender) VALUES (1, 'John Doe', 30, 'Male'), (2, 'Jane Smith', 35, 'Female'), (3, 'Michael Johnson', 28, 'Male'), (4, 'Emily Davis', 32, 'Female'), (5, 'David Wilson', 40, 'Male'); -- Fetch all data from Employee table SELECT * FROM Employee; -- Fetch all data from Student table SELECT * FROM Student; -- Fetch all data from Product table SELECT * FROM Product; -- Fetch all data from Customer table SELECT * FROM Customer; -- Fetch all data from Account table SELECT * FROM Account; -- Fetch all data from Person table SELECT * FROM Person; -- Drop tables DROP TABLE Employee; DROP TABLE Student; DROP TABLE Product; DROP TABLE Customer; DROP TABLE Account; DROP TABLE Person;
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;