0% found this document useful (0 votes)
66 views14 pages

dec-1-MYSQL Commands

The document describes SQL commands and functions. It includes examples of creating databases and tables, inserting data, using comparison and logical operators for selection, ordering and grouping results, performing joins between tables, and using aggregate functions. It also discusses different types of SQL statements like DDL, DML, DCL and provides examples of string, date and numeric functions.

Uploaded by

Kishan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views14 pages

dec-1-MYSQL Commands

The document describes SQL commands and functions. It includes examples of creating databases and tables, inserting data, using comparison and logical operators for selection, ordering and grouping results, performing joins between tables, and using aggregate functions. It also discusses different types of SQL statements like DDL, DML, DCL and provides examples of string, date and numeric functions.

Uploaded by

Kishan Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

https://rextester.

com/l/mysql_online_compiler

https://www.jdoodle.com/online-mysql-terminal

Good editor that shows Execution Plan:

http://sqlfiddle.com/#!9/a6c585/1

SQL(Structured Query Language)

DDL(Data Defination
DML(Data Manipulation
Language)
Language) DCL(Data Control Language)
e.g., create, alter, truncate,
e.g., select, insert, update, e.g., grant, revoke
drop, rename(change
delete
column-MySQL)

show databases;

create database d1;

use rextester;

show tables;

create table try1(name varchar(10),no int);

desc try1;

insert into try1 values('ABC',101);

insert into try1 values('XYZ',102);

insert into try1 values('JKL',103);

insert into try1 values('MNO',104);


insert into try1 values('PQR',105);

insert into try1 values('ABCD',101);

insert into try1 values('HIJKL',103);

insert into try1 values('WXYZ',102);

select * from try1;

desc try1;

alter table try1 ADD PRIMARY KEY(no);

alter table try1 modify name varchar(20);

alter table try1 drop no;

alter table try1 add (name varchar(10));

delete from try1;

alter table try1 change column no clientno int(5);

truncate table try1;

update try1 set name='newclient' where clientno<103;

SELECT statement

Comparison operators

=, <>/!=, <, >, <=, >=, like

Wild card characters

%, _

Logical operators

AND, OR, NOT

Joins, group by with aggregate functions, group by with HAVING clause, order by

- Find customer details containing 2nd alphabet ‘X’ in their name.

select * from try1 where name like '_X__';

- Find customer details with name starting from ‘X’.


select * from try1 where name like 'X__';

- Find customer details whose name contains 3rd last character ‘X’.

select * from try1 where name like '%X__';

- Find customer details whose name ends with ‘X’.

select * from try1 where name like '%X';

- Find customer details whose name starts with ‘x’.

select * from try1 where name like 'x%';

- Find customer details with ‘M’ in their name.

select * from try1 where name like '%M%';

- Find customer details whose name ends with ‘M’.

select * from try1 where name like '%M';

- Find customer details whose name contains 1 alphabet after ‘JKL’.

select * from try1 where name like '%JKL_';

- Find customer details whose name contains ‘JKL’ in name.

select * from try1 where name like '%JKL%';

select * from try1 where (name like '%JKL%');

- Find customer details whose does not contain ‘JKL’.

select * from try1 where NOT(name like '%JKL%');

- Find customer details whose clientno is >102.

select * from try1 where not(clientno<102);

- Find customer details whose clientno is >103.

select * from try1 where clientno>103;

- Find customer details whose clientno is >102.

select * from try1 where clientno>103 or not(clientno<102);

- Find customer details whose clientno is >101.

select * from try1 where clientno>103 and not(clientno<101);

sorted orders : Ascending, Descending


select * from try1;

select * from try1 order by clientno;

select * from try1 order by clientno desc;

select * from try1;

select * from try1 order by clientno;

select * from try1 order by clientno, name;

select * from try1 order by clientno desc;

select * from try1 order by clientno asc, name desc;

create table clientdata(clientname varchar(15), clientproject varchar(15), clientbill float(7,2),


no_of_employees int(4), no_of_hours int);

alter table clientdata add(clientid int(4));

