introduction to database language sql · an introduction to sql - part 1 1.1. introduction. 1.1.1....

103
INTRODUCTION TO DATABASE LANGUAGE SQL © Edinburgh University Computing Service University Library George Square EDINBURGH EH8 9LJ June 1998

Upload: others

Post on 27-May-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

INTRODUCTIONTO

DATABASE LANGUAGESQL

© Edinburgh University Computing ServiceUniversity LibraryGeorge SquareEDINBURGH EH8 9LJ

June 1998

Page 2: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

AnIntroduction

toSQL.

Part 1.

Simple Data Retrieval.

Page 3: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.1. Introduction.

1.1.1. Standards

SQL1 is the language for defining and maintaining relational databases. It is important because it is alanguage that is the subject of a standard; that is there is an approved definition to whichimplementors of SQL products are supposed to adhere.

Standards are important in information processing because they:

* protect investment

* preserve skills

* promote inter-operability

This last bullet is of particular importance as computer networks become more widespread. Byutilising standard languages and exploiting computer networks, applications running on one machineare able to access and maintain data based on other, geographically distributed machines.

SQL is actually the subject of a number of standards: an ISO (international) standard and a number ofnational standards (such as a BS standard here in the UK, an ANSI standard in the US, an AFNORstandard in France and a JIS standard in Japan etc.). Luckily al l these separate standards have beendeveloped from a common source and are therefore functionally equivalent and it is sufficient to talkgenerically about t h e SQL standard2. The Standard does however specify three possible levels ofconformance: Entry, Intermediate, and Full. Most commercial products conform to Entry Level andsupport a range of facilities from Intermediate level SQL.

The SQL standard specified what is called a data sub language; that is it is a language formanipulating and maintaining data; SQL (at least the current standard) does not have any controlstructure mechanism. Hence, when SQL is used, it is either used

i. directly (through some form of interface machanism); the user just enters the SQL statementan SQL statement through some interface mechanism provided by the database managementsystem and the DBMS executes it and returns the results, or

ii . the SQL statements are embedded within some host language program. Typically suchprograms may be written in conventional programming languages such as Cobol or Fortran, orin one of the so-call "4th-generation languages" which enable programmers to produceapplications rather quicker. In an Ingres environment, the 4th generation language is ABF,supported by other tools such as QBF, RBF and the Report Writer

In this course we shall be using SQL through an interactive interface, typing the SQL statements asrequired. We shall be using an Ingres3 database management system4. The interface we are using iscalled ISQL.

Most vendors supply in their products functionality additional to that specified in the standard. Such extensions will be indicated where relevent in these notes. Likewise, where Ingres capabilitiesdeviate from the Standard, that will generally be noted.

1SQL - This originally was an acronym for Structured Query Language but as the language is neitherstructured, nor exclusively a query language, SQL now stands for itself.

2 The current SQL standard is ISO/IEC 9075:1992; frequently referred to as SQL-92.

3 Ingres is used for the course as the version of SQL provided by Ingres conforms closely to SQL-92Entry Level.

4 Because of the importance of MS Access in our environment, frequent reference in made in footnotes

Page 4: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.1.2. Relational tables and Names

The relational model of data, on which SQL is loosely based, represents data as a collectionof tables. Each table has a number of columns and a number of rows:

Person_Name Age

Jill 24

David 28

Ann 19

William 42

Robert 34

The above table has two columns, named Person_Name and Age. There are 5 rows of data. Note that each row consists of a number of values - and there is the same number of values ineach row of a table.

The above example had only two columns but in practice, tables in an SQL database can havea large number of columns (up to some limit determined by the implementation) and in mostimplementations, several million rows.

Also, each table has a table name. The name of this table might meaningfully be Person. Table names and column names are used in SQL queries. Entry level of the SQL-92 allows suchnames to be up to 18 characters in length, starting with an alphabetic character andcontaining only alphabetic character, numeric characters, and the break character "_" . However, some implementations of SQL impose other limits or restrictions5. SQL also has anumber of reserved words. A table name or a column name may not be the same as an SQLreserved word.

Each column also has a data type . The data type restricts the type of data that may bestored in a column. In this table, the column Beer has a data type of Character; that is it canstore character strings. The column Price has a data type of Numeric; that it can storenumbers. The data type of a column in SQL is determined when the table is defined.

1.1.3. Relating tables

Rows in one table may be related to rows in other (or indeed the same!) table by having thesame value or values) in a column (or columns). Consider the following table of Universities:

Person_Name University

Jill London

David Edinburgh

Ann Glasgow

William Edinburgh

5 Full level SQL-92 allows names up to 128 characters. Ingres allows names up to 30 characters; MS

Page 5: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

Robert Oxford

Here, the values in the column Person_Name of the Universities table match values in thecolumn Person_Name in the Person table. In fact, the column Person_Name of theUniversities table is sometimes referred to as a Foreign k ey to the table Person but thatterminology need not concern us at present.

The table Person can now be Joined to the table Universities where the values in thePerson_Name column of the Person table are equal to the Person_Name column in theUniversities table. The resultant table would look as follows:

Person_Name Age University

Jill 24 London

David 28 Edinburgh

Anne 19 Glasgow

William 42 Edinburgh

Robert 34 Oxford

Such a table may be useful for answering questions such as "What is the average age of peoplethat go to Edinburgh University".

1.1.4. Databases.

The term database tends to mean different things to different suppliers6. A good workingdefinition used in SQL is :-

A database consists of one or more database objects and their definitions.

The name of a particular database is implementation dependant and it is likely that theordinary user will be automatically connected to the required database without necessarilyneeding to know its name. However, for this course we are using the "native ISQL" interfaceand that requires the database name.

Because one part of this course involved modifying data, each participant will have theirown Ingres database. The following instructions create an ingres database for you andpopulate it.

Page 6: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.1.5. Creating your database and connecting to ISQL

Connecting to the database will depend upon the database system being used and the generalenvironment. At Edinburgh, we run Ingres on a variety of machines. This course will use a Sunmachine holyrood. Connection to holyrood will be via a network called Edlan.

The instructions to connect to holyrood depend upon the terminal emulator that you are using.Instructions are given for two different terminal emulators: eXceed and for LanWorkPlace. Note that the training unit uses eXceed.

The instructions to connect to holyrood via eXceed are:

l If using a Training unit machine:select option 1 - Windows Courses from the initial menu

l Open the icon eXceedw either by double clicling on it or by selecting it andchoosing Open from the File menu.

l Open the icon Telnet in the eXceedw window either by double clicling on it orby selecting it and choosing Open from the File menu.

l In the host box of the Telnet Connect to Host window, type:holyrood.ed.ac.uk

l You are now connected to holyrood. Now log on to holyrood as indicatedbelow

The instructions to connect to holyrood using LAN WorkPlace under Windows are:

l Open a DOS window (Double click, left button on the MS-DOS icon)

l Start a terminal emulator and call holyroodType DOS command

TNVT220 holyrood.ed.ac.uk

l Now log on to holyrood as indicated below

You are now connected to holyrood. Now you must log on and enter your password:

l Logon to holyrood with the username erey0n (n is a digit 1 through 9 which you willbe assigned at the start of the course) and the password given out for thecourse.

You should now get a Unix "holyrood$" prompt shown. You are now connected ontoholyrood.

Exercise 1.1 - Connecting holyrood and logging on.

If this has not already been done, connect to holyrood and log on with your assigned usernumber and password.

Page 7: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.1.6. Creating your test database

In order that each course participant has their own database and can amend it, your first taskis to create a database and populate it.

The Ingres command createdb creates a database. Databases in an Ingres environment must beuniquely named. For the database name for this course, use the name sqltxx, where the xx arethe two digits of your log-on id (for example, if you log on as erey07, use the database namesqlt07).

The Ingres command createdb defines a large number of system tables, referred to as a catalog . This process is quite a time-consuming task, especially if a whole class tries to do it at thesame time. Therefore, for this course, the databases have already been set up but are empty.

1.1.7. Populating your test database

The databases for the course will be populated using a batch load facility. All SQL productshave some capability of this sort, although they vary and are not standardised in any way. The Ingres environment uses an extension to the SQL language, the copy command. Thiscommand reads data from a file in a format specified by the copy command and loads it intodatabase tables. In the next exercise, the copy commands themselves are contained in a file. That file will be read into the Ingres Interactive SQL program and then executed. Theexecuted copy statements will read data from other previously defined files and load thedata into the tables of your database.

Exercise 1.2 - Populating the test database.

1) Invoke the interactive SQL on your database

l Type isql sqltxx (xx is your log-in id)

2) Populate your database with the previously defined script:

l Select File These options are at the foot of the ingres screenSelect ReadFile

l Type the file name:sql_db/load_sqlt

Note that this file uses SQL to create the tables in your database and then topopulate those tables from data files

l Select Go This options are at the foot of the ingres screen

Delete the results from the screen

l Select Blank(preferably) or End

Blank is preferable as accidentally pressing the End keymore than once (and it is a repeat key!) will take youright out of isql.

Page 8: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.2. The Demonstration Database.

This is a rather crude personnel system consisting of only three tables :-

l The EMP table - a table of details about employees.l The DEPT table - a table containing details of the departments in the company.l The SALGRADE table - a table of employee grades and the salary ranges associated

with those grades.

Exercise 1.3 - To Display the Structure of the Tables Used on this Course.

N.B. - This command is not standard SQL. Ingres use help; Oracle use describe; Both are extensions to the SQL language. Microsoft’s Access displays the table definition usinga grid. Most vendors of SQL databases include in their products various extensions. Ifyou really want to produce portable programs, you should avoid using such extensions.

Some database products(including Ingres) include a Flagger facility which will tellyou if you are using a facility which is not included in the SQL standard.

1) Displaying the structure of the EMP (Employee) table.

l Type help EMP;7

l The following appears :-

1> help EMP

Name: empOwner: ingresCreated: 24-feb-1997 12:56:00 Type: user tableVersion: OPING1.2

Column Information: KeyColumn Name Type Length Nulls Defaults Seqempno integer 2 no no 1ename char 10 yes no job char 9 yes no mgr integer 2 yes no sal integer 2 yes no comm integer 2 yes no deptno integer 2 yes no hiredate date yes no

End of Request

7 Semicolon (;) is a statement separator character in Ingres. If you are executing more than a singlestatement, the the statements must be separated by a semicolon. The semicolon is optional after the

Page 9: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

l Points to note :-

i) The EMPNO field is an employee number. Since it is being used as akey field it is defined as NOT NULL.8

i i ) The ENAME field is the employee's surname. Ten characters havebeen allowed.

i i i ) The JOB field (9 characters long) is self-evident.iv) The MGR field is the EMPNO of the employee's manager.v) The SAL field is the employee's salary.vi) The COMM field only applies to the JOB of SALESMAN, etc.vii) The DEPTNO is a department number. This is a Foreign Key relating

to the DEPTNO table shown below.viii) The HIREDATE field is self-evident and is a DATE field. Date

fields are not standard SQL but most SQL products have them.

l Also worth noting is:

i) The command help could have been typed or help table. The notation used inthe manual to show something that is optional is to enclose it in brackets "["and "]". E.g: help [table].9

i i ) In Ingres, names and commands are not case sensitive. That is help is thesame as HELP or Help. This is not true when referring to data. That is indata, the character string John is a different character string to JOHN.

2) Displaying the structure of the DEPT (Department) table.

l Press Blank to clear the screen of the previous display

l Type help DEPT

l The following appears :-

1> Help dept

Name: deptOwner: ingresCreated: 24-feb-1997 12:51:00 Type: user tableVersion: OPING1.2

Column Information: KeyColumn Name Type Length Nulls Defaults Seqdeptno integer 2 no no 1dname char 14 yes no loc char 13 yes no

End of Request

8 Null Values are considered in more detail in Part 3.

9 The symbols [ and ] are used in the meta-language for SQL and so appear in thedocumentation for many products. MS Access confusingly uses several characters in a non-standardway:

Function MS Access SQL StandardDelimit non standard identifier [ and ] “ and “Delimit character string literal “ ‘Introduce comment ‘ --

Page 10: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

3) Displaying the structure of the SALGRADE (Salary & Grade) table.

l Clear the screen

l Type help SALGRADE.

l The following appears :-

1> Help salgrade

Name: salgradeOwner: ingresCreated: 24-feb-1997 12:52:00 Type: user tableVersion: OPING1.2

Column Information: KeyColumn Name Type Length Nulls Defaults Seqgrade integer 2 yes no losal integer 4 yes no hisal integer 4 yes no

End of Request

Page 11: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.3. The Contents of the Demonstration Database.

The contents of the EMP table is:

empno ename job mgr sal comm deptno hiredate

7369 SMITH CLERK 7902 800 20 17-dec-80

7499 ALLEN SALESMAN 7698 1600 300 30 20-feb-81

7521 WARD SALESMAN 7698 1250 500 30 22-feb-81

7566 JONES MANAGER 7839 2975 20 02-apr-81

7654 MARTIN SALESMAN 7698 1250 1400 30 28-sep-81

7698 BLAKE MANAGER 7839 2850 30 01-may-81

7782 CLARK MANAGER 7839 2450 10 09-jun-81

7788 SCOTT ANALYST 7566 3000 20 27-jun-90

7839 KING PRESIDENT 5000 10 17-nov-81

7844 TURNER SALESMAN 7698 1500 010 30 08-sep-81

7876 ADAMS CLERK 7788 1100 20 31-jul-90

7900 JAMES CLERK 7698 950 30 03-dec-81

7902 FORD ANALYST 7566 3000 20 03-dec-81

7934 MILLER CLERK 7782 1300 10 23-jan-82

The contents of the DEPT table.

deptno dname loc

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

The contents of the SALGRADE table.

grade losal hisal

1 700 1200

2 1201 1400

3 1401 2000

4 2001 3000

5 3001 9999

10This is deliberate - see Part 3 under Null Values.

Page 12: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.3.1. Simple Data Retrieval

The SQL command to retrieve data is the SELECT statement11. The result of a SELECTstatement is a table (this table may have many rows but it may also have only one row oreven no rows)

A SELECT Statement must have at least two clauses :-

SELECT the expressions (frequently columns) to retrieve.FROM the tables from which data is to be retrieved.

Other parts of the SELECT statement, allowing the selection from multiple tables, or ofspecific rows will be covered in a later part of the course. Here we shall data will beretrieved from a single table only.

Note that when entering statements interactively into Ingres:Statements must be separated with a semicolon (;). You may omit the semicolon

from the last (or only) statement. Newlines and spaces that are not contained within data are all ignored.The statement is only executed when you select Go, not when you press Return..

Exercise 1.4. Selecting Specific Columns from a Table.

1) To select the columns DNAME and DEPTNO from the table DEPT.

l Type :-

SELECT DNAME, DEPTNO FROM DEPT;

N.B. You can edit an incorrect statement in Ingres but if the change is non-trivial,select Edit. This initiates your default editor (Vued for this course)

l The table formed by the SELECT statement is displayed12 :-

dname deptno

ACCOUNTING 10

RESEARCH 20

SALES 30

OPERATIONS 40

2) The order of the column names in the SELECT statement determines the order of thecolumns in the table formed by the SELECT statement.

l Type :-

SELECT DEPTNO, DNAME FROM DEPT;

(and note the difference from 1).

11 Select Statement is not the name of this construct in the SQL Standard.12 Note in particular that the order of rows in a derived result table is not generally defined unless you

Page 13: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

