CREATE TABLE Student ( StudentID INT PRIMARY KEY, Name VARCHAR(50), Age INT, Gender VARCHAR(10), Address VARCHAR(100), ContactNumber VARCHAR(15) ); INSERT INTO Student (StudentID, Name, Age, Gender, Address, ContactNumber) VALUES (1, 'John Doe', 20, 'Male', '123 Main St, City A', '123-456-7890'), (2, 'Jane Smith', 22, 'Female', '456 Elm St, City B', '987-654-3210'), (3, 'Michael Johnson', 21, 'Male', '789 Oak St, City C', '555-123-4567'), (4, 'Emily Davis', 19, 'Female', '321 Pine St, City D', '444-789-1234'), (5, 'Chris Wilson', 23, 'Male', '567 Maple St, City E', '321-654-9870'), (6, 'Sarah Brown', 20, 'Female', '654 Cedar St, City F', '777-888-9999'), (7, 'David Taylor', 21, 'Male', '987 Birch St, City G', '111-222-3333'), (8, 'Jessica Martinez', 22, 'Female', '234 Birch St, City H', '666-777-8888'), (9, 'Matthew Anderson', 24, 'Male', '876 Oak St, City I', '333-222-1111'), (10, 'Amanda Lee', 19, 'Female', '345 Cedar St, City J', '999-888-7777'), (11, 'Daniel Clark', 20, 'Male', '567 Maple St, City K', '222-333-4444'), (12, 'Laura Rodriguez', 21, 'Female', '789 Pine St, City L', '888-777-6666'), (13, 'Kevin Wright', 22, 'Male', '876 Elm St, City M', '555-444-3333'), (14, 'Megan Hall', 23, 'Female', '987 Oak St, City N', '444-333-2222'), (15, 'Ryan Allen', 20, 'Male', '234 Cedar St, City O', '333-222-1111'), (16, 'Olivia Young', 21, 'Female', '345 Pine St, City P', '222-111-0000'), (17, 'Jacob Scott', 22, 'Male', '654 Birch St, City Q', '111-000-9999'), (18, 'Sophia Green', 23, 'Female', '876 Elm St, City R', '000-999-8888'), (19, 'Ethan King', 20, 'Male', '987 Maple St, City S', '999-888-7777'), (20, 'Isabella Baker', 21, 'Female', '654 Cedar St, City T', '888-777-6666'), (21, 'Noah Adams', 22, 'Male', '321 Pine St, City U', '777-666-5555'), (22, 'Ava Perez', 23, 'Female', '456 Oak St, City V', '666-555-4444'), (23, 'William Torres', 24, 'Male', '789 Elm St, City W', '555-444-3333'), (24, 'Chloe Flores', 20, 'Female', '123 Maple St, City X', '444-333-2222'), (25, 'Liam Collins', 21, 'Male', '234 Cedar St, City Y', '333-222-1111'); CREATE TABLE Course ( CourseID INT PRIMARY KEY, CourseName VARCHAR(50), Instructor VARCHAR(50), StudentID INT, FOREIGN KEY (StudentID) REFERENCES Student(StudentID) ); INSERT INTO Course (CourseID, CourseName, Instructor, StudentID) VALUES (101, 'Mathematics', 'Prof. Johnson', 1), (102, 'Physics', 'Prof. Smith', 2), (103, 'Chemistry', 'Prof. Williams', 3), (104, 'Biology', 'Prof. Brown', 4), (105, 'Computer Science', 'Prof. Lee', 5), (106, 'English', 'Prof. Taylor', 6), (107, 'History', 'Prof. Martinez', 7), (108, 'Geography', 'Prof. White', 8), (109, 'Economics', 'Prof. Clark', 9), (110, 'Art', 'Prof. Rodriguez', 10), (111, 'Music', 'Prof. Wright', 11), (112, 'Physical Education', 'Prof. Hall', 12), (113, 'Political Science', 'Prof. Allen', 13), (114, 'Psychology', 'Prof. Moore', 14), (115, 'Sociology', 'Prof. King', 15), (116, 'Anthropology', 'Prof. Baker', 16), (117, 'Philosophy', 'Prof. Perez', 17), (118, 'Religious Studies', 'Prof. Torres', 18), (119, 'Foreign Language', 'Prof. Adams', 19), (120, 'Engineering', 'Prof. Perez', 20), (121, 'Medicine', 'Prof. Adams', 21), (122, 'Law', 'Prof. Brown', 22), (123, 'Business Administration', 'Prof. Torres', 23), (124, 'Architecture', 'Prof. Flores', 24), (125, 'Environmental Science', 'Prof. Collins', 25); select * from course;
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;