Beruflich Dokumente
Kultur Dokumente
Lecture 3 c
Korra Sathya Babu
Department of Computer Science
NIT Rourkela
11/29/15
Lecture 3
SQL
Structured Query
Language
SQL
Structured Query Language (SQL) is a language
that provides an interface to relational database
systems
SQL was developed by IBM in the 1970s for use
in System R, and is a de facto standard, as well as
an ISO and ANSI standard
SQL encompasses DML (Data Manipulation
Language), for INSERTs, UPDATEs, DELETEs and
DDL (Data Definition Language), used for creating
and modifying tables and other database
structures
The development of SQL is governed by
standards. A major revision to the SQL standard
was completed in 1992, called SQL2. in 1999
11/29/15
Lecture 3
4
called
SQL3 support
object extensions and
are
SQL Standards
1986 SQL86 (SQL87) Adopted by ANSI (86) and
ISO(87)
1989 SQL89 (FIPS 127-1) After minor revision
adopted by Federal Information Processing Systems
1992- SQL92 (SQL2 or FIPS 127-2) Major Revisions
1999- SQL99 (SQL3) - Added regular expression matching, recursive
queries, triggers, support for procedural and control-of-flow statements, nonscalar types, and some object-oriented features
11/29/15
Lecture 3
Oracle
11/29/15
Lecture 3
SQL Queries
With SQL, we can query a database and have a result set returned.
A query like this:
SELECT LastName FROM Persons
Hospital Database
Lecture 3
12
Lecture 3
13
11/29/15
Lecture 3
14
Operators
Arithmetic Operators
equal to (=), less than (<), greater than (>), less than equal to (<=) ,
greater than equal to(>=), not equal to (!=, <>, ^=)
LIKE operator
LIKE Sa%
LIKE _ _ I %
Lecture 3
15
Queries (2)
Display all charges greater than Rs. 5.00/- for patient number 1116
SELECT charge
FROM billed
WHERE patient_no=1116 and charge>5.00;
Find out how many bills have been issued to patient 1117
SELECT count(*)
FROM billed
WHERE patient_no=1117
11/29/15
Lecture 3
16
Queries (3)
Display a calculated charge from table item that is increased by 6%. The
calculated charge column should bear the name as Increased Charge
SELECT normal_charged, normal_charges * 1.06 Increased Charge
FROM Item;
List all the patients who were charged between Rs.160 and Rs.170 for
item 2245
SELECT patient_no, charge
FROM billed
WHERE item_code=2245 AND charge BETWEEN 160 AND 170;
11/29/15
Lecture 3
17
SQL Functions
Character functions
concat, initcap, lower, lpad, rpad, substr, upper, ltrim, instr, length
Numeric functions
mod, power, round, sign, sqrt, trunc
Date functions
add_months, last_day, months_between, next_day, round, trunc
Miscellaneous functions
nvl, decode
11/29/15
Lecture 3
18
Joins
11/29/15
Lecture 3
19
Joins
An Inner Join selects only those rows from both the joining
tables
Outer Join selects all the rows that satisfy the join condition
and those rows from one table for which no rows from the
other table satisfy the join condition
The (+) outer join operator is used for which there will be
NULL values in terms of matching
example.
Display the patient_no, name and charge for all the patients
irrespective of the fact whether the patients has not yet
incurred any charges
SELECT p.patient_no, pat_name, b.charge
FROM billed b, patient p
WHERE p.patient_no = b.patient_no (+) ;
11/29/15
Lecture 3
20
Example
Sn
o
Sname
22
29
Sid
bid
day
bid
bname
color
22
101
10/10/08
101
Interlake
Blue
22
102
10/10/08
102
Interlake
Red
33.
0
22
103
10/08/08
103
Clipper
Green
22
104
10/07/08
104
Marine
red
Ratin
g
Age
Dustin
e
45.
0
Brutus
31
Lubbe
r
55.
5
31
102
10/10/08
32
Andy
25.
5
31
103
11/06/08
31
104
11/12/08
58
Rusty
10
35.
0
64
101
09/05/08
64
102
09/08/08
74
103
09/08/08
64
Horati
o
71 Instance
Zorba of 10
Sailors
74
Horati
o
85 11/29/15
Art
35.
0
16.
0
35.
0
25.
5
Instance of Boats
Instance of Reserves
Lecture 3
21
AND, OR
Find the names of sailors who have reserved a green boat or
red boat
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid
AND (B.color = red OR B.color=green) ;
Find the names of sailors who have reserved a green boat and
red boat
Can we simply replace OR with AND ?
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid
AND (B.color = red AND B.color=green) ;
11/29/15
Lecture 3
22
AND
The above query returns names of sailors but the integrity
constraint says that a bid cannot have 2 values. So the query
will always return an empty set. The following query can be
used with AND operator.
Find the names of sailors who have reserved a green boat and
red boat
SELECT S.sname
FROM Sailors S, Reserves R1, Boats B1, Reserves R2, Boats B2
WHERE S.sid = R1.sid AND R1.bid = B1.bid
AND S.sid = R2.sid AND R2.bid = B2.bid
AND B1.color = red AND B2.color=green;
Lecture 3
23
UNION, INTERSECT
Find the names of sailors who have reserved a green boat or
red boat
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = red
UNION
SELECT S2.sname
FROM Sailors S2, Reserves R2, Boats B2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = green) ;
Find the names of sailors who have reserved a green boat and
red boat
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = red
INTERSECT
SELECT S2.sname
FROM Sailors S2, Reserves R2, Boats B2
WHERE S2.sid = R2.sid Lecture
AND R2.bid
= B2.bid AND B2.color
11/29/15
3
24= green) ;
EXCEPT (MINUS)
Find the sids of sailors who have reserved a red boat but not green
boat
SELECT R.sid
FROM Reserves R, Boats B
WHERE R.bid = B.bid AND B.color = red
EXCEPT
SELECT R2.sid
FROM Reserves R2, Boats B2
WHERE R2.bid = B2.bid AND B2.color = green) ;
The result of all the above queries is derived first and then the
operator UNION, INTERSECT, EXCEPT is applied
All of the above used operators eliminate duplicate by default
To include duplicate tuples use UNION ALL, INTERSET ALL,
EXCEPT ALL
11/29/15
Lecture 3
25
Nested Queries
At first the nested sub query computes the set of sids who
have reserved boat 103 (ex. The set contains 22, 31 and 74)
The top level query retrieves the names of sailors whose sid is
in this set
The IN operator allows to test whether a value is in a given set
of elements
To find the names of sailors who have not reserved the boat
103 just put NOT before IN operator
11/29/15
Lecture 3
26
11/29/15
Lecture 3
27
The sub query clearly depends on the current row S and must
be re-evaluated for each row in Sailors
The occurrence of S in the sub query (S.sid) is called a
correlation
11/29/15
Lecture 3
28
The ANY and ALL are also set comparison operators along with EXIST,
IN
SOME is also a set comparison operator but its just a synonym of ANY
Find the sids of sailors whose rating is better than some sailor called
Horatio
SELECT S.sid
FROM Sailors S
WHERE S.rating > ANY ( SELECT S2.rating
FROM Sailors S2
WHERE S2.name = Horatio) ;
Lecture 3
29
Find the sids of sailors whose rating is better than every sailor called
Horatio
SELECT S.sid
FROM Sailors S
WHERE S.rating > ALL ( SELECT S2.rating
FROM Sailors S2
WHERE S2.name = Horatio) ;
Lecture 3
30
More Queries
Find the name of the sailor who has the kth highest rating
SELECT S.sname
FROM Sailors S
WHERE k = ( SELECT count (distinct S2.rating)
FROM Sailors S2
WHERE S.rating <= S2.rating) ;
Find the name of the sailor who has the first three high rating
SELECT S.sname
FROM Sailors S
WHERE 3 >= ( SELECT count (distinct S2.rating)
FROM Sailors S2
WHERE S.rating <= S2.rating) ;
Lecture 3
31
Views
Views are logical tables of data extracted from existing
(generally multiple) tables. It can be queried just like a table but
doesnt require space
Advantages
Security: Can be used to hide sensitive columns
Convenience: Can be used to hide complex queries involving multiple
relations or queries that are used commonly by many users
Check options can be provided to UPDATE and INSERT of views
Syntax
CREATE or REPLACE VIEW <Viewname> AS <query> [WITH READ ONLY]
[WITH CHECK OPTION];
ex. CREATE VIEW EMP_VIEW AS SELECT emp_no, emp_name from emp;
ex. CREATE OR REPLACE VIEW STUDENT AS SELECT * FROM STUDENTDB
WHERE INSITITUTE = NITR WITH CHECK OPTION ;
To drop a view
DROP VIEW <Viewname>;
11/29/15
Lecture 3
32
Sequences
A special database object that generates integers according to
specified rules. Generally used for generating primary key
values automatically
Syntax
CREATE SEQUENCE <Sequence_name>
[INCREMENT BY <integervalue>
START WITH <integervalue>
MAXVALUE <integervalue>/ NOMAXVALUE
MINVALUE <integervalue>/ NOMINVALUE
CYCLE/NOCYCLE]
example
CREATE SEQUENCE countdown100
START WITH 100
INCREMENT BY -1
NOMAXVALUE
CYCLE
ORDER;
11/29/15
Lecture 3
33
Sequences
Sequences are referenced using CURRVAL and NEXTVAL virtual
columns
Once the NEXTVAL column is referenced, the value in CURRVAL
becomes the value in NEXTVAL, and prior value in CURRVAL is
lost
example usage
INSERT INTO expense (expense_no, empid, amt, submit_date)
VALUES(expense_sequence_01.nextval, 2353, 25000, 08-NOV-08);
11/29/15
Lecture 3
34
Syntax
CREATE TABLE <table_name>
(<column 1> <data_type> <size> [Integrity constraint] [REFERENCES
<tablename> [ON DELETE CASCADE]],
example
CREATE TABLE employee (
ecode number(4) Primary Key,
ename varchar2(18),
grade char(1),
);
11/29/15
Lecture 3
35
Integrity Constraints
Unique Constraint
No two rows have the same value
Default Constraint
A default value can be specified for the column
Check Constraint
Limits values that can be inserted into a column
11/29/15
Lecture 3
36
DESCRIBE <Tablename>
DELETE FROM <Tablename> WHERE <Predicate>
UPDATE <Tablename> SET <column> = <value>
Alter Table <Tablename> <ADD>/ <MODIFY> (<column>
<type> <width>)
CREATE USER <username> identified by <password>
GRANT CONNECT, RESOURCE, DBA TO <username>
11/29/15
Lecture 3
37