create table user(UId int primary key, name varchar(30) not null, email varchar(30)not null, password varchar(10) not null, street_address varchar(40)not null, dob date not null);

create table songs(SongId int primary key,artist varchar(30) not null,song_title varchar(20) not null,album varchar(20) not null, year date not null);

create table fav_songs(UId int not null, SongId int not null);
ALTER TABLE fav_songs ADD FOREIGN KEY (UID) REFERENCES user(UID);
ALTER TABLE fav_songs ADD FOREIGN KEY (songID) REFERENCES songs(songID);
alter table fav_songs add primary key(UID,songID);

create table groups_table(GId int primary key auto_increment, group_name varchar(20) not null, fav_artist varchar(30) not null, description varchar(40) not null);

create table group_members(UId int not null, GId int not null);
ALTER TABLE group_members ADD FOREIGN KEY (UID) REFERENCES user(UID);
ALTER TABLE group_members ADD FOREIGN KEY (GID) REFERENCES groups_table(GID);
alter table group_members add primary key(UID,GID);

create table group_message(date_time datetime not null , senderUId int, to_GId int not null,body varchar(60) not null,foreign key(senderUId) references user(UId), foreign key(to_GId) references groups_table(GId));
alter table group_message add primary key(date_time,senderUId);

create table chat_message(date_time datetime not null , senderUId int, to_UId int not null,body varchar(60) not null,foreign key(senderUId) references user(UId), foreign key(to_UId) references user(UId));
alter table chat_message add primary key(date_time,senderUId);


create table friends(UId1 int not null, UId2 int not null, foreign key(UId1) references user(UId), foreign key(UId2) references user(UId));
alter table friends add primary key(UId1,UId2);


insert into user values(1,"ajay","[email protected]","123","bombay",'2001-02-01');
insert into user values(2,"vijay","[email protected]","1234","Kolkata",'2001-03-01');
insert into user values(3,"sanjay","[email protected]","12345","Delhi",'2002-08-09');
insert into user values(4,"jay","[email protected]","123456","bombay",'1998-05-10');
insert into user values(5,"pranay","[email protected]","1234567","Gurgaon",'2000-04-11');


insert into songs values(1,"Charlie Puth","we dont talk anymore","nine track mind",'2016-08-03');
insert into songs values(2,"BTS","Permission to dance","Butter",'2021-07-09');
insert into songs values(3,"The Score","Unstoppable","Atlas",'2017-10-27');
insert into songs values(4,"The Score","Legend","Atlas",'2018-07-27');
insert into songs values(5,"Taylor Swift","trouble","swifty",'2012-11-27');
insert into songs values(6,"Selena Gomez","Lose you to love me","rare",'2019-10-23');
insert into songs values(7,"Maroon5","Girls Like You","Red Pill Blues",'2018-05-30');
insert into songs values(8,"BTS","Go Go","Love Yourself",'2017-09-18');
insert into songs values(9,"One Direction","Drag Me Down","Made in the A.M.",'2018-07-31');
insert into songs values(10,"Taylor Swift","wildest dreams","1989",'2015-08-31');

insert into friends values("1","2");
insert into friends values("1","4");
insert into friends values("2","1");
insert into friends values("2","3");
insert into friends values("3","2");
insert into friends values("4","1");
insert into friends values("5","4");
insert into friends values("1","5");
insert into friends values("5","1");
insert into friends values("2","5");
insert into friends values("5","2");

insert into fav_songs values("1","1");
insert into fav_songs values("1","2");
insert into fav_songs values("1","4");
insert into fav_songs values("2","1");
insert into fav_songs values("2","3");
insert into fav_songs values("2","4");
insert into fav_songs values("3","2");
insert into fav_songs values("3","5");
insert into fav_songs values("4","1");
insert into fav_songs values("4","2");
insert into fav_songs values("4","5");
insert into fav_songs values("5","1");
insert into fav_songs values("5","2");
insert into fav_songs values("5","3");
insert into fav_songs values("5","4");


 

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;