57
1 Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/ CS 252: Fundamentals of Relational Databases: SQL1

CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

  • Upload
    buingoc

  • View
    220

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

1

Dr. Alexandra I. Cristea

http://www.dcs.warwick.ac.uk/~acristea/

CS 252: Fundamentals of

Relational Databases: SQL1

Page 2: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

2

CS252 Fundamentals of Relational Databases 2

Lecturers & Practical Work

• Hugh Darwen

• Alexandra I. Cristea

• Zabin Visram

• Adrian Hudnott

• Other invited talks?: TBA

Page 3: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

3

CS252 Fundamentals of Relational Databases 3

Schedule

• Usual:

– Mon 14:00-15:00, 15:00-16:00 seminars (Z. Visram)

– Tue 14:00-15:00 (Fundamentals, H. Darwen)

– Thu 13:00-14:00 (SQL, A. Cristea)

– Fri 13:00-14:00 (H. Darwen)

• Exceptions:

– Others: TBA: check forum, website, course

Page 4: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

4

CS252 Fundamentals of Relational Databases 4

Slides, acknowledgements and thanks

• Thanks to:– Mr. Tim Heron

• http://www.tim-heron.com/

– Dr. Richard Cartwright • (former undergraduate, doctoral student and lecturer at Warwick)

– Dr. Paul Goldberg:• http://www.dcs.warwick.ac.uk/people/academic/Paul.Goldberg/cs319/cs319index.html

– Dr. Meurig Beynon:• http://www.dcs.warwick.ac.uk/people/staff/Meurig.Beynon/

– Dr. Ad Aerts:• http://wwwis.win.tue.nl/~aaerts/

– Prof. Dr. Paul De Bra:• http://wwwis.win.tue.nl/~debra/

– Others: mentioned directly

This course is based on material from previous CS319 courses. This is based on

material which was administered by Tim Heron; he based his SQL slides on

slides from Richard Cartwright; however, this course was given for a long time

by Meurig Beynon, so many of the corrections come from him.

Also, I was teaching databases at Eindhoven University of Technology, together

with Ad Aerts and Paul De Bra. So another part of the slides will be based on

material from there.

Other slides used here from outside sources will be mentioned directly.

Page 5: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

5

CS252 Fundamentals of Relational Databases 5

Contact

• Forum:

http://forums.warwick.ac.uk/wf/browse/category.jsp?cat=24

• IF (and ONLY IF) a question is personal, you might address

send personal email to HD or AIC

– FORMAT: subject of email should contain ‘CS252’ and topic of the

email (otherwise it will be filtered out)

Page 6: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

6

CS252 Fundamentals of Relational Databases 6

Course site(s):

• Current:

– http://www.dcs.warwick.ac.uk/~acristea/courses/CS252/

– Will contain current slides, as taught at the course

– Will contain notifications: check BEFORE & AFTER

the course

• Official:– http://www.dcs.warwick.ac.uk/undergraduate/modules/cs252.html

The current site will contain all necessary material and links to material in order

to successfully follow the course, with exception of the oral instructions at the

course itself (which are necessary for a complete understanding).

Page 7: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

7

CS252 Fundamentals of Relational Databases 7

Books• Korth and Silberschatz, Database System Concepts, McGraw-

Hill,1991.

• C. Date, "An Introduction to Database Systems", Addison-Wesley Longman, 2003 (8th ed.)

• C. Date, "Database in Depth: The Relational Model for Practitioners", O'Reilly, 2005

• C. Begg, T. Connolly, "Database Systems: A Practical Approach to Design, Implementation and Management (International Computer Science S.)", Addison-Wesley Longman, 2004 (4th ed.)

• C. Date, "The Relational Database Dictionary", O'Reilly, 2006

• Oracle and JDBC references:

• G. Reese, “Database Programming with JDBC and Java”, O’Reilly and Associates, 1997.

• Links to Oracle documentation are on the course website.

Page 8: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

8

CS252 Fundamentals of Relational Databases 8

Purpose of this course

• To show how the theory of relational algebra serves as a framework and a foundation for the efficient organisation and retrieval of large amounts of data.

• To introduce students to some standard notations (for example, SQL and Tutorial D) that implement important parts of relational algebra.

• To give students practical experience of the use and limitations of some database notations (such as SQL) that are widely used in industry and business.

Page 9: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

9

CS252 Fundamentals of Relational Databases 9

Overlaps and sequencing

• Prerequisite of

– CS253: Topics in Database Systems

Previously also: CS319: Theory of Databases (discontinued)

Page 10: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

10

CS252 Fundamentals of Relational Databases 10

Organization of the course

• 7.5 CATS

• CS, CSE, CBS, Mathematics

• 14 one-hour lectures and 5 one-hour seminars

• Exam 1.5 hours (70%) coursework (30%)

• Rules of the game:– Read also comments on the slides.

– Presence is optional, but beware: slides-only are NOT ENOUGH to learn from for the exam; you need to participate, take your own notes, read more: so self-study!

– Go to seminars to practice! Do problems on worksheets at home. Participate in the forum.

Page 11: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

11

CS252 Fundamentals of Relational Databases 11

Goal SQL Lectures:

Lectures with a practical emphasis:

� Using SQL

Page 12: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

12

CS252 Fundamentals of Relational Databases 12

Contents:

� Introduction, getting started with Oracle,

simple queries

�Formulating queries, inserting – deleting

– modifying rows

�Creating and altering tables, relational

algebra, constraints

�Views, functions, dates & times

Page 13: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

13

