Lec 05
Lec 05
Lec 05
CS3402 1
SQL: Structured Query Language
Data Definition Language(DDL): define the structures in a database,
i.e. create, modify, and remove database objects such as tables,
indexes, and users. Commands include CREATE, ALTER, and
DROP.
Data Manipulation Language (DML): deal with the manipulation of
data present in database. Commands include : INSERT, UPDATE ,
and DELETE.
Data Query Language (DQL): make queries for data in databases.
Commands include : SELECT.
Note:
Each statement in SQL ends with a semicolon (;)
SQL commands are case insensitive
Despite the existence of the standard, SQL code is not completely
portable among different database systems without adjustments.
CS3402 2
DDL: Define database
CS3402 3
CREATE Table
Create Database:
CREATE SCHEMA database_name AUTHORIZATION user-name;
Update Table:
ALTER TABLE table-name ALTER TABLE table-name
ADD Aj, Dj; DROP Aj;
(to add new attribute Aj with (to delete attribute Aj from
domain Dj to an existing table) an existing table)
CS3402 4
SQL CREATE TABLE data definition statements
for defining the COMPANY schema
Standard
data types
CS3402 5
Data Types and Domains: Numeric
CS3402 6
Data Types and Domains: Character
string
The TIME data type has at least eight positions, with the components
HOUR, MINUTE, and SECOND in the form HH:MM:SS.
CS3402 8
Data Types and Domains: Boolean
A Boolean data type is defined as Boolean and has the traditional
values of TRUE or FALSE. In SQL, because of the presence of NULL
values, a three-valued logic is used, so a third possible value for a
Boolean data type is UNKNOWN.
Not AND T U F OR T U F
3-valued
T F T T U F T T T T
logic:
U U U U U F U T U U
F T F F F F F T U F
And Or
● Only T-T returns T ● Only F-F returns F
● And-ing F with anything results ● Or-ing T with anything results
with F with T
● The rest is UNKNOWN ● The rest is UNKNOWN
CS3402 9
SQL CREATE TABLE data definition statements
for defining the COMPANY schema
Indicate this
column does
not accept
NULL value.
Define
primary key
ensures that
all values in
a column
are different
CS3402 10
Attribute Constraints
CS3402 11
Adding Constrains after creating
table
CS3402 12
SQL CREATE TABLE data definition statements
for defining the COMPANY schema
CS3402 13
One possible database state for the
COMPANY relational database schema
CS3402 14
One possible database state for the
COMPANY relational database schema
Slide 6- 13
CS3402 15
DQL: make query of data
CS3402 16
The SELECT-FROM-WHERE
Structure of Basic SQL Queries
Queries in SQL can be very complex. We will start with simple queries.
The basic form of the SELECT statement formed of the three clauses
SELECT, FROM, and WHERE and has the following form:
Specified attributes
Projection attributes
Selection
conditions
CS3402 18
Retrieval from two tables
Two tables
Join conditions
A B C D E F
ABC DEF XYZ
ABC DEF AAAA
ABC DEF BBBB
ABC DEF CCCCC
CS3402 20
Retrieval from three tables
Select-project-join
query
CS3402 23
Substring Pattern Matching
Dnumber
CS3402 25
Aliasing, and Renaming
The ambiguity of attribute names also arises in the case of queries
that refer to the same relation twice.
Must declare alternative relation, called aliases or tuple
variables using AS
E.g. 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.
CS3402 27
Unspecified WHERE Clause
All possible
Values of all combinations
tuples
CS3402 28
Use of the Asterisk
Specify an asterisk (*)
Retrieve all the attribute values of the selected tuples
SQL allows the user to order the tuples in the result of a query by
the values of one or more of the attributes that appear in the query
result, by using the ORDER BY clause.
• List the employee names in descending order of last name, and if several
employees have the same last name, order them in ascending order by the
first name
SELECT Fname, Minit, Lname
FROM EMPLOYEE
ORDER BY Lname DESC, Fname ASC;
CS3402 30
Tables as Sets in SQL
SELECT does not automatically eliminate duplicate tuples (the
attributes of two tuples have same values) in query results (NOT a
set)
Use the keyword DISTINCT in the SELECT clause
Only distinct tuples should remain in the result
ALL: Distinct:
Same attribute
CS3402 33
DML: Manipulate data
CS3402 34
INSERT Command
INSERT is used to add a single tuple (row) to a relation
(table ). We must specify the relation name and a list of
values for the tuple.
CS3402 36
UPDATE Command
UPDATE command is used to modify attribute values of
one or more selected tuples.
CS3402 37
Views (Virtual Tables)
Base table (base relation)
Relation and its tuples are actually (physically) created and
stored as a file by the DBMS
The tables are stored in the secondary storage in the specified
format
CS3402 38
Views (Virtual Tables)
CREATE VIEW command
In V1, attributes retain the names from base tables. In V2,
attributes are assigned new names
New attribute
names
We can think of a view as a way of specifying a table that we need
to reference frequently, even though it may not exist physically.
CS3402 39
Views (Virtual Tables)
Once a View is defined, SQL queries can use the View relation in
the FROM clause. Views can be regarded and retrieved as ordinary
tables.
E.g.,
SELECT Fname
FROM WORKS_ON1
WHERE HOURS > 5.0;
CS3402 41
Summary of SQL Syntax
CS3402 42
Summary of SQL Syntax
CS3402 43
References
6e
● Ch. 4. p. 83 – 107
● Ch. 5, p. 111 – 126
CS3402 44