Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16

Similar presentations


Presentation on theme: "MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16"— Presentation transcript:

1 MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16 prakash.chatterjee@uwe.ac.uk http://www.cems.uwe.ac.uk/~pchatter/courses/msc/dm prakash.chatterjee@uwe.ac.uk http://www.cems.uwe.ac.uk/~pchatter/courses/msc/dm Lecture 3 : Structured Query Language (SQL)

2 UFIE8K-15-M Data Management 20082 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); 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 own (proprietary) extensions.

3 UFIE8K-15-M Data Management 20083 Components of SQL The SQL language has several parts: Data-definition language (DDL). The SQL DDL provides commands for defining 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 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. Transaction control. SQL includes commands for specifying the beginning and ending of transactions. Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL statements can be embedded within general-purpose programming languages, such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran. 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 rights to relations and views.

4 UFIE8K-15-M Data Management 20084 SQL Example (1) The Supplier-and-Parts Database snosnamestatuscity s1Smith20London s2Jones10Paris s3Blake30Paris s4Clark20London s5Adams30Athens s pnopnamecolourweightcity p1NutRed12.0London p2BoltGreen17.0Paris p3ScrewBlue17.0Oslo p4ScrewRed14.0London p5CamBlue12.0Paris p6CogRed19.0London snopnoqty s1p1300 s1p2200 s1p3400 s1p4200 s1p5100 s1p6100 s2p1300 s2p2400 s3p2200 s4p2200 s4p4300 s4p5400 p sp

5 UFIE8K-15-M Data Management 20085 SQL Example (2) Project the columns sname Smith Jones Blake Clark Adams SELECT sname FROM s computed columns: SELECT sname, status * 5 FROM s snamestatus * 5 Smith100 Jones50 Blake150 Clark100 Adams150 renamed columns: SELECT sname AS Supplier, status * 5 AS 'Status times Five' FROM s SupplierStatus times Five Smith100 Jones50 Blake150 Clark100 Adams150

6 UFIE8K-15-M Data Management 20086 SQL Example (3) Restrict the rows SELECT * FROM s WHERE city=‘London’ snosnamestatuscity s1Smith20London s4Clark20London complex condition: SELECT * FROM s WHERE city=‘London’ OR status = 30 snosnamestatuscity s1Smith20London s3Blake30Paris s4Clark20London s5Adams30Athens

7 UFIE8K-15-M Data Management 20087 SQL Example (4) Restrict & Project city London SELECT city FROM s WHERE sname='smith' OR status='20' remove duplicate rows: SELECT DISTINCT city FROM s WHERE sname='smith' OR status='20' city London

8 UFIE8K-15-M Data Management 20088 SQL Example (4) Use the ‘GROUP BY’ clause to aggregate related rows cityTotal Status Athens30 London40 Paris40 SELECT city, SUM(status) AS 'Total Status' FROM s GROUP BY city Group By and Having Use the ‘HAVING’ clause to restrict rows aggregated with ‘GROUP BY’ cityTotal Status London40 Paris40 SELECT city, SUM(status) AS 'Total Status' FROM s GROUP BY city HAVING SUM(status) > 30

9 UFIE8K-15-M Data Management 20089 SQL Functions SQL provides a wide range of predefined functions to perform manipulation of data. Four types of functions arithmetic (sqrt(), log(), mod(), round() …) date (sysdate(), month(), dayname() …) character (length(), lower(), upper()…) aggregate (min(), max(), avg(), sum() …)

10 UFIE8K-15-M Data Management 200810 Joins (1) The m-f Database idnameage 1tom23 2dick20 3harry30 idnameage 1mary23 2anne30 3sue34 mf

11 UFIE8K-15-M Data Management 200811 Joins (3) Product (or Cartesian Product) idnameageidnameage 1tom231mary23 2dick201mary23 3harry301mary23 1tom232anne30 2dick202anne30 3harry302anne30 1tom233sue34 2dick203sue34 3harry303sue34 SELECT * FROM m, f Synonymous with the CROSS JOIN, hence: SELECT * FROM m CROSS JOIN f; would return the same result. This is not very useful but is the basis for all other joins.

12 UFIE8K-15-M Data Management 200812 Joins (4) Natural join Joins tables using some shared characteristic – usually (but not necessarily) a foreign key. SELECT * FROM m,f WHERE m.age = f.age idnameageidnameage 1tom231mary23 3harry302anne30

13 UFIE8K-15-M Data Management 200813 Joins (5) Inner joins The previous example, besides being a natural join, is also an example of an inner join. An inner join retrieves data only from those rows where the join condition is met. idnameageidnameage 3harry301mary23 SELECT * FROM m,f WHERE m.age > f.age

14 UFIE8K-15-M Data Management 200814 Joins (6) Outer joins Unmatched rows can be included in the output using as outer join. idnameageidnameage 1tom231mary23 2dick20NULL 3harry302anne30 idnameageidnameage 1tom231mary23 3harry302anne30 NULL 3sue34 Right outer join: SELECT * FROM m RIGHT OUTER JOIN f ON m.age = f.age Left outer join: SELECT * FROM m LEFT OUTER JOIN f ON m.age = f.age

15 UFIE8K-15-M Data Management 200815 Joins (7) Self Join Special case of the inner join – here the table employee shows employees and their managers. Ruth manages Joe who manages Tom, Dick and Harry. emp_idemp_namemgr_id 1Tom4 2Dick4 3Harry4 4Joe5 5RuthNULL EmployeeManager TomJoe DickJoe HarryJoe Ruth Show who manages who by name: SELECT E1.emp_name AS Employee, E2.emp_name AS Manager FROM employee AS E1 INNER JOIN employee AS E2 ON E1.mgr_id = E2.emp_id

16 UFIE8K-15-M Data Management 200816 Bibliography / Readings / Home based activities Bibliography - An Introduction to Database Systems (8 th ed.), C J Date, Addison Wesley 2004 An Introduction to Database Systems - Database Management Systems, P Ward & G Defoulas, Thomson 2006 Database Management Systems - Database Systems Concepts (4 th ed.), A Silberschatz, H F Korth & S Sudarshan, McGraw-Hill 2002 Readings - Introduction to SQL’ McGraw-Hill/Osbourne (handout) Home based activities - Ensure you download xampp and install on home PC or laptop (if you have a slow home internet connection – download to data key or CD here at UWE) - Copy the SQL Workbook onto your data key or CD. - Import the tables from the SQL Workbook into your home MySQL DB. Begin working through some of the query examples in the workbook using PHPMyAdmin.


Download ppt "MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16"

Similar presentations


Ads by Google