Você está na página 1de 36

Structured Query Language

(SQL)
Background
Query Language is a language in which a user requests information from
the db.
These languages are on a level higher than that of standard programming
language.
2 types:
a) Procedural Language
b) Non- Procedural Language
Procedural Language: In this language, the user instructs the system to
perform a sequence of operations on the db to compute the desired
results.
Non- Procedural Language: In this language, the user describes the desired
information without giving a specific procedure for obtaining that
information.
Most commercial DBMS offers a query language that includes elements of
both the procedural and non-procedural approaches. The most widely used
query language is SQL.
Contd
IBM developed the original version of SQL, called as SEQUEL (Structured
English Query Language) as part of System R project in the early 1970s at
the IBM San Jose Research Laboratory.
Renamed as Structured Query Language (SQL).
In 1986, SQL was declared as the Standard Relational Database Language
by the ANSI (American National Standards Institute) & ISO.
ANSI (American National Standards Institute) and ISO (International
Standards Organization) standard SQL are :
SQL-86
SQL-87- IBMs own Corporate SQL Standard
SQL-89
SQL-92
SQL:1999 (language name became Y2K compliant!)
SQL:2003
SQL:2005
SQL Data Types
The SQL standard supports a variety of built-in domain types which
includes:

char(n). Fixed length character string, with user-specified length n.
varchar(n). Variable length character strings, with user-specified
maximum length n.
int. Integer (a finite subset of the integers that is machine-dependent).
smallint. Small integer (a machine-dependent subset of the integer
domain type).
numeric(p,d). Fixed point number, with user-specified precision of p
digits, with n digits to the right of decimal point.
real, double precision. Floating point and double-precision floating point
numbers, with machine-dependent precision.
float(n). Floating point number, with user-specified precision of at least n
digits.
SQL Data Definition Language (DDL)
DDL provides commands for defining relation
schemas, deleting relations, and modifying relation
schemas.
These commands are used to create, alter and drop
tables/relations.
Following are DDL commands:
- CREATE Table
- ALTER Table
- DROP Table
Create Table Construct
An SQL relation is defined using the create table command:
create table r (A
1
D
1
, A
2
D
2
, ..., A
n
D
n
,
(integrity-constraint
1
),
...,
(integrity-constraint
k
));
r is the name of the relation
each A
i
is an attribute name in the schema of relation r
D
i
is the data type of values in the domain of attribute A
i

Example:
create table branch
(branch_name char(15) not null,
branch_city char(30),
assets integer);

Integrity Constraints in Create Table
not null
primary key (A
1
, ..., A
n
)

Example: Declare branch_name as the primary key
for branch
create table branch
(branch_name char(15),
branch_city char(30),
assets integer,
primary key (branch_name));

Alter Table Construct
The alter table command is used to add attributes to an
existing relation:
alter table r add A D;
where A is the name of the attribute to be added to relation r
and D is the data type of A.
All tuples in the relation are assigned null as the value for the new
attribute.
The alter table command can also be used to drop attributes
of a relation:
alter table r drop column A;
where A is the name of an attribute of relation r.
Dropping of attributes are not supported by many databases.
Alter Database Construct
The alter database command can be used to change the data
type and size of an existing attribute/column:
alter table r modify A new_data_ type (new_size);
where A is the name of the existing attribute in relation r.
Drop Table Construct
The drop table command deletes all information
about the relation from the database.
Drop table r;

SQL Data Manipulation Language (DML)
It provides commands for inserting, deleting and
modifying data or tuples in the database.
DML commands are:
INSERT
DELETE
UPDATE

