Anda di halaman 1dari 26

And

Presented by: Ashish Agarwal Roll No : 0609610096 CS(7th Sem) V.I.E.T. Ghaziabad

Introduction to SQL
What is SQL?
When a user wants to get some information from a database file, he can issue a query. A query is a userrequest to retrieve data or information with a certain condition. SQL is a query language that allows user to specify the conditions. (instead of algorithms)

Introduction to SQL
Concept of SQL

The user specifies a certain condition. The program will go through all the records in the database file and select those records that satisfy the condition.(searching). Statistical information of the data. The result of the query will then be stored in form of a table.

General Structure

SELECT ...... FROM ...... WHERE ......


SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHERE condition

I
eg. 1 Result
id name 9801 Peter 9802 Mary 9803 Johnny 9804 Wendy 9805 Tobe : :

General Structure

List all the student records.


SELECT * FROM student
dob 06/04/86 01/10/86 03/16/86 07/09/86 10/17/86 : sex M F M F M : class 1A 1A 1A 1B 1B : mtest 70 92 91 84 88 : hcode R Y G B R : dcode SSP HHM SSP YMT YMT : remission .F. .F. .T. .F. .F. :

I
eg. 5 Result

General Structure
List the names, id of 1A students with no fee remission.
SELECT name, id, class FROM student ; WHERE class="1A" AND NOT remission
name Peter Mary Luke Bobby Aaron Ron Gigi : id 9801 9802 9810 9811 9812 9813 9824 : class 1A 1A 1A 1A 1A 1A 1A :

II

Comparison
expr IN ( value1, value2, value3) expr BETWEEN value1 AND value2 expr LIKE "%_"

II
eg. 7 Result

Comparison
List the students who were not born in January, March, June, September.
SELECT name, class, dob FROM student ; WHERE MONTH(dob) NOT IN (1,3,6,9)
name Wendy Tobe Eric Patty Kevin Bobby Aaron : class 1B 1B 1C 1C 1C 1A 1A : dob 07/09/86 10/17/86 05/05/87 08/13/87 11/21/87 02/16/86 08/02/86 :

II
eg. 9 Result

Comparison
List the students whose names start with "T".
SELECT name, class FROM student ; WHERE name LIKE "T%"
name Tobe Teddy Tim class 1B 1B 2A

III

Grouping
SELECT ...... FROM ...... WHERE condition ; GROUP BY groupexpr [HAVING requirement] Group functions: COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
groupexpr specifies the related rows to be grouped as one entry. Usually it is a column.

WHERE condition specifies the condition of individual rows before the rows are group. HAVING requirement specifies the condition involving the whole group.

III
eg. 11

Grouping
List the number of students of each class.
SELECT class, COUNT(*) FROM student GROUP BY class ;
class 1A 1B 1C 2A 2B 2C cnt 10 9 9 8 8 6

Result

III
eg. 12
class 1A 1B 1C 2A 2B 2C

Grouping
List the average Math test score of each class.
SELECT class, AVG(mtest) FROM student GROUP BY class ;

Result

avg_mtest 85.90 70.33 37.89 89.38 53.13 32.67

IV

Display Order
SELECT ...... FROM ...... WHERE ...... GROUP BY ..... ; ORDER BY colname ASC / DESC

IV
eg. 16
name Peter Johnny Luke Bobby Aaron Ron

Display Order
List the boys of class 1A, order by their names.

SELECT name, id FROM student WHERE sex="M" AND class="1A" ORDER BY name ;
id 9801 9803 9810 9811 9812 9813
Result

ORDER BY dcode

name Aaron Bobby Johnny Luke Peter Ron

id 9812 9811 9803 9810 9801 9813

Natural Join

A Natural Join is a join operation that joins two tables by their common column. This operation is similar to the setting relation of two tables.

SELECT a.comcol, a.col1, b.col2, expr1, expr2 ; FROM table1 a, table2 b ; WHERE a.comcol = b.comcol

4
eg. 25
id

Natural Join
Make a list of students and the instruments they learn. (Natural Join)
name class id type

9801
Student id

Same id

9801

Join
name class

Music type

9801
Product

4
eg. 25 Result

Natural Join
Make a list of students and the instruments they learn. (Natural Join)
SELECT s.class, s.name, s.id, m.type FROM student s, music m WHERE s.id=m.id ORDER BY class, name ;
class 1A 1A 1A 1A 1A 1A 1A : name Aaron Bobby Gigi Jill Johnny Luke Mary : id 9812 9811 9824 9820 9803 9810 9802 : type Piano Flute Recorder Piano Violin Piano Flute :

4
eg. 26

Natural Join
Find the number of students learning piano in each class.
SELECT s.class, COUNT(*) FROM student s, music m WHERE s.id=m.id AND m.type="Piano GROUP BY class ORDER BY class ;

Result

class 1A 1B 1C

cnt 4 2 1

Programming in Oracle with PL/SQL


Procedural Language Extension to SQL

PL/SQL Blocks
PL/SQL code is built of Blocks, with a unique structure. There are two types of blocks in PL/SQL:
1. Anonymous Blocks: have no name (like scripts)
can be written and executed immediately in SQLPLUS can be used in a trigger

2. Named Blocks:
Procedures Functions

Anonymous Block Structure:


DECLARE BEGIN (optional) (mandatory)
/* Here you declare the variables you will use in this block */
/* Here you define the executable statements (what the block DOES!)*/

EXCEPTION (optional)
/* Here you define the actions that take place if an exception is thrown during the run of this block */

END; /

(mandatory)
A correct completion of a block will generate the following message: PL/SQL procedure successfully

Always put a new line with only a / at the end of a block! (This tells Oracle to run the block)

DECLARE
Syntax
identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];

Examples
Declare birthday age name magic valid

Notice that PL/SQL includes all SQL types, and more

DATE; NUMBER(2) NOT NULL := 27; VARCHAR2(13) := 'Levi'; CONSTANT NUMBER := 77; BOOLEAN NOT NULL := TRUE;

Declaring Variables with the %TYPE Attribute


Examples
DECLARE sname fav_boat my_fav_boat 'Pinta'; ...
Accessing column sname in table Sailors

Sailors.sname%TYPE; VARCHAR2(30); fav_boat%TYPE :=


Accessing another variable

Creating a Cursor
We create a Cursor when we want to go over a result of a query (like ResultSet in JDBC) Syntax Example: DECLARE cursor c is select * from sailors; sailorData c%ROWTYPE; BEGIN open c; fetch c into sailorData;
sailorData is a record that can hold a ROW from the sailors table

Here the first row of sailors is inserted into sailorData

Example
RAD_VAL S

radius

Rad_cursor

3 6 8

DECLARE Pi constant NUMBER(8,7) := 3.1415926; area NUMBER(14,2); cursor rad_cursor is select * from RAD_VALS; rad_value rad_cursor%ROWTYPE ; BEGIN open rad_cursor; fetch rad_cursor into rad_val; area:=pi*power(rad_val.radius,2); insert into AREAS values (rad_val.radius, area); close rad_cursor; END; /

f e t c h
Rad_val

AREAS Radius Area 3 28.27

Anda mungkin juga menyukai