Lecture 3 Simple SQL
Lecture 3 Simple SQL
DATABASES
Simple Queries in SQL
NGUYEN Hoang Ha
Email: [email protected]
Objectives
2
SQL OVERVIEW
Mathematics to Computer: RA to SQL
π σ ρ ⋈c
Math
RA is the conceptual basis for RDB
Computer
4
What is SQL?
SEQUEL (Structured English QUEry Language) was
developed by IBM in 1974,
later became Structural Query Language (SQL)
Standard language to work with RDBMS
Easy to learn
Close to English
Less than 100 words
5
6
From RA to SQL
Based on relational algebra, but not entirely identical.
Relations Tables
Tuples Rows
Attributes Columns
Like a relation, a table is a bag of rows. Duplicates are not
automatically removed.
This is for practical reasons. Duplicate eliminations are inefficient in
implementation.
Unlike a relation, the order of rows in a table is relevant.
7 Slide 7
SQL Revisions
1992 SQL-92 SQL2, FIPS 127-2 Major revision (ISO 9075), Entry Level SQL-92 adopted as FIPS 127-2.
1999 SQL:1999 SQL3 Added regular expression matching, recursive queries, triggers, support for procedural
and control-of-flow statements, non-scalar types, and some object-oriented features.
2003 SQL:2003 Introduced XML-related features, window functions, standardized sequences, and
columns with auto-generated values (including identity-columns).
2006 SQL:2006 ISO/IEC 9075-14:2006 defines ways in which SQL can be used in conjunction with
XML. It defines ways of importing and storing XML data in an SQL database,
manipulating it within the database and publishing both XML and conventional SQL-
data in XML form. In addition, it provides facilities that permit applications to
integrate into their SQL code the use of XQuery, the XML Query Language published
by the World Wide Web Consortium (W3C), to concurrently access ordinary SQL-data
and XML documents.
2008 SQL:2008 Defines more flexible windowing functions, clarifies SQL 2003 items that were still
unclear [1]
8
Sub-languages of SQL
CREATE
DDL ALTER
DROP
INSERT
SQL
UPDATE
DML
DELETE
DCL
SELECT (correspond to RA)
TCL
PSM
9
SQL Enviroment
Terminal tool
Network:
MySQL Protocol
Other Applications
10
SQL Commands Are Sequential
Commands are executed in the order they are encountered.
DDL commands are not like C/Java declarations.
DDL and DML commands can be mixed
For example, you can define a table, fill it up with contents, and delete a
columns.
That is, table definitions (relation schema) can be changed during the
lifespan of a database.
The ability of doing so does imply it is a good practice.
It is best the schema/design of a database is well thought through before its
use.
11 Slide 11
Element of SQL Code
Statement: A complete instruction to the database
SELECT * FROM Employees
WHERE LastName = 'Smith'
ORDER BY FirstName;
12
Coding in SQL
SQL is case insensitive
Convention:
Keywords are all in UPPER CASE
Identifier name: vary on each project and team, but must be consistent
Tables and Columns:
PascalCase, e.g. :UsthStudent, InvoiceDetails, TeachingLog)
13
Coding in SQL (cont’)
14
Coding in SQL (cont’)
Comments
Use inline comments for complex logic or calculations.
SELECT column_name -- This is a comment
FROM table_name;
15
DDL
DDL Commands
CREATE DATABASE
CREATE TABLE
ALTER TABLE
RENAME TABLE
DROP TABLE
CREATE INDEX
DROP INDEX
Also – CREATE VIEW
17
18
Referential Integrity Constraints
A referential integrity constraint is used to link (or reference)
relations. This means that a foreign key in a relation must also
exist in the relation in which it serves as the primary key
Super_ssn must be found in Ssn of Employee
Mgr_ssn of Department must exist in Ssn of Employee
Dno of Employee must exist in Dnumber of Department
Dnum of Project must exist in Dnumber of Department
Dnumber of Dept_Location must exist in Dnumber of Department
19
Create Database Example
To create
CREATE DATABASE Company;
To use (or switch to) the database
USE Company;
Subsequent commands will operate on the Company
database by default.
20 Slide 20
CREATE TABLE
CREATE TABLE base-table-name (colname
datatype [column constraints – NULL/NOT
NULL, DEFAULT…, UNIQUE, CHECK…, PRIMARY
KEY],
[,colname datatype [column constraints
…]]
...
[table constraints – PRIMARY KEY…, FOREIGN
KEY…, UNIQUE…, CHECK…]
[storage specifications]);
21
Datatypes
Each column must have a datatype specified
Standards include various numeric types, fixed-length and
varying-length character strings, bit strings, and user-defined
types
Available datatypes vary from DBMS to DBMS
22
Datatypes
char(n). Fixed length character string, with user-specified length n.
varchar(n). Variable length character strings, with user-specified maximum length n.
int. Integer (a finite subset of the integers that is machine-dependent).
smallint. Small integer (a machine-dependent subset of the integer domain type).
numeric(p,d). Fixed point number, with user-specified precision of p digits, with d
digits to the right of decimal point. (ex., numeric(3,1), allows 44.5 to be stores
exactly, but not 444.5 or 0.32)
real, double precision. Floating point and double-precision floating point numbers,
with machine-dependent precision.
float(n). Floating point number, with user-specified precision of at least n digits.
Date: Made up of year-month-day in the format yyyy-mm-dd
Time: Made up of hour:minute:second in the format hh:mm:ss
Timestamp: Has both DATE and TIME components
Others: Boolean, Float, Double Precision
See user’s manual for more data types.
23
CREATE TABLE Example
CREATE TABLE Department (
Dname VARCHAR(10) NOT NULL,
Dnumber INTEGER DEFAULT 0,
Mgr_ssn CHAR(9),
Mgr_Start_date CHAR(9),
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn) REFERENCES Employee(Ssn)
);
25
The Check Clause
Used to specify user-defined constraints
Assume that dept. numbers are from 0 to 99.
CREATE TABLE Department (
…
Dnumber INTEGER Default 0
CHECK (Dnumber>=0 AND Dnumber<=99),
…);
“Check” can also be a clause of the entire table.
CRATE TABLE Department (
…
Dept_create_date date,
Mgr_start_date date,
CHECK (Dept_create_date <= Mgr_start_date)
);
26
Review: Multi attribute Key
The bar and beer together are the key for Sells:
CREATE TABLE Sells (
bar CHAR(20),
beer VARCHAR(20),
price REAL,
PRIMARY KEY (bar, beer)
);
27
Exercise
Create the table WORKS_ON, assuming tables EMPLOYEE
and PROJECT have been created and Hours ranges from 1
to 56.
28
Add Columns to Existing Tables
To add spouse SSN (S_ssn) to Employee
ALTER TABLE Employee
ADD COLUMN S_ssn char(9);
The new attribute will have NULLs in all the tuples of the
relation right after the command is executed
29
Delete Columns from Existing Tables
To delete column S_ssn
ALTER TABLE Employee DROP COLUMN S_ssn;
30
Referential Integrity Options
Causes of referential integrity violation for a foreign key FK
(consider the Mgr_ssn of Department).
On Delete: when deleting the foreign tuple
What to do when deleting the manager tuple in Employee ?
On Update: when updating the foreign tuple
What to do when updating/changing the SSN of the manager tuple in
Employee is changed ?
31
The Mgr_ssn Example
32
Another Example
CREATE TABLE EMP(
…
SSN CHAR(9),
DNO INTEGER DEFAULT 1,
SUPERSSN CHAR(9),
PRIMARY KEY (ESSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT
ON UPDATE CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES EMP
ON DELETE SET NULL
ON UPDATE CASCADE
);
33
Miscellaneous Commands
SHOW DATABASES;
Show all the databases on the server
SHOW TABLES;
Show all the tables of the present database
DESCRIBE table_name
Show structure of table, only for MySQL
34
SIMPLE DML QUERIES
SELECT commands
A SELECT statement retrieves information from the database. Using a
SELECT statement, you can do the following:
Projection:You can use the projection capability in SQL to choose the
columns in a table that you want returned by your query.
Selection:You can use the selection capability in SQL to choose the
rows in a table that you want returned by a query (with WHERE clause)
Joining:You can use the join capability in SQL to bring together data
that is stored in different tables by creating a link between them.
36
SQL data retrieval query structure
37
Syntax for a simple SELECT queries
❖ SELECT clause identifies what columns
▪ ALL: Specifies that duplicate rows can appear in the result set. ALL is the default
▪ DISTINCT: Specifies that only unique rows can appear in the result set. Null values
are considered equal for the purposes of the DISTINCT keyword
▪ TOP n [ PERCENT ]:Specifies that only the first n rows are to be output from the
query result set. n is an integer between 0 and 4294967295. If PERCENT is also
specified, only the first n percent of the rows are output from the result set. When
38
A trick for reading & writing queries
It’s generally easiest to examine a SELECT-FROM-
WHERE query by:
First looking at the FROM clause to learn which relations
are involved in the query
Then, move to the WHERE clause to learn what it is
about tuples that is important to the query
Finally, look at the SELECT clause to see what the output
format is
The same order: FROM, then WHERE, then SELECT
is often useful when writing queries of your own as
well
39
Example: SELECT all columns
40
Projection in SQL
We can, if we wish, eliminate some of the components of
the chosen tuples; that is, we can project the relation
produced by a SQL query onto some of its attributes
In place of the * of the SELECT clause, we may list some of
the attributes of the relation mentioned in the FROM clause.
The result will be projected onto the attributes listed
41
Example: Projection in SQL
42
Example: Extended projection using Arithmetic Operators
44
Example: ALIAS
The example displays the last names and annual salaries of
all the employees.
Because Annual Salary contain a space, it has been enclosed
in double quotation marks.
Notice that the column heading in the output is exactly the
same as the column alias.
45
Duplication Eliminating with SELECT distinct
46
Selection in SQL or Restricting data
While retrieving data from the database, you may need to
restrict the rows of data that are displayed
In that case, the solution is to use the WHERE clause
The WHERE clause is equal to the selection operator of
relational algebra
The expression that may follow WHERE include conditional
expressions like those found in C or Java
47
Selection in SQL (or Restricting data)
SELECT [ ALL | DISTINCT ]
[ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
[FROM table]
[WHERE conditions]
49
Example: Restricting data
50
Example: Restricting data
51
Comparison of Strings
Two strings are equal if they are the same sequence
of characters. Recall from the section 2.3.2 that
strings can be stored as fixed-length strings (using
CHAR) or variable-length strings (using VCHAR)
When comparing strings with different declarations,
only the actual strings are compared (SQL ignores
any “pad” characters that must be presenet in the
database in order to give a string its required
length)
We can use “<“, “>”, “=“ and “<>” operators to
compare two strings
52
Pattern matching in SQL
SQL also provides the capability to compare strings on the
basis of a simple pattern match. An alternative form of
comparision expression is:
s LIKE p
where:
S: is a string
P: is a pattern (with the optional use of some special characters: “%”,
“_” ..)
Similarly, “s NOT LIKE p” is true if and only if string s does
not match pattern p
53
Wildcards
_ Underscore for any single character “h_t” finds hot, hat, hit
SQL Server
54
Dates and Times
SQL generally support dates and times as special data types.
These values are often representable in a variety of formats
such ash:
‘05/14/1948’ or
’14 May 1948’
55
NULL values
Null means 'nothing' or without value or consequence
Null is a special marker used in Structured Query Language
(SQL) to indicate that a data value does not exist in the
database. Introduced by the creator of the relational
database model
Since Null is not a member of any data domain, it is not
considered a "value", but rather a marker (or placeholder)
indicating the absence of value. Because of this, comparisons
with Null can never result in either True or False, but always
in a third logical result, Unknown
However, certain operations on Null can return values if the
value of Null is not relevant to the outcome of the
operation
56
Ordering the Output
While retrieving data from the database, you may need to
specify the order in which the rows are displayed.
In that case, the solution is to use the ORDER BY clause
57
Ordering the Output
If you use the ORDER BY clause, it must be the last clause of the SQL
statement.
Expression: Specifies a column on which to sort.
A sort column can be specified as a name or column alias (which can be qualified
by the table or view name), an expression, or a nonnegative integer representing
the position of the name, alias, or expression in select list.
Multiple sort columns can be specified. The sequence of the sort columns in the
ORDER BY clause defines the organization of the sorted result set.
59
Exercise 1
Write SQL queries to create the following tables:
Studio (name, address)
Star (name, address, phone)
Movie (title, year, length, genre)
60
Exercise 2
63
Exercise 5
Write a SQL query to show all SALARY (but
eliminating duplications) in table EMPLOYEES
Write a SQL query to show all DEPARTMENT_ID
(but eliminating duplications) in table EMPLOYEES
64
DML STATEMENTS
UPDATE INSERT DELETE
Reference: Section 6.5 Jeffrey D. Ullman, Jennifer Widom: A First Course in Database
Systems, Pearson, 3rd Edition (2007)
Insert record
66
Insert data from a select query
67
Update records
UPDATE TableName
SET column1 = value1, column2 = value2,.
WHERE condition
UPDATE employees
SET city = ‘SEAT’
WHERE city = ‘seattle’
68
Delete records
69
Exercise 6
Write a SQL query to delete all tuples in
EMPLOYEES table
Write a SQL query to delete all tuples with NULL
value in DEPARTMENT_ID
70
Exercise 7
Write a SQL query to set DEPARTMENT_ID to
the value 10
Write a SQL query to set DEPARTMENT_ID to
the value 10 if DEPARTMENT_ID is NULL
71
Exercise 8
Write a SQL query to insert some new tuples into
EMPLOYEES
72