Specifying the names of columns in a SELECT statement is the approved method, especially if theSELECT statement appears in programs. However, a convenient shortcut which may be used whentyping commands at a terminal is shown in the next exercise.

Exercise 1.5 - Selecting all the Columns of a Table.

1) Type :-

SELECT * FROM EMP;

l Rows displayed :-

empno ename job mgr sal comm deptno hiredate

7369 SMITH CLERK 7902 800 20 17-DEC-80

7499 ALLEN SALESMAN 7698 1600 300 30 20-FEB-81

7521 WARD SALESMAN 7698 1250 500 30 22-FEB-81

7566 JONES MANAGER 7839 2975 20 02-APR-81

7654 MARTIN SALESMAN 7698 1250 1400 30 28-SEP-81

7698 BLAKE MANAGER 7839 2850 30 01-MAY-81

7782 CLARK MANAGER 7839 2450 10 09-JUN-81

7788 SCOTT ANALYST 7566 3000 20 27-JUN-90

7839 KING PRESIDENT 5000 10 17-NOV-81

7844 TURNER SALESMAN 7698 1500 0 30 08-SEP-81

7876 ADAMS CLERK 7788 1100 20 31-JUL-90

7900 JAMES CLERK 7698 950 30 03-DEC-81

7902 FORD ANALYST 7566 3000 20 03-DEC-81

7934 MILLER CLERK 7782 1300 10 23-JAN-82

l The notation * means all the columns in the table in the order they appear in thetable definition.

l Note that ISQL, depending on the terminal type, may not provide facilities forviewing the right hand columns of "long" tables in the way that qbf does. If the tableis too wide for the screen and you need to see all of it (i.e. you cannot just rearrange thecolumns), then you must write it to a file and print/view it. Displaying tables thatare wider than the screen is always a problem with any product.

You may also qualify the * by a table name to mean all the columns of the named table. This is a veryuseful alternative when more than one table is involved in a query.

Exercise 1.6 - Using a qualified select *.

1) Type :-

SELECT EMP.* FROM EMP;

l Note that the result is the same as before.

Page 14: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

Exercise 1.7 - Suppressing Duplicate Rows.

l Type :-

SELECT JOB FROM EMP;

l Rows displayed :-

job

CLERK

SALESMAN

SALESMAN

MANAGER

SALESMAN

MANAGER

MANAGER

ANALYST

PRESIDENT

SALESMAN

CLERK

CLERK

ANALYST

CLERK

l Note that some of the jobs are duplicated.

l In order to eliminate duplicate rows from a result table, use the keyword DISTINCT. Type :-

SELECT DISTINCT JOB FROM EMP;

l Rows displayed :-

job

ANALYST

CLERK

MANAGER

PRESIDENT

SALESMAN

l The way in which the SELECT statement that includes DISTINCT works is to firstconstruct a result table and then to eliminate any redundant duplicate rows (usually,but not always, by sorting the result table). A row is a duplicate of another if a l l thevalues in the row are identical to the corresponding values in another row.

Page 15: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.4. The WHERE Clause in a SELECT Statement.

The WHERE Clause in a SELECT Statement is used to select the set of rows that are to appear in theresult table.

There now three clauses in a SELECT Statement :-

SELECT expressionsFROM tableWHERE search_condition

Search_condition is something that yields a truth value.

The operation of the SELECT statement is as follows:

l Construct a table identical to that specified in the FROM clausel For each row in that table, evaluate the search_condition of the Where clause and

discard the row if the search_condition is not true for that row (any reference to acolumn is taken to be a reference to the value in that column in the row underconsideration).

l from that table, evaluate the expressions identified in the select_list part of theSELECT statement

Exercise 1.8 - Selecting rows for the result table.

l What would you expect the result of the following Select statement to be:

SELECT 1 FROM EMP

l Did you get the number of rows correct?

PredicatesNote that there are a number of types of search_condition. We shall start with the simplest, called acomparison_predicate.

A comparison_predicate, in its simplest form consists of :-

l an expression13

l a comparison operator (e.g =, > , < , <>)l another expression

Note:i) The WHERE Clause can appear in other SQL Statements such as UPDATE and

DELETE. It's function then is still to identify a set or rows. The syntax is identical.

i i ) It is not necessary to display the columns used in the WHERE Clause (though theymust of course be columns that are in the table(s) identified in the FROM clause). However, it is often uselful to do so, especially in complex queries, to make sure youhave got the right answer. All the following exercises will generally do this.

Page 16: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

Exercise 1.9 - To display the names and jobs of all the employees in department 10.

l Type :-

SELECT ENAME, JOB, DEPTNOFROM EMPWHERE DEPTNO = 10;

l Rows displayed :-

ename job deptno

CLARK MANAGER 10

KING PRESIDENT 10

MILLER CLERK 10

l The predicate DEPTNO = 10 is applied to each row in the EMP table. Only thoserows for which the predicate is true are retrieved.

l Note that numeric literals (that is literals that are numbers) are just written asnumbers. Character string literals would have appeared within quotes (e.g 'string').

l The data type of a column determines the data type of a literal to be applied to it. E.g DETPNO = '10' is not be legal14

The column in the above exercise is defined as numeric and the literal specified was a numeric literal. The format for a character literal (as required when comparing value in a column defined as typeCHAR) is demonstrated in the following exercise).

Page 17: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

ƒ

Exercise 1.10 - To display the names of all the managers in the company.

l Type :-

SELECT ENAME, JOBFROM EMPWHERE JOB = 'MANAGER';

l Rows displayed :-

ename job

JONES MANAGER

BLAKE MANAGER

CLARK MANAGER

Note that comparisons involving character string data are case sensitive; that is 'MANAGER' is notequal to 'manager'15.

Exercise 1.11 - To display the names of employees whose job is 'manager'

l Type :-

SELECT ENAME, JOBFROM EMPWHERE JOB = 'manager';

l Rows displayed :-

ENAME JOB

Page 18: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

Ingres and a number of other products also support a data type of DATE. DATE is not a data typesupported in earlier versions of the SQL standard (or in the Entry Level of the 1992 version of SQLwhich products like Ingres, Oracle etc. claim conformance to). However, the DATE data type doesappear in the Intermediate conformance level of SQL-92 and so slowly products are beginning toconform. In particular, although the presentation format for dates may show only two digits for theyear, a l l conforming products actually store dates with a four digit year, removing many of theproblems associated with the roll-over to the year 2000. However, there is still not too muchconformatity in the functions that different products define for dates. In Ingres, DATE literals appearlike character string literals and must be enclosed in single quotes.

Exercise 1.12 - To display t h e names and hire dates of a l l employees h ired on or after 1st January1990.

l Type :-

SELECT ENAME, HIREDATE FROM EMPWHERE HIREDATE >= '1-JAN-1990';

l Rows displayed :-

ENAME HIREDATE

SCOTT 27-JUN-90

ADAMS 31-JUL-90

l Althoughin Ingres the format of the date literal is the same as that of the characterstring literal16 (i.e they both are enclosed within quotes), Ingres can determine thedata type of the literal from the context in which it occurs. In this case, the columnHIREDATE is a column of type DATE.

l The full century (1990) is specified here as Ingres expands dates with 2-digit years toinclude the current century17

l The data is displayed here using a 2-digit year. Generally such a display is notdesirable. Far better to display using a full 4-digit year.18

16 In MS Access, date/time literals are delimited in the character #17 This is alterable to be a window – rather like access by setting an environment variable.18 Data can be forced to always display using a 4-digit year in Ingres by setting an environment

Page 19: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

Thus far we have considered rows which have a particular value in one column. We shall now look athow to exclude rows of a particular value.

Exercise 1.13 - To display the names and jobs of all employees who are not managers.

l Type :-

SELECT ENAME, JOB FROM EMPWHERE NOT JOB = 'MANAGER';

l Rows displayed :-

ename job

SMITH CLERK

ALLEN SALESMAN

WARD SALESMAN

MARTIN SALESMAN

SCOTT ANALYST

KING PRESIDENT

TURNER SALESMAN

ADAMS CLERK

JAMES CLERK

FORD ANALYST

MILLER CLERK

l This is the general method of inverting a logical expression. In this case a moreconcise (and preferred) way is to use the "not equals" operator in the predicate:-

WHERE JOB <> 'MANAGER'

Ingres also allowsfor compatibility reasons the alternative operator for <>: !=

Page 20: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.5. More complicated Search_conditions.

In the previous examples, the search_condition contained just a single predicate . In general, asearch_condition can comprise a complex expression of predicates linked by the logical operatorsAND and OR. The predicates may also refer to different columns.

The next exercise gives an example of a search_condition containing two comparison_predicates.

Exercise 1.14 - To display t h e names, jobs and salaries of a l l employees in department 20 who earnmore than $2000 per month.

l Type :-

SELECT ENAME, JOB, SAL, DEPTNOFROM EMPWHERE DEPTNO = 20 AND SAL > 2000;

l Rows displayed :-

ename job SAL DEPTNO

JONES MANAGER 2975 20

SCOTT ANALYST 3000 20

FORD ANALYST 3000 20

l Again, the search_condition is applied to the table identified in the FROM clause. The result, which has been displayed, contains only rows that satisfy bothconditions, DEPTNO = 20 and SAL > 2000.

The next exercise gives an example of two values in the same column.

Exercise 1.15 - To display t h e names, jobs and salaries of a l l employees whose monthly salaries arebetween $1000 and $2000 per month.

l Type :-

SELECT ENAME, JOB, SALFROM EMPWHERE SAL >= 1000 AND SAL <= 2000;

l Rows displayed :-

ename job sal

ALLEN SALESMAN 1600

WARD SALESMAN 1250

MARTIN SALESMAN 1250

TURNER SALESMAN 1500

ADAMS CLERK 1100

MILLER CLERK 1300

Page 21: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

The previous example contained a search-condition comprising two comparison_predicates. In factSQL contains a special predicate called a between_predicate which does the same job:

Exercise 1.16 - To display t h e names, jobs and salaries of a l l employees whose monthly salaries aregreater than or equal to $1000 and less than $2500 per month using the between_predicate

l Type :-

SELECT ENAME, JOB, SALFROM EMPWHERE SAL BETWEEN 1000 AND 2000;

l The results should be the same as previously.

It is possible to combine normal and inverted logical expressions.

Exercise 1.17 - To display t h e names, jobs and salaries of a l l employees whose monthly salaries aregreater than $2500 per month and are not managers.

l Type :-

SELECT ENAME, JOB, SALFROM EMPWHERE SAL > 2500 AND JOB != 'MANAGER';

l Rows displayed :-

ename job sal

SCOTT ANALYST 3000

KING PRESIDENT 5000

FORD ANALYST 3000

Page 22: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

The following example uses OR to connect three comparison_predicates:

Exercise 1.18 - To display the names, jobs and departments of a l l employees who are clerks, salesmenor managers.

l Type :-

SELECT ENAME, JOB, DEPTNOFROM EMPWHERE JOB = 'CLERK' OR JOB = 'SALESMAN' OR JOB = 'MANAGER';

l Rows retrieved :-

ename job deptno

SMITH CLERK 20

ALLEN SALESMAN 30

WARD SALESMAN 30

JONES MANAGER 20

MARTIN SALESMAN 30

BLAKE MANAGER 30

CLARK MANAGER 10

TURNER SALESMAN 30

ADAMS CLERK 20

JAMES CLERK 30

MILLER CLERK 10

An alternative and shorted way of expression a series of comparison_predicates on a single column allconnected by OR is to use an in_predicate:

Exercise 1.19 - To display the names, jobs and departments of a l l employees who are clerks, salesmenor managers using an in-predicate.

l Type :-

SELECT ENAME, JOB, DEPTNOFROM EMPWHERE JOB IN ('CLERK', 'SALESMAN', 'MANAGER');

l The rows retrieved should be identical to those in the previous exercise.

Page 23: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.6. Ordering of the Display of Data.

Rows are retrieved from a table in an implementation-dependent19 order. Usually this order is notparticularly useful; frequently it is the order in which the rows were stored. The order in which rowsare presented can be controlled by the ORDER BY clause in the SELECT Statement.

Rows may be ordered :-

l in ascending order or descending orderl on single or multiple columns

The ORDER BY clause is the last clause in a SELECT Statement.

Exercise 1.20 - To display the salaries and names of all employees in ascending order of their salaries.

Note:. The default is ascending order.

l Type :=

SELECT SAL, ENAMEFROM EMPORDER BY SAL;

l Rows displayed :-

sal ename

800 SMITH

950 JAMES

1100 ADAMS

1250 WARD

1250 MARTIN

1300 MILLER

1500 TURNER

1600 ALLEN

2450 CLARK

2850 BLAKE

2975 JONES

3000 SCOTT

3000 FORD

5000 KING

19 Implementation dependent is a technical SQL term that means that the result may vary betweenSQL implementations and that further, the implementor need not tell you what the result will be -

Page 24: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

To change from the default ascending order, it is simply a matter of adding the word DESC to theORDER BY clause.

Exercise 1.21 - To display t h e salaries and names of a l l employees in descending order of the irsalaries.

l Type :

SELECT SAL, ENAMEFROM EMPORDER BY SAL DESC;

l Rows displayed :-

sal ename

5000 KING

3000 SCOTT

3000 FORD

2975 JONES

2850 BLAKE

2450 CLARK

1600 ALLEN

1500 TURNER

1300 MILLER

1250 WARD

1250 MARTIN

1100 ADAMS

950 JAMES

800 SMITH

Page 25: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

1.7. LIKE predicate

SQL is not a good language for searching text. Firstly, searches are case dependent - that is,'MANAGER' is different from 'manager', which is different from 'Manager' - so searches have to bequite carefully thought out. Also punctuation and spaces are not ignored - so 'a space' is different from' a space' which is different from 'a space'.

However, SQL does have one predicate that provides a limited pattern matching capability, theLIKE predicate.

The LIKE predicate takes the form:

Column_name [NOT] LIKE pattern [ESCAPE [escape_character] ]

Note in particular that:

l the syntax specifies Column_name and not expression.

l pattern is a character string l iteral , not a column.

l escape_character is a character string l iteral used to change the meaning ofcharacters in the pattern. It allows you to search for the characters % or _ in acolumn.

The following are special characters in a pattern20:

l Percent (%) denotes 0 or more arbitrary characters

l Underscore (_) denotes exactly one arbitrary character.

Hence, if a column ENAME contained the value 'JONES', the following LIKE predicates would returnthe value true:

ENAME LIKE 'J%' matches any string beginning with a J

ENAME LIKE 'JAME_' matches string JAME plus one character

ENAME LIKE '_A%' matches any string where A is the second character.

Note that spaces are significant, including trailing ones for fixed length strings. That is if ENAME isdefined as char(9) and contains the value 'FRED ', the following predicate is false:

ENAME LIKE 'FRED'

Page 26: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 1

Exercise 1.22 - Display the names of all employees whose name contains 'LL'.

l Type :

SELECT ENAME FROM EMPWHERE ENAME LIKE '%LL%';

l Rows displayed :-

ename

ALLEN

MILLER

Page 27: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

AnIntroduction

toSQL.

Part 2.

More Advanced Data Retrieval.

Page 28: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 2

2.1. Introduction.

In general there are usually a number of different ways of expressing the same query in SQL and themore complex the query the more possible alternative ways there may be of specifying it..

