4.introduction To SQL
4.introduction To SQL
• SQL is an abbreviation for “ Structured Query Language” and pronounced either see_kwell or as
separate letters
• SQL is a Standardized Query Language for requesting information from a database
• The original version called SEQUEL(Structured English Query Language) was designed
• by IBM research center in 1974 and 1975
Insurting Tuple
Update
Getting Values of a Relation
select * from < table name >;
select * from account ;
Select Clause
select Branch_name from loan;
Where Clause
select loan_no from loan where Branch_name=”Hindmotor”, and amount>1200
Logical Connectives Use
and, or & not
Comparision Used
< , <= , > , >= , < >
Renaming
oldname as newname
Find the names of all branches that have assets greater than atleast one branch
located in Howrah
select distinct T.branchname
from branch as T,branch as S
where T.assests>S.assets and S.branchcity='Howrah'
NULL VALUES
Find all loan numbers that appear in the loan relation with null values for amount
select Ioan_no
from loan where amount is null;
• amount field is null means it is void or empty. Don't think it is field up with zero
Find all loan numbers that appear in the loan relation without null values for amount
select Ioan_no from loan
where amount is not null;
• Here zero is also treated as not null
• While testing the values having null/not null dont use the '=' operator
Set Membership
(Nested Subquries)
Find all customer names who have both a loan and an account at the bank. (intersection)
select distinct cname
from borrower
where cname in(select cname from depositor);
Find those customer names who are having only account but no loan on bank. (Set difference/except)
select cname
from depositor
where cname notin(select cname from borrower);
Set Comparision(Some)
Find the names of all branches that have assets greater than those of atleast
one branch located in Kolkata.
select bname
from branch
where assets>some (select assets
from branch
where bcity='Kolkata');
Set Comparision(all)
Find the names of all branches that have assets greater than Each branch located in Kolkata
select bname
from branch
where assets>all (select assets
from branch
where bcity='Kolkata');
• SQL also allows <all ,<= all, >=all, =all and <>all comparisons
• <> all is identical to not in
Views
The form of create view command is
create view v as <query expression>
Create a view consisting of branch name and customer name who have either an account or a loan at that
branch.,
Create view allcustomer as
(select bname, cname
from depositor,account
where depositor.ano=account.ano)
union
(select bname,cname
from borrower,loan
where borrower.lno=loan.lno)
You can't update records in multiple tables when the view references more than
one base table. You can only update columns that belong to a single base table.
Modification of Database
• Deletion
delete from r where p
• Delete all of Rupa's account record
Delete from depositor
where cname =”Rupa”
Trigger In SQL
A trigger is a stored procedure in database which automatically invokes whenever a
special event in the database occurs.
For example, a trigger can be invoked when a row is inserted into a specified table or
when certain table columns are being updated.