24
Lecture 11 UFCEKG202 : Data, Schemas and Applications Lecture 11 Database Theory & Practice (5) : Introduction to the Structured Query Language (SQL)

UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Lecture 11UFCEKG‐20‐2 : Data, Schemas and Applications

Lecture 11Database Theory & Practice (5) : Introduction to the 

Structured Query Language (SQL)

Page 2: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Origins & history• Early 1970’s – IBM develops Sequel as part of the System 

R project at its San Hose Research Lab;• 1986 ‐ ANSI & ISO publish the standard SQL‐86;• 1987 – IBM publishes its own “standard” SQL called 

Systems Architecture Database Interface (SAA SQL);Systems Architecture Database Interface (SAA‐SQL);• 1989 – SQL‐89 published by ANSI (extended version of 

SQL‐86););• 1992 – SQL‐92 published with better support for algebraic 

operations; • 1999 – SQL‐1999 published with support for typing, stored 

procedures, triggers, BLOBs etc.

SQL‐92 remains the most widely implemented standard – and most database vendors also provide their ownand most database vendors also provide their own 

(proprietary) extensions.

Page 3: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Components of SQLpThe SQL language has several parts:• Data‐definition language (DDL). The SQL DDL provides commands for 

defining relation schemas deleting relations and modifying relationdefining relation schemas, deleting relations, and modifying relation schemas.

• Interactive data‐manipulation language (DML). The SQL DML includes a query language based on both the relational algebra andincludes a query language based on both the relational algebra and the tuple relational calculus. It includes also commands to insert tuples into, delete tuples from, and modify tuples in the database.

• View definition. The SQL DDL includes commands for defining views.Q g• Transaction control. SQL includes commands for specifying the 

beginning and ending of transactions.• Embedded SQL and dynamic SQL. Embedded and dynamic SQL define Q y Q y Q

how SQL statements can be embedded within general‐purpose programming languages, such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran.I i h SQL L i l d d f if i i i• Integrity. The SQL DDL includes commands for specifying integrity constraints that the data stored in the database must satisfy. Updates that violate integrity constraints are disallowed.

• Authorization The SQL DDL includes commands for specifying access• Authorization. The SQL DDL includes commands for specifying access rights to relations and views.

Page 4: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SQL Example (example db)SQL Example (example db)

• The Supplier‐Parts Database

sno sname status city

1 Smith 20 Londons sno pno qty

1 1 300

1 2 200

sp

2 Jones 10 Paris

3 Blake 30 Paris

4 Clark 20 London

1 2 200

1 3 400

1 4 200

1 5 1005 Adams 30 Athens

pno pname color weight city

1 5 100

1 6 100

2 1 300

2 2 400p1 Nut Red 12.0 London

2 Bolt Green 17.0 Paris

3 Screw Blue 17.0 Oslo

2 2 400

3 2 200

4 2 200

4 4 3004 Screw Red 14.0 London

5 Cam Blue 12.0 Paris

6 Cog Red 19.0 London

4 5 400

Page 5: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SQL E l ( j t)SQL Example (project)

• Project the columnsSELECT sname FROM s

renamed columns:

SELECT sname AS Suppliersname

Smith

J

computed columns:

SELECT sname, status * 5 FROM s

SELECT sname AS Supplier, status * 5 AS 'Status times Five' FROM s

Supplier Status times FiveJones

Blake

Clark

Adams

sname status * 5

Smith 100

Supplier Status times Five

Smith 100

Jones 50

Blake 150Adams Smith 100

Jones 50

Blake 150

Blake 150

Clark 100

Adams 150

Clark 100

Adams 150

Page 6: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

S C ( i )SELECT statement (restrict)

• Restrict the rows• Restrict the rowsSELECT * FROM s WHERE city=‘London’

complex condition:y

sno sname status city

SELECT * FROM s WHERE city=‘London’ OR status = 30

sno sname status citys1 Smith 20 London

s4 Clark 20 London

sno sname status city

s1 Smith 20 London

s3 Blake 30 Paris

s4 Clark 20 Londons4 Clark 20 London

s5 Adams 30 Athens

Page 7: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SELECT statement (restrict & project)

• Restrict & ProjectSELECT city FROM s WHERE sname='smith' OR status='20'

city

London

London

remove duplicate rows:

SELECT DISTINCT cit FROM s WHERE sname 'smith' OR stat s '20'SELECT DISTINCT city FROM s WHERE sname='smith' OR status='20'

