structured query language chandra s. amaravadi 1

50
STRUCTURED QUERY LANGUAGE Chandra S. Amaravadi 1

Upload: cornelius-griffith

Post on 02-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

STRUCTURED

QUERY LANGUAGE

Chandra S. Amaravadi

1

IN THIS PRESENTATION

Codd’s rules for relational systems

Types of SQL

DDL and DML

Examples

2

CODD’S RULES

AND

TYPES OF SQL

3

CODD’S RULES FOR RDBMSs

Information representation

Guaranteed access

Dynamic on-line catalog

Comprehensive data sub-language

View updating

Note: Codd was a research fellow at IBM in the ’70s

Codd has written a paper in which he outlined the rules forrelational systems. These are as follows:

4

High-level insert, update and delete

Physical data independence

Logical data independence

Integrity independence

Distribution independence

Non-subversion

CODD’S RULES FOR RDBMSs..

5

CODD’S RULES FOR RDBMSs

Information representation -- all information should be represented as atomic values

in tables.

Guaranteed access -- given a row, column and table name we should be able to

access values in the table.

Online catalog -- the system catalog (data dictionary) should be online

and accessible by the system.

Comp. data lang. -- there should be a language for data definition and data

manipulation.

View updating -- the system must be able to automatically update views

based on a base table.

6

CODD’S RULES FOR RDBMSs

High level insertion, update.. -- insertion, deletion and update should operate on a

table.

Physical data independence -- should be able to change internal storage structures

without affecting application programs.

Logical data independence -- should be able to change logical scheama

without affecting application programs.

Integrity independence -- integrity controls must be independent of appln. prog.

Distribution independence -- the users/appln. programs should not be affected

by where the data is physically stored.

Non subversion -- should not be able to bypass integrity rules by

using the data sub language.

7

based on relational calculus (uses select, project, join etc.)

standardized in ’82,’95, ‘05 Embedded SQL a standard in ‘89 Two major types and three other types

DDL, DMLSQL/T, SQL/I, DCLEmbedded SQL

STRUCTURED QUERY LANGUAGEStructured Query Language originated from SEQEL (early 1980’s). SQL (is/was):

8

There are five types of SQL as follows:

DDL - Creating / modifying tables, views and indexes

DML - Retrieving / inserting / updating information

SQL/T - Transaction boundaries

SQL/I - Integrity constraints

DCL. - To authorize access to the database objects

STRUCTURED QUERY LANGUAGE

9

DATA DEFINITION

LANGUAGE

10

DATA DEFINITION LANGUAGE

Create/Open Database (not discussed) Create Schema Employee; Create/Alter/Drop Table Create/Drop View Create/Drop Index

DDL is the language used to define/modify the databaseSchema.

11

DATA DEFINITION LANGUAGE..

Action DatabaseComponent ComponentName…

TYPICAL FORMAT

Create Drop

TableViewIndex

Cust…..Emp…..…………

12

Alter Table Emp…..

CREATE TABLE Table name (attribute attr. type, attribute attr.type..)[CONSTRAINT Constr name TYPE attr];

ALTER TABLE Table name ADD attr. attr. type, attr. attr. type;

DATA DEFINITION LANGUAGE..

Command used to create tables

Command used to change attributes in tables

Command used to delete table definitions

DROP TABLE Table name ;

13

CREATE TABLE

Dept.

d_no d_name d_mgr_ssn d_phone

CREATE TABLE DEPT. ( d_no Integer, d_name VarChar(15), d_mgr_SSN Char(9), d_phone Char(12));

Creates a table (schema)

14

ALTER, DROP TABLE

Dept.

d_no d_name d_mgr_ssn d_phone no_of_emp

ALTER TABLE DEPT. ADD no_of_emp Integer;

DROP TABLE Dept.;

Alter table adds/drops attributes; Drop table drops the entire table.

15

ALTER TABLE DEPT. DROP no_of_emp;

CREATE VIEW Viewname AS SELECT...

CREATE VIEW

Command used to create VIEWS

A view is the way a user looks at the data.

Views are subsets of the data in the database.

A view could include data from more than one table.

All application programs access data via views.

Views provide logical data independence.

Reports can be created from views (as well as from tables)

16

CREATE VIEW..

Emp.

e_ssn e_name e_title

CREATE VIEW AnalystsAS SELECT e_name, e_title FROM Emp. WHERE e_title = “analyst”;

Create a view of employees who are analysts

Creates a query; AS is an “alias” or name

17

DROP VIEW ANALYSTS;

18

CREATE/DROP INDEX

CREATE INDEX TI_INDEXON EMP(e_title);

DROP INDEX TI_INDEX;