INSERT Command
Used to add new tuple/row to a relation.
The syntax for INSERT command is given as;
INSERT INTO (Table_name) [(Attribute_name)]
VALUES (&Attribute_name 1, &Attribute_name 2, .,
&Attribute_name n);
In the above syntax, Attribute_name with table name
is optional.
Contd
Examples:
Insert into CUSTOMERS values
(XXX, 20-feb-2003, mumbai,
YYY, 21-mar-2004, delhi);
- Insert into ITEMS (Item_no, Desc) values
(30, clips,35, staplers);
DELETE Command
Is used to delete or remove tuples or rows from a
relation.
The syntax for DELETE command is given as;
DELETE FROM (Table_name) WHERE Predicate(s));
Examples:
Delete from ITEMS where Item_no=25;
Delete from ITEMS;
Delete from STORE where Item_no IN (select
Item_no from ITEMS where weight=4);
UPDATE Command
Is used to modify attribute values of one or more
selected tuples or records by a predicate in WHERE
clause and the new values for the attributes can be
mentioned by SET clause.
The syntax for UPDATE command is given as;
UPDATE (Table_name)
SET (target_value_list)
WHERE (predicate);
Contd
Examples:
Update CUSTOMERS
set DOB= 20-feb-2003,
CUST_CITY= Mumbai
where CUST_NAME= XXX;

- Update STORE
set QTY_HELD= 700
where ITEM_NO in (select ITEM_NO from
ITEMS where WEIGHT=>6);
SQL Data Query Language (DQL)
Commonly used SQL statements that enable the
users to put their queries to retrieve information
from one or more relations.
A typical SQL query has the form:

select A
1
, A
2
, ..., A
n

from r
1
, r
2
, ..., r
m

where P;
A
i
represents an attribute
R
i
represents a relation
P is a predicate/condition
Contd
The select clause list the attributes desired in the
result of a query.
Example: Find the names of all branches in the loan
relation:
select branch_name
from loan;

Contd
SQL allows duplicates in relations as well as in query results.
To force the elimination of duplicates, insert the keyword
distinct after select clause.
Example: Find the names of all branches in the loan relation,
and remove duplicates
select distinct branch_name
from loan;
The keyword all specifies that duplicates are not removed
from the resultant relation.
select all branch_name
from loan;

Contd
An asterisk (*) in the select clause denotes all attributes
select *
from loan;
The select clause can contain arithmetic expressions involving
the operations +, , , and /, and are operating on constants
or attributes of tuples.
The query:
select loan_number, branch_name, amount 100
from loan;
would return a relation that is same as the loan relation,
except that the value of the attribute amount is multiplied by
100.
Contd
The where clause specifies conditions that the result must
satisfy.
Example: To find out all loan number for loans made at the
Perryridge branch with loan amounts greater than $1200.
select loan_number
from loan
where branch_name = 'Perryridge' and amount > 1200;
Comparison results can be combined using the logical
connectives and, or, and not.
Comparisons can be applied to the results of arithmetic
expressions.
Contd
SQL includes between as a comparison
operator.
Example: Find the loan number of those loans
with loan amounts between $90,000 and
$100,000 (that is, >= $90,000 and <= $100,000)
select loan_number
from loan
where amount between 90000 and 100000;

Contd
The from clause lists the relations involved in the query.
Example: Find all the tuples from borrower and loan relations
select
from borrower, loan;
Example: Find the name, loan number and loan amount of all
customers having a loan at the Perryridge branch.
select customer_name, borrower.loan_number, amount
from borrower, loan
where borrower.loan_number = loan.loan_number and
branch_name = 'Perryridge' ;


Rename Operation
The SQL allows renaming relations and attributes using
the as clause:
old-name as new-name
Ex: Find the name, loan number and loan amount of all
customers also rename the attribute name loan_number
as loan_id.

select customer_name, borrower.loan_number as
loan_id, amount
from borrower, loan
where borrower.loan_number = loan.loan_number;


String Operations
SQL includes a string-matching operator for comparisons on character strings. The
operator like uses patterns that are described using two special characters:
percent (%) - The % character matches any substring.
underscore (_) - The _ character matches any character.
Patterns are case sensitive, i.e. uppercase characters do not match with
lowercase characters or vice versa.
Find the names of all customers whose street includes the substring Main.
select customer_name
from customer
where customer_street like '%main%' ;
%main% matches any string containing main as a substring.
Main% matches any string beginning with Main.
_ _ _ matches any string of exactly three characters.
_ _ _ % matches any string of at least three characters.

