P. 1
19 Structured Query Language

19 Structured Query Language

|Views: 0|Likes:
Published by Tajwar Qayum

More info:

Published by: Tajwar Qayum on Nov 05, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less





Structured Query Language (SQL

CIS*2450 Advanced Programming Concepts

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 San Jose, California published “A Relational Model of Data for Large Shared Data Banks” (Communications of the ACM, Vol. 13, No. 6, June 1970) in which he described a set of abstract principles for database management - the relational model. The field of relational database technology has its origins in that paper.

The first IBM prototype was called SEQUEL-XRM (1974-75).Structured Query Language • This research described several relational languages which implement some/all of the features of the abstract relational model. 3 . defined by Donald Chamberlin and others at IBM Labs. One of these languages which was created in 1974-75 was the “Structured English Query Language” (SEQUEL).

The Oracle database was released before IBM's own product. • A prototype of this became operational in 1977 called System R. vendors rushed to create their own SQL products.Structured Query Language • A revised version of SEQUEL-XRM was defined in 1976-77 and named SEQUEL/Z. • Due to the success of System R. 4 . The name was changed to SQL for legal reasons.

the X3H2 proposal was ratified by ANSI which consisted essentially of the IBM dialect of SQL. the American National Standards Institute (ANSI) chartered its Database Committee (X3H2) to develop a standard for a relational language. • In 1987. 5 . the ANSI standard was accepted as an international standard by the International Organization for Standards (ISO). In 1986.Structured Query Language • In 1982.

• Enhancements were made over time – SQL/89 .Structured Query Language • The original standard is also known as SQL/86.“SQL3” incorporates object-oriented access – A consortium of vendors known as the SQL Access Group has been working to enhance interoperability across different systems 6 .ISO and ANSI developed a revised standard also known as SQL2 – SQL/99 .included an Integrity Enhancement Feature – SQL/92 .

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 good points.

8 .Good Points • Standards reduce training costs. so applications using them should as well. • They promote application portability. – Standards have a reasonably long life.Are Standards Good? -. • Standards promote application longevity.

9 .Are Standards Good? -. • Customer Choice – If products support the same interface then customers can focus on the implementation that best meets their own needs without having to choose among different interfaces. – Different database management systems can function equally well on a single database if they support the same standard interface.Good Points • Intersystem Communications – are more easily achieved.

Are Standards Good? -. • SQL has some flaws in its design. some of which are considered severe.system implementers may be prevented from providing “the best” solution because the standard prescribes some alternative.Bad Points • Standards can stifle creativity . 10 .

• A relational database is one which is perceived as a collection of tables by the user. manipulate.SQL Overview • SQL is used to define. and control data in relational databases. 11 . • A table is an unordered collection of rows.

A Suppliers-and-Part Database Example • Tables – S : suppliers – P : parts (and where to ship an order) – SP : shipment of parts by suppliers 12 .

Table S SNO SNAME STATUS CITY S1 S2 S3 S4 Smith Jones Blake Clark 20 10 30 20 London Paris Paris London 13 Primary Key = SNO .

Table P PNO PNAME COLOUR WEIGHT CITY P1 P2 P3 P4 Nut Bolt Screw Screw Red Green Blue Red 12 17 17 14 London Paris Rome London 14 Primary Key = PNO .

Table SP SNO PNO QTY S1 S1 S2 S3 S4 P1 P2 P1 P2 P4 300 200 300 200 400 15 Primary Key = SNO and PNO .

– Invocation from a program means the results are made available as input to the program. • SQL statements can be invoked either interactively or from within an application. 16 . – Interactive SQL generally displays the results on the screen. with rows representing records and the columns as fields.Tables • Tables can be thought of as files. • The SQL standard always uses the terms table. row and column.

SNO = 'S4' • Result: CITY ------London 17 .CITY FROM S WHERE S.The Select Statement • SQL Query: SELECT S.

SELECT • SELECT is used for data retrieval.>.=). – You can use all of the standard comparisons (<. • FROM indicates the table from which to retrieve the data. – Literal strings must appear in single quotes.>=.<=. – No WHERE statement will cause all rows to be returned. • WHERE is used to describe column features that are desired in the retrieved data. 18 .

STATUS DECIMAL(3). PRIMARY KEY (SNO) ) 19 .Creating Tables • Empty tables are constructed using the CREATE TABLE statement. SNAME CHAR(20). • Data must be entered later using INSERT. CREATE TABLE S ( SNO CHAR(5). CITY CHAR(15).

Creating Tables • A table name and unique column names must be specified. 20 . • Columns which are defined as primary keys will never have two rows with the same key value. • Primary key may consist of more than one column (values unique in combination) called composite key.

PNO must appear in P.a shipment cannot exist unless the supplier and part also exist. 21 . – Any key in SP. QTY DECIMAL(5).SNO must also appear in S. FOREIGN KEY (PNO) REFERENCES P ) • Foreign keys refer to other tables.PNO). FOREIGN KEY (SNO) REFERENCES S. PNO CHAR(5). PRIMARY KEY (SNO.SNO and any key in SP.PNO .Creating Tables CREATE TABLE SP ( SNO CHAR(5).

change values in existing records DELETE .Data Manipulation • There are four basic SQL data manipulation operations.retrieves data INSERT .add a new row UPDATE . – – – – SELECT .remove row(s) 22 .

INSERT INSERT INTO SP ( SNO. QTY ) VALUES ( 'S4'. 1000 ) • Row added to Table SP. 23 . PNO. 'P1'.

UPDATE UPDATE S SET STATUS = 2 * S.CITY = 'London' • Status doubled for suppliers in London (S1 and S4) 24 .STATUS WHERE S.

WEIGHT > 15 • Rows deleted from P where WEIGHT > 15 (P2 and P3) 25 .DELETE DELETE FROM P WHERE P.

• The result is another (new) table. 26 .SELECT • SELECT has the general form SELECT-FROM-WHERE.

CITY <> 'Paris' COLOUR CITY ---------------Red London Blue Rome [ Red London ] .CITY FROM P WHERE P.SELECT SELECT DISTINCT P.WEIGHT > 10 AND P. P. 27 .COLOUR.eliminated because of DISTINCT statement which removes multiple copies of rows results in the table.

no duplicate rows. 28 .all rows of FROM table are returned. • No WHERE . • SELECT * is short for select the entire row (all columns).SELECT • DISTINCT .

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->