sql statement,functions and joins

Post on 26-Jun-2015

379 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Yasir musthafa ppyasirmusthafapp@gmail.co

mwww.facebook.com/

yasirmusthafatwitter.com/yasirmusthafappin.linkedin.com/in/

yasirmusthafapp8891396749

Sql statements ,functions and joins

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Sql statements

• Query or command to perform a task in a database

3 type1. DDL2. DML3. DCL

DDL

Data definition language• Create• Alter• Drop• truncate

Create

Syntax in mysqlCREATE TABLE table_name

(column_name1 data_type(size),column_name2 data_type(size),....);Example

create table tbl_stock(pk_int_stock_id int auto_increment,vchr_name varchar(20),int_quantity int,int_price int,primary key(pk_int_stock_id));

Alter

Syntax in mysqlAlter table table_name modify column

column_name data_type;Examplealter table tbl_stock modify column int_price

float;

Drop

Syntax in mysqlDrop table table_name;Example Drop table tbl_student;

Truncate

Syntax in mysqlTruncate table table_name;Example Truncate table tbl_student;

DML

Data manipulation language• Select• Insert• Update• delete

Select

Syntax in mysqlSelect * from table_name;Example Select * from tbl_stock;

Insert

Syntax in mysqlinsert into table_name values( );Example insert into tbl_stock values(NULL,"mouse",10,500,1);

Update

Syntax in mysqlUpdate table_name set column_name=Example • update tbl_stock set int_price=int_price+1.50;

delete

Syntax in mysqlDelete from table_name; Example Delete from tbl_stock;

Difference between delete and truncate??

DCL

Data control language• Grant• Revoke• Commit• rollback

Grant

Syntax in mysqlGRANT privilege_type ON table_name TO

‘user_name'@'localhost';Example GRANT select ON tbl_supplier TO

'john'@'localhost';

Revoke

Syntax in mysqlREVOKE privilege_type ON table_name FROM

‘user_name'@'localhost';Example REVOKE select ON tbl_supplier FROM

'suhail'@'localhost';

functions

• Built in function-perform calculation on data1. Aggregate function2. Scalar function

Aggregate function

• Return a single value for all the values in the column after calculation

AVG()Mysql syntaxSelect avg(coulmn_name) from table_name;ExampleSelect avg(int_salary) from employee;

COUNT(),FIRST(),LAST(),MAX(),MIN(),SUM()

Group by

• To group the result set by one or more column• Often used in conjunction with aggregate

functionSyntax in mysqlSELECT column_name, aggregate_function(column_name)

FROM table_nameGROUP BY column_name;

ExampleSelect vchr_city,sum(mark) from tbl_student group by vchr_city;

Scalar function

• Return single value for each value in a columnUCASE()Syntax in mysqlSelect UCASE (column_name) from table_name;ExampleSelect UCASE(first_name) from tbl_student;

• LCASE(),MID(),LEN(),ROUND(),NOW(),FORMAT()

Joins

• Used to combine rows from two or more tables

Different joins• inner join• Left join• Right join

Inner join

• Returns all rows when there is at least one match in BOTH tables

Syntax in mysqlSelect column_name(s) from table1 join table2 on

table1.column_name=table2.column_name;

Pk_int_dept_id Vchr_dept_name

1 CS

2 EC

3 EE

4 MECH

pk_int_class_id Vchr_class_name int_dept_id1 CS100 1

2 CC300 5

3 EC100 2

4 MECH100 4

Exampleselect vchr_dept_name,vchr_class_name,pk_int_dept_id,int_dept_id from tbl_dept join tbl_classes on tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;

The result set will be

Vchr_department_name

Vchr_class_name Pk_int_dept_id int_dept_id

CS CS100 1 1

EC EC100 2 2

MECH MECH100 4 4

Left join

• returns all rows from the left table (table1), with the matching rows in the right table (table2).

• Syntax in mysqlSELECT column_name(s)

FROM table1LEFT JOIN table2ON table1.column_name=table2.column_name;

Pk_int_dept_id Vchr_dept_name

1 CS

2 EC

3 EE

4 MECH

Pk_int_class_id Vchr_class_name int_dept_id

1 CS100 1

2 EC100 2

3 CC100 5

4 MECH100 4

select vchr_dept_name,vchr_class_name,pk_int_dept_id,int_dept_id from tbl_dept LEFT join tbl_classes on tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;

The result will be

Vchr_dept_name

Vchr_class_name

Pk_int_dept_id

int_dept_id

CS CS100 1 1

EC EC100 2 2

EE NULL 3 NULL

MECH MECH100 4 4

Right join

• Return all rows from the right table, and the matched rows from the left table

Syntax in mysqlSELECT column_name(s)

FROM table1RIGHT JOIN table2ON table1.column_name=table2.column_name;

Pk_int_dept_id Vchr_dept_name

1 CS

2 EC

3 EE

4 MECH

Pk_int_class_id Vchr_class_name int_dept_name

1 CS100 1

2 EC100 2

4 MECH100 4

select vchr_dept_name,vchr_class_name,pk_int_dept_id,int_dept_id from tbl_dept RIGHT join tbl_classes on tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;

The result will be

Vchr_dept_name

Vchr_class_name

Pk_int_dept_id

int_dept_id

CS CS100 1 1

EC EC100 2 2

MECH MECH100 4 4

NULL CC100 NULL 5

Thank you

Want to learn more about programming or Looking to become a good programmer?

Are you wasting time on searching so many contents online?

Do you want to learn things quickly?

Tired of spending huge amount of money to become a Software professional?

Do an online course @ baabtra.com

We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.Email: info@baabtra.com

Contact Us

top related