SQL
SQL
| sid |
sname |
dob
per | course |
fee
|
----------------------------------------------------------------------------| 12 |
72
| MCA
| 40000
|
----------------------------------------------------------------------------| 13 |
70
| BE(EC) | 60000
|
----------------------------------------------------------------------------| 14 |
|
----------------------------------------------------------------------------| 15 |
nidhi
| nidhi@yahoo | 1986-02-02
|
----------------------------------------------------------------------------| 16 |
radha
| radha@hotmail | 1993-04-23
| 74
| BE(IT) | 50000
|
----------------------------------------------------------------------------database terminology->
(1)Database->A database is a collection of data that is organized in a specific
way.
The data is stored in a way that makes it easy to access using quer
ies.
(2)Field->Each table has a set of fields.(no. of columns)
in the student table it has 7 field.it is also known as attributes.
{ sid , sname , email , dob , per , course , fee }
(3)schema->the overall structure of the dabase is called schema.
the student schema is ->{ sid , sname , email , dob , per , course ,
fee }
(4)tuple ->it is known as rows . in the above table 5 tuple is present.
Working with MySQL Server->
-------------------------*in MySQL you need to select DATABASE where the information will be stored.
*DATABSE is name of directory in MySQL.
some useful queries->
-------------------(1)to verify all the databases available ->show databases;
(2)to select required databse -> use <DBNAME>
(3)to create a new database -> create database <DBNAME>
(4)to drop database* -> drop database <DBNAME> (this command execute from root)
(5)to see the list of table under a DB-> show tables;
ex->
insert into students
00,'1986-07-07');
insert into students
23,'1992-07-13');
insert into students
29,'1988-11-28');
insert into students
22,'1991-02-17');
insert into students
13,'1985-08-03');
insert into students
11,'1982-10-15');
city in('blore','hyd','pune');
or
city='blore' or city = 'hyd' or city='pune';
-------------------------------------------------------------------------------------------------------BETWEEN Operator->it is used to match the value in specified range
syntax-> <col_name>between<val1>and<val2>;
ex-> cost between 250 and 450;
or
cost>=250 and cost<=450;
--------------------------------------------------------------------------------------------------------LIKE Operator->(1)to match the value with specified pattern
(2)to define the pattern you can use following character
% - occurance of zero or more character
_ - occurance of only one character
ex->
* from books;
bid,bname,cost from books;
* from books where pub='pearson';
* from books where author like 'sri%';
18000,10000,80
'blore','mkr','fc',
18000,9000,900
Q:
(1)display total fee collection.
(2)display the fee paid by 'mkr' branch.
(3)display the total fee balance of 'btm' branch.
(4)display the branch wise fee collection in the following format.
branch name - total collection
btm
13000
mha
8000
mkr
36000
c.p.
22000
sadar
17000
cant
10000
(5)diplay the city wise fee paid in the following format
city name - total fee paid
blore
23000
pune
10000
jhanshi
17000
delhi
15000
(6)display the city wise(blore and pune)only fee balance in the following format
city name - total balance
blore
- 21000
pune
- 3000
A:select * from student
(1)select sum(totfee) from student;
(2)select sum(feepaid)from student where branch='mkr';
select branch,sum(totfee) from student where branch='mkr';
(3)select sum(feebal) from student where branch='btm';
// not ok
into
into
into
into
models
models
models
models
values(11,'subhag');
values(12,'radhe');
values(13,'krishna');
values(14,'nidhi');
create table orders (id int PRIMARY KEY,modelid int NOT NULL,
description VARCHAR(20),FOREIGN KEY (modelid) REFERENCES models (modelid
));
insert into Orders values(22 ,11 ,'mouse');
insert into Orders values(23 ,12 ,'monitor');
note->error will come
in parent table->
delete from models where modelid = 12; // error
update models set modelid=19 where name='subhag'; // error
update models set modelid=19 where name='nidhi'; // no error
update models set name ='jyoti' where modelid =13;
in child table->
Q:
(1)display cname,email,accno,bal of all customers.
(2)display cname,email,status,city,country of all the customers.
(3)display cname,email,accno,bal of the customers who have saving a/c.
(4)dispaly cname,status,city,country of customers who is staying in delhi and ar
e active.
(5)display accno,atype,bal,city of the customers who have current a/c and bal be
tween
5000 to 10000 and staying in banglore,delhi and pune.
(6)display cname,status,accno,bal,city and country of all customers.
(7)display cname,status,accno,bal,city and country of all customers who are inac
tive
and bal<10000 and not staying in india.
execute queries 1-> select cname,email,acno,bal from customer5 cs,account5 ad where cs.cid=ad.ci
d;
2->select cname,email,status,city,country from customer5 cs, address5 ad where c
s.cid=ad.cid;
3->select cname,email,acno,bal from customer5 cs,account5 ad where cs.cid=ad.cid
and ad.atype='saving';
4->select cname,status,city,country from customer5 cs , address5 ad where cs.cid
=ad.cid and cs.status='active' and ad.city='delhi';
5->select acno,atype,bal,city from account5 a1,address5 a2 where a1.cid=a2.cid a
nd a1.atype='current'and a1.bal between 5000 and 10000 and a2.city in('banglore'
,'delhi','pune');
6->select cname,email,status,acno,bal,city from customer5 c1,account5 a1,address
5 a2 where c1.cid=a1.cid and c1.cid=a2.cid;
7->select cname,status,acno,bal,city,country from customer5 c1,account5 a1,addre
ss5 a2 where c1.cid = a1.cid and c1.cid = a2.cid and c1.status='inactive'and a1.
bal<10000 and city<>'india';
------------------------------------------------------------------------------------------------------------------------table for all join operations-->
create table jlcstud(sid int primary key ,name varchar(12),email varchar(15));
create table jlcfee(fid int primary key, fee float , sid int);
create table jlcadd(aid int primary key , location varchar(15),sid int);
insert
insert
insert
insert
insert
insert
insert
into
into
into
into
into
into
into
jlcstud->
jlcstud values(1,'ram','ram@gmail');
jlcstud values(2,'lakshman','lakshman@gmail');
jlcstud values(3,'bharat','bharat@gmail');
jlcstud values(4,'satrughan','satu@gmail');
jlcstud values(5,'sita','sita@gmail');
jlcstud values(6,'urmila','urmila@gmail');
insert
insert
insert
insert
into
into
into
into
jlcfee
jlcfee
jlcfee
jlcfee
values(123
values(124
values(125
values(126
,12455.11,1);
,15475.13,2);
,54732.29,3);
,52132.22,4);
insert
insert
insert
insert
insert
into
into
into
into
into
jlcadd->
jlcadd values(501,'delhi',3);
jlcadd values(502,'pune',4);
jlcadd values(503,'banglore',5);
jlcadd values(504,'chennai',6);
FID FEE
1
2
3
4
ram@gmail
lakshman@gmail
bharat@gmail
satu@gmail
123
124
125
126
ram
lakshman
bharat
satrughan
SID
12455.11
15475.13
54732.29
52132.22
1
2
3
4
1
2
3
4
ram@gmail
lakshman@gmail
bharat@gmail
satu@gmail
ram
lakshman
bharat
satrughan
FEE
12455.11
15475.13
54732.29
52132.22
FID FEE
bharat
bharat@gmail 125 54732.29 3
satrughan satu@gmail
126 52132.22 4
501
502
delhi
pune
3
4
bharat
bharat@gmail
satrughan satu@gmail
FEE
LOCATION
54732.29
52132.22
delhi
pune
FID
FEE
SID
1
2
3
4
ram
lakshman
bharat
satrughan
ram@gmail
lakshman@gmail
bharat@gmail
satu@gmail
123
124
125
126
12455.11
15475.13
54732.29
52132.22
1
2
3
4
FID
1
2
3
4
ram@gmail
lakshman@gmail
bharat@gmail
satu@gmail
123
124
125
126
ram
lakshman
bharat
satrughan
FEE
12455.11
15475.13
54732.29
52132.22
FID
FEE
1 ram
ram@gmail
123 12455.11
2 lakshman lakshman@gmail 124 15475.13
3 bharat
bharat@gmail 125 54732.29
4 satrughan satu@gmail
126 52132.22
------------------------------------------------------------------------------------------------------------------------left outer join->
---------------it will give "matching record from joined table + record remains in the left sid
e table"
ex->
select * from jlcstud left join jlcfee on jlcstud.sid = jlcfee.sid;
select * from jlcstud left outer join jlcfee on jlcstud.sid = jlcfee.sid;
select * from jlcstud left outer join jlcfee using(sid);
right outer join->
----------------it will give "matching record from joined table + record remains in the right si
de table"
insert into jlcfee values(127 ,53132.22,7);
insert into jlcfee values(128 ,52132.22,8);
ex->
select * from jlcstud right join jlcfee on jlcstud.sid = jlcfee.sid;
select * from jlcstud right outer join jlcfee on jlcstud.sid = jlcfee.sid;
select * from jlcstud right outer join jlcfee using(sid);
full outer join->
----------------it will give "matching records from joined table + record remaing in the left si
d + record remaing in right sid table "
ex->select * from jlcstud left join jlcfee on jlcstud.sid = jlcfee.sid union
select * from jlcstud right join jlcfee on jlcstud.sid = jlcfee.sid;
-------------------------------------------------------------------------------------------------------------------------
(imp)self join->
---------------(1)joining the table to itself is called as self join.
(2)self join is same as any other join but in this join multiple instance of
same table will participate the join query.
create
insert
insert
insert
insert
ename mgrid
A
103
B
103
C
104
D
101
|
|
|
|
|
|
|
|
ename mgrname
A
C
B
C
C
D
D
A
2->select acno,bal from account5 where cid in(select cid from customer5 where st
atus='active');
3-> select bal from account5 where cid in(select cid from address5 where city='b
anglore');
4->select cname,status from customer5 where cid in(select cid from address5 wher
e city in('banglore','pune','delhi'));
5->select cname,email from customer5 where cid in(select cid from account5 where
atype='saving' and bal between 5000 and 10000);
-----------------------------------------------------------------------------------------------------------------------creation of table using sub query->
we can create table using sub query by using 'as' keyword
1-> create table cust1 as select * from customer5;
select * from cust1;
2-> create table cust2 as select cname,email from customer5;
select * from cust2;
3-> create table cust3 as select c.cid ,cname,email,acno,bal from customer5 c,ac
count5 a where c.cid=a.cid;
select * from cust3;
------------------------------------------------------------------------------------------------------------------------auto_increment->
--------------it can be used to generate numeric value in mysql.
ex->
create
insert
insert
insert
select @@autocommit;
set autocommit=0; // off the autocommit
if we want the rollback then we off the "autocommit" option
select * from employee;
1->delete from employee;
rollback // it restore the original result
2->truncate from employee;
rollback // statement will execute but will not store the original result
------------------------------------------------------------------------------------------------------------------------*PL/SQL BLOCK->
--------------with pl/sql you can define the block where multiple sql statements can be submit
ted to database engine at once.
note->mysql doen not support pl/sql.only stored procedure can executed on mysql
syntax:
declare
// varaible or constant declaration
begin
// statements
end;
/
statements:
*declare variables
*declare constants
*conditional statements
*looping control statements
*sql statements
Q:write a pl/sql block to print welcome message
A:
begin
dbms_output.put_line('welcome to stored procedure');
end;
/
o/p->pl/sql procedute successfully completed but it will not show any
note->so if we want the output then we write first
set serveroutput on;
*variable declaration->
---------------------syntax: <var_name><datatype>
ex-> eid int;
ename varchar(10);
*constant declaration->
----------------------syntax: <const_name>constant<datatype>:=<val>;
ex-> max constant int:=225;
*variable initialization->
-------------------------syntax: <var_name>:=<value>;
ex-> eid:=100;
ename:='subhag';
*declaration and initialization->
-------------------------------syntax: <var_name><datatype>:=<value>;
ex-> eid int:=100;
ename varchar(10):='subhag';
->write a pl/sql block to display the sum of two number
(1) declare
a int;
b int;
c int;
begin
a:=10;
b:=20;
c:=a+b;
dbms_output.put_line(c);
end;
/
(2)declare
a int:=&a;
b int:=&b;
c int;
begin
c:=a+b;
dbms_output.put_line('total is '||c);
end;
/
& : is used to take the value from user at run time
|| : is used for concatenation
/ : is used to execute last query
condition statement->
syntax:1
-------if(<cond>)then
s1;
end if
syntax:2
-------if(<cond>)then
s1;
else
s2;
end if;
syntax:3
---------if(<cond>)then
s1;
elseif(<cond>)then
s2;
:
:
else
sN;
end if;
->write a pl/sql block to take two no from the user and display max
declare
a int:=&a;
b int:=&b;
max1 int;
begin
if(a>b)then
max1:=a;
else
max1:=b;
end if;
dbms_output.put_line('maximum is '||max1);
end;
/
*Looping Statements->
-------------------1->simple loop
2->for loop
3->while loop
simple loop->
-----------syntax:
loop
// statement
<incr/decr>;
exit when(a>10);
end loop;
end;
/
forward order
------------declare
a int;
begin
a:=1;
loop
dbms_output.put_line(a);
a:=a+1;
exit when(a>10);
end loop;
end;
/
reverse order
------------declare
a int;
begin
a:=10;
loop
dbms_output.put_line(a);
a:=a-1;
exit when(a<1);
end loop;
end;
/
*for Loop->
---------syntax:
for <var>in[reverse]<start_val>..<end_val>
loop
//statement
end loop;
forward order
------------begin
for a in 1..10
loop
dbms_output.put_line(a);
end loop;
end;
/
reverse order
------------begin
for a in reverse 1..10
loop
dbms_output.put_line(a);
end loop;
end;
/
*while loop->
-----------syntax:
while(<cond>)
loop
// statements
<ince/decr>;
end loop;
forward order
------------declare
a int;
begin
a:=1;
while(a<=10)
loop
dbms_output.put_line(a);
a:=a+1;
end loop;
end;
/
reverse order
------------declare
a int;
begin
a:=10;
while(a>=1)
loop
dbms_output.put_line(a);
a:=a-1;
end loop;
end;
/
->write a pl/sql to take a no. from user and print its table
declare
a int:=&a;
begin
for i in 1..10
loop
dbms_output.put_line(a*i);
end loop;
end;
/
*stored procedure->
-----------------1->stored procedure is a named pl/sql block.
2->it is created and stored in the database.
3->when you submit pl/sql block to the database engine
then it will be compiled every time.
4->when you submit stored procedure to the database engine
then it will be compiled and saved into the databse.
5->when you want to execute the stored procedure then you can call
with its name it will be executed directly without re-compilation.
...............
Request time
...............
:
:------------------------->:
:->compilation
:
:
5ms/query
:
: 5ms/query
:
java
:
: Database :
: Program
:
:
:
:
:
Response time
:
:
:
:<-------------------------:
:<-execution
:.............:
5ms/query
:.............: 5ms/query
*using pl/sql
------------for 1st time execution=5ms+5ms+5ms+5ms = 20ms
for next 100 time executin=100*20 = 2000ms
*using stored procedure
----------------------for 1st time execution=5ms+0ms+5ms+5ms = 15ms
for next 100 time executin=100*15 = 1500ms
note->in this compilation time is saved.
note->syntax is different of stored procedure in oracle as compared to mysql
call p1('A');
output parameter->
----------------(1)it is used to bind the variable to the procedure at runtime.
(2)the value of variable which you are passing can be changed inside
the procedure and you can access that modified value outside the procedure.
(3)to specify output parameter you have to use "out" keyword
ex->
create procedure p2(ch in char , n out int)
as
begin
n:=ascii(ch);
end;
/
to execute->
sql> var n number;
sql> call p2('D',n);
call completed.
sql> print n;
input output parameter->
----------------------(1)it is used to bind the variable with some inetialized value to the
procedure at run time.
(2)the va alue of variable which you are passing can be changed inside the proce
dure and
you can access that modified value outsde the procedure.
(3)to specify input output parameter you have to use "in out" keyword.
ex->
create or replace procedure p3(x in out int)
as
begin
x:=x+10;
end;
/
to execute->
sql> var x number;
sql> exec:x=10;
sql> call p3(:x);
sql> print x;
*using sql in stored procedure->
-------------------------------create table proctable(id int primary key , name varchar(12) , fee float);
inserting data into table->
-------------------------create or replace procedure insertInfo(sid int,snm varchar , sfee float)
as
begin
insert into proctable values(sid , snm, sfee);
end;
/
to invoke->
call insertInfo(11,'subhag',20000);
call insertInfo(12,'radha',25000);
call insertInfo(12,'ram'); // error
deleting data from table->
------------------------create or replace procedure deleteInfo(sid int)
as
begin
delete from proctable where id = sid;
end;
/
to invoke->
call deleteInfo(11);
udating data of table->
---------------------create or replace procedure updateInfo(sid int,snm out varchar,sfee in out float
)
as
begin
update proctable set fee = fee+sfee where id = sid;
select name,fee into snm,sfee from proctable where id=sid;
end;
/
to invoke->
var nm varchar2(10);
var fe number;
exec:fe:=1200;
call updateInfo(11,:nm,:fe);
dropping the procedure->
----------------------drop procedure <proc_name>;
ex-> drop procedure p1;
Trigger->A Trigger is a PL/SQL block that is invoked automatically by DBMS
-------- when insert,delete or update operation is executed on a databse table.
cursor->
------(1)it is a database object that holds the records returned by select satement.
(2)from cursor object you can access the record one by one;
(3)when we want to fetch multiple result from the table then we use "sys_refcurs
or"
at output parameter
ex->
create or replace procedure p6(emp out sys_refcursor)
as
begin
open emp for select * from employee;
end;
/
to execute->
sql>var empdata refcursor
sql>call p6(:empdata);
sql>print empdata;
special query(for mySql)->
------------------------create table account(aid int primary key,name varchar(20),email varchar(20),bal
float);
display record