An index is an example of file organization used to facilitate retrieval

DATA MANIPULATION

LANGUAGE

19

DATA MANIPULATION LANGUAGE

INSERTinsert a record

UPDATEchange values

DELETEdelete records

SELECTchoose record

DML is the language used to create/modify and deletedata in the database.

20

THE INSERT COMMAND..

INSERT INTO TABLENAMEVALUES (attr1 value, attr2 value….);

FORMAT

Inserts a record into a table with name TABLENAME.

21

THE INSERT COMMAND..

INSERT INTO EMPVALUES (‘978-98-9878’, ‘Sullivan’, ‘developer’);

Insert employee record

EMP.

e_ssn e_name e_title

e_ssn e_name e_title

978-98-9878 Sullivan developer

22

THE UPDATE COMMAND

EMP.

e_ssn e_name e_title

UPDATE EMPSET e_title = ‘analyst’WHERE e_name = ‘Sullivan’;

Update employee title to ‘analyst’

/* table name *//* new values *//* condition */

e_ssn e_name e_title

978-98-9878 Sullivan analyst

EMP.

23

THE DELETE COMMAND

DELETE FROM EMP.WHERE e_name = ‘Sullivan’;

Delete Employee record

e_ssn e_name e_title

EMP.

e_ssn e_name e_title

978-98-9878 Sullivan analyst

What if you were to issue,DELETE FROM EMP?

EMP.

24

THE SELECT STATEMENT

25

THE SELECT STATEMENT

SELECT <Attribute list>FROM <Relations>[WHERE <Conditions> AND/OR <Conditions>]

[GROUP BY <Attribute list>][HAVING <Conditions>]

[ORDER BY <Attribute list> DESC/ASC];

Group by is used to organize data into groups and provide summary

information. Having is used for the group condition.

26

ADDITIONAL NOTES ON SELECT

The Select part can include literals (“The number of..”)

Some functions can be included in SELECT part or WHERE part

More than one table and one condition can be specified

Conditions are connected by logical operators -- and/or etc.

When GROUP BY is used, the WHERE clause is not used.

Instead the group condition is specified by HAVING.

ORDER BY is optional and used if sorting is required.

Notes on the select statement

27

DISCUSSION

The output of a SELECT statement is:a) an attribute ?b) a single record ?c) table ?

28

FUNCTIONS IN SQL

Logical Arithmetic String Date

IN WHICH PART OF THE QUERY ARE FUNCTIONS USED?

Operators to carry out different types of calculations

29

LOGICAL OPERATORS

“=“, “>“, “<“ “>=“, “<=“ “<>“ OR “!=“ (NOT) BETWEEN X1 AND X2 (inclusive) LIKE“_” or “%” IN(NOT) NULL

FinanceFinancial Records

Logical operators are generally used to carry out comparison

(Select….Where D_Name like “%fin%”)

30

d_name d_phone

Finance 845-9878

Sales 989-0087

Marketing 787-9934

Fin. records 884-5768

Dept.

LOGICAL OPERATORS..

Select cust.cust#From custWhere cust.zip in (61455, 60601….)

Select emp#, emp nameFrom empWhere income between 70000 and 90000;

To select customers in Macomb, Chicago or Bloomington

To select employees between certain income range

31

cust# zip

1156 61455

1157 61455

1158 54555

1160 60601

Cust.

BUILT-IN ARITHMETIC FUNCTIONS

ABS ROUND TRUNC COUNT SUM AVG MAX MIN

Select count(emp_name)… Select max(emp_salary)…..

Arithmetic functions are used to carry out math operations

32

STRING FUNCTIONS

LENGTH(string) SUBSTR(string, start, no of ch.) LOWER UPPER

Select substr(prob_descr, 0, 10)………

String functions are used to carry out string manipulation

Prob_descr = “I am unable to log in…”

33

DATE FUNCTIONS

ADD_MONTHS(1/1/15, 5) = 6/1/15 MONTHS_BETWEEN (sysdate, hiredate) NEXT_DAY(hiredate, ‘Friday’) TO_DATE(string, picture)

TO_DATE(“12/09/14”, ‘DY th MM, YYYY’) = 9 th DEC, 2014

Next_day(2/20/14, ‘Friday’) = 2/21/14

Date functions are used to carry out date arithmetic

34

SIMPLE RETRIEVALS

EMP.

e_ssn e_name e_title

SELECT e_nameFROM EMPWHERE e_title = ‘analyst’;

Select employees who are analysts

35

EMPLOYEEEMPLOYEE

SIMPLE RETRIEVALS..

e_ssn e_name e_title e_salary

456-34-8895 Smith developer $35,000

