Database Systems
Instructor : Dr. Natash Ali Mian
`
Switch off mobile phones during
lectures, or put them into silent mode
2
Contents
•Introduction to SQL
•DDL Statements
•DML Statements
Objectives
Definition of terms
Interpret history and role of SQL
Define a database using SQL data definition language
Write single table queries using SQL
Establish referential integrity using SQL
Discuss SQL:1999 and SQL:2003 standards
5
SQL Overview
Structured Query Language
The standard for relational database management
systems (RDBMS)
RDBMS: A database management system that manages
data as a collection of tables in which all relationships are
represented by common values in related tables
6
History of SQL
1970–E. Codd develops relational database concept
1974-1979–System R with Sequel (for Structured English
QUEry Language) created at IBM Research Lab, renamed to
SQL later
1979–Oracle markets first relational DB with SQL
1986–ANSI SQL standard released
1989, 1992, 1999, 2003–Major ANSI standard updates
Current–SQL is supported by most major database vendors. Most
DBMS are SQL-99 compliant, with partial SQL-2003 compliant
Database major players--- Oracle (41%), IBM (31%), Microsoft
(13%), MySQL (open source)
7
Purpose of SQL Standard
Specify syntax/semantics for data definition and
manipulation
Define data structures and basic operations
Enable portability
Specify minimal (level 1) and complete (level 2)
standards
Allow for later growth/enhancement to standard
8
Benefits of a Standardized Relational
Language
Reduced training costs
Productivity
Application portability
Application longevity
Reduced dependence on a single vendor
Cross-system communication
9
SQL Environment
Catalog
A set of schemas that constitute the description of a database
Schema (or Database)
The structure that contains descriptions of objects created by a user (base
tables, views, constraints)
Data Definition Language (DDL)
Commands that define a database, including creating, altering, and dropping
tables and establishing constraints
Data Manipulation Language (DML)
Commands that maintain and query a database
Data Control Language (DCL)
Commands that control a database, including administering privileges and
committing data
10
Figure 7-1
A simplified schematic of a typical SQL environment, as
described by the SQL-2003 standard
11
Some SQL Data types
12
Figure 7-4
DDL, DML, DCL, and the database development process
13
Common SQL Commands
Data Definition Language (DDL):
Create Drop
Alter
Data Manipulation Language (DML):
Select Update
Insert Delete
Data Control Language (DCL):
Grant Revoke
14
Writing SQL Statements
SQL statements are not case sensitive
(but criteria within quotation marks are for some RDBMS)
SQL statements can be on one or more lines
Clauses are usually placed on separate lines
Keywords cannot be split across lines
Tabs and spaces are allowed to enhance readability
Each SQL statement (not line) ends with a
semicolon (;)
15
SQL Database Definition
Data Definition Language (DDL)
Major CREATE statements:
CREATE SCHEMA (DATABASE)–defines a portion of the
database owned by a particular user
CREATE TABLE–defines a table and its columns
CREATE VIEW–defines a logical table from one or more tables
or views
16
DDL--Table Creation Steps in table creation:
1. Identify data types for
Figure 7-5 General syntax for CREATE TABLE
attributes
2. Identify columns that can
and cannot be null
3. Identify columns that
must be unique
(candidate keys)
4. Identify primary key–
foreign key mates
5. Determine default values
6. Identify constraints on
columns (domain
specifications)
7. Create the table and
associated indexes 17
Creating table--constraints
Domain constraints
Constraints about one or more columns
NOT NULL, CHECK, DEFAULT
Entity constraints
Constraints about individual rows
PRIMARY KEY, UNIQUE
Referential Integrity constraints
FOREIGN KEY
18
The following slides create tables for this
enterprise data model
19
Figure 7-6 SQL database definition commands for Pine Valley Furniture
Overall table
definitions
20
Defining attributes and their data types
21
Non-nullable specification
Primary keys
can never have
Identifying primary key NULL values
22
Non-nullable specifications
Primary key
Some primary keys are composite–
composed of multiple attributes
23
Controlling the values in attributes
Default value
Domain constraint
24
Identifying foreign keys and establishing relationships
Primary key of
parent table
Foreign key of
dependent table
25
DDL--Data Integrity Controls
Referential integrity–constraint that ensures that foreign key
values of a table must match primary key values of a related
table in 1:M relationships
Restricting:
Deletes of primary records
Updates of primary records
Inserts of dependent records
26
DDL--Changing and Removing Tables
ALTER TABLE statement allows you to change column
specifications:
ALTER TABLE table_name action
The action can be
ADD, ALTER or DROP Column,
ADD or DROP table constraint
Example
ALTER TABLE CUSTOMER_T ADD (TYPE VARCHAR(2) default
“commercial”);
DROP TABLE statement allows you to remove tables from your
schema:
DROP TABLE CUSTOMER_T
Can be qualified by ‘restrict’ or ‘cascade’
27
DML--Insert Statement
Adds data to a table
Inserting into a table: every attribute is supplied
INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’,
‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);
Inserting a record that has some null attributes requires entering
null explicitly for the empty fields or identifying the fields that
actually get data
INSERT INTO PRODUCT_T (PRODUCT_ID,
PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE,
PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);
Inserting from another table
INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T WHERE STATE =
‘CA’;
28
Creating Tables with Identity Columns
New with SQL:2003
Inserting into a table does not require explicit customer ID entry or field
list
INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’,
‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);
29
DML--Delete Statement
Removes rows from a table
Delete certain rows
DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’;
Delete all rows
DELETE FROM CUSTOMER_T;
Be cautious: always use SELECT clause to display the
records first to make sure only desired records are to be
deleted!
30
DML--Update Statement
Modifies data in existing rows
UPDATE PRODUCT_T SET UNIT_PRICE = 775
WHERE PRODUCT_ID = 7;
31
Internal Schema Definition
Control processing/storage efficiency:
Choice of indexes
File organizations for base tables
File organizations for indexes
Data clustering
Statistics maintenance
Creating indexes
Speed up random/sequential access to base table data
Example
CREATE INDEX NAME_IDX ON
CUSTOMER_T(CUSTOMER_NAME)
This makes an index for the CUSTOMER_NAME field of the
CUSTOMER_T table
DROP INDEX NAME_IDX
32
DML--SELECT Statement Syntax
SELECT [DISTINCT] column_list
FROM table_list
[WHERE conditional expression]
[GROUP BY column_list]
[HAVING conditional expression]
[ORDER BY column_list] ;
33
SELECT Statement
Used for queries on single or multiple tables
Clauses of the SELECT statement:
SELECT
List the columns (and expressions) that should be returned from the query
FROM
Indicate the table(s) or view(s) from which data will be obtained
WHERE
Indicate the conditions under which a row will be included in the result
GROUP BY
Indicate categorization of results
HAVING
Indicate the conditions under which a category (group) will be included
ORDER BY
Sorts the result according to specified criteria
34
SELECT Example
Find products with standard price less than $275
SELECT PRODUCT_DESCRIPTION, STANDARD_PRICE
FROM PRODUCT_t
WHERE STANDARD_PRICE < 275;
When you want to select all columns in the table , use * in the SELECT
Clause
SELECT *
FROM PRODUCT_t
WHERE STANDARD_PRICE < 275;
Table 7-3: Comparison Operators in SQL
35
SELECT Example Using Alias
Alias is an alternative name you give to a column or a table in query
Qualified name: [TABLE NAME].[COLUMN NAME]
SELECT CUST.CUSTOMER_NAME AS [NAME],
CUST.CUSTOMER_ADDRESS AS [ADDRESS]
FROM CUSTOMER AS CUST
WHERE CUSTOMER_NAME= 'Home Furnishings';
36
Aggregate Functions
(Numeric Functions)
COUNT: the number of rows containing the
specified column (attribute)
SUM: the sum of all values for a selected
column
AVG: the arithmetic mean (average) for the
specified column (attribute)
MAX: the largest value in the column
MIN: the smallest value in the column
37
SELECT Example Using a Function
Using the COUNT aggregate function to find the number of different items that were
ordered in an order
SELECT COUNT(*) AS #LINEITEMS
FROM ORDER_LINE_t
WHERE ORDER_ID = 1004;
Find the average price for all products
SELECT AVG (STANDARD_PRICE) AS AVERAGE
FROM PRODUCT_t;
Find the maximum price for all products
SELECT MAX (STANDARD_PRICE) AS MAXIMUM
FROM PRODUCT_t;
Find the total quantity of products that were ordered in an order
SELECT SUM(ORDERED_QUANTITY) AS #ALLITEMS
FROM ORDER_LINE_t
WHERE ORDER_ID=1004
38
Using function
This is wrong:
SELECT PRODUCT_ID, COUNT(*)
FROM ORDER_LINE_t
WHERE ORDER_ID=1004;
While product_id returns row values, count returns a
aggregate value. SQL can not return both in a single query
39
wildcards
Asterisk (*) in a SELECT clause represents all columns
or all rows, depending on situation
Wildcard characters % and _ are often used with
keyword LIKE in a WHERE clause
% represents 0, 1 or any number of characters
Eg: LIKE ‘%Desk’ matches both ‘Computer Desk’ and ‘Writers
Desk’
Underscore (_) represents exactly one character
Eg: LIKE ‘_-drawer’ matches ‘3-drawer’ as well as ‘5-drawer’
40
Comparison Operators
Which orders were placed after 10/24/2006?
SELECT ORDER_ID, ORDER_DATE FROM
ORDER_t WHERE ORDER_DATE>’24-OCT-2006’
What furniture aren’t made of cherry?
SELECT PRODUCT_DESCRIPTION,
PRODUCT_FINISH FROM PRODUCT_t WHERE
PRODUCT_FINISH!=‘Cherry’
41
Boolean operators
Used in WHERE clause to join two conditions
And: returns results only when all conditions are true
OR: returns results when any conditions are true
NOT: negates a expression
Order of the operators: NOT first, then AND, then OR
Use parenthesis to change default order of operators
42
SELECT Example–Boolean Operators
AND, OR, and NOT Operators for customizing conditions in
WHERE clause
SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH,
STANDARD_PRICE
FROM PRODUCT_t
WHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’
OR PRODUCT_DESCRIPTION LIKE ‘%Table’)
AND UNIT_PRICE > 300;
Note: the LIKE operator allows you to compare strings using wildcards.
For example, the % wildcard in ‘%Desk’ indicates that all strings that
have any number of characters preceding the word “Desk” will be allowed
43
Venn Diagram from Previous Query
44
SELECT Example–Boolean Operators
Note this query is different from the previous example
SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH,
STANDARD_PRICE
FROM PRODUCT_t
WHERE PRODUCT_DESCRIPTION LIKE ‘%Desk’
OR PRODUCT_DESCRIPTION LIKE ‘%Table’
AND UNIT_PRICE > 300;
45
Venn Diagram from Previous Query
46
Using ranges for qualification
Which products have a standard price between 200 and 300?
SELECT PRODUCT_DESCRIPTION, STANDARD_PRICE FROM
PRODUCT_t
WHERE STANDARD_PRICE>199 AND STANDARD_PRICE<301;
SELECT PRODUCT_DESCRIPTION, STANDARD_PRICE FROM
PRODUCT_t
WHERE STANDARD_PRICE BETWEEN 200 AND 300;
BETWEEN is inclusive (include 200 and 300)
47
Distinct values
What are the distinct order numbers included in the
ORDER_LINE table?
SELECT DISTINCT ORDER_ID
FROM ORDER_LINE_t;
What are the unique combinations of order number and
order quantity included in the ORDER_LINE table?
SELECT DISTINCT ORDER_ID, ORDERED_QUANTITY
FROM ORDER_LINE_t;
48
Using IN and NOT IN with list
List all customers live in warmer states
SELECT CUSTOMER_NAME, CITY, STATE
FROM CUSTOMER_t
WHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’);
49
SELECT Example –
Sorting Results with the ORDER BY Clause
Sort the results first by STATE, and within a state by
CUSTOMER_NAME
SELECT CUSTOMER_NAME, CITY, STATE
FROM CUSTOMER_t
WHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)
ORDER BY STATE, CUSTOMER_NAME;
Note: the IN operator in this example allows you to include rows whose
STATE value is either FL, TX, CA, or HI. It is more efficient than separate
OR conditions
50
SELECT Example–
Categorizing Results Using the GROUP BY Clause
For use with aggregate functions
Divide table into subsets (by groups), then aggregate function values
are calculated for each group
Scalar aggregate: single value returned from SQL query with aggregate function
Vector aggregate: multiple values returned from SQL query with aggregate
function (via GROUP BY)
Eg: count the number of customers in each state
SELECT STATE, COUNT(STATE) AS NO_CUSTOMERS
FROM CUSTOMER_t
GROUP BY STATE;
51
SELECT Example– Qualifying Results by
Categories Using the HAVING Clause
For use with GROUP BY
Similar to a WHERE clause, except that the restriction is applied on
groups instead of on rows
Eg: Find the states with more than one customers
SELECT STATE, COUNT(STATE) AS NO_CUSTOMERS
FROM CUSTOMER_t
GROUP BY STATE
HAVING COUNT(STATE) > 1;
Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those
groups with total numbers greater than 1 will be included in final result
52
SELECT Example– Qualifying Results by
Categories Using the HAVING Clause
Example: list the product finish and average standard price for selected finishes
where the average standard price is less than 750
SELECT Product_Finish, AVG(Standard_Price) AS AVG_PRICE
FROM PRODUCT
WHERE Product_Finish IN ('Cherry', 'Natural Ash', 'Natural Maple', 'White
Ash')
GROUP BY Product_Finish
HAVING (AVG(Standard_Price) < 750)
ORDER BY Product_Finish
53
Figure 7-10
SQL statement
processing
order (adapted
from van der
Lans, p.100)
54
Using and Defining Views
Views simplify query command, provide users controlled access to tables
Base Table–table containing the raw data
Dynamic View
A “virtual table” created dynamically upon request by a user
No data actually stored; instead data from base table made available to user
Based on SQL SELECT statement on base tables or other views
You can think of views as stored queries
Materialized View
Copy or replication of data
Data actually stored
Must be refreshed periodically to match the corresponding base tables
55
Sample CREATE VIEW
CREATE VIEW EXPENSIVE_STUFF_V AS
SELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICE
FROM PRODUCT_T
WHERE UNIT_PRICE >300
WITH CHECK OPTION;
View has a name
View is based on a SELECT statement
CHECK OPTION—applied to updatable
view
56
Sample CREATE VIEW
Update of view will result in a update of the base table
from which the view is derived
Check options
Cause the UPDATE or INSERT statement to be rejected if the
statements would cause updated or inserted rows to be
removed from the view.
Eg: the following statement will be rejected
UPDATE EXPENSIVE_STUFF_V
SET STANDARD_PRICE=295
WHERE PRODUCT_ID=5;
57
Advantages of Views
Simplify query commands
Assist with data security (but don't rely on views for
security, there are more important security measures)
Enhance programming productivity
Contain most current base table data
Use little storage space
Provide customized view for user
Establish physical data independence
58
Disadvantages of Views
Use processing time each time view is referenced
May or may not be directly updateable
59
Thanks