Chapter05
Chapter05
- SENG 351 -
CHAPTER 5
Basic SQL
Slide 5- 1
Chapter 5 Outline
SQL Data Definition and Data Types
Specifying Constraints in SQL
Basic Retrieval Queries in SQL
INSERT, DELETE, and UPDATE Statements in
SQL
Additional Features of SQL
Slide 5- 2
Basic SQL
SQL language
Considered one of the major reasons for the commercial
success of relational databases
SQL
The origin of SQL is relational predicate calculus called tuple
calculus which was proposed initially as the language SQUARE.
SQL Actually comes from the word “SEQUEL” which was the original
term used in the paper: “SEQUEL TO SQUARE” by Chamberlin and
Boyce. IBM could not copyright that term, so they abbreviated to SQL
and copyrighted the term SQL.
Now popularly known as “Structured Query language”.
SQL is an informal or practical rendering of the relational data model
with syntax
Slide 5- 3
SQL Data Definition, Data Types,
Standards
Terminology:
Table, row, and column used for relational model
terms relation, tuple, and attribute
CREATE statement
Main SQL command for data definition
The language has features for : Data definition, Data
Manipulation, Transaction control (Transact-SQL),
Indexing, Security specification (Grant and Revoke),
Active databases, Multi-media, Distributed databases
etc.
Slide 5- 4
Schema and Catalog Concepts in
SQL
We cover the basic standard SQL syntax – there
are variations in existing RDBMS systems
SQL schema
Identified by a schema name
Includes an authorization identifier and descriptors
for each element
Schema elements include
Tables, constraints, views, domains, and other
constructs
Each statement in SQL ends with a semicolon
Slide 5- 5
Schema and Catalog Concepts in
SQL (cont’d.)
CREATE SCHEMA statement
CREATE SCHEMA COMPANY AUTHORIZATION
‘Jsmith’;
Catalog
Named collection of schemas in an SQL
environment
SQL also has the concept of a cluster of catalogs.
Slide 5- 6
The CREATE TABLE Command in
SQL
Specifying a new relation
Provide name of table
Specify attributes, their types and initial
constraints
Can optionally specify schema:
CREATE TABLE COMPANY.EMPLOYEE ...
or
CREATE TABLE EMPLOYEE ...
Slide 5- 7
The CREATE TABLE Command in
SQL (cont’d.)
Base tables (base relations)
Relation and its tuples are actually created and
stored as a file by the DBMS
Virtual relations (views)
Created through the CREATE VIEW statement.
Do not correspond to any physical file.
Slide 5- 8
COMPANY relational database
schema (Fig. 5.7)
Slide 5- 9
One possible database state for the
COMPANY relational database schema
(Fig. 5.6)
Slide 5- 10
One possible database state for the
COMPANY relational database schema –
continued (Fig. 5.6)
Slide 5- 11
SQL CREATE TABLE data definition statements
for defining the COMPANY schema from Figure
5.7 (Fig. 6.1)
Slide 5- 12
SQL CREATE TABLE data definition
statements for defining the COMPANY
schema from Figure 5.7 (Fig. 6.1)-continued
Slide 5- 13
Attribute Data Types and Domains in
SQL
Basic data types
Numeric data types
Integer numbers: INTEGER, INT, and SMALLINT
Floating-point (real) numbers: FLOAT or REAL, and
DOUBLE PRECISION
Character-string data types
Fixed length: CHAR(n), CHARACTER(n)
Varying length: VARCHAR(n), CHAR
VARYING(n), CHARACTER VARYING(n)
Slide 5- 15
Attribute Data Types and Domains in
SQL (cont’d.)
Bit-string data types
Fixed length: BIT(n)
Varying length: BIT VARYING(n)
Boolean data type
Values of TRUE or FALSE or NULL
DATE data type
Ten positions
Components are YEAR, MONTH, and DAY in the form
YYYY-MM-DD
Multiple mapping functions available in RDBMSs to
change date formats
Slide 5- 16
Attribute Data Types and Domains in
SQL (cont’d.)
Additional data types
Timestamp data type
Includes the DATE and TIME fields
Plus a minimum of six positions for decimal fractions of
seconds
Optional WITH TIME ZONE qualifier
INTERVAL data type
Specifies a relative value that can be used to increment or
decrement an absolute value of a date, time, or timestamp
DATE, TIME, Timestamp, INTERVAL data types can be
cast or converted to string formats for comparison.
Slide 5- 17
Attribute Data Types and Domains in
SQL (cont’d.)
Domain
Name used with the attribute specification
Makes it easier to change the data type for a domain
that is used by numerous attributes
Improves schema readability
Example:
CREATE DOMAIN SSN_TYPE AS CHAR(9);
TYPE
User Defined Types (UDTs) are supported for object-
oriented applications. Uses the command: CREATE
TYPE
Slide 5- 18
Specifying Constraints in SQL
Basic constraints:
Relational Model has 3 basic constraint types that
Slide 5- 19
Specifying Attribute Constraints
Other Restrictions on attribute domains:
Default value of an attribute
DEFAULT <value>
NULL is not permitted for a particular attribute
(NOT NULL)
CHECK clause
Dnumber INT NOT NULL CHECK (Dnumber >
0 AND Dnumber < 21);
Slide 5- 20
Specifying Key and Referential
Integrity Constraints
PRIMARY KEY clause
Specifies one or more attributes that make up the
primary key of a relation
Dnumber INT PRIMARY KEY;
UNIQUE clause
Specifies alternate (secondary) keys (called
CANDIDATE keys in the relational model).
Dname VARCHAR(15) UNIQUE;
Slide 5- 21
Specifying Key and Referential
Integrity Constraints (cont’d.)
FOREIGN KEY clause
Default operation: reject update on violation
Attach referential triggered action clause
Options include SET NULL, CASCADE, and SET
DEFAULT
Action taken by the DBMS for SET NULL or SET
DEFAULT is the same for both ON DELETE and ON
UPDATE
CASCADE option suitable for “relationship” relations
Slide 5- 22
Giving Names to Constraints
Using the Keyword CONSTRAINT
Name a constraint
Useful for later altering
Slide 5- 23
Default attribute values and referential
integrity triggered action specification (Fig.
6.2)
Slide 5- 24
Specifying Constraints on Tuples
Using CHECK
Additional Constraints on individual tuples within a
relation are also possible using CHECK
CHECK clauses at the end of a CREATE TABLE
statement
Apply to each tuple individually
CHECK (Dept_create_date <=
Mgr_start_date);
Slide 5- 25
Basic Retrieval Queries in SQL
SELECT statement
One basic statement for retrieving information from
a database
Slide 5- 26
The SELECT-FROM-WHERE
Structure of Basic SQL Queries
Basic form of the SELECT statement:
Slide 5- 27
The SELECT-FROM-WHERE Structure
of Basic SQL Queries (cont’d.)
Slide 5- 29
Basic Retrieval Queries (Contd.)
Slide 5- 30
Ambiguous Attribute Names
Same name can be used for two (or more)
attributes in different relations
As long as the attributes are in different relations
Must qualify the attribute name with the relation
name to prevent ambiguity
Slide 5- 31
Aliasing, and Renaming
Aliases or tuple variables
Declare alternative relation names E and S to refer
to the EMPLOYEE relation twice in a query:
Query 8. For each employee, retrieve the employee’s first and last name
and the first and last name of his or her immediate supervisor.
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
Recommended practice to abbreviate names and to
prefix same or similar attribute from multiple tables.
Slide 5- 32
Aliasing,Renaming and Tuple
Variables (contd.)
The attribute names can also be renamed
EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd,
Addr, Sex, Sal, Sssn, Dno)
Note that the relation EMPLOYEE now has a
implementations
Slide 5- 33
Unspecified WHERE Clause
and Use of the Asterisk
Missing WHERE clause
Indicates no condition on tuple selection
Effect is a CROSS PRODUCT
Result is all possible tuple combinations (or the
Algebra operation of Cartesian Product– see Ch.8)
result
Slide 5- 34
Unspecified WHERE Clause
and Use of the Asterisk (cont’d.)
Specify an asterisk (*)
Retrieve all the attribute values of the selected
tuples
The * can be prefixed by the relation name; e.g.,
EMPLOYEE *
Slide 5- 35
Tables as Sets in SQL
SQL does not automatically eliminate duplicate tuples in
query results
For aggregate operations duplicates must be accounted
for
Use the keyword DISTINCT in the SELECT clause
Only distinct tuples should remain in the result
Slide 5- 36
Tables as Sets in SQL (cont’d.)
Set operations
UNION, EXCEPT (difference), INTERSECT
Corresponding multiset operations: UNION ALL,
EXCEPT ALL, INTERSECT ALL)
Type compatibility is needed for these operations
to be valid
Slide 5- 37
Substring Pattern Matching and
Arithmetic Operators
LIKE comparison operator
Used for string pattern matching
% replaces an arbitrary number of zero or more characters
underscore (_) replaces a single character
Examples: WHERE Address LIKE ‘%Houston,TX%’;
WHERE Ssn LIKE ‘_ _ 1_ _ 8901’;
BETWEEN comparison operator
E.g., in Q14 :
WHERE(Salary BETWEEN 30000 AND 40000)
AND Dno = 5;
Slide 5- 38
Arithmetic Operations
Slide 5- 39
Ordering of Query Results
Use ORDER BY clause
Keyword DESC to see result in a descending order
of values
Keyword ASC to specify ascending order explicitly
Typically placed at the end of the query
Slide 5- 40
Basic SQL Retrieval Query Block
Slide 5- 41
INSERT, DELETE, and UPDATE
Statements in SQL
Three commands used to modify the database:
INSERT, DELETE, and UPDATE
INSERT typically inserts a tuple (row) in a relation
(table)
UPDATE may update a number of tuples (rows) in
a relation (table) that satisfy the condition
DELETE may also update a number of tuples
(rows) in a relation (table) that satisfy the
condition
Slide 5- 42
INSERT
In its simplest form, it is used to add one or more
tuples to a relation
Attribute values should be listed in the same
order as the attributes were specified in the
CREATE TABLE command
Constraints on data types are observed
automatically
Any integrity constraints as a part of the DDL
specification are enforced
Slide 5- 43
The INSERT Command
Specify the relation name and a list of values for
the tuple. All values including nulls are supplied.
Slide 5- 44
BULK LOADING OF TABLES
Another variation of INSERT is used for bulk-loading of
several tuples into tables
A new table TNEW can be created with the same
attributes as T and using LIKE and DATA in the syntax,
it can be loaded with entire data.
EXAMPLE:
Slide 5- 45
DELETE
Removes tuples from a relation
Includes a WHERE-clause to select the tuples to be
deleted
Referential integrity should be enforced
Tuples are deleted from only one table at a time (unless
CASCADE is specified on a referential integrity
constraint)
A missing WHERE-clause specifies that all tuples in the
Slide 5- 47
UPDATE
Used to modify attribute values of one or more
selected tuples
A WHERE-clause selects the tuples to be
modified
An additional SET-clause specifies the attributes
to be modified and their new values
Each command modifies tuples in the same
relation
Referential integrity specified as part of DDL
specification is enforced
Slide 5- 48
UPDATE (contd.)
Example: Change the location and controlling
department number of project number 10 to
'Bellaire' and 5, respectively
Slide 5- 49
UPDATE (contd.)
Example: Give all employees in the 'Research' department a
10% raise in salary.
U6: UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
In this request, the modified SALARY value depends on the
original SALARY value in each tuple
The reference to the SALARY attribute on the right of =
refers to the old SALARY value before modification
The reference to the SALARY attribute on the left of =
refers to the new SALARY value after modification
Slide 5- 50
Summary
SQL
A Comprehensive language for relational database
management
Data definition, queries, updates, constraint
specification, and view definition
Covered :
Data definition commands for creating tables
Commands for constraint specification
Simple retrieval queries
Database update commands
Slide 5- 51