ALTER TABLE clientdata ADD PRIMARY KEY(clientid);

create table projectdata(projectid int, projectname varchar(15) references clientdata(clientproject),


duration int);

create table teams(teamid int, technology varchar(15), projectname varchar(15) references


clientdata(clientproject), members int, hours int);

alter table projectdata add primary key (projectname);

alter table clientdata add constraint cons_fk foreign key (clientproject) references
projectdata(projectname);

alter table teams add constraint cons_fk1 foreign key (projectname) references
projectdata(projectname);

desc projectdata;
insert into projectdata values(1001, 'AutocallSOS',260);

insert into projectdata values(1002, 'FastCheck',390);

insert into projectdata values(1003, 'EvenServe',430);

insert into projectdata values(1004, 'SmartAppDev',600);

insert into projectdata values(1005, 'Payease',800);

insert into projectdata values(1006, 'prj5',200);

insert into projectdata values(1007, 'FastCheck1',900);

insert into projectdata values(1008, 'EaseGST',700);

select * from projectdata;

desc teams;

insert into teams values(2001,'.Net', 'SmartAppDev',6,12);

insert into teams values(2002,'Java', 'FastCheck',7,90);

insert into teams values(2003,'PHP', 'Payease',10,12);

insert into teams values(2004,'.Net', 'prj5',6,12);

insert into teams values(2005,'Android', 'EvenServe',7,210);

select * from teams;

desc clientdata;

insert into clientdata values("Client1","EaseGST", 50000,15,70,101);

insert into clientdata values("Client-xyz","EvenServe", 6000,15,70,102);

insert into clientdata values("Client3","AutocallSOS",40000.00,3,100,103);

insert into clientdata values("Client4","EaseGST",7000.00,9,120,104);

insert into clientdata values("Client5","prj5",90000.00,6,144,105);


insert into clientdata values("Client8","prj5",70000.00,6,80,106);

insert into clientdata values("TasteIT","EvenServe",10000.00,7,210,107);

insert into clientdata values("NHAI","FastCheck",98000.00,2,90,108);

insert into clientdata values("NHAI","FastCheck1",80000.00,2,90,109);

insert into clientdata values("Client8","Payease",3000.00,5,12,110);

insert into clientdata values("SmartAppDev","FastCheck1",9000.00,5,12,111);

insert into clientdata values("LearnFast","FastCheck",17000.00,5,12,112);

select * from clientdata;

Group by :

select clientproject, clientbill from clientdata group by clientproject;

select clientproject, sum(clientbill) from clientdata group by clientproject;

select count(no_of_hours) from clientdata;

select count(no_of_hours) from clientdata group by no_of_employees;

select no_of_employees, count(no_of_hours) from clientdata group by no_of_employees;

JOINS :
Set Theory :

Set A = {1,2,A}

Set B = {A, B, 1}

Cartesian Product : 3*3=9

A×B = {(1,A),(1,B),(1,1),

(2,A),(2,B),(2,1),

(A,A),(A,B),(A,1)}

Intersection : A∩B = {1,A} = inner join/natural join/equi join (= operator in query)

A B

Union : AUB = {1,2,A,B,A,1} = Full Outer join / non equality join (< or > or <> operators in query)

A B

Set Difference : Set A = {1,2,A}

Set B = {A, B, 1}

A-B = {2}

A B

B-A = {B}
A B

LEFT OUTER JOIN : A∩BU(A-B)

A B

RIGHT OUTER JOIN : A∩BU(B-A)

A B

Teams

work
order

Projectdata

Clientdata

Use of Single table:


select projectname,technology from teams where projectname='EvenServe';

Use of two tables without join : (Cartesian Product)

select clientdata.clientname, teams.technology, teams.projectname from clientdata, teams where


teams.projectname='EvenServe';

Use of two tables with join :

select clientdata.clientname, teams.technology, teams.projectname from clientdata, teams where


teams.projectname='EvenServe' and clientdata.clientproject=teams.projectname;

select clientdata.clientname, teams.technology, teams.projectname, projectdata.duration from