In an ideal world, the SQL Query Optimiser should always be able to translate any queryrepresentation into an optimal form prior to execution. Unfortunately, many products are far fromoptimum, and it may be that expressing a query in a different way may cause execution to proceedsignificantly quicker (or slower!). However, it is usually not desirable to rely too much on gleanedknowledge on the way in which a query optimiser works - the algorithms can (and indeed should!)vary with data volumes, with the distribution of data and with the physical organisation of thedata. In any event, the implementors may choose to do things differently in the next softwarerelease!1.

In the examples that follow, the emphasis is on understanding the query and not on how fast it goes.

A good strategy in practice, if the query is to be more than a one off, is to get it working first and tune itafterwards. To this end generally, some products provide additional tools so that a trained person cansee how a query optimiser is decomposing a query and can identify possible bottle-necks in theprocessing. The correct action when such problems are identified is to change the physicalorganisation of the data, typically by adding a new index.

Note also, when specifying complex SQL queries it is very easy to specify a query that is syntacticallycorrect and returns a result but was not actually the query you thought you were asking. This is usuallyeasy to detect on small databases such as the one used for this course; it is not always easy to detectthis on a large database. Always try to verify that a complex query is actually doing what youthought it should be doing.

1 The way in which an SQL implementation optimises and executes a query is Implementationdependent

Page 29: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 3

2.2. Joining Tables.

One of the most common forms of what is described here as "advanced data retrieval" is the(relational) joining of two or more tables.

The way in which SQL joins tables in a select statement is to include more than one table name in theFROM part of the select statement. When more than one table is identified in the FROM part of theselect statement, conceptually SQL constructs a working table that is the cartesian product of thetables identified. The following shows an example of a cartesian product of two tables EMP and JOB.

Consider the following original tables2:

JOB EMP

J_NAME E_NO E_NO E_NAME SALARY

Analysis 5001 5001 Fred 15,000

Design 5002 5002 John 20,000

Building 5001

Result of Cartesian product is the following table:

JOB x EMP

JOB.J_NAME JOB.E_NO EMP.E_NO EMP.E_NAME EMP.SALARY

Analysis 5001 5001 Fred 15,000

Analysis 5001 5002 John 20,000

Design 5002 5001 Fred 15,000

Design 5002 5002 John 20,000

Building 5001 5001 Fred 15,000

Building 5001 5002 John 20,000

Note that the number of columns in the cartesian product is sum of the columns in the participatingtables. The number or rows in the cartesian product is the product of number of rows in theparticipating tables.

Note also the column names used in the cartesian product. The columns have been prefixed by thenames of the tables form which they came. This is necessary in some cases - for example todifferentiate between JOB.E_NO and EMP.E_NO. Where the column names in the original tables arehowever unique, the additional qualifier (the JOB. and the EMP. - note the ".") may be dropped whenreferring to the columns in the product table.

2 These are not tables from your example database.

Page 30: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 4

In SQL, the cartesian product is represented by a SELECT statement where there is more than onetable in the FROM clause and no WHERE clause. E.g:

SELECT * FROM JOB, EMP3

However, the raw cartesian product is not usually of much interest. Almost invariably a predicate4 isapplied to the result to select only those rows of interest. The following example, which selects onlythose rows where the E_NO in the JOB table is equal to the E_NO in the EMP table illustrates this.

SELECT * FROM JOB, EMPWHERE JOB.E_NO = EMP.E_NO

JOB.J_NAME JOB.E_NO EMP.E_NO EMP.E_NAME EMP.SALARY

Analysis 5001 5001 Fred 15,000

Design 5002 5002 John 20,000

Building 5001 5001 Fred 15,000

Further, note that the * in

SELECT * FROM JOB, EMPWHERE JOB.E_NO = EMP.E_NO

is equivalent to:

SELECT JOB.J_NO, JOB.E_NO, EMP.E_NO, EMP.NAME, EMP.SALARYWHERE JOB.E_NO = EMP.E_NO

That is the * meant, as before, "select all the columns".

3 This is an illustrative example only. Do not try typing it; the table JOB does not exist in yourdatabase.4 Here the predicate is applied using a WHERE clause; later a JOIN operator will be introduced.

Page 31: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 5

Exercise 2.1 - To list the names of all employees and their places of work.

l The names of the employees are in the EMP table and the places of work (locations)are in the DEPT table. The field by which these two tables are connected is DEPTNOwhich appears in both tables.

l Type :-

SELECT EMP.ENAME, DEPT.LOCFROM EMP, DEPTWHERE EMP.DEPTNO = DEPT.DEPTNO;

Note that it is necessary to qualify the field name DEPTNO. Failure to do so islikely to result in an error message to the effect that the field name is ambiguous. It isnot necessary to qualify either ENAME although it has been in this example.

l Results displayed :-

ename loc

CLARK NEW YORK

KING NEW YORK

MILLER NEW YORK

ADAMS DALLAS

FORD DALLAS

JONES DALLAS

SCOTT DALLAS

SMITH DALLAS

ALLEN CHICAGO

BLAKE CHICAGO

MARTIN CHICAGO

JAMES CHICAGO

TURNER CHICAGO

WARD CHICAGO

l Now re-run the query without the WHERE clause. The result is the cartesian productof the two tables. Let this be a lesson to you! It is particular importance in statementsthat update a database. In particular, omitting the WHERE clause from a DELETEstatement will delete all the rows in a table.

Page 32: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 6

It is not necessary that any predicate be used to select rows from the cartesian product. There is norequirement for the same column name to appear in each table; nor is there any requirement for thepredicate to be a comparison predicate that specifies equality. The following example shows onepossibility; the between predicate has been used to compare a salary in one table with a salary rangein the other table.

Exercise 2.2 - To display the name and grade of every employee.

l As before the employee name is in the EMP table. The various grades are in theSALGRADE table, defined by a salary range. The tables are connected as shown.

l Type :-

SELECT EMP.ENAME, SALGRADE.GRADEFROM EMP, SALGRADEWHERE EMP.SAL BETWEEN SALGRADE.LOSAL AND SALGRADE.HISAL;

l Results displayed :-

ename grade

SMITH 1

ALLEN 3

WARD 2

JONES 4

MARTIN 2

BLAKE 4

CLARK 4

SCOTT 4

KING 5

TURNER 3

ADAMS 1

JAMES 1

FORD 4

MILLER 2

l Note that there is no pre-determined ordering of the results. Any ordering apparentwould have been purely fortuitous.

Page 33: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 7

2.3. The Inner Joining OperatorA capability which appears in many SQL products now, although its defined in Intermediate levelSQL rather than Entry Level is the (Inner) JOIN operator. The (Inner) JOIN operator allows tablesto be joined on equal values (as was done in the cartesian product above) but allows the join to bespecified in the FROM clause rather than the WHERE clause - which makes the sql statementeasier to read. It is referred to as Natural join as the join is on equal values in the tables concerned.

For the moment we are only concerned with INNER JOINS (OUTER JOINS are dealt with later). Consider example 2.1:

Exercise 2.3 - To list the names of all employees and their places of work.

l The names of the employees are in the EMP table and the places of work (locations)are in the DEPT table. The field by which these two tables are connected is DEPTNOwhich appears in both tables.

l Type :-

SELECT EMP.ENAME, DEPT.LOCFROM EMP INNER JOIN DEPT ON EMP.DEPTNO = DEPT.DEPTNO;

Note that there is no longer a need for a where clause.

l Results displayed are as before :-

ename loc

CLARK NEW YORK

KING NEW YORK

MILLER NEW YORK

ADAMS DALLAS

FORD DALLAS

JONES DALLAS

SCOTT DALLAS

SMITH DALLAS

ALLEN CHICAGO

BLAKE CHICAGO

MARTIN CHICAGO

JAMES CHICAGO

TURNER CHICAGO

WARD CHICAGO

Using the JOIN operator instead of a WHERE clause to specify joins usually makes a complex SQLstatement more readable. If several JOINs are required in a single statement, brackets may be used todetermine the order of evaluation.

Page 34: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 8

2.4. Subqueries.

The result of a SELECT statement is a table. This is an ephemeral thing in that it is not (usually)stored in the database and is thus not available for further processing. Many common queries appearto require the use of just such a table as an intermediate stage in providing an answer and it by usingsuch intermediate ephemeral tables that SQL is made the powerful language that it is. This isillustrated by the following example :-

Exercise 2.4 - To display the names of all employees who have the same job as BLAKE.

l Since it is assumed that we don't know how Blake's job is stored in the database, firstfind Blake's job by typing :-

SELECT JOBFROM EMPWHERE ENAME = 'BLAKE';

l This query returns a single column from a single row (a single value) :-

job

MANAGER

This you might want to write this down on a handy piece of paper.

l It has now been established that BLAKE is a MANAGER. Now display al l managersby typing :-

SELECT ENAMEFROM EMPWHERE JOB = 'MANAGER';

l Results displayed :-

ename

JONES

BLAKE

CLARK

Page 35: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 9

2.4.1. Simple Sub-queries.

The previous example is obviously a two stage operation, probably involving a piece of paper and thepossibility of typing "MANAGER" wrong (probably in lower case!). In fact, we don't want to knowBlake's job at all. The two stages may be combined using a subquery as in the following exercise. Thebracketed SELECT statement is referred to as a subquery.

Exercise 2.5 - To display t h e names of a l l employees who h a v e t h e same job as BLAKE using asubquery.

l Type :-

SELECT ENAMEFROM EMPWHERE JOB = (SELECT JOB FROM EMP WHERE ENAME = 'BLAKE');

l The results displayed should be the same as in the previous exercise (although therows may not be in the same order!).

The subquery in the previous example returns only a simple, single value5 - and so may appear in acomparison predicate. Subqueries that return a table of values require a different predicate - see theIN predicate in a future section.

5 In the SQL standard, this is referred to as a Scalar Subquery.

Page 36: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 10

Exercise 2.6 - Display t h e names of a l l employees who earn more than t h e average salary for t h ecompany.

l The first step is to test out the subquery that produces the average salary. Type :-

SELECT AVG (SAL)FROM EMP;

This gives a value of 2073.214. It is always a good idea to check out subqueries first toensure that they return roughly what you expect. Note the use of the function AVG.This will covered more fully in Part 3 of this course.

l To execute the full query, type :-

SELECT ENAMEFROM EMPWHERE SAL > (SELECT AVG (SAL) FROM EMP);

l Results displayed :-

ename

JONES

BLAKE

CLARK

SCOTT

KING

FORD

Thus far the subqueries have only returned a single value. Exercises 2.5 and 2.6 would fail if thesubquery returned more than one value; this is because the subquery was used in a comparison predicateand that only compares simple values. If there were more than one Blake on the payroll, the subqueryin Exercises 2.5 and 2.6 would have returned more than one row and the main query would have failedwith some sort of error message (implementation dependent).

The IN predicate is specifically designed to do comparisons of a value against a table of values. Thisis described in the next section.

Page 37: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 11

2.4.2. Subqueries that return more than one row.

When a subquery returns more than one row, it returns a SET of values; the algebra of sets applies. Only the simplest concepts will be considered here.

A value is either a member of a set or it isn't. If a value is a member of a set, it is said to be IN thatset. Conversely, if a value is not a member of a set, it is said to be NOT IN that set.

Because of the limited amount of data available in the demonstration database, the followingexercises are somewhat contrived to illustrate the point.

Exercise 2.7 - To display the names of all employees who work in Dallas or Chicago.

l The subquery to be employed is :-

SELECT DEPTNOFROM DEPTWHERE LOC = 'DALLAS' OR LOC = 'CHICAGO';

l Try it just to see what happens.

l Combine the subquery with the main query using an in predicate. Type :-

SELECT ENAMEFROM EMPWHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS' OR LOC = 'CHICAGO');

l Results displayed :-

ename

ADAMS

ALLEN

BLAKE

FORD

JAMES

JONES

MARTIN

SCOTT

SMITH

TURNER

WARD

Page 38: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 12

The converse of the previous exercise is shown below.

Exercise 2.8 - To display the names of all employees who do not work in Dallas or Chicago.

l The subquery is the same as before. The difference is in the main query. Type :-