city

Londono do

Page 8: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SELECT statement (group by & having)

• Use the ‘GROUP BY’ clause to aggregate related rows• Group By and Having

city Total Status

Athens 30

SELECT city, SUM(status) AS 'Total Status' FROM s GROUP BY city

Athens 30

London 40

Paris 40

• Use the ‘HAVING’ clause to restrict rows aggregated with ‘GROUP BY’SELECT city, SUM(status) AS 'Total Status' FROM s GROUP BY city HAVING SUM(status) > 30

city Total Status

London 40

Paris 40Paris 40

Page 9: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SELECT statement s mmari ed

For many of the modern uses of databases, it is often

SELECT statement summarized :

For many of the modern uses of databases, it is often necessary to select some subset of the records from a table, and let some other program manipulate the results. In SQL the SELECT statement is the workhorse for these operations.

A s mmar of the SELECT statementA summary of the SELECT statement:

SELECT columns or computationsFROM tableFROM tableWHERE conditionGROUP BY columnsHAVING conditionHAVING conditionORDER BY column [ASC | DESC]LIMIT offset,count;

Page 10: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SQL Comparison operators

I SQL th WHERE l i d t t b t f

SQL Comparison operators :

In SQL, the WHERE clause is used to operate on subsets of a table. The following comparison operators are available:

•Usual logical operators: < > <= >= = <>•BETWEEN used to test for a range•IN used to test group membership•Keyword NOT used for negationLIKE t ll ild d•LIKE operator allows wildcards

• _means single character, % means anything• SELECT salary WHERE name LIKE ’Fred %’;SELECT salary WHERE name LIKE Fred % ;

Page 11: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SQL data t pes

SQL t l b f d t t & f t

SQL data types :

SQL supports a very large number of data types & formats for internal storage of data.

Numeric• INTEGER, SMALLINT, BIGINT• NUMERIC(w,d), DECIMAL(w,d) ‐ numbers with 

width w and d decimal places• REAL DOUBLE PRECISION machine and database• REAL, DOUBLE PRECISION ‐machine and database

dependent• FLOAT(p) ‐ floating point number with p binaryFLOAT(p) floating point number with p binary

digits of precision

Page 12: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SQL data types (cont ) :

Character

SQL data types (cont.) :

• CHARACTER(L) - a fixed‐length character of length L• CHARACTER VARYING(L) or VARCHAR(L) - supports 

i l h fmaximum length of L

BinaryBinary• BIT(L), BIT VARYING(L) - like corresponding characters• BINARY LARGE OBJECT(L) or BLOB(L)

Temporal• DATE• DATE• TIME• TIMESTAMP

Page 13: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

SQL iSQL Functions :

• SQL provides a wide range of predefined functions to perform data manipulation.

• Four types of functions:arithmetic (sqrt() log() mod() round() )arithmetic (sqrt(), log(), mod(), round() …)

date (sysdate(), month(), dayname() …)character (length() lower() upper() )character (length(), lower(), upper()…)aggregate (min(), max(), avg(), sum() …)

Page 14: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

b & bl d dDatabase & Table description commands :

Si i l t d t b hSince a single server can support many databases, eachcontaining many tables, with each table having a variety of columns, it’s often necessary to view which databases arecolumns, it s often necessary to view which databases are available and what the table structures are within a particular database.

The following SQL commands are often used for these purposes :purposes :

• SHOW DATABASES;• SHOW TABLES IN database;• SHOW COLUMNS IN table;• DESCRIBE table; ‐ shows the columns and their typesDESCRIBE table; shows the columns and their types

Page 15: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Inserting Records :Individual records can be entered using the INSERT command:INSERT INTO s VALUES(6, Thomas, 40, Cardiff);

Using the column names:INSERT INTO s (sno, sname, status, city)INSERT INTO s (sno, sname, status, city)VALUES(6, Thomas, 40, Cardiff);

Insert multiple records:INSERT INTO s (sno, sname, status, city)VALUES(6, Thomas, 40, Cardiff),VALUES(6, Thomas, 40, Cardiff),

(7, Hamish, 30, Glasgow);

Upload from file:LOAD DATA INFILE ’supplier.tab’INTO TABLE sINTO TABLE sFIELDS TERMINATED BY ’\t’;

Page 16: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Updating (Editing) Existing Records :Updating (Editing) Existing Records :