CS252 Fundamentals of Relational Databases 13

This lecture

• Introduction

• Module outline

• History of SQL

• Accessing Oracle

• Some SQL!

Page 14: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

14

CS252 Fundamentals of Relational Databases 14

Worksheets

Two worksheets (numbered 2 and 5) will be given at this lecture and in the lecture in week 5.

Working through each worksheet and using a database between each lecture is an important part of the course.

Each weeks lectures build on the week before plus the worksheet.

The questions in the assignment will take a form similar to the

worksheets.

This is an introductory course.

Worksheet 2: Getting Started with Oracle

Page 15: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

15

CS252 Fundamentals of Relational Databases 15

Databases Available at Warwick

IT Services Oracle Database

All examples from these lectures and the

worksheets are “verified” with the

Oracle 9i database on mimosa.

Some information on basic Oracle use is

available via the module webpage.

IMPORTANT: Please note that using your queries in SQL only ‘verifies’ the

syntax, not the correctness of your queries.

It is NOT a proof that a query works just because it works on a certain database

instance.

Page 16: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

16

CS252 Fundamentals of Relational Databases 16

Databases available at home

• Personal Oracle 9i (downloadable from http://www.oracle.com/technology/software/products/oracle9i/)

• Open source includes mySQL and PostgreSQL– MySQL (http://www.mysql.com)

– PostgreSQL (http://www.postgresql.org/)

• Microsoft SQL Server 2005 (a free version called SQL Server Express is available: http://www.microsoft.com/sql/editions/express/default.mspx)

• Microsoft Access (a long way away from ISO SQL standard)

Page 17: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

17

CS252 Fundamentals of Relational Databases 17

Background to SQL• SQL abbreviation for Structured Query Language.

• Original name was SEQUEL, correct modern pronunciation is S-Q-L.

• Used for relational databases; where relations are tables, attributes are columns, tuples are rows …

• Chronology:

• 1970s IBM - first relational database System R, then DB2. Others include:

• Ingres Database - query language QUEL

• Digital - Relational Database Operator

• ISBL - relational algebra DML

• dBase family of products for PCs

• 1979 Oracle

• 1980s First standardisation efforts.

SQL might more accurately be characterized as an ad hoc database language with

no firm logical foundation; but it was in many respects nevertheless inspired by

E.F. Codd’s Relational Model of Data the theory you learn on CS252.

Actually, the first commercial SQL product was Oracle, based heavily on an IBM

research project known as System/R. Oracle appeared in 1979. Its appearance is

considered to have propelled IBM’s release of its first SQL product, SQL/DS, in

1980. Until the appearance of Oracle IBM had been reluctant to release any SQL

product because of its huge investment in its pre-relational DBMS called IMS,

whose language, DL/1, was considered to be based on an hierarchical model of

data. (IMS is still running on some IBM mainframes to the present day.)

A less well-known commercial RDBMS made by IBM was Business System 12,

developed for its so-called “Bureau Service” (time-sharing for paying customers,

using terminals, on mainframes run by IBM itself). This was available in many

countries, but not the USA, during the 1980s until the closure of the Bureau

Service, occasioned by the advent of the PC. Business System 12 was a faithful

implementation of relational theory, modelled on ISBL.

Page 18: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

18

CS252 Fundamentals of Relational Databases 18

• 1984 ISO SQL standard - many flaws but universally adopted.

• 1992 Update to standard called SQL92 - The basic standard for any modern database

• 1999 Update to standard called SQL99 - Oracle database conforms to SQL99.

• 2003/6/7 Current standard SQL2006 - Not many databases fully support this standard yet.

• Major benefit: Virtually all relational databases can be manipulated using the same language.

• SQL combines:

• Data Description Language (DDL) - how the tables represent the data

• Query and data manipulation (DML)

• 2008? next

Actually, the first edition of the ISO (International Standards Organisation)

standard for SQL was SQL:1986. Subsequent revisions were SQL:1989,

SQL:1992, SQL:1999 and SQL:2003. The next and possibly final revision is

expected in 2008. SQL:2003 consists of nine parts of which the biggest and most

important, SQL/Foundation, occupies over 1300 pages. Other parts include ones

covering SQL’s relationships with Java and XML.

See also: http://www.standards.org.au/downloads/ABS-2005-12.pdf

Page 19: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

19

CS252 Fundamentals of Relational Databases 19

• However, there are many non-standard

extensions to SQL :

• PL/SQL - Oracle Procedural SQL

• DB2 Procedural SQL - IBM DB2

• Transact-SQL - Microsoft SQL Server

• To write portable SQL stick to standard

SQL.

Each of the “extensions” mentioned here provides a programming language for

writing user-defined functions and procedures that can be executed within the

SQL server (i.e., the DBMS) and also portions of application code. In 1996 an

extension to the ISO SQL standard was published under the title SQL/PSM

(“Persistent Stored Modules” the title is rather misleading!), defining such a

programming language. Since then the various SQL vendors have converged

reasonably well on this standard.

Page 20: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

20

CS252 Fundamentals of Relational Databases 20

Database M. Systems (DBMS)

Data is persistent - each user has their own database space - stored tables and data will remain unless modified or dropped.

Oracle uses an SQL interpreter called sqlplus as the main interface to the DBMS:Standard SQL operations

Report generation

DBMS = Database Management Systems

Page 21: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

21

CS252 Fundamentals of Relational Databases 21

Oracle at Warwick

Located on server mimosa (mimosa.csv.warwick.ac.uk).

1. Login to mimosa using ssh. ITS usercodes apply. Only use mimosa for database access.

2. Type “orasetup". This is a script that modifies your .bash profile or .profiles by adding information required for running Oracle.

3. Log out of mimosa and log back in again

4. To run the text SQL interpreter, type “sqlplus /". Remember the "/" as it represents your personal username and password for Oracle.

Confirming I've added additional users and granted CREATE SEQUENCE

privilege to role UW_CREATE_STD for all (non-CS) student accounts as

requested. Students please use your CS usercodes to logon to Oracle when

possible (for those whom have more than 1 usercodes).

For line size:

Type, SET LINESIZE <number> <return> from inside SQLPlus to get Oracle to

assume that lines up to <number> characters long can be displayed without

wrapping. Then configure whichever SSH terminal client you're using to match

this number by either adjusting its preferences or by resizing the window with the

mouse.

Page 22: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

22

CS252 Fundamentals of Relational Databases 22

Creating a TableFrom the depths of a CD collection:

Collection

1997Capital13The Colour and the ShapeFoo Fighters

1997Virgin13Urban HymnsThe Verve

1996Junior8Second Toughest in the InfantsUnderworld

1991Island12Achtung BabyU2

1988Island17Rattle and HumU2

1984Island10The Unforgettable FireU2

yearcompanytracksalbumartist

Like relations, the abstract concept of an SQL table can be depicted in the

concrete form of a tabular picture. Indeed, the picture shown on this slide could

equally well be of a relation. We will encounter several ways in which an SQL

table can violate the definition of relation, but for now just note that in SQL the

order of columns has some significance, whereas the attributes of a relation have

no particular ordering to them. Like the tuples of a relation, however, the rows of

an SQL table have no ordering.

We can interpret this particular table in the way we interpret relations, by a

predicate, which might be as follows:

The CD album, by artist, produced by company in the year year, consists of

tracks tracks.

The italicized variables of the predicate correspond to column names of the table.

Page 23: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

23

CS252 Fundamentals of Relational Databases 23

Defining the table in SQL:CREATE TABLE Collection

( artist CHAR(16),

album CHAR(40),

tracks INTEGER,

company CHAR(16),

year INTEGER );

Note:

1. Convention to write SQL keywords in CAPITALS.

2. Oracle matches lower/upper case the same in table and column names.

3. ISO Standard - all table names and column names in capitals + numbers + underscore “_".

4. Semicolon “;" terminates every input (usually).

The SQL statement (also known as a command) shown on this slide creates a variable to which the table depicted on the previous slide could be assigned (we shall see how later). The variable, named Collection, is an example of SQL’s counterpart of the relation variable (or relvar) that you learn about in the theory section of CS252. In SQL it is called a base table, often abbreviated to just table (unfortunately, as the context isn’t always clear as to whether the variable or the value is intended when this term is used).

Like a base relvar in Tutorial D, a base table continues to exist until it is explicitly destroyed. The command to destroy Collection is

DROP TABLE Collection;

The definition of the base table is very similar to the definition of a relvar in Tutorial D, but note the use of parentheses rather than braces to enclose the list of column definitions. The braces in Tutorial D indicate that the elements are unordered. As we have already mentioned, the columns of a table are ordered. Here we can (and do) say that artist is the first column, album the second, and so on.

1. the convention applies to the slides and notes used in CS252. It is a commonly used convention but is not universal. Key words in standard SQL (and all well known implementations) are case-insensitive.

2. Identifiers (such as table names and column names) are also case-insensitive in standard SQL.

3. The ISO standard uses all caps in its examples, but it’s only the convention used in that particular text. The language itself is case-insensitive. However, this shows best practice and you should use it that way.

• underscore are used in table names and column names of the standard SQL catalog.

4. The comment about semicolons is not strictly accurate. Semicolons are used to delimit nested statements within the various kinds of compound statement but a semicolon is not required at the end of an outermost statement. That said, many user interfaces that support direct entry of SQL statements do require the semicolon to be added.

Page 24: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

24

CS252 Fundamentals of Relational Databases 24

To insert the data into the table Collection use the

INSERT INTO statement:

INSERT INTO Collection

VALUES ('U2', 'The Unforgettable Fire',

10, 'Island', 1984);

INSERT INTO Collection

VALUES ('U2', 'Rattle and Hum',

17, 'Island', 1988);

For every successful insertion, Oracle reports:

1 row created.

INSERT INTO is SQL’s counterpart of Tutorial D’s INSERT; the VALUES expression is SQL’s

counterpart of Tutorial D’s RELATION { … } known as the relation selector. There are

some important points to note, however:

1. The example illustrates the point we have already made concerning the ordering of the

columns of a table. Note that the input table denoted by the VALUES expression has no

column names, so the system has to match the parenthesized elements with the columns of

the target table by position rather than by column name.

2. The two INSERT INTO statements shown on the slide each have an input table consisting of

exactly one row. The SQL standard’s definition of VALUES allows one or more rows to be

specified, simply by separating the parenthesized rows by commas. Thus, the two statements

could be combined into one like this

INSERT INTO Collection

VALUES (‘U2’, ‘The Unforgettable Fire’,

10, ‘Island’, 1984),

(‘U2’, ‘Rattle and Hum’,

17, ‘Island’, 1988) ;

However, in Oracle and many other SQL implementations the VALUES expression is restricted

to just one row.

Page 25: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

25

CS252 Fundamentals of Relational Databases 25

Otherwise an error message of the form:

the line that caused the error

ERROR at line 1: ORA-1438: value

larger than specified precision

allows for this column.

Page 26: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

26

CS252 Fundamentals of Relational Databases 26

Simple QueriesRetrieval from tables uses the SELECT clause.

Order of statement:

1. Attribute list

2. target list of tables

3. condition

To view an entire base table:

SELECT *

FROM Collection;

ARTIST ALBUM TRACKS COMPANY YEAR

--------------------------------------------------------------------------

U2 The Unforgettable Fire 10 Island 1984

U2 Rattle and Hum 17 Island 1988

U2 Achtung Baby 12 Island 1991

Underworld Second Toughest in the Infants 8 Junior 1996

The Verve Urban Hymns 13 Virgin 1997

Foo Fighters The Colour and the Shape 13 Capital 1997

Queries are expressed using the SELECT clause that operate on tables, and that

these are analogous to Tutorial D’s relational expressions based on relational

algebra.

The expression SELECT * FROM Collection yields (rather obviously) the table

that is the current value of the base table Collection.

An equivalent expression in Tutorial D would be, quite simply:

Collection

To be precise, SQL’s * is shorthand for a list of the names of the columns of

Collection, separated by commas in the order in which the columns of Collection

are defined:

SELECT artist, album, tracks, company, year FROM Collection

In Tutorial D this is

Collection { artist, album, tracks, company, year }

but note that now the order of the names is irrelevant. Equivalently it could be

written as

Collection { ALL BUT }

where ALL BUT means all attributes apart from those in the following list

(which in this case is empty, so ALL BUT followed by nothing is Tutorial D’s

counterpart of SQL’s *.).

As an exercise, insert also the rows that have not been inserted in the previous

slides, in order to obtain the table as depicted in this slide.

(hint: you need to insert also rows starting with artists Underworld, The Verve

and Foo Fighters).

Page 27: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

27

CS252 Fundamentals of Relational Databases 27

Single Column Selection

To view one column of

a table :

SELECT artist

FROM Collection;

Foo Fighters

The Verve

Underworld

U2

U2

U2

artist

In this example we replace the * of our first example by a single column name, referencing one of the columns of Collection. Note very carefully that the resulting table, unlike Collection itself, cannot be interpreted as a relation. That is because three of the rows are identical. There is no sense in which the same tuple can appear more than once in the body of a relation. That is why there is no counterpart in Tutorial D for this SQL expression.

So this example illustrates the second of SQL’s deviations from relational theory. The first, recall, was the fact that the order of columns in a table is significant.

In case you are thinking that perhaps not much harm is done by the appearance of those duplicates, consider the points that (a) it is only by chance that all the U2’s happen to appear next to each other in the display, and (b) in general, a table can contain a huge number of rowsif those three U2’s appeared at random points in the display, perhaps separated from each other by several hundred rows, it would be practically impossible for the user to notice that there are in fact three of them. In other words, whatever information is to be conveyed by the multiple appearances is really not very well conveyed by using duplicate rows. Better, as many authorities have observed, would be a row pairing the artist name U2 with the number 3 (in a separate column).

Perhaps you are beginning to appreciate the importance that some people attach to reserving the terms table, row and column for SQL objects and sticking to relation, tuple and attribute when speaking or writing about those relational constructs. That is the practice we adhere to in these notes.

Page 28: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

28

CS252 Fundamentals of Relational Databases 28

Output not necessarily

unique. Add qualifier

DISTINCT to achieve

uniqueness :

SELECT DISTINCT artist

FROM Collection;

Foo Fighters

The Verve

Underworld

U2

artist

To avoid multiple appearances of the same row (commonly referred to as

“duplicate rows”), use the key word DISTINCT immediately after the word

SELECT. Now the result (in this case) can be interpreted as a relation, and we

have an easy Tutorial D counterpart:

Collection { artist }

Note, by the way, that the rows in the SQL result now appear in alphabetical

order. As we have said, the order of the rows carries no significance in SQL, so

when a table is displayed in tabular format the user interface software is free to

place the rows in whatever order it likes (unless the user explicitly asks for some

particular order). Typically it will just use the order in which the DBMS server

presents them to the client application. Perhaps the alphabetical order in this

example arose because the DBMS sorted the rows to enable it easily to detect and

eliminate duplicates.

Page 29: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

29

CS252 Fundamentals of Relational Databases 29

Multi-column Selection

To select more than one column from one

table:

SELECT artist, album, year

FROM Collection;

This restricts the output to columns artist,

album and year.

The addition of the columns album and year in this example means that, as it

happens, no duplicate rows arise even when DISTINCT is omitted. Of course, if

the current value of Collection happened to contain two or more rows with the

same values for artist, album and year, then we would need DISTINCT to avoid

confusion. But it would probably be nonsense to have two or more such rows, for

surely no artist would ever produce two or more albums with the same title, and

certainly not in the same year!

In both sections of CS252 (theory and SQL) you will learn about database

constraintsa mechanism by which “business rules” such as the one just

suggested can be enforced by the DBMS. If some constraint makes it impossible

for two rows ever to appear at the same time in Collection that have the same

combination of artist, album and year, then the user might know that it is safe to

omit DISTINCT and thus avoid the possible performance overhead that

DISTINCT incurs. By the same token, if the user does write DISTINCT in this

query, the DBMS might be clever enough to realise that it can, thanks to that

constraint, ignore it safely! This is a very important issue to be addressed by

optimizers in truly relational DBMSs. For in the Tutorial D counterpart of this

query

Collection { artist, album, year }

DISTINCT is implied and there is no option to retain duplicates.

Page 30: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

30

CS252 Fundamentals of Relational Databases 30

Results from a SELECT statement do not have a

specified order. To sort the output into

alphabetical order by artist name, use the ORDER BY statement:

SELECT artist, album, year

FROM Collection

ORDER BY artist;

ARTIST ALBUM TRACKS COMPANY YEAR

--------------------------------------------------------------------------

U2 The Unforgettable Fire 10 Island 1984

U2 Rattle and Hum 17 Island 1988

U2 Achtung Baby 12 Island 1991

Underworld Second Toughest in the Infants 8 Junior 1996

The Verve Urban Hymns 13 Virgin 1997

Foo Fighters The Colour and the Shape 13 Capital 1997

It is important to understand that ORDER BY is not actually part of the SELECT

expression. The example on this slide should be read as a command to the client

application to retrieve the result of the query SELECT artist, album, year FROM

Collection and display it in tabular format, placing the rows in alphabetical order

by artist.

As it happens, the sorting is done within the SQL server because the SQL

mechanism by which query results are transmitted from the server to the client

application supports that ORDER BY clause directly. The application effectively

presents the text of the query to the server, accompanied by that ORDER BY

clause, and then says, “now evaluate the query and present the rows of the result

to me, one by one in the specifies order, as I ask for them”.

As we shall see eventually, SQL queries can be used for several other purposes

where the order of presentation of the rows could not possibly have any

significance. That is why ORDER BY is not really part of the query expression

per se, but only something that can be added on in one particular use of queries.

Page 31: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

31

CS252 Fundamentals of Relational Databases 31

Simple Restriction ConditionsPredicate = logical expression that must be satisfied

(evaluate to true) for a row to be selected.

Select all albums from 1997 using the WHERE statement and “=“ (equals):SELECT artist, album, year

FROM Collection

WHERE year = 1997;

All data in left-hand side column must exactly match right-hand side expression.

ARTIST ALBUM YEAR

-------------------------------------------------------------------------

The Verve Urban Hymns 1997

Foo Fighters The Colour and the Shape 1997

Restriction is the term used in relational theory for deriving a relation from a given relation by picking just those tuples that satisfy a given condition. SQL has a very similar construct in fact you might as well regard it as an identical construct until further notice.

Restriction is specified in both SQL and Tutorial D by the key word WHERE. In both cases the key word appears in between the specification of the input (in SQL, a table, in Tutorial D, a relation) and the restriction condition, a truth-valued expression (such as a simple comparison, for example).

Consider the example shown on the slide, WHERE year = 1997. The input table is, as specified in the immediately preceding FROM clause, the current value of Collection. For each row of Collection in turn, the condition is evaluated by substituting that row’s year value for the column reference. When that substitution results in 1997 = 1997, the condition evaluates to true and the row is picked. When it results in something other than 1997 = 1997, the row is not picked.

Now is the time to explain something rather surprising about SQL’s SELECT-FROM-WHERE structure for expressing queries. Note very carefully that it doesn’t make sense for the three clauses to be evaluated in the sequence in which they are written. The expression SELECT artist, album, year isn’t a complete expression there’s no way it can be evaluated in isolation; it must have an input table for it to make sense. On the other hand, FROM Collection certainly is complete in that sense its value is in fact the current value of the base table Collection.

So, to understand the SELECT-FROM-WHERE structure properly, you have to know the order in which the three clauses are evaluated. And in fact the order is as follows:

FROMWHERESELECT

If WHERE is omitted, you can think of the SELECT as operating immediately on the result of FROM.

It follows from this ordering that the following query is also valid:

SELECT artist, albumFROM CollectionWHERE year = 1997

Notice that year is not included in the SELECT clause. Of course it makes good sense not to “select” year because the WHERE condition explicitly states that the year value will be 1997 in every row of the result.

At the moment this observation about the order of execution might seem quite obvious and intuitive to you, but please commit it to memory anyway, because later we will encounter traps that you can fall into if you forget about it!

Page 32: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

32

CS252 Fundamentals of Relational Databases 32

String Matching

Matching an identical string with “=":

SELECT artist, album

FROM Collection

WHERE artist = 'U2';

Select all artists beginning with the letter

“U" using the LIKE

statement and wildcard “%":

This slide and the next three slides show some of the kinds of expressions you

can write in conditions. Mostly they are as found in most computer languages.

LIKE (on the next slide) and BETWEEN (slide 27) are SQL specials whose

meaning is quite intuitive.

The example shown on this slide involves comparison of character strings. You

should be aware that SQL, like COBOL, treats trailing blanks in character strings

as insignificant. Thus, the WHERE clause in the example is equivalent to

WHERE artist = ′U2 ′

Although SQL’s implied “trimming” is often very useful, it does have some

unfortunate consequences. For example, suppose that the artist name U2, in the

three rows in which it appears in Collection, was recorded with a different

number of trailing blanks on each occasion. Which of those distinct values

would you expect to see in the result of the following query?

SELECT DISTINCT artist

FROM Collection

Try this exercise out and think on its implications in terms of duplication of data

and possible errors intractable in the input.

Page 33: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

33

CS252 Fundamentals of Relational Databases 33

SELECT artist, album

FROM Collection

WHERE artist LIKE 'U%';

ARTIST ALBUM

-----------------------------------------------------

U2 The Unforgettable Fire

U2 Rattle and Hum

U2 Achtung Baby

Underworld Second Toughest in the Infants

Wildcard " " matches single characters. To escape wildcards use the "@" symbol. So to match "10%" use "10@%".

Can also use NOT LIKE.

Page 34: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

34

CS252 Fundamentals of Relational Databases 34

Additional Comparison Operators

To select everything below a certain numerical value, use “<":SELECT artist, album, tracks

FROM Collection

WHERE tracks < 11;

Similarly for:

• greater-than “>"

• less-than-or-equal-to “<="

• greater-than-or-equal-to “>="

• two forms of not-equal-to “!=" and “<>“

ARTIST ALBUM TRACKS

-----------------------------------------------------

U2 The Unforgettable Fire 10

Underworld Second Toughest in the Infants 8

The comparison operators shown on this slide are as found in most programming

languages, but the compound symbol != for “not equal to” is not included in

standard SQL, though Oracle is by no means the only SQL implementation to

support it.

This is a good point at which to note that the usual logical connectives, AND, OR

and NOT are available in SQL. Thus the example is equivalent to the following

more complicated expression:

SELECT artist, album, tracks

FROM Collection

WHERE NOT ( tracks > 11 OR tracks = 11 )

Actually, these operators (and BETWEEN-AND on the next slide) can be used

with values of any type whose values are ordered (i.e., for which the “less than”

operator is defined). This includes character strings, whose ordering is

alphabetical with respect to the unaccented letters of western alphabets.

Page 35: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

35

CS252 Fundamentals of Relational Databases 35

Can also use operator BETWEEN-AND to test inclusion within a range of values:

SELECT artist, album, year

FROM Collection

WHERE year BETWEEN 1980 AND 1992;

ARTIST ALBUM YEAR

-------------------------------------------------------

U2 The Unforgettable Fire 1984

U2 Rattle and Hum 1988

U2 Achtung Baby 1991

BETWEEN-AND is inclusive.

To leave sqlplus type “exit".

BETWEEN-AND is inclusive: meaning, the ends of the interval are included, thus in our

example, the years 1980 and 1992.

Page 36: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

36

CS252 Fundamentals of Relational Databases 36

List Tables (from the catalog db)

Non-standard SQL, specific to each database.

To list the tables in your area in an Oracle system use :SELECT TABLE_NAME FROM

USER_TABLES;

To view definition of a table use:DESCRIBE Collection;

In the given example, the table name USER_TABLES refers to a table in the

system catalog in which each row provides information about one of the tables in

the database. Note that the system catalog is itself an SQL database.

The ISO SQL standard has included a definition of the catalog since the 1992

edition, but many SQL implementations, including Oracle, provided catalogs

long before that date. Such implementations of course have to continue to

support their old proprietary catalogs even if they have more recently

incorporated support for the standard catalog as well.

Page 37: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

37

CS252 Fundamentals of Relational Databases 37

SQL operators covered so far

CREATE TABLE Create a new, empty table.

INSERT INTO…VALUES Insert a row of related data.

SELECT…FROM Retrieve data from a table.

DISTINCT Unique selection of data.

ORDER BY Sort data into a particular order before display on selection.

WHERE Select a row from a table only if the data in that row satisfies a predicate expression.

LIKE % Wildcard string matching.

< > <= >= = != <> Logical selection operators.

BETWEEN-AND Select within a certain range.

Page 38: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

38

CS252 Fundamentals of Relational Databases 38

NULL

What if not all the data values are known?

• insert dummy values and change them later

• insert a marker - NULL

Consider example question 4 of worksheet 2:… a new 11 track album called

Leftism by Leftfield.

We know artist, album and tracks but not company or year.

The following SQL will fail:INSERT INTO Collection

VALUES ('Leftfield', 'Leftism', 11);

In this form of INSERT, values for all columns must be given.

We come to what is perhaps SQL’s most controversial and troublesome special

feature: NULL. You won’t find this construct in any other well known

computer language.

NULL is something that in general (but with many exceptions) can appear

wherever a value could appear but is not itself a value. The term “marker”, used

on this slide, is sometimes used for such a construct. For the time being we will

focus on the use of NULL as a column value in a row, but note that NULL can

also appear in any of the following:

the result of evaluating a scalar expression;

an argument to an invocation of an operator;

the “value” of a local variable.

Although the key word NULL denotes the marker of that name, it (the key word)

cannot be used everywhere where you can write literals in general SQL. For

example, you cannot write year = NULL as a condition in a WHERE clause.

The presence of NULL in SQL represents another of its departures from

relational theory, which is very firmly founded on the principle that tuples

consists solely of values.

Page 39: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

39

CS252 Fundamentals of Relational Databases 39

Partial Inserts

To insert the incomplete information about the Leftfieldalbum, we can use:

INSERT INTO Collection

VALUES ('Leftfield', 'Leftism', 11,

NULL, NULL);

Alternatively, we can specify what data is given and in what order:

INSERT INTO Collection

(album, artist, tracks)

VALUES ('Leftism', 'Leftfield', 11);

Missing fields will be set to “NULL".

The term field used on this slide is the official SQL term for a column value in a

row.

It’s good practice always to write a list of column names in parentheses after the

target table name of an INSERT INTO statement. Then it is 100% clear which of

the values specified in the VALUES expression applies to which column.

As the slide says, in the example at hand NULL will appear for those missing

fields in the row for the album Leftism by Leftfield. This is not always the case

with missing fields. It is possible to specify a so-called default value for a

column. For example, you might specify the empty string, ′′, as the default value

for company and the value 9999 as the default value for year.

Page 40: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

40

CS252 Fundamentals of Relational Databases 40

Testing for presence of NULL:

SELECT artist, album

FROM Collection

WHERE year IS NULL;

Can also use “IS NOT NULL".

IS NULL is used in SQL to test if NULL is the result of evaluating its operand expression (in this case a simple column reference, year). We must now explain why special syntax is needed for this purpose, and why it would not be appropriate to use “= NULL”.

The comparison operators of SQL are not the comparison operators you are familiar with! In other languages, an expression such as year = 1997 evaluates to either true or false and cannot possible result in anything other than these two, the familiar truth values of classical logic. In SQL that expression can indeed evaluate to true and it can indeed evaluate to false, but it doesn’t have to evaluate to either of those! In fact, if and only if the column reference year evaluates to NULL, year = 1997 evaluates to SQL’s third truth value, unknown. In fact, whenever either operand of a comparison evaluates to NULL, the result of that comparison is unknown.

The existence of a third truth value in SQL leads to so many ramifications, complications, common traps, and counterintuitive effects that we do not have time to explore them all in CS252 (nearly all undergraduate courses that teach SQL have the same problem). Although NULL can appear to be quite convenient at times, we encourage you to avoid it wherever possible.

Now we can explain why year = NULL is inappropriate. If NULL were a regular literal, in the way that 3 is a literal denoting the number of that name, then year = NULL would always result in unknown, regardless of the value of year itself!

By the way, if condition cond evaluates to unknown, then NOT (cond) also evaluates to unknown.

Among the many strange consequences of NULL and unknown, observe that if year IS NULL evaluates to true, then both year = year and year != year evaluate to unknown.

Now that you know about NULL and its effect on comparisons, what do you think should be the result of

SELECT DISTINCT year FROM Collection

if two more rows in Collection have NULL for year?? The answer is that just one such row appears in the result. So SQL does not always use its own “equals” operator when it needs to test if two things are in fact the same thing.

Page 41: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

41

CS252 Fundamentals of Relational Databases 41

Logical Connectives

The usual connectives, AND and OR, are available in SQL.

SELECT artist, album

FROM Collection

WHERE artist LIKE 'U%'

AND company != 'Island';

This command lists just the artist and album for the Underworld

CD.

How about all CDs by artists with an “a" or an “e" in their names

with 10 tracks or more?

You should be wondering how AND and OR are defined in SQL, now that you

know SQL has three truth values instead of the usual two. First of all, whenever

neither operand is unknown (i.e., each is either true or false), then the result is as

in regular logic. Here are the rules for when unknown is an operand:

unknown AND true = true AND unknown = unknown

unknown AND unknown = unknown

unknown AND false = false AND unknown = false

unknown OR true = true OR unknown = true

unknown OR unknown = unknown

unknown OR false = false OR unknown = unknown

Page 42: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

42

CS252 Fundamentals of Relational Databases 42

SELECT artist, album, tracks

FROM Collection

WHERE artist LIKE '%e%'

OR artist LIKE '%a%'

AND tracks >= 10;

ARTIST ALBUM TRACKS

------------------------------------------------------

Underworld Second Toughest in the Infants 8

The Verve Urban Hymns 13

Foo Fighters The Colour and the Shape 13

Page 43: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

43

CS252 Fundamentals of Relational Databases 43

Using ParenthesesNote how “AND" takes precedence over “OR".

Use parentheses to indicate a different priority:

SELECT album, tracks

FROM Collection

WHERE (album LIKE '%y%'

OR album LIKE '%S%')

AND tracks >= 10;

ALBUM TRACKS

------------------------------------------------

Urban Hymns 13

The Colour and the Shape 13

Page 44: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

44

CS252 Fundamentals of Relational Databases 44

Testing for presence in a ListA list is of the form:

numbers – “(1, 2, 3)"

strings – “('U2', 'Underworld', 'The Verve')"

In SQL, use keyword “IN" to test for presence in a list of values:

SELECT album, year

FROM Collection

WHERE year IN (1984, 1996, 1997);

This selects all albums from years 1984, 1996 and 1997.

The example shows what a useful shorthand IN provides. If IN were not

available you would have to write

SELECT album, year

FROM Collection

WHERE year = 1984

OR year = 1996

OR year = 1997

Page 45: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

45

CS252 Fundamentals of Relational Databases 45

To select all items by artists U2, Underworld

and Foo Fighters, use query:

SELECT album, year

FROM Collection

WHERE artist IN ('U2', 'Underworld',

'Foo Fighters');

Page 46: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

46

CS252 Fundamentals of Relational Databases 46

Subqueries

A subquery in SQL is a query (i.e., a SELECT expression) that appears inside another query. It is specified by placing parentheses around a query.

Subqueries can be used in several different ways:

1. To denote a tablee.g., as the source for INSERT INTO or an operand of FROM.

2. To denote a “single value”when the query yields a table with one column and one row.

3. To denote a “list of values”, for use with INwhen the result has one column and any number of rows.

A query, as we have seen, results in a table. A subquery can

therefore in principle appear wherever a table operand is expected. Examples

include the input table for INSERT INTO (where so far we have only seen the

VALUES expression, limited to just one row in Oracle) and the FROM clause of

a query (where so far we have only seen a simple base table name).

2. If the query yields a table with just one column and one row, a

subquery can be used to denote the single value appearing in that row in that

column. Such a subquery is called a scalar subquery.

3. If the query yields a table with a single column but any number of

rows it can appear as the right-hand operand of IN, where so far we have seen

only a list of values specified in parentheses.

Page 47: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

47

CS252 Fundamentals of Relational Databases 47

Single Values

Subquery should return only one value:SELECT album, year

FROM Collection

WHERE year <(SELECT year

FROM Collection

WHERE album = 'Urban Hymns');

Selects all albums in the table Collection in years prior to the year related to the album Urban Hymns.

The example on this slide depends on an assumption that no more than one row

in Collection has an album value of ‘Urban Hymns’.

What happens if we try:

SELECT album, year

FROM Collection

WHERE year <

(SELECT year

FROM Collection);

ERROR at line 4:

ORA-01427: single-row subquery returns more than one row

Why is that? What is the danger of

comparing with scalars when we are not

sure of the cardinality of the return of

the subquery?

Page 48: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

48

CS252 Fundamentals of Relational Databases 48

“List” of Values

Subquery should return one column of values:

SELECT album, year

FROM Collection

WHERE artist IN

(SELECT artist

FROM Collection

WHERE album LIKE 'The%');

Subquery returns “list”:

('U2', 'Foo Fighters')

Page 49: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

49

CS252 Fundamentals of Relational Databases 49

Oracle output from whole query is:

ALBUM YEAR

--------------------------------------

The Colour and the Shape 1997

The Unforgettable Fire 1984

Rattle and Hum 1988

Achtung Baby 1991

So we are selecting the title and year of all the albums written by bands who have written albums that start with 'The' !

Page 50: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

50

CS252 Fundamentals of Relational Databases 50

Commitment and RollbackChanges to the database need to be committed. This

can be automatic.

Uncommitted values are not permanent in the tables and can be rolled back.

To find out whether this is currently automated, type:show autocommit;

autocommit OFF

OFF is the default value.

Page 51: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

51

CS252 Fundamentals of Relational Databases 51

To set automatic:

set autocommit ON;

show autocommit;

autocommit IMMEDIATE

If other users access this data they see the old

data until the commit has been performed.

Page 52: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

52

CS252 Fundamentals of Relational Databases 52

To commit recent changes associated with INSERT, DELETE or UPDATE statements, type “COMMIT;".

Commitment occurs implicitly after the commands:

• QUIT or EXIT

• CREATE TABLE or CREATE VIEW

• DROP TABLE or DROP VIEW

• GRANT, REVOKE, CONNECT, DISCONNECT, ALTER, AUDIT, NOAUDIT

To undo changes since the last commitment, use command

ROLLBACK;. Returns the database to state after last explicit or

implicit commitment.

If the plug gets pulled out, uncommitted changes are rolled back.

See more at: http://infolab.stanford.edu/~ullman/fcdb/oracle/or-nonstandard.html

Page 53: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

53

CS252 Fundamentals of Relational Databases 53

Deleting Rows

To delete rows requires a predicate. Using the DELETE statement:

DELETE FROM Collection

WHERE year < 1990;

2 rows deleted.

Deletes all CDs from the table Collection prior to 1990.

The WHERE clause is as for SELECT expressions.

Rollback any unwanted deletions.

Page 54: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

54

CS252 Fundamentals of Relational Databases 54

Updating: modifying existing rows

It is theoretically possible to achieve all database alterations to data using INSERT and DELETE.

More efficient for simple changes to use UPDATE. Consider adding the missing values for the Leftfield album:

• company - Columbia

• year - 1995

UPDATE Collection

SET company = 'Columbia',

year = 1994

WHERE artist = 'Leftfield'

AND album = 'Leftism';

Page 55: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

55

CS252 Fundamentals of Relational Databases 55

If the previous SQL contained a mistake

(e.g., the year is wrong) - it is also

possible to increment values with

update (and to fix the mistake):

UPDATE Collection

SET year = year + 1

WHERE album = 'Leftism';

It is important to understand that when the SET clause contains several

assignments, they are considered to be executed “simultaneously” without regard

to the order in which they are written. Thus, if base table T has columns X and Y

and one row with X = 1 and Y = 1, the effect of the statement

UPDATE T SET X = 5, Y = X + 1

is to change that single row to have X = 5 and Y = 2 (not 6!).

Page 56: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

56

CS252 Fundamentals of Relational Databases 56

Update with Scalar SubqueriesThe value SET can be selected from a table.

Imagine that Island merge with the record company related with the Leftism album. To update the database:

UPDATE Collection

SET company =

(SELECT company

FROM Collection

WHERE album = 'Leftism')

WHERE company = 'Island';

ARTIST ALBUM COMPANY

-----------------------------------------------------------

U2 The Unforgettable Fire Columbia

U2 Rattle and Hum Columbia

U2 Achtung Baby Columbia

Leftfield Leftism Columbia

Recall that a scalar subquery is a query, written inside parentheses, whose result

has one column and one row. Here we show how such a subquery can usefully

appear on the right-hand side of an assignment in a SET clause.

Please also note that if the result of the subquery is not a scalar (if there are two

companies, e.g., with album Leftism) than the whole query will generate an error.

Do you know why that is? How could we prevent this?

Page 57: CS 252: Fundamentals of Relational Databases: SQL1clux.x-pec.com/files/mathstuff/4thyear/CS252 Fundamentals of... · Relational Databases: SQL1. 2 CS252 Fundamentals of Relational

57

CS252 Fundamentals of Relational Databases 57

Interim Summary

Material covered so far:

• Everything for data stored in one base table.

• Creating base tables without constraints.

• Inserting rows and partial rows, NULL.

• Expressing queries using (SELECT … FROM … WHERE).

• Predicates (WHERE) and predicate combination (AND and OR).

• Commitment and rollback.

• Deleting (DELETE) and updating (UPDATE) rows.

• Subqueries.