SELECT ENAMEFROM EMPWHERE DEPTNO NOT IN (SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS' OR LOC = 'CHICAGO');

l Rows displayed :-

ename

CLARK

KING

MILLER

Page 39: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 13

Both the previous exercises could have been answered using either implicit joins (i.e. cartesianproducts together with appropriate where clauses) or using an inner join operator. The cartesianproduct alternative for Exercise 2.7 is as follows :-

SELECT ENAMEFROM EMP,DEPTWHERE EMP.DEPTNO = DEPT.DEPTNOAND (LOC = 'DALLAS' OR LOC = 'CHICAGO');

The query could also have been written replacing the cartesian product by an explicit inner join:

SELECT ENAMEFROM EMP INNER JOIN DEPT ON EMP.DEPTNO = DEPT.DEPTNOWHERE (LOC = 'DALLAS' OR LOC = 'CHICAGO');

Likewise, for exercise 2.8 the answer would be :-

SELECT ENAMEFROM EMP,DEPTWHERE EMP.DEPTNO = DEPT.DEPTNOAND NOT (LOC = 'DALLAS' OR LOC = 'CHICAGO');

Or using the inner join opperator:

SELECT ENAMEFROM EMP INNER JOIN DEPT ON EMP.DEPTNO = DEPT.DEPTNONOT (LOC = 'DALLAS' OR LOC = 'CHICAGO');

IMPORTANT NOTE - While subqueries and joins may on occasions be interchangeable as in the above,this is not GENERALLY true. In particular, if any intermediate value is a computed value (as inexercise 2.6), this must be in a subquery in order to do the evaluation6.

6 This restriction will change in later versions of the SQL Standard when general query expressionswill be allowed instead of just tablenames in the FROM clause.

Page 40: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 14

2.4.3. Subqueries that return more than one column.

It is possible to have subqueries that return more than one column7. Once again this is a set which mayhave one or more members. Unfortunately, standard SQL (and Ingres) does not allow a row subquery ina comparison predicate (although some products, including Oracle) do. In Standard SQL, you have touse the EXISTS predicate.

Exercise 2.9 - To display t h e names, jobs and department of a l l employees who work in t h e samedepartment and have the same job as WARD.

l The subquery to be employed here is :-

SELECT JOB, DEPTNOFROM EMPWHERE ENAME = 'WARD';

l Try it out and note that only one row is returned.

l Combine the subquery with the main query. Type :-

SELECT ENAME, JOB, DEPTNOFROM EMPWHERE (JOB, DEPTNO) = (SELECT JOB, DEPTNO FROM EMP WHERE ENAME = 'WARD');

l Note the error message (not actually very helpful)

7 referred to in the SQL standard as a Row Subquery.

Page 41: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 15

2.4.4. Exists predicate

The EXISTS predicate has been considered under subqueries because it can only be used in conjunctionwith a subquery. The EXISTS predicate tests for an empty set. The EXISTS predicate always returnseither the value True or the value False (later, in section 3, when we consider the implications of nullvalues we shall see that this is not true for all predicates!).

Exercise 2.10 - To display the name, number, and location of any department that has no employees.

l The subquery to be employed here is :-

SELECT DEPTNO, DNAME, LOCFROM DEPTWHERE NOT EXISTS (SELECT * FROM EMP WHERE EMP.DEPTNO = DEPT.DEPTNO)

l The rows displayed are:

deptno dname loc

40 OPERATIONS BOSTON

l Note that because the Exists predicate is only testing for "emptiness" in the subquery,it is irrelevant what you place in the select list of the subquery in the predicate.

l Try the above statement using a different select list:

SELECT DEPTNO, DNAME, LOCFROM DEPTWHERE NOT EXISTS (SELECT 'anything' FROM EMP WHERE EMP.DEPTNO = DEPT.DEPTNO)

l Note that the answer is as before.

Page 42: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 16

2.4.5. Correlation names

Another facility often used in conjunction with subqueries8 is a correlation name. A correlation name isnecessary when you need to refer to the same table in more than one context. The correlation name iseffectively an alias for the table name. It is also a useful device when you have long meaningful tablenames as a short correlation name may be used whenever the table name would be used.

Correlation names are specified in the FROM clause. The correlation name then effectively replacesthe original table name within the SQL statement. The scope of the correlation name is the same asthe original table name would have been - namely the SQL statement or the subquery.

Consider the problem that we had in a previous exercise.

Exercise 2.11 - To display t h e names, jobs and department of a l l employees who work in t h e samedepartment and have the same job as WARD.

l We tried unsuccessfully to use the following query:

SELECT ENAME, JOB, DEPTNOFROM EMPWHERE (JOB, DEPTNO) = (SELECT JOB, DEPTNO FROM EMP WHERE ENAME = 'WARD');

l However, by using correlation names and the EXISTS predicate we can move thecomparison into the WHERE clause of the subquery and try:

SELECT ENAME, JOB, DEPTNOFROM EMP E1WHERE EXISTS (SELECT * FROM EMP E2 WHERE E2.ENAME = 'WARD' AND

E1.JOB = E2.JOB AND E1.DEPTNO = E2.DEPTNO);

l This works. The rows displayed are:-

ename job deptno

ALLEN SALESMAN 30

MARTIN SALESMAN 30

TURNER SALESMAN 30

WARD SALESMAN 30

8 And with joins or in fact any SQL operation that involves more than onetablename.

Page 43: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 17

Page 44: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 18

2.4.6. Multiple Subqueries.

A subquery is used in a predicate and a search condition may contain more than one predicate. Hence,a WHERE clause could contain more than one subquery. It is therefore possible to construct complexsearch conditions.

Exercise 2.12 - Display the names and jobs of all employees who have the same job as Smith or Martin.

l Type :-

SELECT ENAME, JOBFROM EMPWHERE JOB = ( SELECT JOB FROM EMP

WHERE ENAME = 'SMITH')OR JOB = ( SELECT JOB FROM EMP

WHERE ENAME = 'MARTIN');

l Rows displayed :-

ename job

SMITH CLERK

ALLEN SALESMAN

WARD SALESMAN

MARTIN SALESMAN

TURNER SALESMAN

ADAMS CLERK

JAMES CLERK

MILLER CLERK

l Note that you don't require correlation names for the table EMP although it is used in3 separate contexts. This is because the scoping rules for table names remove anyambiguity. The EMP referred to in each of the subqueries is the EMP defined in theFROM clause of that subquery. The EMP in the outermost (first) FROM clause is notreferred to within the subqueries.

l Note also of course that the above query could have been typed rather more conciselyusing an IN predicate rather than a comparison predicate:

l Try:

SELECT ENAME, JOBFROM EMPWHERE JOB IN (SELECT JOB FROM EMP WHERE ENAME = 'SMITH' OR ENAME = 'MARTIN');

Page 45: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 19

2.4.7. Nested Subqueries.

Some complex queries will require that some of the intermediate tables specified as subqueries willthemselves require subqueries in their specification. Again, this generality in SQL allows queries ofalmost arbitrary complexity to be constructed9.

Exercise 2.13 - To display t h e names and jobs of a l l employees tha t h a v e t h e same grade as t h emanager in Dallas.

This is a complex query involving all three tables in the Demonstration Database. There areseveral ways of getting the answer, but we will concentrate on the use of subqueries.

l Note first that DALLAS only appears in the DEPT table in the LOC column. Also,the EMP table and the DEPT table are connected by the DEPTNO column in eachtable. We therefore need the the number(s) of the department(s) in DALLAS. Thiscan be found using the following SQL statement :-

SELECT DEPTNOFROM DEPTWHERE LOC = 'DALLAS';

l Type this in and you will see that only one row is returned giving the departmentnumber as 20.

l The next step is to find the salary of the employees who are MANAGERs and whosdepartment is in the set of departments whose location is DALLAS. Continuing ourtheme of subqueries, the following SQL statement should give the manager's salary(for JONES) as 2975 :-

SELECT SALFROM EMPWHERE JOB = 'MANAGER'AND DEPTNO IN ( SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS');

l Try it to see what happens.

l Note that because there was only one department in DALLAS we could have used acomparison predicate rather than an IN predicate. However, using an IN predicatemeans our query would have worked regardless of the number of departments bases inDALLAS.

l An alternative way of formulating this query without a subquery is of course:-

SELECT SAL FROM EMP, DEPTWHERE EMP.DEPTNO = DEPT.DEPTNOAND LOC = 'DALLAS'

9 Ingres has a limit of something like 30 tables referenced in a query.

Page 46: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 20

AND JOB = 'MANAGER';

l The next step is to find the grade from the SALGRADE table. Note that we arecontinuing to program defensively, allowing for more than one MANAGER and morethan one department for DALLAS.

SELECT GRADEFROM EMP,SALGRADEWHERE SAL BETWEEN LOSAL AND HISAL AND JOB = 'MANAGER' AND DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS');

l When you type this, the answer should be 4.

l Now, you want to retrieve the ENAME and JOB of a l l employees with the gradedetermined above :-

SELECT ENAME,JOBFROM EMP,SALGRADEWHERE SAL BETWEEN LOSAL AND HISAL AND GRADE IN (

SELECT GRADEFROM EMP,SALGRADEWHERE SAL BETWEEN LOSAL AND HISAL AND JOB = 'MANAGER' AND DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC = 'DALLAS'));

l The rows returned are :-

ename job

JONES MANAGER

BLAKE MANAGER

CLARK MANAGER

SCOTT ANALYST

FORD ANALYST

l Inspect the data to ensure that this is what you would have expected.

Page 47: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 21

2.5. The GROUP BY Clause.

It is often a requirement to retrieve data in summary form - i.e. grouped according to some criterion. The GROUP-BY clause in a select statement is applied to the table that results after the applicationof the WHERE clause. The GROUP-BY clause identifies one or more grouping columns in the tableidentified by the WHERE clause. The intermediate table is effectively sorted on these groupingcolumns. A group is a collection of rows with the same value(s) for their grouping columns.

The result table comprises one row for each group. Any functions appearing in the select list areapplied only to the rows in a group and the result of the function appears in the result table row forthe group.

Obviously, the select list can only contain references to grouping columns or to functions (as only thesethings have a single value for each group). A typical example is shown in the following exercise.

Exercise 2.14 - To display the monthly salary bill for each department.

l Type :-

SELECT DNAME, SUM (SAL)FROM EMP, DEPTWHERE EMP.DEPTNO = DEPT.DEPTNOGROUP BY DNAME;

l Results displayed :-

dname Col2

ACCOUNTING 8750

RESEARCH 10875

SALES 9400

N.B. 1) The fact that the DNAME column appears in sorted order is fortuitous and shouldnot be relied upon (although often an implementation has to sort on the groupingcolumns).

2) When a GROUP BY clause is used, the select list must consist either of columnsspecified in the GROUP BY clause, or one of the Aggregate Functions (AVG, COUNT,MAX, MIN, SUM).

l Note the "system generated" column name Col2. This name has no significance andcannot be used. We could however rename the column using AS.

l Renaming a column using AS :-

SELECT DNAME, SUM (SAL) AS TOTAL_SALFROM EMP, DEPTWHERE EMP.DEPTNO = DEPT.DEPTNOGROUP BY DNAME;

Page 48: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 2

8 March 2000 Page 22

2.5.1. The HAVING Clause10

The HAVING clause allows you to apply a predicate to the table that is the result of a GROUP BYclause. The following are typical examples of using a HAVING clause:

Exercise 2.15 - To display t h e monthly salary bill for each department whose monthly salary bill isbetween 10,000 and 12,000.

l Type :-

SELECT DNAME, SUM (SAL)FROM EMP, DEPTWHERE EMP.DEPTNO = DEPT.DEPTNOGROUP BY DNAMEHAVING SUM (SAL) BETWEEN 10000 and 12000;

l Results displayed :-

dname Col2

RESEARCH 10875

Another very common use of HAVING is to locate duplicate rows in table:

Exercise 2.16 - Check for duplicate grades in the SALGRADE table

l Type :-

SELECT GRADE, COUNT(*)FROM SALGRADEGROUP BY GRADEHAVING COUNT(*) > 1;

l The result is of course empty as our data is carefully chosen!

10 In MS Access, Grouped queries are specified using the Summation symbol on the tool bar. The Criteria row then becomes the place where HAVING type criteria are specified. WHERE typecriteria are specified using the where specifier in the row called Totals on the query grid.

Page 49: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

AnIntroduction

toSQL.

Part 3.

Data Definition & Management,Miscellaneous Topics.

Page 50: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 2

Introduction.

As was stated at the beginning of Part 1, SQL is more than just a query language. It contains manyfacilities for the management and definition of data. While the basic data definition facilities arestandardised, many of the options are implementation defined1. Implementation-defined facilitiesshown here that are peculiar to Ingres are marked as such. Other implementations have similarfacilities, but differ in detail.

Data management facilites such as UPDATE and DELETE are standardised and conform over mostimplementations.

All products also contain a large number of facilities concerned with database administration. Thesehave not been standadised as they invariable have to be both product and environment specific. These will not be considered in this course.

1 Implementation-defined is a term defined in the SQL Standard to refer to a feature that is not standardised (i.e itwill vary between SQL implementations) but iss fully defined in the documentation for a specific implementation. Note that it is different from Implementation-dependent.

Page 51: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 3

TransactionsAn important concept when developing database applications is the concept of a transaction. ATransaction is a set of discrete operations which must be regarded by the application as atomic that is, either the operations must either completely succeed or the effects entire set of operationsmust be removed from the database. Typically a transaction2 changes the database from oneconsistent state to another consistent state.

Transactions are also the mechanism by which SQL database systems provide Serial isabi l i ty ; thatis the mechanism by which the results of an incomplete transaction cannot be affected bymodifications undertaken by another incomplete transaction; effectively it is as if transactions ranserially, not starting until the previous transaction had been completed. Obviously in a real systemtransactions do not run serially; the database software has inbuilt code to lock rows, pages, andtables while one transaction is using them, causing other transactions to wait until the requiredresources are free3. As the locking software does not actually serialise transactions it must also beable to resolve deadlock conditions; it does this by rolling back one or more of the blockedtransactions.

An SQL transaction is deemed to have been initiated when an SQL statement is initiated4 and notransaction is active. A Transaction is terminited by the explicit (or implicit) action of either:

COMMIT All changes to the database are made permanent and visible toother concurrent users of the database

ROLLBACK All changes to the database by the current transaction areremoved and the current transaction is terminated.

Exercise 3.1 - COMMIT the current transaction to free any locks that are active and to save any da tachanges.

l Type :-

COMMIT;

2 Sometimes referred to as a Success Unit for obvious reasons.3 One of the main differences between a server DBMS product such as Oracle or Ingres and a workstation productsuch as MS Access is that MS Access does not really have the mechanisms for handling transactions. This makesthe development of true robust multi-user applications potentially quite difficult.4 For technical reasons, some SQL statements do not initiate transactions. They are not relevant in the context ofthis course.

Page 52: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 4

Transaction level rollback can be illustrated by the following exercise

Exercise 3.2 - Demonstrate the effect of a ROLLBACK

l Type the following sequence of operations:-

SET AUTOCOMMIT OFF; This is discussed below; this makes sure its off.

Help EMP; display information about the table EMP

DROP TABLE EMP; Delete the table EMP

Help EMP; display information about the table EMP

l The last operation will fail - as you have deleted the table! However, the results ofthe transaction have not yet been committed so the table, although not visible to yourtransaction, has not yet been deleted

l Type the following sequence of operations:-

ROLLBACK; Rollback the transaction that deleted the table EMP

Help EMP; display information about the table EMP to verify that it isback

Note that the above example also illustrated another concept; that of Statement level rollback;this is every SQL statement is atomic, it either succeeds or fails, and if it fails it will have no effecton the contents of the database.

If an application terminates without issuing a COMMIT, any transaction that is active will beterminated with an implicit ROLLBACK5 - so becareful when writing applications to ensure youdon’t lose the last transactions6.

If AUTOCOMMIT is ON, then an implicit COMMIT is performed after each SQL statement7. Thisis efficient but can be dangerous; changes cannot be rolled back.

Exercise 3.3 - Set AUTOCOMMIT ON

l Type :-

SET AUTOCOMMIT ON;

5 It is therefore axiomatic that if an application does not terminate normally, say because of a hardware orsoftware malfunction, the DBMS has to roll back any changes made by incomplete transactions. All major DBMSsdo this automatically when they are initiated.6 ISQL automatically COMMITs uncompleted transactions when it terminates normally.7 SET AUTOCOMMIT is not defined in the SQL Standard but most major SQL implementations provide it.

Page 53: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 5

Views8.

A view is a (named) table that does not have any existence in its own right, but is instead derivedfrom one or more underlying base tables when the view is used.

A View is defined in terms of a SELECT Statement. This is best demonstrated in the followingexercise :-

Exercise 3.4 - To create a view on the EMP table that contains only the ENAME, JOB and SAL columns.

l Type :-

CREATE VIEW EMPV1 AS SELECT ENAME, JOB, SAL FROM EMP;

l Check the effect by typing :-

SELECT * FROM EMPV1;

The previous example shows a view which restricts the number of columns to be displayed. It is alsopossible to restrict the number of rows by the introduction of a WHERE Clause.

Exercise 3.5 - To create a new view to display only those employees in EMPV1 whose salaries are lessthan $1000.

l Type :-

CREATE VIEW EMPV2 AS SELECT ENAME, JOB, SAL FROM EMPV1 WHERE SAL < 1000;

l Check the effect by typing :-

SELECT * FROM EMPV2;

As can be deduced, two major purposes of a view are:

l hiding columns by suppressing them from the view

l hiding rows by suppressing them from the view

8 In MS Access an SQL View is referred to as a Query. This is confusing as the same name is used for updatequeries etc.

Page 54: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 6

The previous examples showed a view on a single table. The next examples demonstrate a view onmore than table.

Exercise 3.6 - To create a View which displays t h e employees name, number, location and salarygrade.

l Type :-

CREATE VIEW EMPV3 AS SELECT EMPNO, ENAME, GRADE, DNAME FROM EMP E, DEPT D, SALGRADE S WHERE E.DEPTNO = D.DEPTNO AND

SAL BETWEEN S.LOSAL AND S.HISAL;

l Check the effect by typing :-

SELECT * FROM EMPV3;

Exercise 3.7 - Verify that you could of course written the above view definition using a Join instead of acartesian product

l Type :-

CREATE VIEW EMPV4 AS SELECT EMPNO, ENAME, GRADE, DNAME FROM (EMP E JOIN DEPT D ON E.DEPTNO = D.DEPTNO), SALGRADE S WHERE

SAL BETWEEN S.LOSAL AND S.HISAL;

l Check the effect by typing :-

SELECT * FROM EMPV4;

Note that while GROUP BY is allowed in a View definition, ORDER BY is not.If you want sorted output from a View it is necessary to apply the ORDER BY to a SELECT on thatview.

Page 55: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 7

Exercise 3.8 - To display a sorted list of employees.

l Type the following just to see what happens:-CREATE VIEW SORT_EMP AS SELECT * FROM EMP ORDER BY ENAME;

