0% found this document useful (0 votes)
4 views13 pages

4.introduction To SQL

SQL, or Structured Query Language, is a standardized language for managing and querying databases, originally developed by IBM in the 1970s. The document covers various SQL commands including creating tables, inserting and updating data, querying information, handling NULL values, and using triggers. It also explains advanced concepts like subqueries, views, and set comparisons.

Uploaded by

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

4.introduction To SQL

SQL, or Structured Query Language, is a standardized language for managing and querying databases, originally developed by IBM in the 1970s. The document covers various SQL commands including creating tables, inserting and updating data, querying information, handling NULL values, and using triggers. It also explains advanced concepts like subqueries, views, and set comparisons.

Uploaded by

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

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

Schema definition of SQL


Create table < table name >(list of attributes & their types);
create table account

Insurting Tuple

Update
Getting Values of a Relation
select * from < table name >;
select * from account ;

Getting Rid of a Table


drop table < table name >;
drop table account;

Getting Information about Your Databasse


• For recalling the names of all the tables in the current database
select tablename from usertables
• To see all attributes of user tables
select * from usertables
• To recall the attributes of the table after knowing its name
describe <table name>

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'

list cutomer name ,loan number,and amount by increasing the amount by 6%


against loans having loan amount greater than or equal to 100000
select cname,b.lno as loanid,amount *1.06 as new amount
from borrower as b,loan as l
where b.lno=l.lno and l.amt>=1000

String Related Problems


• % matches with any substring
• - matches with any character
Example:
• 'Trans%' matches any string beginning with “tran” like transfer,transformation
• '%know% matches any string containing “know”
like knowledge (all string either begining with know because % can be NULL )
misknow (can be ended with know because % can be also NULL)
acknowledgement(know will be anywhere within the string)

• ‘---' matches any string of exactly three characters


• '---%' matches any string of atleast three characters or more than three
Find the average balance for each customer who live in“Howrah” and has atleast two accounts
select d.cname, avg(bal)
from depositor as d,account as a,customer as c
where d.ano=a.ano
and d.cname=c.cname
and ccity=”Howrah”
having count(disticnt d.ano)>=2;

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');

• The phrase “greater than atleast one” is represented in SQL by >some


• SQL also allows <some ,<= some, >=some,=some and <>some comparisons
• =some is identical to in ,<> some is not the same as not in

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

Test for Empty Relation (Where Exists)


• SQL includes a feature for testing whether a subquery has any tuples in its results
• The exists construct returns the value true if the argument subquery is non empty.
Find all customers who have both an account and a loan at the bank.
select cname
from borrower
where exists(select * from depositor
where borrower.cname=depositor.cname);
• If some tuples will be produced in the inner query then where exists is true then the recpective
cname will be going to the output .
• If no tuples(null)has been produced then where exists will produce false then the recpective cname
will not be going to the output .
• Intersection A∩B≈B∩A So, we can interchange depositor and borrower in this query

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.

Types of Triggers in SQL


Following are the six types of triggers in SQL:
AFTER INSERT Trigger
This trigger is invoked after the insertion of data in the table.
AFTER UPDATE Trigger
This trigger is invoked in SQL after the modification of the data in the table.
AFTER DELETE Trigger
This trigger is invoked after deleting the data from the table.
BEFORE INSERT Trigger
This trigger is invoked before the inserting the record in the table.
BEFORE UPDATE Trigger
This trigger is invoked before the updating the record in the table.
BEFORE DELETE Trigger
This trigger is invoked before deleting the record from the table.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy