/* Delete the tables if they already exist */ drop table if exists Movie; drop table if exists Reviewer; drop table if exists Rating; /* Create the schema for our tables */ create table Movie(mID int primary key, title text, year int, director text); create table Reviewer(rID int primary key, name text); create table Rating(rID int, foreign key (rID)references Reviewer(rID), mID int, foreign key(mID) references Movie(mID), stars int, ratingDate date); /* Populate the tables with our data */ insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming'); insert into Movie values(102, 'Star Wars', 1977, 'George Lucas'); insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise'); insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg'); insert into Movie values(105, 'Titanic', 1997, 'James Cameron'); insert into Movie values(106, 'Snow White', 1937, null); insert into Movie values(107, 'Avatar', 2009, 'James Cameron'); insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg'); insert into Reviewer values(201, 'Sarah Martinez'); insert into Reviewer values(202, 'Daniel Lewis'); insert into Reviewer values(203, 'Brittany Harris'); insert into Reviewer values(204, 'Mike Anderson'); insert into Reviewer values(205, 'Chris Jackson'); insert into Reviewer values(206, 'Elizabeth Thomas'); insert into Reviewer values(207, 'James Cameron'); insert into Reviewer values(208, 'Ashley White'); insert into Rating values(201, 101, 2, '2011-01-22'); insert into Rating values(201, 101, 4, '2011-01-27'); insert into Rating values(202, 106, 4, null); insert into Rating values(203, 103, 2, '2011-01-20'); insert into Rating values(203, 108, 4, '2011-01-12'); insert into Rating values(203, 108, 2, '2011-01-30'); insert into Rating values(204, 101, 3, '2011-01-09'); insert into Rating values(205, 103, 3, '2011-01-27'); insert into Rating values(205, 104, 2, '2011-01-22'); insert into Rating values(205, 108, 4, null); insert into Rating values(206, 107, 3, '2011-01-15'); insert into Rating values(206, 106, 5, '2011-01-19'); insert into Rating values(207, 107, 5, '2011-01-20'); insert into Rating values(208, 104, 3, '2011-01-02'); /*select * from Reviewer re1,Rating r1,Reviewer re2,Rating r2 where re1.rID=r1.rID and re2.rID=r2.rID and r1.mID=r2.mID and re2.name="Chris Jackson" and re1.name!="Chris Jackson" and r1.stars=r2.stars; select * from Movie m,Reviewer re1,Rating r1,Reviewer re2,Rating r2 where re1.rID=r1.rID and re2.rID=r2.rID and r1.mID=r2.mID and m.mID=r1.mID and re1.name="Sarah Martinez" and re2.name="Mike Anderson"; */ /*select distinct m.title from Movie m,Reviewer re1,Reviewer re2,Rating r1,Rating r2 where re1.name="Sarah Martinez" and re1.rID=r1.rID and re2.name="Mike Anderson" and re2.rID=r2.rID and r1.mID=r2.mID and r1.mID=m.mID;*/ select distinct re1.name from Reviewer re1, Reviewer re2, Rating r1, Rating r2 where re1.rID = r1.rID AND re2.rID = r2.rID AND re2.name = "Brittany Harris" AND r1.mID = r2.mID AND re1.name!="Brittany Harris"; select title,count(mID) from Movie group by(mID);
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;