l This will result in an error message The reason for the message is because a view is atable, and tables are inherently not ordered. Instead try :-SELECT * FROM EMPV3ORDER BY ENAME;

which will give the required result.

Hence, a third function of views is to hide structural complexity, making complex structures appear tothe user as a simple table. Views are also useful to define transient objects to simplify a complexquery.

3.1.1. Updating through Views.

For data retrieval purposes (i.e SELECT statements), a View may be treated as just another table. That is it may be used in all respects as if it were a normal base table.

Specifying a view as the subject table of an INSERT or UPDATE statement however should be treatedwith great caution.

Whether you are allowed to update or delete through a view depends on the view definition. Inparticular, if the view was formed by joining tables, the the view will in general not be updatable9.

However, if the view is

a). based upon a single table and

b) the view definition does not specify GROUP BY, and

c) each column definition in the view is a simple column name (i.e not an expression),

then generally it will be updatable; that is:

l if you update a row in the view, the corresponding row in the base table will getupdated;

l if you delete a row in the view, the corresponding row in the base table will getdeleted.

Special consideration has to be given to updates if the update would cause the row to leave the view. The WITH CHECK option allows this to be checked for but this is outwith the scope of this course.

9 MS Access does allow certain update operations through JOINS. the special MS Access DISTINCTROWwhich can be specified in a select query causes Access to maintain sufficient information for some updates to beperformed. This is not Standard SQL. See The MS Access manual for details.

Page 56: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 8

Data Definition.

For the purposes of this course you will add some tables and views to the database. Most productsprovide specific interfaces for the easy creation and maintenance of table definitions10, tables can alsobe created using SQL statements. Views11 in Ingres can only be created using SQL statements. Thefollowing tables will be added to the database:

Student Table.

Student Tutor No.

ANNE 1

BOB 1

EDWINA 2

FRED 1

JIM 2

Tutor Table.

Tutor No. Tutor

1 NEWTON

2 EINSTEIN

The relationships in this very simple database are self-evident and should not require furtherexplanation. The creation of these two tables will be demonstrated, as will a crude method ofentering the data. Various other data management statements will also be demonstrated using theseand the original tables.

Note especially that being able to create tables and views in ISQL is often very useful whenanswering complex queries. Temporary tables may be used to store partially evaluated queries;temporary views may be used to define a partial result for use in a more complex query. This secondcapability is especially useful for getting around some of the more tiresome restrictions associatedwith aggregate functions.

10 Ingres uses the Tables option of Ingmenu. MS Access uses a grid.11 MS Access confusingly calls a View a Select Query

Page 57: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 9

3.1.2. Create Table.

Before data can be entered into a table, that table must be created using a CREATE TABLE Statement.In its simplest form the syntax of this statement is :-

CREATE TABLE tablename (column_definition [, column_definition] ...);

Inevitably, different implementations have a number of extra bits, most of which are beyond thescope of this course but usually defaults values for these are provided. Even column_definition varies. In Ingres there are a number of data types, the most frequently used being :-

Fixed length Character Values - CHAR (size).Variable length Character Values - VARCHAR (size)Integer Numeric Values - INTEGER12

Approximate Numeric Values - FLOAT13

Date Values - DATE

Exercise 3.9 - To Create the STUDENT Table.

l Type :-

CREATE TABLE STUDENT (STUDENT_NAME CHAR (12), TUTOR_NO INTEGER);

In the terminology of the Standard, TUTOR_NO in the STUDENT Table is a Foreign Key into theTUTOR Table.

Exercise 3.10 - To Create the TUTOR Table.

l Type :-

CREATE TABLE TUTOR (TUTOR_NO INTEGER NOT NULL14, TUTOR_NAME CHAR (12));

The effect of a CREATE TABLE statement is to insert a table descriptor into the database catalog ofyour Ingres database. Ingres uses this descriptor when any operation refers to that table. You cannotcreate a table whose name corresponds to an existing table - you have to delete the old table first. TheIngres HELP command allows you to view information in the table descriptor.

12 Standard SQL allows an optional scale and precision

13 Standard SQL allows an optional scale and precision

14 In Ingres there is a subtle distinction between specifying NOT NULL on a column definition and adding a NOTNULL constraint. The former, tells Ingres not to allow any space for NULL values; the latter is a constraint thatmakes the DBMS check a value before its inserted into the column to see if it is null. The latter constraint can bedropped; the former can’t without redefining the table.

Page 58: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 10

3.1.3. ConstraintsWithin SQL it is possible to get the DBMS to make certain checks on the data to ensure the thedatabase remains consistent. Placing database constraints within the database schema ensures thatthey are always actioned; it is much better than relying on application programs to perform thecorrect actions. The constraints that can be added using facilities conformant with Entry level SQL-92 are:

l Check Constraint15

This verifies that a value in a column (or columns) is (are) correct. It takes the form:CHECK (search_condition)16

l Primary Key Constraint17

This defines that a column (or collection of columns) is a primary key18. Theconstraint Primary Key is equivalent to the two separate check constraints:

Column IS NOT NULL19

Column IS UNIQUE.

l Foreign Key Constraint20

The foreign key constraint specifies a referential constriant; that is it specifies thatthe value(s) in the foreign key column(s) of the table with the constraint mustmatch the valu(s) in the primary key field of the referenced table.

Constraints can be included in the table definition when the table is defined21. They can also beadded using the ALTER TABLE statement.

Exercise 3.11 - Make TUTOR_NO the unique (primary key) of the TUTOR table

l Type :-

ALTER TABLE TUTOR ADD UNIQUE (TUTOR_NO);

15 MS Access defines check constraints using the validation property of the column.16 In Entry level SQL-92,the search_condition must be simple; it cannot contain a subquery.17 MS Access defines primary key constraints using the primary key property of the table.18 Each row in the table has a unique value in the primary key column(s).19 Nulls are discussed in a later section.20 MS Access defines referential constraints using the relationships diagram.21 If the design has been done correctly, the constraints will be defined when the cable is designed.

Page 59: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 11

Constraints may also be named. It is very useful to name your constraints in SQL as you can only dropthem by referring to them by name22!

Exercise 3.12 - Add a Check constraint that specifies NOT NULL called TUTOR_NO_NOT_NULL onthe TUTOR_NO column of the TUTOR table

l Type :-

ALTER TABLE TUTOR ADD CONSTRAINT TUTOR_NO_NOT_NULL

CHECK (TUTOR_NO IS NOT NULL);

l (This is of course a redundant constraint; the TUTOR_NO column is not nullable aswhen the table was defined, no storage was set aside to hold the NULL value.)

Now a primary key is effectively defined on the table TUTOR, a referential constraint can bedefined between the tables TUTOR and STUDENT

Exercise 3.13 - Add a Referential Key constraint called REF_TUTOR on t h e STUDENT table toreference the TUTOR table

l Type :-

ALTER TABLE STUDENT ADD CONSTRAINT REF_TUTOR

FOREIGN KEY (TUTOR_NO) REFERENCES TUTOR (TUTOR_NO);23

The effect of the referential constraint is that every student that has a tutor number must identify atutor in the tutor table.

22 This does not apply to MS Access, where all properties for the table are displayed and can be amended ordeleted.23 The column(s) in the referenced table must have a unique constraint or be defined as a primary key. Corresponding columns in the two lists must have the same data type.

Page 60: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 12

3.1.4. System CatalogsDescriptors for objects such as tables, constraints etc. are stored within the DBMS in animplementation-dependent manner24 but most systems make descriptor information available touser programs. In Ingres, information about constraints is held in a table called IICONSTRAINTS.

Exercise 3.14 - View the constraint descriptors

l Type :-

SELECT * FROM IICONSTRAINTS;

l Note that your unnamed constraint has been allocated a system-generated name. Write down this name as it will be required for a later exercise..

With most systems, the catalog is generally self-describing. If you view the Ingres catalog tablethat contains the table-names of tables in the database you will find that it contains a large numberof tables owned by the special user $ingres.

Exercise 3.15 - View the table descriptors

l Type :-

SELECT * FROM IITABLES;

l Note that your tables are in the minority! Do not try updating catalog tables directlyunless you really know what you are doing. Users generally only update catalogtables indirectly, say as a result of creating a table or other SQL object.

24 The repository for descriptors is often referred to as a catalog.

Page 61: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 13

3.1.5. Changing Columns to a Table

Assuming you get your database design right first time, Altering a table should never be necessary! However, it is included here just in case you aren't prefect (or the application area that your databaseis supporting changes). Of course, one would expect that ALTER TABLE would allow you to add/dropcolumns etc. Unfortunately, that capability is not in Entry level of SQL-92 - it is in the IntermediateLevel. Some products do have ALTER TABLE but unfortunately, Ingres does not yet25! Neither does itallow a table to be renamed. However, it does permit a small extension to create and populate a tablein one statement. This can sometimes be used to advantage, especially when adding columns to a table

Exercise 3.16 - Add a date column to the SALGRADE table. Populate the column with todays date.

l Create a temporary table called TEMP containing the same as the SALGRADE table. Use the function date('today') to insert todays date into a column in the select list. Type: -

CREATE TABLE TEMP ASSELECT GRADE, LOSAL, HISAL, DATE('TODAY') AS START_DATE FROM SALGRADE;

l Note the use of an arbitrary expression in the select list. This expression has beengiven the column name START_DATE.

l Drop the original table - see section 3.3.4

DROP TABLE SALGRADE;

l Recreate SALGRADE as a copy of the new table TEMP

CREATE TABLE SALGRADE ASSELECT * FROM TEMP;

l Drop the temporary table

DROP TABLE TEMP;

l Commit the changes to make the table permanent.

COMMIT;

l In fact in this example, the COMMIT was redundant as you have set AUTOCOMMITON. Usually its better not to use AUTOCOMMIT but to explicitly COMMIT whenappropriate..

25 It is promised for the next release.

Page 62: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 14

Data Manipulation.

3.1.6. INSERTing Data and Constraints

The INSERT Statement26, when VALUES is specified, adds ONE new row to a table. the position inthe table where the new row will be inserted is undefined.

To add 1000 different rows to a table using INSERT requires 1000 spearate INSERT Statements - not avery sensible way to go about things. To enter data into a table from outside of the database youwould normally use one of the implementor provided database load products (for instance the IngresCOPY command) or write your own including an embedded INSERT statement.

In the following exercises INSERT statements are use to populate the tables created in previousexercises.

To demonstrate the use of column names in an INSERT statement, the following exercise is slightlydifferent but the effect is the same.

Exercise 3.17 - To populate the TUTOR Table.

l Type :

INSERT INTO TUTOR (TUTOR_NO, TUTOR_NAME) VALUES (1, 'NEWTON');INSERT INTO TUTOR (TUTOR_NO, TUTOR_NAME) VALUES (2, 'EINSTEIN');

l Use an appropriate SELECT Statement to check what you have entered.

If you specify a column list but didn't name every column in the table, then the columns notspecifically referenced are set to their default value.

Exercise 3.18 - Attempt to place a row with a duplicate TUTON_NO in the TUTOR Table.

l Type :

INSERT INTO TUTOR (TUTOR_NO, TUTOR_NAME)VALUES (1, 'HEISENBERG');

l Note the error message. This arises because of the UNIQUE constraint on the columnTUTOR_NO

26 Referred to in MS Access as an Append Query.

Page 63: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 15

If data is being entered in the order that the columns are defined within the table, then the columnnames can be omitted - though this is not good practice.

Exercise 3.19 - To populate the STUDENT Table.

l Type :

INSERT INTO STUDENT VALUES ('ANNE', 1);INSERT INTO STUDENT VALUES ('BOB', 1);INSERT INTO STUDENT VALUES ('EDWINA', 2);INSERT INTO STUDENT VALUES ('FRED', 1);INSERT INTO STUDENT VALUES ('JIM', 2);

l Use an appropriate SELECT Statement to check what you have entered.

Note that since column names are not used in this example, a value must be provided for every column.

Further note that because of the referential constraint, its not possible to input a student for a tutorthat does not exist:

Exercise 3.20 - Attempt to add a student with an incorrect TUTOR_NO:.

l Type :

INSERT INTO STUDENT VALUES ('HELEN', 3);

l Note the error message naming the referential constraint.

However, it is possible to input a student who doea not have a tutor at all. However, as we were notinserting data into al l the columns of the table a column list must be specified to indicate whichcolumns are being populated.

Exercise 3.21 - Add a student with a null TUTOR_NO:.

l Type :

INSERT INTO STUDENT (STUDENT_NAME) VALUES (‘ROBERT’);

l Note that the row is accepted.

Note that the way that SQL handles insert is:

l Construct an empty row

l Initialise columns to their default values

l Update those values where appropriate with values supplied by the Insertstatement

l Check any constraints that are affected by the row

l Insert the row into the database.

Page 64: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 16

Had we wanted to ensure that every student hada tutor we would have had to put a NOT NULLconstraint on the TUTOR_NO column of the STUDENT table. However, we cannot do that nowbecause some rows of the student table would violate the constraint:

Exercise 3.22 - Attempt to add a constraint to the student table to ensure that TUTOR_NO is not null:.

l Type :

ALTER TABLE STUDENTADD CONSTRAINT TUTOR_NO_IN_STUDENT_NOT_NULL

CHECK (TUTOR_NO IS NOT NULL)

l Note that the constraint is rejected.

3.1.7. Inserting multiple rows into a table

The INSERT satement lso allows data from a query expression to be inserted into a table. Such astatement is able to move large numbers of rows. The following exercise illustrates creating andpopulating a new table from the STUDENT and TUTOR table by creating a new table and then usingINSERT to place rows into in:

Exercise 3.23 Create and populate a table of student names with their respective tutor names.

l Type :

CREATE TABLE STUDENT_TUTORS(STUDENT_NAME CHAR(12), TUTOR_NAME CHAR (12));

l This creates a new table

l Populate the new table using a query expression :

INSERT INTO STUDENT_TUTORSSELECT STUDENT_NAME, TUTOR_NAMEFROM STUDENT S JOIN TUTOR T

ON S.TUTOR_NO = T.TUTOR_NO;

l Verify the result :

SELECT * FROM STUDENT_TUTORS;

Page 65: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 17

3.1.8. The UPDATE Statement.

The values in existing rows in a table may be changed using an UPDATE Statement. The followingexamples show what is possible - the first is a single row change, while the second is a multiple rowchange.

Exercise 3.24 - Einstein has left to work on his General Theory of Relativity. He is to be replaced byHeisenberg.

l Type :-

UPDATE TUTORSET TUTOR_NAME = 'HEISENBERG'WHERE TUTOR_NAME = 'EINSTEIN';

l Use an appropriate SELECT statement to check the result.

The previous exercise actually would amend a l l rows in the TUTOR table for which the predicateTUTOR_NAME = 'EINSTEIN' was true. Had there been more than one EINSTEIN, now they wouldal l be replaced by HEISENBERG! For this reason, it is highly desirable that every table has acolumn or set of columns that always perform the function of being a unique key to the table. Inrelational terminology, this is saying that every table should have a Primary key.

The table TUTOR is also the target of a a referential constraint from the table STUDENT. Thisfurther constrain updates to the TUTOR table. For example we cannot change the TUTOR_NO if thatTUTOR has any STUDENTs:

Exercise 3.25 - Attempt to alter the TUTOR_NO of a TUTOR with students

l With a suitable select statement, verify that the TUTOR whose TUTOT_NO is 1 hassome STUDENTs :

