Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to the Structured Query Language (SQL) UFCE8K-15-M: Data Management.

Similar presentations


Presentation on theme: "Introduction to the Structured Query Language (SQL) UFCE8K-15-M: Data Management."— Presentation transcript:

1 Introduction to the Structured Query Language (SQL) UFCE8K-15-M: Data Management

2 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 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 SQL Example (example db) The Supplier-Parts Database sidsnamestatuscity 1Smith20London 2Jones10Paris 3Blake30Paris 4Clark20London 5Adams30Athens s pid pnamecolour weightcity 1NutRed12.0London 2BoltGreen17.0Paris 3ScrewBlue17.0Oslo 4ScrewRed14.0London 5CamBlue12.0Paris 6CogRed19.0London sidpid qty 11300 12200 13400 14200 15100 16 21300 22400 32200 42 44300 45400 p sp

5 SQL Example (project) Project the columns sname Smith Jones Blake Clark Adams SELECT sname FROM s computed columns: SELECT sname, status * 5 FROM s sname status * 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 SELECT statement (restrict) Restrict the rows SELECT * FROM s WHERE city=‘London’ sidsnamestatuscity s1Smith20London s4Clark20London complex condition: SELECT * FROM s WHERE city=‘London’ OR status = 30 sidsname status city s1Smith20London s3Blake30Paris s4Clark20London s5Adams30Athens

7 SELECT statement (restrict & project)  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 SELECT statement (group by & having) 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 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 summary of the SELECT statement: SELECT columns or computations FROM table WHERE condition GROUP BY columns HAVING condition ORDER BY column [ASC | DESC] LIMIT offset,count; SELECT statement summarized :

10 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 negation LIKE operator allows wildcards _ means single character, % means anything SELECT salary WHERE name LIKE ’Fred %’; SQL Comparison operators :

11 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 dependent FLOAT(p) - floating point number with p binary digits of precision SQL data types :

12 Character CHARACTER(L) - a fixed-length character of length L CHARACTER VARYING(L) or VARCHAR(L) - supports maximum length of L Binary BIT(L), BIT VARYING(L) - like corresponding characters BINARY LARGE OBJECT(L) or BLOB(L) Temporal DATE TIME TIMESTAMP SQL data types (cont.) :

13 SQL Functions : o SQL provides a wide range of predefined functions to perform data manipulation. o Four types of functions: arithmetic (sqrt(), log(), mod(), round() …) date (sysdate(), month(), dayname() …) character (length(), lower(), upper()…) aggregate (min(), max(), avg(), sum() …)

14 Database & Table description commands : Since a single server can support many databases, each containing many tables, with each table having a variety of columns, 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 : SHOW DATABASES; SHOW TABLES IN database; SHOW COLUMNS IN table; DESCRIBE table; - shows the columns and their types

15 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) VALUES(6, Thomas, 40, Cardiff); Insert multiple records: INSERT INTO s (sno, sname, status, city) VALUES(6, Thomas, 40, Cardiff), (7, Hamish, 30, Glasgow); Upload from file: LOAD DATA INFILE ’supplier.tab’ INTO TABLE s FIELDS TERMINATED BY ’\t’;

16 Updating (Editing) Existing Records : To 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 + 10 WHERE city=’London’; Note that the UPDATE command without a WHERE clause will update all the rows of a table.

17 Deleting Records : To delete existing record/s the DELETE FROM command is used. Note 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! DELETE FROM s WHERE city=’London’;

18 Joins (1) o The m-f database idnameage 1tom23 2dick20 3harry30 idnameage 1mary23 2anne30 3sue34 mf Joins are used to re-combine records which have data spread across many tables. The following simple example database with two tables - m, f – is used to illustrate the various kinds of joins.

19 Joins (2) Product (or Cartesian Product) idname age idnameage 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.

20 Joins (3) Natural join Joins tables using some shared characteristic – usually (but not necessarily) a foreign key. SELECT * FROM m,f WHERE m.age = f.age idnameage idnameage 1tom231mary23 3harry302anne30

21 Joins (4) 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

22 Joins (5) Outer joins Unmatched rows can be included in the output using as outer join. id name ageidname age 1tom231mary23 2dick20NULL 3harry302anne30 idname age idname age 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

23 Joins (6) 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

24

25 o A database daemon is a process that looks after a number of DATABASES each of which comprises a number of TABLES each of which comprises data arranges in ROWS and COLUMNS. o You don't access database information directly on the disc from any application program - you access it via your database daemon (a program called mysqld) which is usually running all the time and MUST be running whenever you want to access your data. o You SHOW to look at the structure but you SELECT to look at the content. o You UPDATE the content of table rows but you ALTER the structure of a table. o You choose rows from a result set with a WHERE clause but you choose rows after you've grouped them with a HAVING clause. o You add tables to each other "side by side" with a JOIN and you add results sets above/below each other with a UNION. MySQL & SQL – some key facts (1)

26 o You use a USE command (or other function call) to set the current DATABASE but there is no concept of current TABLE to carry through from one query to the next on a connection. o REPLACE gets rid of existing rows and puts new data in their place, but UPDATE changes existing rows in situe and leaves columns that you don't re-specify unaltered. DELETE gets rid of rows completely, and INSERT puts in completely new rows. o Every row in a table has the same structure (homogeneous) but different columns will have different structures (hetrogeneous). o If you JOIN tables, you tell MySQL how the tables are to be connected to each other (which row(s) in the first table match which row(s) in the second) using an ON clause. o A regular JOIN return only result rows where the ON clause matches up a row from each table exactly, and a LEFT JOIN return the same results and IN ADDITION any records from the left hand table specified, NULL padded to make a complete result record. o You use = to check whether something has an exact value, but you use IS NULL to check for the total absence of any defined value. MySQL & SQL – some key facts (2)


Download ppt "Introduction to the Structured Query Language (SQL) UFCE8K-15-M: Data Management."

Similar presentations


Ads by Google