# drop the pet table if it exists, then recreate it DROP TABLE IF EXISTS pet; CREATE TABLE pet ( name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE ); insert into pet (name, owner, species, sex, birth, death) values ('Fluffy', 'Harold', 'cat', 'f', '1993-02-04', null); insert into pet (name, owner, species, sex, birth, death) values ('Claws', 'Gwen', 'cat', 'm', '1994-03-17', null); insert into pet (name, owner, species, sex, birth, death) values ('Buffy', 'Harold', 'dog', 'f', '1989-05-13', null); insert into pet (name, owner, species, sex, birth, death) values ('Fang', 'Benny', 'dog', 'm', '1990-08-27', null); insert into pet (name, owner, species, sex, birth, death) values ('Bowser', 'Diane', 'dog', 'm', '1979-08-31', '1995-07-29'); insert into pet (name, owner, species, sex, birth, death) values ('Chirpy', 'Gwen', 'bird', 'f', '1998-09-11', null); insert into pet (name, owner, species, sex, birth, death) values ('Whistler', 'Gwen', 'bird', null, '1997-12-09', null); insert into pet (name, owner, species, sex, birth, death) values ('Slim', 'Benny', 'snake', 'm', '1996-04-29', null); INSERT INTO pet VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL); # drop the event table if it exists, then recreate it DROP TABLE IF EXISTS event; CREATE TABLE event ( name VARCHAR(20), date DATE, type VARCHAR(15), remark VARCHAR(255) ); insert into event (name, date, type, remark) values ('Fluffy', '1995-05-15', 'litter', '4 kittens, 3 female, 1 male'); insert into event (name, date, type, remark) values ('Buffy', '1993-06-23', 'litter', '5 puppies, 2 female, 3 male'); insert into event (name, date, type, remark) values ('Buffy', '1994-06-19', 'litter', '3 puppies, 3 female'); insert into event (name, date, type, remark) values ('Chirpy', '1999-03-21', 'vet', 'needed beak straightened'); insert into event (name, date, type, remark) values ('Slim', '1997-08-03', 'vet', 'broken rib'); insert into event (name, date, type, remark) values ('Bowser', '1991-10-12', 'kennel', null); insert into event (name, date, type, remark) values ('Fang', '1991-10-12', 'kennel', null); insert into event (name, date, type, remark) values ('Fang', '1998-08-28', 'birthday', 'Gave him a new chew toy'); insert into event (name, date, type, remark) values ('Claws', '1998-03-17', 'birthday', 'Gave him a new flea collar'); insert into event (name, date, type, remark) values ('Whistler', '1998-12-09', 'birthday', 'First birthday'); show tables; select name, owner from pet; select name, owner, species from pet; select * from pet; select species, name, sex from pet; select distinct(species) from pet; select all species from pet; -- use db_3xu8gj64n; show tables; desc pet; select 1+6 total; select 1+6 from dual; select curdate(); desc event; select date as "Event Date", type as "Event Type" from event; select 22/7 as PI; select name, birth, death from pet; select name, birth, ifnull(death, "Alive") as "Died On" from pet; select name, 'is a ', species from pet; select name, species from pet where species <> 'dog'; select * from pet where (species = 'dog' or species = 'cat') and sex = 'm'; select * from pet where species in ('bird', 'snake', 'hamster'); select name from pet where name like '%y'; select name from pet where name like 'F%'; select * from pet where name like '____'; select * from pet where death IS NOT NULL; select name from pet order by name desc; select * from pet order by name, sex desc; select char(70, 65, 67, 69); select concat(name, ' is a ', species) as "Name species" from pet order by name; select upper(name) as "Upper Case Name" from pet; select lower(name) as "Upper Case Name" from pet; select substring(name, -5, 4) as "Subset of name" from pet; select trim(' i sdfsdfsfs '); select name, length(name) as "Lenght of Name" from pet; select month(curdate()) as "Month of the year"; select now() as "Current Date and Time"; select distinct name, species from pet; select count(name) from pet; select count(death) from pet; select count(ifnull(death,'Alive')) from pet; select max(name) as "Max of Name" from pet;
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;