l Attempt to update the TUTOR’s TUTOR_NO :

UPDATE TUTORSET TUTOR_NO = 3WHERE TUTOR_NO = 1;

l Note that the update is rejected.

The above update was rejected. However, the Intermediate level of the SQL Standard does allow forsuch updates; rules, referred to as Referential Actions specify the actions on rows in the referencingtable of the referential constraint. Actions include cascading the update or setting the referencingvalues to null. Such actions can be specified in Microsoft Access when a relationship id defined on theRelationship diagram and the Enforce Integrity box is checked.

Page 66: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 18

The following exercise illustrates using an expression in an update statement. Note that had theWHERE clause been omitted in the update statement, the update would have applied to a l l rows inthe table.

Exercise 3.26 - Increase all salaries except the company President’s by 10%.

l Type :-

UPDATE EMPSET SAL = SAL + SAL/10WHERE JOB <> ‘PRESIDENT’;

l Use an appropriate SELECT statement to check the result.

Note that the expression in the Set Clause in Entry Level SQL92 cannot be a scalar subquery thoughsome products (but not Ingres) support it. It is allowed in Intermediate level SQL-92.

This is not an exercise - it does not work in Ingres but illustrates how a subquery could be used. theproblem is:Increase the President’s by 10% plus the average of all employee saleries that are over $2000

UPDATE EMPSET SAL = SAL + SAL/10 +( SELECT AVG(SAL)

FROM EMPWHERE SAL > 2000)

WHERE JOB = ‘PRESIDENT’;

To do the above in Ingres requires the use of a temporary table;

Exercise 3.27 Increase the President’s by 10% plus the average of a l l employee saleries that are over$2000 in Ingres

l Type :-

CREATE TABLE T ASSELECT AVG(SAL) AS ASAL FROM EMP WHERE SAL > 2000;

UPDATE EMPFROM T

SET SAL = SAL + SAL/10 + ASALWHERE JOB = ‘PRESIDENT’;

DROP TABLE T;

Page 67: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 19

3.1.9. The DELETE Statement.

The DELETE statement deletes rows from a table.

WARNING.

If the WHERE clause is omitted from the DELETE statement,ALL ROWS IN THE TABLE ARE DELETED27.

It is recommended that you do a SELECT with an appropriate WHERE clause as a check before doingthe delete.

Exercise 3.28 - To remove all Heisenberg's Students.

l Step 1 - Enter the following SELECT to check the WHERE Clause :-

SELECT STUDENT_NAME, TUTOR_NOFROM STUDENTWHERE TUTOR_NO IN (SELECT TUTOR_NO FROM TUTOR WHERE TUTOR_NAME = 'HEISENBERG');

l Results displayed :-

STUDENT_NAME TUTOR_N O

EDWINA 2

JIM 2

l Step 2 - Enter the following :-

DELETEFROM STUDENTWHERE TUTOR_NO IN (SELECT TUTOR_NO FROM TUTOR WHERE TUTOR_NAME = 'HEISENBERG');

l Now type :-

SELECT * FROM STUDENT;

There should be no students for Tutor 2 left in the table.

27 Its after mistakes like this that you regret setting AUTOCOMMIT ON.

Page 68: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 20

DROPing schema objectsThe command used within SQL to delete an object descriptor is DROP.

3.1.10. Dropping a constraint

A constraint is dropped by referencing its constraint name. There is a mandatory option, referred toas the drop-option which specifies how this drop statement affects other objects. Its particularlyrelevant in the case of constraints but has more significance when dropping other objects. Theoptions are:

RESTRICT The DROP operation fails if the object to be dropped is usedelsewhere in the schema.

CASCADE The DROP operation succeeds but will cause other objects tobe dropped also if the other object references the droppedobject..

RESTRICT is the safe option; use CASCADE only if you know what you are doing!

Exercise 3.29 - drop t h e system-named UNIQUE constraint on t h e table TUTOR invoking t h eCASCADE option

l View the existing constraints :-

SELECT * FROM IICONSTRAINTS;

l Drop the constraint:-

ALTER TABLE TUTOR DROP CONSTRAINT “insert the system defined constraint name “

CASCADE;Note that the constraint name had to be enclosed in double quotes as it contained thespecial character $

l View the constraints existing now :-

SELECT * FROM IICONSTRAINTS;

Note that the referential constraint on the STUDENT table has been dropped aswell. This is because it depended on the UNIQUE constraint on the TUTOR table.

Dropping constraints will not affect data in the data base. However, dropping tables will of coursedelete the data within them

Page 69: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 21

3.1.11. The DROP TABLE Statement.

the DROP TABLE statement deletes al l the data in the named table and deletes the table’sdescriptor from the catalog. Note that the DROP VIEW statement drops a view definition. It has noeffect on the data contained in base tables.

Exercise 3.30 - To drop the student tables in the Database.

l Type :-

DROP TABLE STUDENT;

followed by :_

DROP TABLE TUTOR;

However, note one important and possibly far-reaching effect of the DROP TABLE statement inIngres. If you DROP a table (or a view), the system automatically assumes the CASCADE option anddrops any dependent views. It does this without warning or advising you it has done it. It is thereforealways advisable to save your view definitions in a file. You can then reinstate them if you need to28.

If you have Ingres view definitions already that are not saved in a file, you can in Ingres get the textof the view definition using the command HELP VIEW viewname.

Exercise 3.31 - To view the view definition for the view EMPV1

l Type: -

HELP VIEW EMPV1;

28 Systems differ in their behavior. SQL Schema manipulation statements are defined in Intermediate levelSQL-92. There the options are CASCADE (i.e drop dependent objects) or RESTRICT (reject the DROP if there aredependent objects). Oracle and to an extent MS Access implement a probably more useful third option. Thedescriptors of dependent objects remain (but are unusable); they become usable again if the objects on which theydepend are reinstated.

Page 70: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 22

Functions in SQL Statements.

A Function is a procedure that returns a result29. We have already seen the use of some functions in anSQL SELECT statement.

There are two types of function, aggregate functions and non-aggregate functions.

Aggregate functions operate on columns of values within a table (or groups within a table), where asnon-aggregate functions operate on individual values. The SQL standard defined the followingaggregate functions:

AVG - Compute the average of a column of numbers

COUNT - Count the number of rows in a table.

MAX - The maximum of a column of numbers

MIN - The minimum of a column of numbers, and

SUM - The sum of a column of numbers.

Aggregate functions are applied to entire table if GROUP BY is not specified30; otherwise, thefunction is applied to each of the groups of the table, as constructed using GROUP BY.

All SQL implementations have additional non-aggregate functions which perform similarly,although details may differ. Most, if not all , are concerned with the conversion of data forpresentation purposes: for example integer to string conversion. For embedded SQL, many of these maybe implemented in the host language, with the probable exception of dates. This section illustratesvarious Ingres functions. However, for a full list of functions and a detailed description of theiraction, see the relevant product manual; for Ingres this would be the: SQL Reference manual.

29 This section is only concerned with simple functions whose definition is contained within the SQLimplementation. Many products (including Ingres) support some form of procedural capabilities, often referred toas SQL/PSM, by which users can write their own functions or procedures. SQL/PSM became an ISO standard in1996. It will be a while before products conform to the standard specification.30 The table is considered to comprise a single group.

Page 71: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 23

3.1.12. Ingres Character Functions31.

Because comparison operations on SQL data are case-dependent32, if character columns are to besearched or sorted, a consistent case should be used, i.e. either all upper or all lower case.

Exercise 3.32 - To ensure a l l names in t h e EMP table are in lower case by using t h e LOWERCASEfunction.

l Type :-

UPDATE EMP SET ENAME = LOWERCASE( ENAME );

l Use an appropriate SELECT to view the data

For presentation purposes however, it is often desirable to make the initial letter a capital and therest lower case. Names are a typical of data frequently treated like this. Oracle users are fortunatein that Oracle has a function INITCAPS. Unfortuantely, Ingres does not and life if therefore a littlemore difficult.

Th following example extracts the data just converted to lowercase with initial capitals.

Exercise 3.33 - To demonstrate character functions initial capitalising the ENAME column of EMP

l Type :-

SELECT CONCAT( UPPERCASE( TRIM( LEFT( ENAME,1 ))),

RIGHT( TRIM( ENAME ), LENGTH( TRIM( ENAME )) - 1 ))FROM EMP;

l This returns the employee's names capitalised

l The TRIM function is used to remove trailing blanks and so keep the counts correct.

l If you wanted this functionallity often, it would be a good candidate for a view!

l If you also needed to allow for the possibility of the name containing leading spaces,use the function SQUEEZE instead of TRIM throughout. SQUEEZE removes leadingand trailing spaces (and compresses multiple embedded spaces).

31 All the functions in this section will exist in most products though their names will be different. TheIntermediate level of SQL-92 specifies standard names for the functions but most vendors have not got there yet.32 In MS Access, the way comparisons on character data work is an implememtation option; the default being thatcomparisons are case independent.

Page 72: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 24

3.1.13. Number (Arithmetic) Functions.

Exercise 3.34 - To demonstrate a numeric conversion function converting to money format.

l Type :-

SELECT MONEY( AVG( SAL))FROM EMPGROUP BY JOB;

which gives a correctly formatted money value but puts a $ symbol in.

l View the result

A more realistic example may be:

Exercise 3.35 - To get the same result in Yen to 3 decimal places is harder:33

l Type :-

SELECT CONCAT( 'Y', CONCAT( VARCHAR( INT2( AVG( SAL ))),

CONCAT( '.',RIGHT( CONCAT('000', VARCHAR( INT4( 1000*AVG(SAL)) -

1000 * INT2( AVG( SAL )))), 3 ))))FROM EMP

GROUP BY JOB

which gives the average salary per job title, rounded to 3 decimal places.

l View the result

33 Most implementations provide mechanisms for defining currency formats. Ingres users could set environmentvariables to specify the currency symbol and to control the precision of money amounts.

Page 73: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 25

3.1.14. Date Functions.

The DATE data type is now a standard datatype in Intermediate level SQL-92 but most of the majorDBMS vendors are still in the process of converging their implementation with the standard. Allimplementations have a range of functions associated with dates; most are functionally similar butthose functions will not necessarily have the same names as their Ingres counterpart.

The ingres function DATE translates a date as a character string into an internal date format.

Exercise 3.36 - Name the employees employed after 1st December, 1981

l Type: -

SELECT ENAME FROM EMPWHERE HIREDATE > DATE( '01-DEC-1981' );

l Check the result against the EMP table.

The following exercise illustrates the use of the Ingres function DATE_PART which extracts a fieldfrom the date:

Exercise 3.37 - List the number of employees hired in each year, starting in 1980

l Create a temporary view for use in the query

CREATE VIEW V ASSELECT ENAME, DATE_PART(‘year’, HIREDATE) AS JOINING_YEARFROM EMP;

l Select the required result

SELECT JOINING_YEAR, COUNT(*)FROM V

WHERE JOINING_YEAR >= 1980GROUP BY JOINING_YEAR.

ORDER BY JOINING_YEAR;

l Drop the temporary view

DROP VIEW V;

Page 74: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 26

NULL Values and some potential pitfalls.

The term "Null Value" is a little innaccurate. A column in SQL is described as being either allowingnull values (the column is said to be nullable) or not allowing null values (the column is said to be non-nullable). The value in a row of a nullable column can be NULL. NULL is a value that is differentfrom any value that the column could otherwise legitimately take; it represents the absence of avalue, either because it doesn't exist or is irrelevant. As such, it really represents a state rather thana value.

In general, it is wise if possible to avoid designing SQL databases that use nulls. In particular itshould be noted that null values have very specific effects in predicates and expressions. Even quiteexperienced SQL practitioners often make mistakes in complex expressions or predicates that involvenulls.

Nulls in FunctionsMost functions ignore nulls.

Nulls in ExpressionsIn general, the result of any expression that involves a null value is NULL.

Nulls in PredicatesEspecially note that in predicates, SQL operates on a 3-valued rather than a conventional 2-valued logic. Comparison predicates, when one of the values involved is NULL return thevalue Unknown rather than True or False. Unknown is neither True or False.

Page 75: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 27

3.1.15. NULL predicate

As any NULL value in a predicate returns Unknown, a special predicate is required to identify rowsbased on null values. It is NOT valid syntax to write a predicate in a WHERE clause like:

WHERE COLUMN = NULL34

The following example illustrates the Null predicate

Exercise 3.38 - Using the NULL predicate: find those employees who do not get comission

l Type :-

SELECT * FROM EMPWHERE COMM IS NULL;

Confirm that you could not have done the previous exercise using a comparison predicate:

Exercise 3.39 - Illustrating incorrect use of NULL in a comparison predicate. Find those employeeswho do not get comission

l Type :-

SELECT * FROM EMPWHERE COMM = NULL;

l Note that Ingres deems this to be invalid. This is because NULL is not a permittedliteral value, it is a state.

34 In MS Access, if a criteria of = Null is entered, it is translated to Is Null. This is done to try and hide thecomplexities of Null from the naive database user.

Page 76: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 28

3.1.16. Null values in expressions and the IFNULL35 function

It cannot be over emphasised that nulls can cause unexpected problems. In particular, Any null valueeveluated during the course of an expression causes the result of that expression to be the null value.

Confirm that you cannot award everybody an extra 100 pounds commission with a simple update

Exercise 3.40 - Illustrating t h e effect of NULL in an expression. Award a l l employees an extra 100pounds comission.

l Type :-

UPDATE EMP SET COMM = COMM + 100

l Verify that those previously with a null commission still have a null commission. This is despite the fact that Ingres said that 14 rows were updated!

Fortunately, Ingres provides the IFNULL function that allow a value to be checked for null and asubstitute value to be used in its place. The following erercise verifies that:

Exercise 3.41 - Award everyone an extra 100 pounds commission.

l Type :-

UPDATE EMP SET COMM = IFNULL( COMM, 0) + 100

l Verify that those previously with a null commission now have a comission of 100.

35 The SQL standard, in Intermediate level, provides this capability via the function COALESCE.

Page 77: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 29

3.1.17. The effect of null values in functions

Exercise 3.42 - To investigate the consequences of 'NULL Values' on the COUNT function.

l There are 14 rows in the EMP table. This can be checked as follows :-

l SELECT COUNT (*) FROM EMP;

l Since KING is the president of the company, he doesn't have a manager. Therefore inthe row WHERE ENAME = 'king' the column MGR is NULL.36

l SELECT COUNT (MGR) FROM EMP;

returns a count of 13; only those rows where MGR is not null are counted.

l Only salesmen have commissions and therefore al l other rows have the COMMcolumn NULL.

l SELECT COUNT (COMM) FROM EMP;

returns a count of 4. Here only rows WHERE COMM IS NOT NULL are counted.

The moral of the above is 'be careful what you count'.

NULL has similar consequences on other functions.

Exercise 3.43 - To explore some further consequences of 'NULL Values'.

l First display all the commissions :-

SELECT COMM FROM EMP;

Fourteen rows are selected but only four rows display values as you would expect bynow. One commission is zero just to prove that zero and NULL are not the same. Aquick bit of mental arithmetic will give total commission as $2200.

l To confirm this, type :-

SELECT SUM (COMM) FROM EMP;

l Now type :-

SELECT AVG (COMM) FROM EMP;

which gives the answer 550. This is 2200 divided by 4, which under the circumstances,is more sensible than dividing by 14. In fact, AVG, very reasonably give the sameresult as

SELECT SUM(COMM)/COUNT(COMM) FROM EMP;

which you can verify by typing it.

