Upload
nirmal
View
222
Download
0
Embed Size (px)
Citation preview
8/3/2019 19 Structured Query Language
1/28
1
Structured Query Language
(SQL)
CIS*2450Advanced Programming Concepts
8/3/2019 19 Structured Query Language
2/28
2
Structured Query Language
SQL consists of a set of commands for defining,accessing, and managing relational databases.
In 1970, E.F. Codd at IBM Research Labs in SanJose, California published A Relational Model ofData for Large Shared Data Banks(Communications of the ACM, Vol. 13, No. 6,June 1970) in which he described a set of abstractprinciples for database management - therelational model. The field of relational databasetechnology has its origins in that paper.
8/3/2019 19 Structured Query Language
3/28
3
Structured Query Language
This research described several relationallanguages which implement some/all of the
features of the abstract relational model.One of these languages which was createdin 1974-75 was the Structured EnglishQuery Language (SEQUEL), defined by
Donald Chamberlin and others at IBMLabs. The first IBM prototype was calledSEQUEL-XRM (1974-75).
8/3/2019 19 Structured Query Language
4/28
4
Structured Query Language
A revised version of SEQUEL-XRM was defined
in 1976-77 and named SEQUEL/Z. The name
was changed to SQL for legal reasons. A prototype of this became operational in 1977
called System R.
Due to the success of System R, vendors rushed to
create their own SQL products. The Oracle
database was released before IBM's own product.
8/3/2019 19 Structured Query Language
5/28
5
Structured Query Language
In 1982, the American National Standards
Institute (ANSI) chartered its Database Committee
(X3H2) to develop a standard for a relationallanguage. In 1986, the X3H2 proposal was
ratified by ANSI which consisted essentially of
the IBM dialect of SQL.
In 1987, the ANSI standard was accepted as aninternational standard by the International
Organization for Standards (ISO).
8/3/2019 19 Structured Query Language
6/28
6
Structured Query Language
The original standard is also known as SQL/86.
Enhancements were made over time
SQL/89 - included an Integrity Enhancement Feature SQL/92 - ISO and ANSI developed a revised standard
also known as SQL2
SQL/99 - SQL3 incorporates object-oriented access
A consortium of vendors known as the SQL AccessGroup has been working to enhance interoperability
across different systems
8/3/2019 19 Structured Query Language
7/28
7
SQL as a Standard
Since SQL is such a pervasive standard, let
us review some information on standards -
their good points and their not so goodpoints.
8/3/2019 19 Structured Query Language
8/28
8
Are Standards Good? -- Good
Points Standards reduce training costs.
They promote application portability.
Standards promote application longevity.
Standards have a reasonably long life, so
applications using them should as well.
8/3/2019 19 Structured Query Language
9/28
9
Are Standards Good? -- Good
Points Intersystem Communications
are more easily achieved.
Different database management systems can functionequally well on a single database if they support thesame standard interface.
Customer Choice
If products support the same interface then customers
can focus on the implementation that best meets theirown needs without having to choose among differentinterfaces.
8/3/2019 19 Structured Query Language
10/28
10
Are Standards Good? -- Bad
Points Standards can stifle creativity - system
implementers may be prevented from
providing the best solution because thestandard prescribes some alternative.
SQL has some flaws in its design, some of
which are considered severe.
8/3/2019 19 Structured Query Language
11/28
11
SQL Overview
SQL is used to define, manipulate, and
control data in relational databases.
A relational database is one which is
perceived as a collection of tables by the
user.
A table is an unordered collection of rows.
8/3/2019 19 Structured Query Language
12/28
12
A Suppliers-and-Part Database
Example Tables
S : suppliers
P : parts (and where to ship an order)
SP : shipment of parts by suppliers
8/3/2019 19 Structured Query Language
13/28
13
Table S
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris
S4 Clark 20 London
SNO SNAME STATUS CITY
Primary Key = SNO
8/3/2019 19 Structured Query Language
14/28
14
Table P
P1 Nut Red 12 London
P2 Bolt Green 17 Paris
P3 Screw Blue 17 Rome
P4 Screw Red 14 London
PNO PNAME COLOUR WEIGHT CITY
Primary Key = PNO
8/3/2019 19 Structured Query Language
15/28
15
Table SP
S1 P1 300
S1 P2 200
S2 P1 300
S3 P2 200
S4 P4 400
SNO PNO QTY
Primary Key = SNO andPNO
8/3/2019 19 Structured Query Language
16/28
16
Tables
Tables can be thought of as files, with rowsrepresenting records and the columns as fields.
The SQL standard always uses the terms table,row and column.
SQL statements can be invoked eitherinteractively or from within an application.
Interactive SQL generally displays the results on thescreen.
Invocation from a program means the results are madeavailable as input to the program.
8/3/2019 19 Structured Query Language
17/28
17
The Select Statement
SQL Query:
SELECT S.CITY
FROM SWHERE S.SNO = 'S4'
Result:
CITY-------
London
8/3/2019 19 Structured Query Language
18/28
18
SELECT
SELECT is used for data retrieval.
FROM indicates the table from which to retrieve
the data. WHERE is used to describe column features that
are desired in the retrieved data.
No WHERE statement will cause all rows to bereturned.
You can use all of the standard comparisons(=,=).
Literal strings must appear in single quotes.
8/3/2019 19 Structured Query Language
19/28
19
Creating Tables
Empty tables are constructed using theCREATE TABLE statement.
Data must be entered later using INSERT.CREATE TABLE S ( SNO CHAR(5),
SNAME CHAR(20),
STATUS DECIMAL(3),
CITY CHAR(15),
PRIMARY KEY (SNO) )
8/3/2019 19 Structured Query Language
20/28
20
Creating Tables
A table name and unique column namesmust be specified.
Columns which are defined as primary keyswill never have two rows with the same keyvalue.
Primary key may consist of more than onecolumn (values unique in combination)called composite key.
8/3/2019 19 Structured Query Language
21/28
21
Creating Tables
CREATE TABLE SP ( SNO CHAR(5),
PNO CHAR(5),
QTY DECIMAL(5),
PRIMARY KEY (SNO,PNO),
FOREIGN KEY (SNO) REFERENCES S,
FOREIGN KEY (PNO) REFERENCES P )
Foreign keys refer to other tables.
Any key in SP.SNO must also appear in S.SNO andany key in SP.PNO must appear in P.PNO - a shipmentcannot exist unless the supplier and part also exist.
8/3/2019 19 Structured Query Language
22/28
22
Data Manipulation
There are four basic SQL data manipulation
operations.
SELECT - retrieves data
INSERT - add a new row
UPDATE - change values in existing records
DELETE - remove row(s)
8/3/2019 19 Structured Query Language
23/28
23
INSERT
INSERT
INTO SP ( SNO, PNO, QTY )
VALUES ( 'S4', 'P1', 1000 )
Row added to Table SP.
8/3/2019 19 Structured Query Language
24/28
24
UPDATE
UPDATE S
SET STATUS = 2 * S.STATUS
WHERE S.CITY = 'London'
Status doubled for suppliers in London (S1
and S4)
8/3/2019 19 Structured Query Language
25/28
25
DELETE
DELETE
FROM P
WHERE P.WEIGHT > 15
Rows deleted from P where WEIGHT > 15
(P2 and P3)
8/3/2019 19 Structured Query Language
26/28
26
SELECT
SELECT has the general form
SELECT-FROM-WHERE.
The result is another (new) table.
8/3/2019 19 Structured Query Language
27/28
27
SELECT
SELECT DISTINCT
P.COLOUR, P.CITY
FROM P
WHERE P.WEIGHT > 10
AND P.CITY 'Paris'
results in the table,
COLOUR CITY
----------------
Red London
Blue Rome
[ Red London ] - eliminated
because of
DISTINCT
statement whichremoves multiple copies of rows
8/3/2019 19 Structured Query Language
28/28
28
SELECT
DISTINCT - no duplicate rows.
No WHERE - all rows of FROM table are
returned.
SELECT * is short for select the entire row
(all columns).