459-66-6785 Johnson analyst $27,000

467-89-8898 Weintraub developer $60,000

478-64-8005 Dickson manager $64,000

489-12-5575 Ferrel analyst $47,000

492-93-4438 Rao analyst $71,000

467-89-8898 McDonald manager $85,000

36

SIMPLE RETRIEVALS..

SELECT e_nameFROM EMPWHERE e_name like ‘%son’

Select employees whose name ends with ‘son’

EMP.

e_ssn e_name e_title

37

SIMPLE RETRIEVALS..

SELECT e_nameFROM EMPWHERE e_name not like ‘a%’

Select employees whose name does not begin with ‘a’

EMP.

e_ssn e_name e_title

38

RETRIEVALS WITH AGGREGATES..

SELECT COUNT(e_name)FROM EMPWHERE e_title = ‘developer’

Count the number of developers

EMP.

e_ssn e_name e_title

39

SELECT “Average Salary=“, Avg(e_salary) From EMPWhere e_title = ‘developer’ or e_title = ‘analyst’ ;

EMP.

e_ssn e_name e_title e_salary

RETRIEVALS WITH AGGREGATES..

What does this query do?

40

RETRIEVALS WITH EXPRESSIONS..

List employees and their witholdings (calculated as8% of salary).

EMP.

e_ssn e_name e_title e_salary

41

SELECT e_name, e_salary * 0.08 AS WithholdingsFROM EMP

RETRIEVALS WITH GROUP BY..

SELECT e_title, COUNT(e_name)FROM EMPGROUP BY e_title

List all job titles and the number of emps in each

EMP.

e_ssn e_name e_title

42

GROUP BY..

EMPLOYEEEMPLOYEE

43

e_ssn e_name e_title e_salary

456-34-8895 Smith developer $35,000

459-66-6785 Johnson analyst $27,000

467-89-8898 Weintraub developer $60,000

478-64-8005 Dickson manager $64,000

489-12-5575 Ferrel analyst $47,000

492-93-4438 Rao analyst $71,000

467-89-8898 McDonald manager $85,000

RETRIEVALS WITH HAVING..

List the number of employees in each job category with salary > $50,000

SELECT “Number of: “, e_title, “=“, COUNT(e_name)FROM EMPGROUP BY e_title HAVING e_salary > 50000

EMP.

e_ssn e_name e_title e_salary

44

RETRIEVALS WITH HAVING..

EMP.EMP.

45

e_ssn e_name e_title e_salary

456-34-8895 Smith developer $35,000

459-66-6785 Johnson analyst $27,000

467-89-8898 Weintraub developer $60,000

478-64-8005 Dickson manager $64,000

489-12-5575 Ferrel analyst $47,000

492-93-4438 Rao analyst $71,000

467-89-8898 McDonald manager $85,000

RETRIEVAL FROM MULTIPLE TABLES

Sharing of information a key concept Normalization process leads to multiple tables When data is retrieved from > 1 table, need to

link tables together This is done by equating FK values with PK values

for each set of tables that need to be linked

Emp.

emp#, emp name, dept#

Dept.

dept#, dept name, mgr

46

RETRIEVAL FROM MULTIPLE TABLES

Dept.

d_name d_no d_mgr_ssn d_phone

SELECT Emp.e_name, Dept.d_nameFROM EMP, dept.WHERE EMP.e_ssn = dept.d_mgr_ssn

EMP.

e_ssn e_name e_title

47

RETRIEVAL FROM MULTIPLE TABLES..

DEPARTMENT

EMPLOYEE

d_name d_no d_mgr_ssn d_phone

Manugistics 142 467-89-8898 845-9878

IMS 230 479-99-0045 989-0087

Pilot 345 478-64-8005 787-9934

InfoSec 467 898-98-0967 884-5768

48

e_ssn e_name e_title e_salary

456-34-8895 Smith developer $35,000

459-66-6785 Johnson analyst $27,000

467-89-8898 Weintraub developer $60,000

478-64-8005 Dickson manager $64,000

489-12-5575 Ferrel analyst $47,000

492-93-4438 Rao analyst $71,000

467-89-8898 McDonald manager $85,000

DISCUSSION Write SQL queries for the following:

1. Create Emp table with E_SSN, E_Name, E_title as attr. -- assume data types.

2. Add E_salary to the Employee table.

3. Create an index, “Ti_ndx” on E_title.

4. Insert Bob White with SS# 556455555 as an analyst

5. List employees and job titles in order of title.

6. List employees other than developers.

7. Create a view “hi_flier” listing developers with salary > $100K.

8. Count the #. of employees who are managers.

49

50