36 Exercise 3.32 converted the names to lower case.

Page 78: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 30

Care must also be taken when joining tables if null values are involved. Remember, the joinoperator uses the same comparison operators as the WHERE clause - and NULLS in suchcomparisons evaluate to Unknown rather than True.37 Consider the follow as a candidate to thedetails of each employee in the company, including their manager:

Exercise 3.44 - Get employee details, including the Manager’s name.

l Type :-

SELECT E1.*, E2.ENAMEFROM EMP E1 JOIN EMP E2 ON E1.MGR = E2.EMPNO;

Note the result has only 13 rows instead of the perhaps 14 expected if you count them:

l Type :-

SELECT COUNT(*) FROM E1

The Missing row is of course that of the president - KING, who has no manager. SQL has somespecial join operators to handle such situations - an Outer Join.

37 getting fewer rows than expected is a very common problem in MS Access where the interface is such that it isvery easy to define tables and to build complex queries without engaging one’s brain. Generally users only noticethe problem in extreme cases - usually when they unexpectedly get no rows returned. You have to be very carefulwhen using complex joins.

Page 79: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An Introduction to SQL - Part 3.

8 March 2000 Page 31

Outer Joins

An Outer Join can be used to preserve rows from a table where there is no corresponding row in theother table. Consider the above example:

Exercise 3.45 - Get employee details, including the Manager’s name.

l Type :-

SELECT E.ENAME, E.JOB, E.MGR, M.ENAME AS MANAGERFROM EMP E LEFT JOIN EMP M ON E.MGR = M.EMPNO;38

Note the result now has 14 rows; The manager’s name for Mr King is null.

Note that The Left Outer Join operator and the Right Outer Join operator are symmetric.

Exercise 3.46 - Show that Left and Right Outer Joins are symmetric.

l Type :-

SELECT E.ENAME, E.JOB, E.MGR, M.ENAME AS MANAGERFROM EMP M RIGHT JOIN EMP E ON E.MGR = M.EMPNO;

Note that the result is the same as the previous query.:

The final Outer Join operator is the Full Outer Join which preserves rows from both tables - thoughthis is not often used.

An interesting (and useful) use of an Outer Join is to locate rows in one table that have nocorresponding rows in another table.

Exercise 3.47 - Locate those Departments that do not have any Employees

l Type :-

SELECT DEPT.*FROM DEPT.LEFT JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNOWHERE EMP.DEPTNO IS NULL;

Check the results by looking as the DEPT and EMP tables.

38 Note that the correlation names E1 and E2 are necessary in this query as the same underlying table is being usedin two different contexts.

Page 80: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

AnIntroduction

toSQL.

Part 4.

Simple Embedded SQL.

This section is only relevant if you intend to write programs in alanguage such as Fortran, Pascal, C, etc. that access SQLdatabases.

This section is not relevant if using Access Basic or Visual Basic.

Page 81: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 2

4.1. Introduction.

Until now, we have executed SQL statement interactively. In practice, there are four waysmanipulating data in SQL databases:

l Executing the SQL statements interactively

l Putting the SQL statements into a file and executing them in a batch mode

l Embedding the SQL statements in some Fourth Generation language

l Embedding the SQL statements in a conventional programming language.

The first, directly executing SQL statements, is what we have been doing until now. It is suitable forsimple routine operations, for ad-hoc queries or for one-off database maintenance functions.

The second, executing a file of SQL statements, is similar to the first. It is a useful facility for simpleoperations that are regularly undertaken. However, if one attempting to write "Standard conformingSQL", then one is quite limited. Standard SQL does not include statements to control the flow ofexecution (" i f" statements or "loops")1, only simple straight forward operations can be carried out. However, Ingres has considerable extensions in this area. Ingres SQL procedures, an extension toStandard SQL provide facilities for handling error conditions as well as flow control statements.

The third refers to a Fourth Generation language; Most SQL environments have such a language (inIngres it is ABF); they enable (some) classes of application to be developed very quickly. The fourthGeneration language will provide the procedural capabilities to write more general applications. However, as these languages vary from implementation to implementation, such applications willnot be portable.

The fourth refers to using SQL in conjunction with a conventional programming language such as C,Fortran or Cobol. The host language provides the procedural capabilities while the SQL statementsmanage the data. This interface is standardised (both the SQL part and the host language part) andis the only way in which portable general-purpose SQL applications can be built.

The general methodology for building embedded host language programs may be summarised asfollows:-

1) Write a program (or module) containing one or more embedded SQL statements. Thisprogram will consist of both host language and SQL statements.

2) Pre-compile the module - this converts the SQL statements into (a large number of)host language CALL statements. The resultant program has no SQL languagestatements (usually they are retained but commented out). This program shouldconform to the language standard for the host programming language.

3) Compile the resulting host language program using the ordinary host languagecompiler.

4) Link the output from the compilation with other modules (some from the databasemanagement package and some from other libraries) to form an executable program.

5) Run program and repeat the above from step 2 to debug.

1 Though most implementations now provide the capability in the form of SQL procedures

Page 82: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 3

In general it should be noted that there is a considerable amount of administrative work to be donebefore actually executing any SQL statements that manipulate data (e.g. CONNECTing to andDISCONNECTing from the required database, defining the various communication areas andspecifying code to handle exceptions). This overhead is a l l taken care of by the system normallywhen directly executing SQL statements interactively but must be explicitly coded when usingembedded SQL.

A significant difference between developing applications using a Fourth Generation language and aconventional language such as is described in this section is that in the Fourth Generation languageenvironment, applications do not have to be separately pre-compiled, compiled and linked. Thismakes the development of applications much quicker. It cannot be over stressed that if at all possible,the initial choice for application development should be to use the Fourth Generation languagerather than embedded SQL. Subject to a suitable Forth Generation language being available,embedded SQL should only be the first choice if application portability (to another vendors DBMSsystem) is an over-riding requirement.

The examples that follow illustrate the use of embedded SQL and are based on Ingres practice usingthe programming language Fortran. There is no guarantee that any of the detail applies to otherimplementations, although the principles will be applicable.

Although all the programming is in Fortran, no particular knowledge of the language is required forthis course.

The program demonstrated is an extremely simple one. It allows the input of an employee name andreturns the corresponding salary. It then loops for further employees with the same name. When allthe employees of that name are exhausted, it prompts for a new employee name.

Page 83: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 4

4.2. The Components of a Program Module.

4.2.1 Comments on Style and Debugging

The way in which the SQL language is converted into the host language by the pre-processor and theway in which the generated code handles exceptions is an anathema to al l practitioners of structuredprogramming. Essentially the pre-processor just processes sequentially through the source program ina single forward pass. It does not recognise the concept of "scope" or "program structure". More over,many programming languages themselves impose restrictions on how and where global communicationareas and global variables are defined and used. The embedded SQL programmer must be aware ofboth the SQL and host language restrictions if they are to write programs successfully.

Likewise, the error handling mechanism put into place by the embedded SQL statements do notadhere to good structured programming style; it uses goto statements for such things as error exits. Especially if the program contains subroutines, it is easy for the novice or careless programmer to getinto a mess.

While it may be of interest to look at the code generated by the SQL pre-compiler, there is no need toin terms of interpreting the application for debugging purposes. It is always better to use the original,cleared source code. The reason for this is that should the DBMS implementation change in any way(for example the introduction of a new release) it will be necessary to re-preprocess you applications2. If you have not kept the original source up-to-date, once re-preprocessed and recompiled, youapplications will not run correctly.

However, it is usually necessary to reference the pre-compiled code to interpret host languagecompilation error messages. This because host language compilers frequently refer to line numbers toidentify errors and these of course are line numbers in the precompiled rather than the originalprogram.

2 There are a number of notes later the this section indicating why this is.

Page 84: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 5

4.2.2 The DECLARE SECTION.

Host variables are program variables which can be referenced in both SQL statements and programstatements. They are the means by which information can be passed between SQL and the hostprogram.

The rules for SQL state that such variables must be declared in a DECLARE SECTION. Generallythese must be at the beginning of the module or procedure. In al l events, variable declarations mustappear textually before they are used.

The DECLARE Section begins with the statement :-

EXEC SQL BEGIN DECLARE SECTION;

and ends with the statement :-

EXEC SQL END DECLARE SECTION;

Within SQL statements, host variable names are preceded by a colon (:). For example, in section 4.2.4,Declare cursor, the where clause

WHERE deptno = :deptno

is comparing the value in the column called deptno with the value in a host variable called :deptno.

Examples of the DECLARE SECTION are in subsequent sections.

Page 85: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 6

4.2.3 SQL Declare sections for database tables - DCLGEN Utility

A common requirement is to have host variables that correspond to the columns in a table. Whilethese can of course be declared using the SQL DECLARE section as part of a program, Ingres has autility program, DCLGEN that produces the appropriate code to be included in an SQL DECLAREsection3. The utility program writes the SQL DECLARE compatible code to a file and that file canthen be INCLUDEd by the pre-processor into the program at the appropriate point by using thespecial command

EXEC SQL INCLUDE 'file_name'

An example of a call to the utility for the table EMP in the database sqltxx for Fortran'77 is asfollows:

dclgen -f77 fortran sqltxx emp emp.dcl g_

Here, emp.dcl is the name of the file that is generated. g_ is a prefix added to al l the generatednames4. The use of this utility is illustrated in section XX. The output from the utility is in fileemp.dcl and used in the example and is shown below. Note that the first part which is copieddirectly from the table’s definition is all comment, included for documentation purposes only

C Description of table emp from database sqlt01EXEC SQL DECLARE emp TABLE

1 (empno smallint not null, 1 ename char(10), 1 job char(9), 1 mgr smallint, 1 sal smallint, 1 comm smallint, 1 deptno smallint, 1 hiredate date)

integer*2 g_empnocharacter*10 g_enamecharacter*9 g_jobinteger*2 g_mgrinteger*2 g_salinteger*2 g_comminteger*2 g_deptnocharacter*25 g_hiredate

The relevant code is included into the fortran program as:

EXEC SQL BEGIN DECLARE SECTIONEXEC SQL INCLUDE 'emp.dcl'EXEC SQL END DECLARE SECTION

3 Other vendors provide equivalent utilities.4 The prefix is to provide unique names should you want to have multiple copies of the variables within a singl;eprogram

Page 86: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 7

4.2.4 Declaring the SQL Communications Area SQLCA.

The SQLCA is a standard communication area required by the DBMS. It is used to pass requiredinformation between the host language application and SQL. The SQLCA, unlike the variables thatappear in the DECLARE SECTION is of a fixed format and so its definition can be included by thepre-compiler rather than by the user5. The following statement tells the pre-compiler to include theSQLCA:

EXEC SQL INCLUDE SQLCA;

5 An SQLCA’s format is determined by the specific SQL implementation. If the SQL implementation changes, forexample because a new major release of the DBMS software has been installed, then the SQLCA may indeedchange. Using an incompatible SQLCA will produce inderterminate results. If a new version of the DBMSsoftware is installed it is essential that any subprograms that use an SQLCA are re-preprocessed and recompiled.

Page 87: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 8

4.2.5 The DECLARE CURSOR Statement.

An SQL statement such as a SELECT statement manipulates a set of rows in general, rather than just asingle row. Most conventional host languages are unable to manipulate sets of objects, only singleobjects (mainly because most conventional host languages were developed when files rather thandatabases were the norm!). SQL uses a Cursor mechanism for mapping between sets of rows that SQLstatements manipulate and the single rows that can be passed to and from host languages6. A Cursordefines, by means of a SELECT statement, a set of records that can manipulated. A Cursor must beDECLAREd and later OPENed. The following is the syntax of a DECLARE CURSOR statement:

EXEC SQL DECLARE cursor_name CURSOR FOR

SELECT ... FROM ...

A Cursor definition, unlike a View Definition can include an ORDER BY clause.

The following is an example of a cursor definition that defines a cursor called c1:

EXEC SQL DECLARE c1 CURSOR FOR select ename, job from emp where deptno = :deptno

order by ename;

An application can open a number of cursors, including multiple cursors on the same table - though insome cases it is necessary to read the reference manuals very carefully to be confident of predicting theeffect of cursors whose target row sets overlap.

6 MS Access Basic has a similar construct but it is referred to as a RecordSet. In Access Basis a RecordSet isdefined using the Access Basic command OpenRecordSet which performs the function of declaring and opening acursor. The major parameter of OpenRecordSet is a Select Statement (MS Access Select Query) that defines the setof records to be processed.

Page 88: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 9

4.2.6 The OPEN CURSOR Statement.

Once a cursor has been declared, it must be opened before being used. Opening a cursor effectivelycauses SQL to identify (and possibly pre-fetch) the rows identified in the DECLARE CURSOR. TheOPEN CURSOR statement takes the form :-

EXEC SQL OPEN cursor_name;

Note, OPENing the cursor is the action that really sets the cursor up. It is just a historic quirk of thelanguage that there is a DECLARE CURSOR at all. Hence, in the example application, the cursor isOPENed *after* the value for the employee name is read in.

4.2.7 The FETCH Statement.

The FETCH statement is the statement that actually retrieves data from the database. It retrievesthe next row from the named cursor7. It is only valid to FETCH data after the cursor has been declaredand opened. The FETCH Statement takes the form :-

EXEC SQL FETCH cursor_name INTO list_of_host variables;

Each execution of the FETCH places a single row of data into the specified list of host variables. These variables were the ones defined in the DECLARE SECTION. In the FETCH statement, thenumber and data types of the host variables must match the number and types of the columns definedin the DECLARE CURSOR statement.

4.2.8 The CLOSE CURSOR Statement.

The CLOSE CURSOR statement closes a cursor. Before the cursor can be re-used it must be re-opened. Closing a cursor releases system resources so it is advisable to close cursors when processing of thecursor is complete. Note that closing a cursor and re-opening it effectively re-selects the rows andplaces you back at the beginning of the cursor. The format of CLOSE CURSOR is:-

EXEC SQL CLOSE cursor_name;

7 MS Access Basic works slightly differently. The Current row or a RecordSet may be accessed directly using thecolumn name qualified by the RecordSet name. The explicit command MoveNext is used to make another rowcurrent.

Page 89: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 10

4.2.9 Example cursor handling code

Example code to handle the opening of a cursor and the subsequent fetching of rows is shown in thefollowing example Fortran program:

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc Subroutine procname EXEC SQL INCLUDE SQLCA

EXEC SQL BEGIN DECLARE SECTION character*10 ename character*100 errmsg EXEC SQL INCLUDE 'emp.dcl' EXEC SQL END DECLARE SECTION

EXEC SQL DECLARE empcsr CURSOR FOR 1 SELECT * from emp 2 WHERE ename = :ename

EXEC SQL WHENEVER SQLERROR GOTO 300

10 type *, ' ' type *, 'Enter name of employee or STOP to end:' ename = ' ' accept *, ename if ((ename .ne.'stop') .and. (ename .ne.'STOP')) GOTO 15 type *,'closing session' return

15 EXEC SQL OPEN empcsr

c first employee EXEC SQL WHENEVER NOT FOUND GOTO 200 EXEC SQL FETCH empcsr INTO :g_empno, :g_ename, :g_job, :g_mgr, :g_sal, 1 :g_comm, :g_deptno, :g_hiredate

print *,'Salary for ', g_ename, ' is ', g_sal

c subsequent employees20 EXEC SQL WHENEVER NOT FOUND GOTO 100 EXEC SQL FETCH empcsr INTO :g_empno, :g_ename, :g_job, :g_mgr, :g_sal, 1 :g_comm, :g_deptno, :g_hiredate print *,'Salary for ', g_ename, ' is ', g_sal goto 20

