Karlstad University Department of Information Systems Adapted for a textbook by Date C. J. An Introduction to Database Systems Pearson Addison Wesley, 2004 Introduction to SQL Remigijus GUSTAS Phone: +46-54 700 17 65 E-mail: Remigijus.Gustas@kau.se http://www.cs.kau.se/~gustas/ Terminology for Relational Model SQL terms Internal level terms Relation,Relvar 4-2 Objectives of SQL Database language should allow user to: create the database and relation structures; perform insertion, modification, deletion of data from relations; perform queries. Two major components: Data Definition Language (DDL) Data Manipulation Language (DML) SQL is non-procedural - you specify what information you require, rather than how to get it 4-3 1
ISO SQL Data Types Any given type is either system-defined or user-defined CREATE TYPE <type name> AS <representation> FINAL ; CREATE TYPE NAME AS CHAR(15) FINAL ; <representation> is another (system or user-defined) type 4-4 CREATE TABLE specifies the name and structure of a base table CREATE TABLE ( CHAR(2), NAME, INTEGER, PRIMARY KEY ( ) ) ; 4-5 BNF notation Material in lowercase enclosed in angle brackets "<" and ">" represents a syntactic category that appears on the left side of another production rule, and hence must eventually be replaced by specific items chosen by the user. Vertical bars " " are used to separate alternatives. Square brackets "[" and "]" are used to indicate that the material enclosed in those brackets is optional. 4-6 2
Manipulative operations: INSERT INSERT INTO <table name> (<column name> [, < column name>] ) VALUES (<column value> [, < column name>] ) ; INSERT INTO (,, ) VALUES (,, ) ; 4-7 Manipulative operations: DELETE DELETE FROM <table name> WHERE (<condition>) ; DELETE FROM WHERE = ; 4-8 Manipulative operations: UPDATE UPDATE <table name> SET <column name> = <new value> WHERE <condition> ; UPDATE SET = 20000 WHERE = ; 20000 4-9 3
Relational operators in SQL Restrict is implemented by SELECT Subset of rows Uses WHERE clause to narrow result Uses SELECT * to copy entire table Project is also implemented by SELECT Subset of columns Join is also implemented by SELECT Result is a single table matched by values in the join columns of each source table Uses WHERE clause to specify source tables Uses dot operator to disambiguate column references 4-10 Restrict is implemented by SELECT SELECT,, FROM WHERE <> ; SELECT * FROM WHERE <> ; Result # 4-11 Project is implemented by SELECT SELECT, FROM ; Result 4-12 4
Join is implemented by SELECT EMP EMPNO ENAME SALARY 10.000K E1 Lopez 40K 12.000K E2 Cheng 42K 5.000K E3 Finzi 30K E4 Saito 35K SELECT,,, EMPNO, ENAME, SALARY FROM, EMP WHERE. = EMP. ; 10.000K 10.000K 12.000K 12.000K EMPNO E1 E2 E3 E4 ENAME Lopez Cheng Finzi Saito SALARY 40K 42K 30K 35K 4-13 INSERT, DELETE and UPDATE are operations on sets EMP EMPNO ENAME SALARY DELETE FROM EMP WHERE = ; E1 E2 E3 E4 Lopez Cheng Finzi Saito 40K 42K 30K 35K UPDATE SUPPLIER SET STATUS = 2 * STATUS, CITY = Rome WHERE CITY = Paris ; Doubles the status of the Parisian suppliers and moves them to Rome. 4-14 The Catalog in SQL Every database has a catalog A catalog has many schemas, one per user Each catalog includes one Information Schema The Information Schema is a collection of all other schemas as views Represents a hypothetical Definition Schema for all users 4-15 5
The Information Schema - Examples Tables (includes views) Views (does not include tables) Table constraints Referential constraints Column constraints Privileges Note: All these objects are tables, therefore they can be queried 4-16 Views View is a virtual relation that can be derived from base relations upon request CREATE VIEW <viewname> ; CREATE VIEW GOOD_SUPPLIER AS SELECT SNUMBER, STATUS, CITY FROM SUPPLIER WHERE STATUS > 15; You can operate on the view, for instance: SELECT SNUMBER, STATUS FROM GOOD_SUPPLIER WHERE CITY = London ; 4-17 Views Creating a view, and then selecting from the view is equivalent to a SELECT sub-query SELECT GOOD_SUPPLIER.SNUMBER, GOOD_SUPPLIER.STATUS FROM (SELECT SNUMBER, STATUS, CITY FROM SUPPLIER WHERE STATUS > 15 ) AS GOOD_SUPPLIER; WHERE GOOD_SUPPLIER.CITY = London ; This query is what is actually executed SELECT SNUMBER, STATUS FROM SUPPLIER WHERE STATUS > 15 AND CITY = London ; This query is what is actually executed 4-18 6
Embedded SQL SQL statements can be embedded in a host program SQL standard includes support for C, COBOL, PL/1, Java, and many others Initiated in host via EXEC SQL statement Any embedded SQL statement is valid after EXEC SQL SQL operations retrieve many rows (not just one) and host languages are generally not equipped to handle more than one row at a time 4-19 7