To change one or more values of columns of a table, theTo change one or more values of columns of a table, the UPDATE command can be used. 

Edits are provided as a comma‐separated list of column/value pairs.

UPDATE s SET status=status + 10WHERE city=’London’;WHERE city= London ;

Note that the UPDATE command without a WHERE clause will update all the rows of a table.

Page 17: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Deleting Records :Deleting Records :

To delete existing record/s the DELETE FROMTo delete existing record/s the DELETE FROM command is used. 

DELETE FROM s WHERE city=’London’;

Note the WHERE clause in the DELETE syntax TheNote the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If the WHERE clause is omitted, all ,records will be deleted!

Page 18: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Normalization (avoiding redundancy) :

Repeating data (the same column values across many records) wastes spaceacross many records) wastes space (redundancy) and introduces insert & update anomalies. To avoid this, tables are often normalized and repeating fields are moved to their own tables. These are then related to the base orThese are then related to the base or parent table using foreign keys. 

F i i h Q lFor instance in the Quote example –author and category are moved to their own tables since a specific category can p g yhave many associated quotes and an author can be the source of many quotesquotes. 

Page 19: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Joins (1)( )Joins are used to re‐combine records which have data spread across many tables The following simple examplespread across many tables. The following simple example database with two tables ‐m, f – is used to illustrate the various kinds of joins.

• The m‐f database

j

id name age id name age

m f

1 tom 23

2 dick 20

3 harry 30

1 mary 23

2 anne 30

3 sue 34

Page 20: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Joins (2)• Product (or Cartesian Product)

id name age id name age

1 tom 23 1 mary 23

SELECT * FROM m, f

y

2 dick 20 1 mary 23

3 harry 30 1 mary 23

1 tom 23 2 anne 30

2 dick 20 2 anne 30

3 harry 30 2 anne 30

1 tom 23 3 sue 34

2 dick 20 3 sue 34

3 harry 30 3 sue 34

S ith th CROSS JOIN hSynonymous with the CROSS JOIN, hence: SELECT * FROM m CROSS JOIN f; would return the same result This is not very useful but is thewould return the same result. This is not very useful but is the basis for all other joins.

Page 21: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Joins (3)

N l j i• Natural joinJoins tables using some shared characteristic – usually (but not necessarily) a foreign keynot necessarily) a foreign key.

SELECT * FROM m,f WHERE m.age = f.age

id name age id name age1 tom 23 1 mary 233 harry 30 2 anne 30

Page 22: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Joins (4)

• Inner joinsThe previous example, besides being a natural join, is also an example of an inner join An inner join retrievesalso an example of an inner join. An inner join retrieves data only from those rows where the join condition is met.

SELECT * FROM m,f WHERE m.age > f.age

id name age id name age 3 harry 30 1 mary 23

Page 23: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Joins (5)• Outer joins

Unmatched rows can be included in the output using as outer join. Left outer join: SELECT * FROM m LEFT OUTER JOIN f ON m age = f age

id name age id name age1 tom 23 1 mary 23

SELECT * FROM m LEFT OUTER JOIN f ON m.age = f.age

1 tom 23 1 mary 232 dick 20 NULL NULL NULL3 harry 30 2 anne 30

Right outer join:SELECT * FROM m RIGHT OUTER JOIN f ON m.age = f.age

id name age id name age1 tom 23 1 mary 233 h 30 2 303 harry 30 2 anne 30

NULL NULL NULL 3 sue 34

Page 24: UFCEKG 20 2 Data, Schemas Applicationsnisansa/Classes/02...Special case of the inner join –here the table employee shows employees and th itheir managers. RthRuth manages Joe who

Joins (6)• Self Join

Special case of the inner join – here the table employee h l d th i R th Jshows employees and their managers. Ruth manages Joe who manages Tom, Dick and Harry.

emp_id emp_name mgr_id

1 Tom 4

2 Dick 4

Employee ManagerTom Joe

2 Dick 4

3 Harry 4

4 Joe 5

Dick JoeHarry JoeJoe Ruth

5 Ruth NULLJoe Ruth

Show who manages who by name:SELECT E1 AS E l E2SELECT E1.emp_name AS Employee, E2.emp_name AS ManagerFROM employee AS E1INNER JOIN employee AS E2 ON E1 mgr id =INNER JOIN employee AS E2 ON E1.mgr_id = E2.emp_id