100 print *, 'No more employees with name:', ename EXEC SQL CLOSE empcsr goto 10

200 print *, 'Employee ', ename, ' not found' EXEC SQL CLOSE empcsr goto 10

Page 90: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 11

300 EXEC SQL INQUIRE_SQL (:errmsg=ERRORTEXT) print *, 'Aborting because of an error' print *,errmsg EXEC SQL DISCONNECT stop endccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

Page 91: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 12

4.3. Building a program

4.3.1 File naming conventions

Your files containing your original fortran program, the pre-processed program and the object programshould have default extensions. The extensions that should be used are:

Fortran program with embedded SQL xxxxx.sfpre-processed fortran program xxxxx.f

4.3.2 Job control statements.

The following are the job control statements required to pre-process, compile, link and run an fortranprogram whose name is xxxxx on holyrood8.

to preprocess:esqlf xxxxx

to compile/link:f77 -o xxxxx xxxxx.f -L $II_SYSTEM/ingres/lib -linterp.1 -lframe.1 -lq.1 \

-lcompat.1 -lm -lc -lsocket -lelf

8 The statements required to compile and particularly link an ingres program will vary dependingon the platform and the version of Ingres used.

Page 92: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 13

4.3.3 Building an application: Connecting to and Disconnecting from theDatabase.

A application must be connected to the database. The following is a typical way of doing this. Notethe use of the DECLARE SECTION to define a variable to pass the database name. This is necessaryfor this application as you are all using separate databases.

Note also the code for disconnecting from the database when the session ends.

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc EXEC SQL INCLUDE SQLCA

EXEC SQL BEGIN DECLARE SECTION character*20 dbname EXEC SQL END DECLARE SECTION

EXEC SQL BEGIN DECLARE SECTION EXEC SQL INCLUDE 'emp.dcl' EXEC SQL END DECLARE SECTION

c Prompt for the database name type *, 'Enter database name:' accept *, dbname call opendb(dbname)

call procname

EXEC SQL WHENEVER SQLERROR STOP EXEC SQL DISCONNECT type *, 'normal completion' stop

end

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc Subroutine Opendb(dbname) EXEC SQL INCLUDE SQLCA

character*20 dbname

EXEC SQL WHENEVER SQLERROR STOPc Declared to SQL in the first subroutine EXEC SQL CONNECT :dbname type *, 'database connected successfully' endccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

Page 93: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 14

4.4. The SQL Module.

The example for this course is shown on the next page. The code is available in file9:sql_db/fprog.sf

Copy this to your own account cp sql_db/fprog.sf fprog.sf

Define the include file for the table EMP:dclgen -f77 fortran xxdbname emp emp.dcl g_

(where xxdbname is the name of your database)

Pre-process it10:esqlf fprog

Compile/link it:f77 -o fprog fprog.f -L $II_SYSTEM/ingres/lib -linterp.1 -lframe.1 -lq.1 \

-lcompat.1 -lm -lc -lsocket -lelfRun it:

fprog

You may inspect the pre-processed code - though this is also shown over the page.You may care to amend the cursor definition to use a LIKE predicate rather than a comparisonpredicate.

9 In the training account, the directory sql_db is a symbolic link to ~ingres/ingtrain/sql_db. If you wish to accessthe Fortran code from your own directory on Holyrood, then you would either have to define the symbolic linkyourself or use the full file name ~ingres/ingtrain/sql_db/fprog.sf10 The output from the preprocessing process is likely to change in subtle ways between releases of the DBMSsoftware. When a new version of the DBMS software is installed it is essential to re-preprocess the originalprogram to generate the new code. Just recompiling the original pre-procesed code will include the new version ofincluded source text such as SQLCA but will not implement any changes in parameters required by the new releaseof the DBMS. This may well lead to subtle and hard-to-detect errors when the program executes.

Page 94: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 15

4.4.1 Fortran code with embedded SQL

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc EXEC SQL INCLUDE SQLCA

EXEC SQL BEGIN DECLARE SECTION character*20 dbname EXEC SQL END DECLARE SECTION

EXEC SQL BEGIN DECLARE SECTION EXEC SQL INCLUDE 'emp.dcl' EXEC SQL END DECLARE SECTION

c Prompt for the database name type *, 'Enter database name:' accept *, dbname call opendb(dbname)

call procname

EXEC SQL WHENEVER SQLERROR STOP EXEC SQL DISCONNECT type *, 'normal completion' stop

end

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc Subroutine Opendb(dbname) EXEC SQL INCLUDE SQLCA

character*20 dbname

EXEC SQL WHENEVER SQLERROR STOPc Declared to SQL in the first subroutine EXEC SQL CONNECT :dbname type *, 'database connected successfully' end

Page 95: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 16

Subroutine procname EXEC SQL INCLUDE SQLCA

EXEC SQL BEGIN DECLARE SECTION character*10 ename character*100 errmsg EXEC SQL INCLUDE 'emp.dcl' EXEC SQL END DECLARE SECTION

EXEC SQL DECLARE empcsr CURSOR FOR 1 SELECT * from emp 2 WHERE ename = :ename

EXEC SQL WHENEVER SQLERROR GOTO 300

10 type *, ' ' type *, 'Enter name of employee or STOP to end:' ename = ' ' accept *, ename if ((ename .ne.'stop') .and. (ename .ne.'STOP')) GOTO 15 type *,'closing session' return

15 EXEC SQL OPEN empcsr

c first employee EXEC SQL WHENEVER NOT FOUND GOTO 200 EXEC SQL FETCH empcsr INTO :g_empno, :g_ename, :g_job, :g_mgr, :g_sal, 1 :g_comm, :g_deptno, :g_hiredate

print *,'Salary for ', g_ename, ' is ', g_sal

c subsequent employees20 EXEC SQL WHENEVER NOT FOUND GOTO 100 EXEC SQL FETCH empcsr INTO :g_empno, :g_ename, :g_job, :g_mgr, :g_sal, 1 :g_comm, :g_deptno, :g_hiredate print *,'Salary for ', g_ename, ' is ', g_sal goto 20

100 print *, 'No more employees with name:', ename EXEC SQL CLOSE empcsr goto 10

200 print *, 'Employee ', ename, ' not found' EXEC SQL CLOSE empcsr goto 10

300 EXEC SQL INQUIRE_SQL (:errmsg=ERRORTEXT) print *, 'Aborting because of an error' print *,errmsg EXEC SQL DISCONNECT stop end

Page 96: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 17

4.4.2 Fortran code after pre-processingNote how the original program statements are identified and how the embedded SQL statementsare translated within the preprocessd code.

include '/usr/local/ingres/sys/open.ingres/ingres/files/eqsqlca.f'include '/usr/local/ingres/sys/open.ingres/ingres/files/eqdef.f'include '/usr/local/ingres/sys/open.ingres/ingres/files/eqfdef.f'character*20 dbnameinclude 'emp.f'

type *, 'Enter database name:' accept *, dbname call opendb(dbname) call procname

C File "fprog.sf", Line 21 -- disconnectcall IIsqIn(sqlcax)call IIsqDi()if (sqlcod .lt. 0) then call IIsqSt(sqlcax)end if

C File "fprog.sf", Line 22 -- host_code type *, 'normal completion' stop endccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc Subroutine Opendb(dbname)

include '/usr/local/ingres/sys/open.ingres/ingres/files/eqsqlca.f'include '/usr/local/ingres/sys/open.ingres/ingres/files/eqdef.f'include '/usr/local/ingres/sys/open.ingres/ingres/files/eqfdef.f'

character*20 dbname

C File "fprog.sf", Line 36 -- connectcall IIsqIn(sqlcax)call IIsqCo(2,1,IIsd(dbname))if (sqlcod .lt. 0) then call IIsqSt(sqlcax)end if

C File "fprog.sf", Line 37 -- host_code type *, 'database connected successfully' endccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc Subroutine procname

include '/usr/local/ingres/sys/open.ingres/ingres/files/eqsqlca.f'include '/usr/local/ingres/sys/open.ingres/ingres/files/eqdef.f'include '/usr/local/ingres/sys/open.ingres/ingres/files/eqfdef.f'character*10 enamecharacter*100 errmsginclude 'emp.f'

Page 97: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 18

C File "fprog.sf", Line 56 -- host_code10 type *, ' ' type *, 'Enter name of employee or STOP to end:' ename = ' ' accept *, ename if ((ename .ne.'stop') .and. (ename .ne.'STOP')) GOTO 15 type *,'closing session' return15 continue

C File "fprog.sf", Line 67 -- opencall IIsqIn(sqlcax)call IIcsOp(IIsd('empcsr'),5562,24812)call IIwrit(0,0,0,1,32,0,IIsdno('select * from emp where ename='))call IIputd(0,0,1,32,IIslen(ename),IIsadr(ename))call IIcsQu(IIsd('empcsr'),5562,24812)if (sqlcod .lt. 0) then goto 300end if

C File "fprog.sf", Line 68 -- fetchcall IIsqIn(sqlcax)if(IIcsRe(IIsd('empcsr'),5562,24812).ne.0)then call IIcsGe(0,0,1,30,2,g_empno) call IIcsGe(0,0,1,32,IIslen(g_ename),IIsadr(g_ename)) call IIcsGe(0,0,1,32,IIslen(g_job),IIsadr(g_job)) call IIcsGe(0,0,1,30,2,g_mgr) call IIcsGe(0,0,1,30,2,g_sal) call IIcsGe(0,0,1,30,2,g_comm) call IIcsGe(0,0,1,30,2,g_deptno) call IIcsGe(0,0,1,32,IIslen(g_hiredate),IIsadr(g_hiredate)) call IIcsRt()end ifif (sqlcod .eq. 100) then goto 200else if (sqlcod .lt. 0) then goto 300end if

C File "fprog.sf", Line 71 -- host_code print *,'Salary for ', g_ename, ' is ', g_salc subsequent employees20 continue

Page 98: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 19

C File "fprog.sf", Line 75 -- fetchcall IIsqIn(sqlcax)if(IIcsRe(IIsd('empcsr'),5562,24812).ne.0)then call IIcsGe(0,0,1,30,2,g_empno) call IIcsGe(0,0,1,32,IIslen(g_ename),IIsadr(g_ename)) call IIcsGe(0,0,1,32,IIslen(g_job),IIsadr(g_job)) call IIcsGe(0,0,1,30,2,g_mgr) call IIcsGe(0,0,1,30,2,g_sal) call IIcsGe(0,0,1,30,2,g_comm) call IIcsGe(0,0,1,30,2,g_deptno) call IIcsGe(0,0,1,32,IIslen(g_hiredate),IIsadr(g_hiredate)) call IIcsRt()end ifif (sqlcod .eq. 100) then goto 100else if (sqlcod .lt. 0) then goto 300end if

C File "fprog.sf", Line 77 -- host_code print *,'Salary for ', g_ename, ' is ', g_sal goto 20100 print *, 'No more employees with name:', ename

C File "fprog.sf", Line 81 -- closecall IIsqIn(sqlcax)call IIcsCl(IIsd('empcsr'),5562,24812)if (sqlcod .eq. 100) then goto 100else if (sqlcod .lt. 0) then goto 300end if

C File "fprog.sf", Line 82 -- host_code goto 10200 print *, 'Employee ', ename, ' not found'

C File "fprog.sf", Line 85 -- closecall IIsqIn(sqlcax)call IIcsCl(IIsd('empcsr'),5562,24812)if (sqlcod .eq. 100) then goto 100else if (sqlcod .lt. 0) then goto 300end if

C File "fprog.sf", Line 86 -- host_code goto 10300 continue

C File "fprog.sf", Line 89 -- inquire_sqlcall IILQis(0,0,1,32,IIslen(errmsg),IIsadr(errmsg),63)

Page 99: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Part 4.

8 March 2000 Page 20

C File "fprog.sf", Line 90 -- host_code print *, 'Aborting because of an error' print *,errmsg

C File "fprog.sf", Line 92 -- disconnectcall IIsqIn(sqlcax)call IIsqDi()if (sqlcod .eq. 100) then goto 100else if (sqlcod .lt. 0) then goto 300end if

C File "fprog.sf", Line 93 -- host_code stop end

Page 100: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

AnIntroduction

toSQL.

Revision Examples

Page 101: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Revision examples.

8 March 2000 Page 2

1 Introduction.

This section contains a small number of problems of varying difficulty. Each question has some hintsas to how to tackle the problem.

If you are really stuck, then the file1:

sql_db/examples

contains the solutions as an SQL script. You can load this into ISQL using File ReadFile, edit it usingthe editor, and run a particular exercise.

Within the file, / * and * / delimit comments. These lines are ignored by ISQL.

1 sql_db is a symbolic link defined within the training accounts. If you wish to access these files from you yourown holyrood account you will need to either define a corresponding symbolic link or specify the full path name~ingres/ingtrain/sql_db/examples

Page 102: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Revision examples.

8 March 2000 Page 3

2 Exercises

1. Get the employee details of the longest serving employee

Hint: the longest serving employee will have the earliest hiredate. The MIN function willlocate the smallest (earliest) hire date. Put this into an appropriate subquery.

2. Get the employee details of all employees who work in the same department as james

Note: - we have previously changed all the names to lower case.

Hint: use 'james' in a subquery.

Note: Although we believe that there is only one 'james', i t is good practice to use an INpredicate here rather than a comparison predicate - just in case.

3. Get t h e employee details of a l l employees who work in t h e same department as JAMES,assuming that we don't know anything about the capitalisation of the ENAME column

Hint: Use the UPPERCASE function.

Note especially: for a large table, this would be an inefficient query. Even if there was anindex defined on the ENAME column, ingres would be unable to make use of it. It is muchbetter to hold the base data in a regular, known form.

4. How many clerks do we employ

Hint: the aggregate function COUNT(*) counts the rows in a table.

5. How many clerks do we employ that are based in DALLAS

Hint: Use an IN predicate

The following three examples are to stress how easy it is to ask the wrong question:

6. How many departments are there

Hint: the dept table contains the departments

Page 103: INTRODUCTION TO DATABASE LANGUAGE SQL · An Introduction to SQL - Part 1 1.1. Introduction. 1.1.1. Standards SQL1 is the language for defining and maintaining relational databases

An introduction to SQL - Revision examples.

8 March 2000 Page 4

7. How many departments are there with employees

Hint: The EMP table contains the department numbers that are in use. The functionCOUNT( column_name ) counts the number of entries in the specified column. Further more,the function COUNT( DISTINCT column_name ) counts the number of distinct values in thespecified column.

Note however, that even this could be problematic. Suppose some of the employee'sdepartments had been incorrectly coded - so that they referred to departments that didn'treally exist. They would still be counted as real departments with this solution

8. How many real departments are there with employees

Hint: use of the EXISTS or OUTER JOINpredicate to check that the department is used foremployees

It is often useful to be able to validate data to check for conditions that should not exist:

9. What employees are recorded as having departments that don't exist

Hint: use NOT EXISTS or use OUTER JOIN

10. List the employees that don’t manage anyone

Hint: useOUTER JOIN or EXISTS..

11. What is the average salary for each department with employees */

Hint: use the aggregate function AVG and the GROUP BY clause.

12. What is t h e department name and average employee salary of t h e department wi th t h emaximum average salary

Hint: this is a hard one. I suggest you try and solve this using a temporary view whichcontains the department number and its average salary.