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







 

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;