Download presentation
Presentation is loading. Please wait.
1
Lecture 3 : Structured Query Language (SQL)
MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16 Lecture 3 : Structured Query Language (SQL)
2
UFIE8K-15-M Data Management 2011
Origins & history Early 1970’s – IBM develops Sequel as part of the System R project at its San Hose Research Lab; 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. UFIE8K-15-M Data Management 2011
3
UFIE8K-15-M Data Management 2011
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. UFIE8K-15-M Data Management 2011
4
UFIE8K-15-M Data Management 2011
SQL Example (1) The Supplier-and-Parts Database sp sno pno qty s1 p1 300 p2 200 p3 400 p4 p5 100 p6 s2 s3 s4 s sno sname status city s1 Smith 20 London s2 Jones 10 Paris s3 Blake 30 s4 Clark s5 Adams Athens p pno pname colour weight city p1 Nut Red 12.0 London p2 Bolt Green 17.0 Paris p3 Screw Blue Oslo p4 14.0 p5 Cam p6 Cog 19.0 UFIE8K-15-M Data Management 2011
5
UFIE8K-15-M Data Management 2011
SQL Example (2) Project the columns renamed columns: SELECT sname FROM s SELECT sname AS Supplier, status * 5 AS 'Status times Five' FROM s sname Smith Jones Blake Clark Adams computed columns: SELECT sname, status * 5 FROM s Supplier Status times Five Smith 100 Jones 50 Blake 150 Clark Adams sname status * 5 Smith 100 Jones 50 Blake 150 Clark Adams UFIE8K-15-M Data Management 2011
6
UFIE8K-15-M Data Management 2011
SQL Example (3) Restrict the rows SELECT * FROM s WHERE city=‘London’ complex condition: SELECT * FROM s WHERE city=‘London’ OR status = 30 sno sname status city s1 Smith 20 London s4 Clark sno sname status city s1 Smith 20 London s3 Blake 30 Paris s4 Clark s5 Adams Athens UFIE8K-15-M Data Management 2011
7
UFIE8K-15-M Data Management 2011
SQL Example (4) Restrict & Project SELECT city FROM s WHERE sname='smith' OR status='20' city London remove duplicate rows: SELECT DISTINCT city FROM s WHERE sname='smith' OR status='20' city London UFIE8K-15-M Data Management 2011
8
UFIE8K-15-M Data Management 2011
SQL Example (4) Group By and Having Use the ‘GROUP BY’ clause to aggregate related rows SELECT city, SUM(status) AS 'Total Status' FROM s GROUP BY city city Total Status Athens 30 London 40 Paris 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 UFIE8K-15-M Data Management 2011
9
UFIE8K-15-M Data Management 2011
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() …) UFIE8K-15-M Data Management 2011
10
UFIE8K-15-M Data Management 2011
Joins (1) The m-f Database m f id name age 1 tom 23 2 dick 20 3 harry 30 id name age 1 mary 23 2 anne 30 3 sue 34 UFIE8K-15-M Data Management 2011
11
UFIE8K-15-M Data Management 2011
Joins (3) Product (or Cartesian Product) SELECT * FROM m, f id name age 1 tom 23 mary 2 dick 20 3 harry 30 anne sue 34 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. UFIE8K-15-M Data Management 2011
12
UFIE8K-15-M Data Management 2011
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 id name age 1 tom 23 mary 3 harry 30 2 anne UFIE8K-15-M Data Management 2011
13
UFIE8K-15-M Data Management 2011
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. SELECT * FROM m,f WHERE m.age > f.age id name age 3 harry 30 1 mary 23 UFIE8K-15-M Data Management 2011
14
UFIE8K-15-M Data Management 2011
Joins (6) 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 1 tom 23 mary 2 dick 20 NULL 3 harry 30 anne Right outer join: SELECT * FROM m RIGHT OUTER JOIN f ON m.age = f.age id name age 1 tom 23 mary 3 harry 30 2 anne NULL sue 34 UFIE8K-15-M Data Management 2011
15
UFIE8K-15-M Data Management 2011
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_id emp_name mgr_id 1 Tom 4 2 Dick 3 Harry Joe 5 Ruth NULL Show who manages who by name: Employee Manager Tom Joe Dick Harry Ruth 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 UFIE8K-15-M Data Management 2011
16
Bibliography / Readings / Home based activities
An Introduction to Database Systems (8th ed.), C J Date, Addison Wesley 2004 Database Management Systems, P Ward & G Defoulas, Thomson 2006 Database Systems Concepts (4th 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. UFIE8K-15-M Data Management 2011
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.