SQL supports a variety of string operations such as
concatenation (using ||)
converting from upper to lower case (and vice versa)
finding string length, extracting substrings, etc.
Ordering the Display of Tuples
Example: List in alphabetic order the names of all
customers having a loan at Perryridge branch.
select distinct customer_name
from borrower, loan
where borrower.loan_number =
loan.loan_number and
branch_name = 'Perryridge'
order by customer_name;
We may specify desc for descending order or asc for
ascending order, for each attribute; ascending order
is the default.
Example: order by customer_name desc
Set Operations
The set operations union, intersect, and except operate on
relations
The relations participating in the set operations must be
Compatible i.e., they must have the same set of attributes.
Each of the above operations automatically eliminate
duplicates.
To retain all duplicates in the relations use union all, intersect
all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it
occurs:
m

+ n times in r union all s
min(m,n) times in r intersect all s
max(0, m n) times in r except all s

Contd
Find all customers who have a loan, an account, or both:
(select customer_name from depositor)
union
(select customer_name from borrower);
This query returns a customer name who has several accounts or loans or
both at the bank. But the customer name will appear only once in the
resultant relation.
If we want to retain all duplicates , then we must write the query as,
(select customer_name from depositor)
union all
(select customer_name from borrower);
The no. of duplicates in the resultant relation= total no. of duplicates
appeared in both relations i.e. depositor and borrower.

Contd
Find all customers who have both a loan and an account.
(select customer_name from depositor)
intersect
(select customer_name from borrower);
This query returns a customer name who has several records of
both account and loan in the bank. But the customer name only
appear once in the resultant relation.

If we want to retain all duplicates , then we must write the
query as,
(select customer_name from depositor)
intersect all
(select customer_name from borrower);
The no. of duplicates in the resultant relation= minimum no. of
duplicates appeared in both relations i.e. depositor and
borrower.

Contd
Find all customers who have an account but no loan.
(select customer_name from depositor)
except
(select customer_name from borrower);

If we want to retain all duplicates , then we must write the
query as,
(select customer_name from depositor)
except all
(select customer_name from borrower);
The no. of duplicates in the resultant relation= ( The no. of
duplicate copies in depositor relation) ( The no. of duplicate
copies in borrower relation).
** But the difference always positive.
Aggregate Functions
Aggregate functions are the functions that take a collection
of values as input and return a single value.
SQL offers 5 built-in functions:
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
The input to sum and avg must be a collection of numbers but
the other functions can operate on collections of non-numeric
data types such as strings.
Contd
Find the average account balance at the Perryridge
branch.
select avg (balance)
from account
where branch_name = Perryridge;
Find the number of tuples in the customer relation.
select count (*)
from customer;
Find the number of depositors in the bank.
select count (distinct customer_name)
from depositor;
Aggregate Functions
by using Group By clause
Find the number of depositors for each branch.
select branch_name, count (distinct customer_name)
from depositor, account
where depositor.account_number =
account.account_number group by branch_name;
Note: Attributes in select clause outside of
aggregate functions must appear in group by
list.

Aggregate Functions
by using Having clause
It is also useful to state a condition that applies to groups
rather than to tuples.
SQL applies predicates in the having clause after the
formation of groups.
Example: Find the names of all branches where the average
account balance is more than $1,200.
select branch_name, avg (balance)
from account
group by branch_name
having avg (balance) > 1200;
Note: Predicates in the having clause are applied after the
formation of groups whereas predicates in the where
clause are applied before forming groups.
Null Values
It is possible for tuples to have null values, denoted by null, to
some of their attributes.
null signifies an unknown value or a value that does not exist.
The predicate is null can be used to check for null values.
Example: Find all loan numbers which appear in the loan
relation with null values for amount.
select loan_number
from loan
where amount is null;
The result of any arithmetic expression involving null is null
Example: 5 + null returns null
All aggregate functions except count(*), ignore null values
in their input collection.

Tuple Variables
Tuple variables are defined in the from clause via the use of the
as clause.
In SQL, the tuple variables are associated with a relation.
Ex: Find the customer names, their loan numbers and loan
amount for all customers having a loan at some branch.
select customer_name, T.loan_number, S.amount
from borrower as T, loan as S
where T.loan_number = S.loan_number;

Ex: Find the names of all branches that have greater assets than
some branch located in Brooklyn.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = 'Brooklyn;
Keyword as is optional and may be omitted
i.e. borrower as T borrower T

Você também pode gostar