clientdata, teams, projectdata where teams.projectname='EvenServe' and
clientdata.clientproject=teams.projectname and projectdata.projectname=teams.projectname ;

OUTER JOIN

/* MySQL does not support full outer join. It can be emulated using UNION of LEFT and RIGHT OUTER
JOINS */

select clientdata.clientname, teams.projectname from clientdata full [outer] join teams on


clientdata.clientproject=teams.projectname;

select clientdata.clientname, teams.projectname from clientdata left outer join teams on


clientdata.clientproject=teams.projectname;

select clientdata.clientname, teams.projectname from clientdata right outer join teams on


clientdata.clientproject=teams.projectname;

select clientdata.clientname, teams.projectname from clientdata left outer join teams on


clientdata.clientproject=teams.projectname

union

select clientdata.clientname, teams.projectname from clientdata right outer join teams on


clientdata.clientproject=teams.projectname;
create table from other table’s data :

create table projectdata13 select * from projectdata where projectid >1004;

select * from projectdata13;

String, date, number functions :

String Functions :

ASCII
CHAR_LENGTH
CONCAT
FIND_IN_SET
FORMAT
INSERT
INSTR
LEFT
LENGTH
LOCATE
LOWER
LPAD
LTRIM
MID
POSITION
REPEAT
REPLACE
REVERSE
RIGHT
RPAD
RTRIM
STRCMP
SUBSTR
TRIM
UPPER

Date Functions :

CURDATE
CURTIME
ADDDATE
ADDTIME
DATEDIFF
DATE_FORMAT
DATE_SUB
DAYNAME
DAYOFMONTH
DAYOFWEEK
DAYOFYEAR
EXTRACT
HOUR
MINUTE
YEAR
QUARTER
MONTH
WEEK
DAY
MONTHNAME
TIME_FORMAT

Numeric Function :

ABS
AVG
CEIL
COUNT
DIV
EXP
FLOOR
GREATEST
LEAST
LOG
MAX
MIN
MOD
PI
POWER
RAND
ROUND
SQRT
SUM
TRUNCATE

select projectname,ASCII(projectname) from projectdata;

select projectname,char_length(projectname) from projectdata;

select projectname,concat("Name of the project for ", projectid," is ",projectname) from projectdata;

select duration,format(duration,2) from projectdata;


Subquery :

Nested query/inner query

Query1(query2);

Single valued inner query : =, <>, <, <=, >, >=

Multi valued inner query : IN, NOT IN

select clientname from clientdata where clientproject = (select projectname from teams where
teamid=2003);

select clientname from clientdata where clientproject IN (select projectname from teams);

select clientname, projectname from clientdata,teams where clientproject=projectname and


clientproject IN (select projectname from teams where projectname IN (select projectname from
projectdata where duration>700));

select clientname from clientdata where clientproject IN (select projectname from teams where
projectname IN (select projectname from projectdata where duration>700));

PL/SQL : Procedural Language/SQL

Objects of PL/SQL :

1. Unnamed block-not available in MySQL


2. Named Blocks : (Cursor/Package)
a. Stored Procedure
b. Function
c. View
d. Trigger

---Oracle Blocks----

DECLARE

Message varchar(20):= 'Hello World!';

BEGIN
dbms_output.put_line(Message);

END;

create procedure proc13try()

Message varchar(20):= 'Hello World!';

BEGIN

dbms_output.put_line(Message);

END;

declare

begin

dbms_output.put_line(proc13try());

end;

---MySQL stored procedure

create procedure proctry1()

begin

select count(*) from clientdata;

end;

call proctry1();

create procedure proctry12345678(in param2 varchar(10),out param1 int)

begin

select clientid into param1 from clientdata where clientproject=param2;

end;

call proctry12345678("Payease",@a);

select @a;
-->>>work upon using cursor with loop.

create procedure proctry12345678(in param2 varchar(10),out param1 int)

begin

/*CURSOR id_cur SELECT id FROM emp_ids;*/

select clientid into param1 from clientdata where clientproject=param2;

LOOP

END LOOP;

end;

You might also like