Slides - California State University Channel Islands

advertisement
Introduction to
Relational Database and SQL
Minder Chen
CSU Channel Islands
Minder.Chen@CSUCI.EDU
What is a RDBMS?
• A Data Base (DB) is an integrated collection of
shared data designed to meet the varied
information needs of an organization.
• A Data Base Management System (DBMS)
consists of a collection of software program
that receives and satisfies all requests for data.
• A Relational Data Base Management System
(RDBMS) is a DBMS which processes data with
the Relational Data Model.
©Minder Chen, 1996-2011
RDBMS&SQL - 2
History of RDBMS
• The relational database model was originally developed
by Dr. E.F. Codd in the early 1970s
• The SQL language was originally developed by IBM in a
prototype relational database management system,
System R, in the mid 1970s.
• The original SQL language (SEQUEL 2) was described in
IBM Journal of R&D, November 1976.
• In 1979, Oracle Corporation introduced the first
commercially available implementation of SQL.
• SQL has also been implemented in IBM's DB2 and
SQL/DS database systems in the mid 1980s.
• Today SQL is widely implemented and is accepted as
the industry standard database access language.
©Minder Chen, 1996-2011
RDBMS&SQL - 3
Database Technology
Before Relational systems people has been using:
- File Systems like ISAM, VSAM and B-Tree;
- Hierarchy Database;
- Network Database.
RDBMS has both a physical and a logical structure. By
doing so, the physical storage of data can be managed
without affecting the access to logical storage
structures.
File Systems
©Minder Chen, 1996-2011
Hierarchy
Network
Relational
RDBMS&SQL - 4
Relational Database Introduction
• A relational data base is perceived by its users
as a collection of tables
• Invented by E. F. Codd in 1969
• Dominate the markets since late 1980s
• Strengths:
– Simplicity
– End-user orientation
– Standardization (SQL – Structured Query Language)
– Value-based instead of pointer-based to provide
data independence
– Endorsed by major computer companies
• Most CASE products support the development
of relational data base centered applications
©Minder Chen, 1996-2011
RDBMS&SQL - 5
SQL Terminology
Column
Product Table
Row
Set Theory
Relation
Attribute
Tuple
Domain
p_no
name
quantity
price
101
Color TV
24
500
201
B&W TV
10
250
202
PC
5
2000
Relational DB
Table
Column
Row
Data Type
or Domain
©Minder Chen, 1996-2011
File
File
Data item
Record
Data type
Example
Product_table
Product_name
Product_101's info.
DATE
RDBMS&SQL - 6
SQL Terminology
CREATE TABLE PRODUCT
(p_no
name
quantity
price
CHAR(5)
NOT NULL,
CHAR(20),
SMALLINT,
DECIMAL(10, 2));
INSERT INTO PRODUCT VALUES (101, 'Color TV', 24, 500);
INSERT INTO PRODUCT VALUES (201, 'B&W TV ', 24, 500);
INSERT INTO PRODUCT VALUES (202, 'PC', 5, 2000);
SELECT p_no, name, price
FROM PRODUCT WHERE PRICE > = 1000;
p_no
--------202
name
----------PC
©Minder Chen, 1996-2011
price
---------2000
In Access
CREATE TABLE PRODUCT
(p_no CHAR(5) NOT NULL,
Name CHAR(20),
Quantity INTEGER,
Price Currency,
CONSTRAINT pk_product
PRIMARY KEY (p_no));
RDBMS&SQL - 7
SQL: Data Manipulation Language (DML)
SELECT
UPDATE
INSERT
DELETE
p_no
name
quantity
price
101
201
202
Color TV
B&W TV
PC
24
10
5
500
250
2000
SELECT p_no, name, price
FROM PRODUCT
WHERE PRICE < = 1000
ORDER BY PRICE DESC;
©Minder Chen, 1996-2011
p_no
------101
201
name
--------------Color TV
B&W TV
price
---------500
250
RDBMS&SQL - 8
SQL Principles
• The result of a SQL query is always in a table like format
and is not stored (View or Dynamic Table). The definition
of a query can be stored.
• Rows in a table are considered to be unordered.
• Columns sequence in a table is irrelevant in your
database queries.
• Can be used in interactive programming environments.
• Provide both data definition language (DDL), data
manipulation language (DML), and data control language
(DCL).
• It is a non-procedural language.
• Can be embedded in 3GL (third generation languages),
such as C, COBOL, etc.:
–
–
Embedded SQL: Embedding SQL statements in host languages.
Dynamic SQL: Constructing SQL statements dynamically.
©Minder Chen, 1996-2011
RDBMS&SQL - 9
Database Application in the Client/Server Environments
Customer Management
CUI
client
Networks
Name: John Doe
Address: 101 Dalmation Street
Salary: $43,000
Age: 34
PF1 - Help
PF2 - Add
PF3 - Update
Enter details and press PF3 to update
File
GUI
client
Edit
Customer Management
Window Help
Name:
Address:
John Doe
101 Dalmation St.
Salary:
$48,000
Age:
34
Loan History
Update
©Minder Chen, 1996-2011
Cancel
Database
Server
Disks &
Files
RDBMS&SQL - 10
Database Server in the Client-Server Architecture
Server Machine
Client Machine
2
1
Client
Application
User
5
3
Database
Server
4
3
File Server
1. User enters query through client application's user
interface.
2. Client application sends the formulated query to the data
base server.
3. Database server processes request. Search all records.
4. Database server returns only the results from the query.
5. Client application's user interface displays retrieved
information.
Mechanism: SQL via SQL*Net, ODBC, Stored Procedures,
Remote Procedure Calls
Access is not a database server.
©Minder Chen, 1996-2011
RDBMS&SQL - 11
Software Layers Using Oracle as Example
Custom-build Oracle Database Applications
Developer/2000 (Forms, Reports, Graphics)
JDeveloper Procedure Builder
SQL*Plus
PL/SQL
SQL
Designer/2000
Oracle Server
Operating Systems
Data
Visual Web Developer
Visual Studio .NET
©Minder Chen, 1996-2011
RDBMS&SQL - 12
Users
Naïve users
(tellers, agents,etc)
Application
programmers
Application
interface
Application
programs
object code
Transaction
manager
Sophisticated
users
3GL & 4GL
Embedded
DML
precompiler
Query
tools
Database
schema
DML
DDL
precompiler interpreter
Query
evaluation
engine
Buffer
manager
Indices
Data files
DBMS
Query
processor
Storage
manager
File
manager
• 3GL: Third Generation
Languages such as C
and COBOL
• 4GL: Fourth
Generation Language
such as Visual Basic
and PowerBuilder
Database
Administrator
Statistical data
Data dictionary
Disk
storage
Database Structures
The database structures should be understood in different
views at different stages or for different viewers. In 1974,
the following conceptual framework was introduced in a
conference at New Orleans city, so called New Orleans
Schema. It is also known as ANSI/X3/SPARC DBMS
Framework.
External level
(Individual User View)
Application
Schema 1
Conceptual
Schema
Application
Schema N
Logical
Schema
Logical
Database
Conceptual View
e. g., E-R Diagram
(Community User View)
Physical
Schema
Internal Level
(Storage View)
©Minder Chen, 1996-2011
Physical
Database
* E-R Diagram: Entity Relationship Diagram
RDBMS&SQL - 14
Benefits of RDBMS
• Integration of Data: Data are organized into a single, logical
structure with logical relationships defined between
associated data entities to eliminate redundancy and
inconsistency.
• Sharing of Data: All qualified users in the organization have
access to the same data, for use in a variety of activities or
applications.
• Enforcement of Standards: Centralized data administration
function has authority for defining and enforcing data
standards. All changes to the data standards would have to
be approved by the Database Administrator.
• Ease of Application Development: An end user/
programmer is no longer saddled with the burden of
designing, building and maintaining traditional master files.
©Minder Chen, 1996-2011
RDBMS&SQL - 15
Benefits of RDBMS
• Uniform Security, Privacy and Integrity Controls: The
data administration function has complete jurisdiction over
the database and is responsible for establishing controls for
accessing, updating and protection data. Centralized control
and standard procedures can offer improved level of data
base protection.
• Data Accessibility and Responsiveness: Provides multiple
retrieval paths to each item of data, giving a user much
greater flexibility in locating and retrieving data. A database
system is generally much more responsive to changing
information requirements.
• Data Independence: The separation of data descriptions
from application programs that use the data.
©Minder Chen, 1996-2011
RDBMS&SQL - 16
Benefits of RDBMS
• Reduce Program Maintenance: Due to the data
independence in a database system, within limits, either
the data or the application program that use the data can
be changed without necessitating a change in the others.
• User Friendly: SQL is a non-procedural easy-to-learn
language. It allows you to work with higher level data
structures. Rather than manipulating single rows. A user
can gets the desired data simply by specifying SQL
without go through complex procedure programming
language.
• Supported by Mathematics Theory: RDBMS is the only
DBMS that is supported by mathematics theory. The
relational algebra is formed by a set of operators, each of
which maps one or more relations into another relation.
©Minder Chen, 1996-2011
RDBMS&SQL - 17
The Relational Model
•
•
•
•
A model represents both entities and relationships in a
flat file (table) structure.
Relation: A mathematical concept about an entity or a
relationship.
Properties of a Relation:
- No duplicate rows in a relation (table).
- The order of rows in a relation is insignificant
- The order of columns in a relation is insignificant.
- All attributes must be atomic (single value).
- Each row must be uniquely identified by a primary key.
In the relational model, data are logically presented in two
dimensional tables (files) made up of rows (tuples,
records) and columns (attributes, fields, data item).
©Minder Chen, 1996-2011
RDBMS&SQL - 18
The Relational Model
• Table: A table is a group of homogeneously
defined records, all with identical field names and
types. Within a database, each table has a unique
name.
• Row: The rows of a table are known as records.
They are made up of a collection of values that
describe an entity.
• Field: A field is the intersection of a row and a
column in a table or query result.
• Key: An attribute or a set of attributes which can
uniquely identify a row in a table.
©Minder Chen, 1996-2011
RDBMS&SQL - 19
The Relational Model
DIVISION
PK
FK
D_ID
D010
D020
D030
D040
D050
DIR
52
1
1
1
1
DIV_NAME
ADMINISTRATION
COMPUTER SCIENCE
ACCOUNTING
STATISTICS
MATHEMATICS
PK
INSTRUCTOR
©Minder Chen, 1996-2011
I_NO
1
19
21
52
F_NAME
PATRICK
ALBERT
BRUCE
JEAN
M
D
Y
C
E
L_NAM
LEE
STONE
FLYNN
LOU
DIV
D020
D020
D020
D010
RDBMS&SQL - 20
D
08
22
14
02
The Relational Model
• Primary Key: A key chosen to identify the rows in
a table.
• Simple Key: A key contains only one column.
• Compound(Composite, Concatenated) Key: A
key has more than one column.
• Alternate Key: A key which is other than the
primary key in a relation.
• Foreign Key: An attribute or a set of attributes
appears in a table which also plays the role of a
primary key in another tables.
• Question: What is the trick to implement
relationships in the relational model?
©Minder Chen, 1996-2011
RDBMS&SQL - 21
The Relational Model
• Index: An index is a data structure used to speed up
data retrieval or enforce certain constraints on the
records in a table. Relational databases use automatic
query optimization algorithms to determine which indices to
use when searching the database, therefore the user does
not specify which indices to use in a search.
• Null value: A null value is an empty or undefined value for a
field. It indicates that there is presently no data available for
the field instance.
• View: A view is a logic representation of another table or
combination of tables. A view derives its data from the tables
on which it is based. Views permit you to query subsets of
data, or joins of multiple tables as though they are an actual
table. Only the view definition is stored in the database, not
the data retrieved by a view. Some views are updateable;
update commands actually affect the underlying table. View
is called (stored) Query in Access.
©Minder Chen, 1996-2011
RDBMS&SQL - 22
The Relational Model
• Referential Integrity: The relationship between a
referencing (child) table and a referenced (parent)
table. The referential integrity rule requires that, for any
value in the referencing columns, there must exist a
row in the referenced table such that the value of the
referencing columns equals the value of the
corresponding referenced columns.
• Essence of a Relational Data Model:
– Representation of Entities
– Representation of Relationship
– Database Integrity Rules:
» Primary Key Rule
» Referential Integrity Rule
» Existing Rule (NOT NULL)
» Value Checking Rule
– DB Languages: DDL, DML, DCL
©Minder Chen, 1996-2011
RDBMS&SQL - 23
Foreign Keys & Primary Keys in a Sample Database (ACCESS)
©Minder Chen, 1996-2011
RDBMS&SQL - 24
SQL Programming
• SQL is a standard language used to define and
manipulate data in a RDBMS, and can be used
interactively or embedded in host languages
such as C, PASCAL, COBOL, etc. SQL
generally functions on sets of records rather
than individual records. References to data are
symbolic; they do not use any physical data
structures such as pointers.
• SQL are divided into three sublanguages:
– DDL (Data Definition Language)
– DML (Data Manipulation Language)
– DCL (Data Control Language)
©Minder Chen, 1996-2011
RDBMS&SQL - 25
SQL Programming
• Data Definition Language (DDL)
–
–
–
–
Create, alter and drop database and database objects
Impose integrity constraints on tables
Automatically update system catalog tables
Add comments to the tables.
• Data Manipulate Language (DML)
–
–
–
–
Retrieve,
insert,
update, and
delete rows from database tables.
• Data Control Language (DCL)
– Grant and Revoke privileges and roles
– Controls concurrent access to database.
©Minder Chen, 1996-2011
RDBMS&SQL - 26
CREATE TABLE
Data Type
Table name
Mandatory or
Optional
CREATE TABLE division
(D_ID
CHAR(4) NOT NULL,
DIR
NUMBER(4),
DIV_NAME VARCHAR(15) NOT NULL,
CONSTRAINT pk_division
A Constraint
PRIMARY KEY (D_ID));
Name
* The Primary Key column(s) of
a table should be "NOT NULL"
©Minder Chen, 1996-2011
A Constraint
Type
Primary Key
Column(s)
RDBMS&SQL - 27
Create a Table in Access
CREATE TABLE division2
(D_ID
CHAR(4)
NOT NULL,
DIR
Integer,
DIV_NAME
VARCHAR(15) NOT NULL,
CONSTRAINT pk_division PRIMARY KEY (D_ID));
What is AutoNumber in Access?
©Minder Chen, 1996-2011
RDBMS&SQL - 28
Basic Elements
Character Set
–
–
–
–
The upper- and lower-case letters A .. Z, a .. z
The numerals 0 .. 9
Tabs, spaces, and carriage returns
The symbols ( ) + - * / < > = ! ~ ; : . ' @ % , " # $ ^ & |
_ { } ? [ ]
Identifiers An identifier consists of a letter optionally
followed by more letters, numerals and the special
characters ‘#’, ‘$’ , ‘_’, ‘%’, ‘&’.
Comments
– Single-line comments begin with a double hyphen (--)
anywhere on a line and extend to the end of the line.
– Multiline comments begin with a slash-asterisk (/*),
end with an asterisk-slash (*/), and can span multiple
lines.
©Minder Chen, 1996-2011
RDBMS&SQL - 29
Data Types
• Every constant and variable has a data type, which
specifies a storage format, constraints, and valid
range of values.
– NUMBER: General numeric type.
– DEC(p,s): Decimal value. Precision(p) is the total
number of digits. Scale(s) is the number of fractional
digits. E.g., 9999.99 should be declared as DEC(6,2)
– FLOAT: Single precision floating-point number.
– SMALLINT: Half word (2 bytes) binary integer. Value
can range from -32767 to 32767.
– INT: Full word(4 bytes) binary integer. Value can range
from -2147483647 to 2147483647.
– CHAR: Fixed length character string.
– VARCHAR: Variable length character string.
– DATE: Data stored in Gregorian date format.
©Minder Chen, 1996-2011
RDBMS&SQL - 30
NULL
DATA ELEMENT CHARACTERISTIC
A
B
C
NOT NULL
must be a given value,
including blank or zero.
NULL
Value not required.
(Column flagged null -- “empty value”)
NOT NULL WITH DEFAULT
will provide default value if one
is not entered.
©Minder Chen, 1996-2011
RDBMS&SQL - 31
NULL
NOTE: In SQL, NULL is an empty value, it is not
equal to blank or space. Be aware of the results
when use NULL in:
– COMPARISON:
Result is unknown
– COMPUTATION:
Result is unknown
– FUNCTIONS:
Null rows are omitted from AVG,
SUM, MAX and MIN functions.
– COUNT(DISTINCT):
©Minder Chen, 1996-2011
Does not count NULLS.
RDBMS&SQL - 32
Operators
Logical negation:
Identity:
Negation:
Multiplication:
Division:
Addition:
Subtraction:
Concatenation:
Comparison:
NOT
+
*
/
+
||
=, !=, <, >, <=, >= ,
IS NULL, LIKE, BETWEEN,
IN, EXIST,ALL, SOME, ANY
Conjunction:
Inclusion:
©Minder Chen, 1996-2011
AND
OR
RDBMS&SQL - 33
PRIMARY KEY
• A PRIMARY KEY constraint is identical to a
UNIQUE constraint, except that a table can have
at most one constraint condition specified as a
PRIMARY KEY. The purpose of the primary key
is to uniquely identify each row of a table.
• The fields in the primary key must be defined as
NOT NULL. A unique index will be built
automatically by RDBMS on the primary key.
©Minder Chen, 1996-2011
RDBMS&SQL - 34
FOREIGN KEY
• FOREIGN KEY is used to enforce referential integrity -- the
relationship between referencing (child) table and a
referenced (parent) table.
• The referential integrity rule requires that, for any value in
the referencing columns, there must exist a row in the
referenced table such that the value of the referencing
columns equals the value of the corresponding referenced
columns.
• This is enforced as the following:
–
–
UPDATE/INSERT Rule: The set of values placed in the referencing
columns must match a set of values that exists in the referenced
table.
DELETE Rules (When you delete rows of the referenced table):
» RESTRICT: The row in referenced table can be deleted if no other row
referencing it.
» SET NULL: That each nullable column of the matched foreign key in
each referencing row is set to null.
» CASCADE: The designated rows in the referenced table and all match
referencing rows will be deleted.
©Minder Chen, 1996-2011
RDBMS&SQL - 35
FOREIGN KEY
• In order to create a referential integrity constraint, you must
create a FOREIGN KEY constraint on the referencing table.
The FOREIGN KEY constraint must reference the PRIMARY
(or a UNIQUE) KEY on the referenced table.
Referencing
table
Column name(s) of the
referencing table
alter table OFFERING
add constraint FK_OFFERING_OFFERS_FOR_COURSES foreign key (C_ID)
references COURSE (C_ID) ON DELETE CASCADE;
Referenced
table
PK in the
Referenced table
• The foreign key must reference an existing UNIQUE or
PRIMARY KEY. An index will be built on the referencing
columns.
create index Offers_for_Courses_FK on OFFERING (C_ID asc)
©Minder Chen, 1996-2011
RDBMS&SQL - 36
Sample Database
REGISTRATION
P,F
C_ID
P,F
O_NO
P,F
S_NO
GRADE
OFFERING
P,F
C_ID
P
O_NO
START_DATE
LOCATION
F
I_NO
©Minder Chen, 1996-2011
STUDENT
P
S_NO
NAME
ADDRESS
CITY
STATE
ZIP
TEL_NO
INSTRUCTOR
P
I_NO
F_NAME
M
L_NAME
F
DIV
D_O_H
TITLE
ED
SEX
D_O_B
SALARY
COURSE
P
C_ID
TITLE
F
DIV
FEE
DIVISION
P
D_ID
F
DIR
DIV_NAME
RDBMS&SQL - 37
Relationships in Access Database
©Minder Chen, 1996-2011
RDBMS&SQL - 38
Edit Relationships in Access
• Referential Integrity
©Minder Chen, 1996-2011
RDBMS&SQL - 39
DDL
• CREATE DATABASE: Creates a database.
• CREATE TABLE: Creates a table and defines its
columns and their data types.
• CREATE SYNONYM: Creates an alternate name for a
table or view.
• CREATE VIEW: Defines a logical table, or view of data
derived from columns and rows of existing tables and
views.
• CREATE INDEX: Creates an index on one or more
columns of a table, for the purpose of speeding data
retrieval, and enforcing uniqueness constraints.
• ALTER: Adds, removes or renames table columns, or
changes the data types of columns. Also used to create
or drop UNIQUE, PRIMARY, FOREIGN KEY and CHECK
constraints to enforce uniqueness, referential integrity.
©Minder Chen, 1996-2011
RDBMS&SQL - 40
DDL
• DROP DATABASE: Deletes database and all objects
associated with it.
• DROP TABLE: Deletes a table and the indexes built on
that table.
• DROP SYNONYM: Deletes an alternate name for a table
or view from the system catalog.
• DROP VIEW: Deletes a view from the system catalog.
• DROP INDEX: Deletes a specified index. Note that an
index created as the result of a UNIQUE, PRIMARY or
FOREIGN KEY constraint can only be dropped by
dropping the constraint through the ALTER command.
• RENAME: Changes a table name.
• COMMENT ON: Places a remark on a database object in
the appropriate system catalog table.
©Minder Chen, 1996-2011
RDBMS&SQL - 41
DML
• SELECT: Retrieves data from one or more
tables.
• UPDATE: Modifies the data in one or more
rows of a table.
• INSERT: Inserts one or more rows into a table.
• DELETE: Deletes one or more rows from a
table.
©Minder Chen, 1996-2011
RDBMS&SQL - 42
DCL
• COMMIT: Instructs the system to make all DML
commands executed by a transaction
permanent.
• ROLLBACK: Instructs the system to reverse all
DML commands executed by a transaction.
• GRANT: Assigns database access privileges to
database users.
• REVOKE: Cancels database access privileges
from database users.
• LOCK: Permits users to explicitly acquire table
locks.
• UNLOCK: Used to unlock a table prior to the
commit point.
©Minder Chen, 1996-2011
RDBMS&SQL - 43
CREATE TABLE: Data Type in Access
CREATE TABLE STUDENT
(S_NO
NUMBER(4) NOT NULL,
NAME
VARCHAR(10),
ADDRESS VARCHAR(10),
CITY
VARCHAR(10),
STATE
VARCHAR(2),
ZIP
NUMBER(5),
TEL_NO VARCHAR(10),
CONSTRAINT S_NO_ZERO
CHECK (S_NO > 0),
CONSTRAINT
STUDENT_PRIMARY_KEY
PRIMARY KEY (S_NO));
NOTE:
The fields in the primary key must be defined as NOT NULL.
©Minder Chen, 1996-2011
RDBMS&SQL - 44
CREATE TABLE
CREATE TABLE INSTRUCTOR
(I_NO NUMBER(4)
NOT NULL,
F_NAME VARCHAR(8) NOT NULL,
M
CHAR(1)
NOT NULL,
L_NAME VARCHAR(5) NOT NULL,
DIV
CHAR(4),
TEL
VARCHAR(10),
D_O_H
DATE,
TITLE
VARCHAR(8),
ED
SMALLINT,
SEX
CHAR(1),
D_O_B
DATE,
SALARY DECIMAL(9,2),
CONSTRAINT INSTRUCT_FOREIGN_KEY
FOREIGN KEY (DIV)
REFERENCES DIVISION(D_ID),
CONSTRAINT INSTRUCT_PRIMARY_KEY
PRIMARY KEY (I_NO));
©Minder Chen, 1996-2011
RDBMS&SQL - 45
CREATE TABLE
CREATE TABLE COURSE
( C_ID CHAR(4)
NOT NULL,
TITLE VARCHAR(10) NOT NULL,
DIV
CHAR(4)
NOT NULL,
FEE
DECIMAL(5,2),
CONSTRAINT COURSE_FOREIGN_KEY
FOREIGN KEY (DIV)
REFERENCES DIVISION(D_ID),
CONSTRAINT COURSE_PRIMARY_KEY
PRIMARY KEY (C_ID));
©Minder Chen, 1996-2011
RDBMS&SQL - 46
CREATE TABLE
CREATE TABLE OFFERING
(C_ID
CHAR(4)
NOT NULL,
O_NO
NUMBER(4) NOT NULL,
START_DATE DATE
NOT NULL,
LOCATION VARCHAR(12),
CONSTRAINT OFFERING_FOREIGN_KEY
FOREIGN KEY (C_ID)
REFERENCES COURSE(C_ID)
ON DELETE CASCADE,
CONSTRAINT OFFERING_PRIMARY_KEY
PRIMARY KEY (C_ID, O_NO));
©Minder Chen, 1996-2011
RDBMS&SQL - 47
CREATE TABLE
CREATE TABLE REGISTRATION
(C_ID
CHAR(4)
NOT NULL,
O_NO
NUMBER(4)
NOT NULL,
S_NO
NUMBER(4)
NOT NULL,
GRADE
SMALLINT,
CONSTRAINT
REGISTRATION_FOREIGN_KEY
FOREIGN KEY (C_ID,O_NO)
REFERENCES OFFERING(C_ID, O_NO)
ON DELETE CASCADE,
CONSTRAINT STUDENT_FOREIGN_KEY
FOREIGN KEY (S_NO)
REFERENCES STUDENT(S_NO),
CONSTRAINT REGISTRATION_PRIMARY_KEY
PRIMARY KEY (C_ID, O_NO, S_NO));
©Minder Chen, 1996-2011
RDBMS&SQL - 48
Create Index
This command creates an index on one or more
columns of a table.
TYPE OF INDEXES
– UNIQUE INDEX: An index built on a unique columns (or
columns) in a table.
– NON-UNIQUE INDEX: An index built on a columns (or
columns) which may have the same value in more than
one row.
EXAMPLE
CREATE UNIQUE INDEX DIVISION_NAME
ON DIVISION(DIV_NAME ASC);
CREATE INDEX LOC_INDEX
ON OFFERING(LOCATION DESC);
©Minder Chen, 1996-2011
RDBMS&SQL - 49
Create View
This command is used to define a view (a logical or
virtual table) based on one or more (base) tables or
views. In Access, views are saved queries.
CREATE VIEW TEACHER AS
SELECT I_NO, F_NAME, L_NAME, TITLE, SALARY
FROM INSTRUCTOR
WHERE TITLE = 'TEACHER';
SELECT * FROM TEACHER;
I_NO F_NAME
L_NAM TITLE
SALARY
--------- -------- ----- -------- --------19 ALBERT
STONE TEACHER
60000
21 BRUCE
FLYNN TEACHER
48000
©Minder Chen, 1996-2011
RDBMS&SQL - 50
Using a View
Query against a saved query
SELECT * FROM TEACHER
WHERE SALARY >= 50000;
I_NO F_NAME
L_NAM TITLE
SALARY
--------- -------- ----- -------- --------19 ALBERT
STONE TEACHER
60000
The query is merged with the defining query of the view
TEACHER to produce the following query to be executed by
the DBMS:
SELECT I_NO, F_NAME, L_NAME, TITLE, SALARY
FROM INSTRUCTOR
WHERE TITLE = 'TEACHER' and SALARY >= 50000;
DROP VIEW TEACHER; - - Delete the view
©Minder Chen, 1996-2011
RDBMS&SQL - 51
Create View
• Define a subset of a single table.
• Combine data from two or more tables.
• The data presented via the view is retrieved
dynamically from the base table.
• Restrictions:
– ORDER BY and UNION can not be used in the definition
of a view.
– A view is "read only" if its definition involves
»
»
»
»
»
A JOIN
The keyword DISTINCT
A GROUP BY clause
A HAVING clause
A function
– If you insert a row into a view, the base table columns
that are not in the view must allow NULLs or set to
default values.
©Minder Chen, 1996-2011
RDBMS&SQL - 52
Processing A View
PROCESS
VIEW
BASE TABLE
SELECT
UPDATE
COLUMN
DELETE
ROW
INSERT
ROW
?
©Minder Chen, 1996-2011
NULL or default
value
RDBMS&SQL - 53
Access DMBS: DMBS vs Database
©Minder Chen, 1996-2011
RDBMS&SQL - 54
Basic Structures and Contents of a Database
• Database Schema
(Meta Data)
– Tables
» Columns
• Column Name
• Data Types and Size
• Primary key
– Relationships
» Foreign Keys
– Indexes
• Actual data stored in
the Database
©Minder Chen, 1996-2011
RDBMS&SQL - 55
Relationships among Access Database Objects
Database Application
Form
Report
Table
Query
(View)
Basic Database Objects
•
•
•
•
A saved SELECT query is officially called View in SQL.
QUERY in Access can be SELECT, INSERT, UPDATE, or
DELETE.
You can create a query against a table or a query.
You can create a form or report against a table or a query.
©Minder Chen, 1996-2011
RDBMS&SQL - 56
Stakeholders of a Database Environment
Database Designer
Database
Query/Reporting
Tools
Internal End Users
Internal End Users
E-commerce
Web site
External End Users
DBA:
Database
Administrator
Advanced
Users
Application Programmers
©Minder Chen, 1996-2011
RDBMS&SQL - 57
Query
• Four types of operations against data in a
database
– CRUD: Create, Read, Update, Delete
– SQL Data Manipulation Language (MDL):
» Insert, Select, Update, Delete
• Select statement allows you to retrieve data from the
database (read only). It is also referred to as a query in
Access.
• A definition of a query (not the query result) written in
SQL statement can be saved in the database to be run
later.
• A saved query is called “View” in SQL and you can
created a query against a saved query (view)
©Minder Chen, 1996-2011
RDBMS&SQL - 58
SELECT
• The SELECT command is a versatile and
powerful command used to retrieve information
from the database. SELECT commands may be
executed alone, or embedded in CREATE,
UPDATE, INSERT and DELETE commands.
• The SELECT command is composed of 8
clauses:
SELECT [DISTINCT | ALL ] projection list
FROM table or view list
[WHERE search condition ]
[GROUP BY column list]
[HAVING search condition]
[ORDER BY {expr|position} [ ASC|DESC]]
[FOR UPDATE OF column list]*
[INTO host variable list]*
©Minder Chen, 1996-2011
RDBMS&SQL - 59
SELECT
• The SELECT clause and the FROM clause are
required for all SELECT commands.
• THE WHERE clauses are permitted in all
SELECT commands as a filter defining the
criteria of rows to be selected.
• The WHERE clause of a subquery (i.e.,
subselect) may also contain nested subquery.
• Subquery cannot contain the UNION operator
or an ORDER BY clause.
©Minder Chen, 1996-2011
RDBMS&SQL - 60
Create a new query: Choose Create > Query Design
Highlight a table or a query, click Add; or
Double click on a table or a query.
You may select more than one table when
performing SQL JOIN
©Minder Chen, 1996-2011
RDBMS&SQL - 61
Select Columns
You may select a column and use it in a
criteria or for sorting without show it’s
value in the query result.
• Field: Double click a column from the table to select it to
•
•
•
•
the query design form column, or drag and drop it to the
form (columns listed in the SQL SELECT clause);
Table: A table or query from the database (SQL FROM Clause)
Sort: Sorting the query result (SQL ORDER BY clause)
Show: Whether to display the column’s value in the query result
Criteria: Define criteria (filter) for rows in a table (SQL WHERE clause)
©Minder Chen, 1996-2011
RDBMS&SQL - 62
SQL Select and Query Design in Access
SELECT COURSE.C_ID, COURSE.TITLE, COURSE.FEE
FROM COURSE
WHERE COURSE.FEE>250 And COURSE.FEE)<=350
ORDER BY COURSE.FEE DESC;
©Minder Chen, 1996-2011
RDBMS&SQL - 63
Choose Design tab control under Query Tools.
Click on View
(not the Icon above it)
from the Toolbar:
• Datasheet: See the result of the query, i.e., RUN
the query
• SQL View: Edit SQL statement directly
• Design view: Create SQL via Query Design tool.
©Minder Chen, 1996-2011
RDBMS&SQL - 64
SELECT
Selecting All Columns:
EXAMPLE
SELECT * FROM STUDENT;
RESULT
S_NO NAME ADDRESS
CITY
ST ZIP
TEL_NO
-------- ---------- ---------------- ----------------- ------------- -------------5001 MARY 102 1ST ST FAIRFAX
VA 12345 7037772311
5002 TOM
39 5th AVE ARLINGTON DC 22313 2021112222
5003 LINDA 1 ROCK RD ROCKVILLE MD 20852 3019380975
5004 JAMES
7039784444
5005 KATHY 2 KING LA FAIRFAX
VA 12345 7037775534
5006 MIKE
P.O.BOX 1 FAIRFAX
VA 12345 7037771122
NULL values
©Minder Chen, 1996-2011
Null means that there is no data and it is
different from empty string.
RDBMS&SQL - 65
SELECT
Selecting some columns:
EXAMPLE
SELECT D_ID, DIV_NAME
FROM DIVISION;
RESULT
D_ID DIV_NAME
------- --------------D010 ADMINISTRATION
D020 COMPUTER SCIENCE
D030 ACCOUNTING
D040 STATISTICS
D050 MATHEMATICS
©Minder Chen, 1996-2011
RDBMS&SQL - 66
SELECT with a calculated column
Create a calculated column (Expression):
SELECT L_NAME, F_NAME, (SALARY-20000)*0.1 AS BONUS
FROM INSTRUCTOR;
RESULT
L_NAM
----LEE
STONE
FLYNN
LOU
F_NAME
BONUS
-------- --------PATRICK
5200
ALBERT
4000
BRUCE
2800
JEAN
200
SELECT L_NAME, F_NAME, ([SALARY]-20000)*0.1 AS [Annual BONUS]
FROM INSTRUCTOR;
©Minder Chen, 1996-2011
RDBMS&SQL - 67
SELECT Criteria
Select some rows:
EXAMPLE
SELECT L_NAME, M, F_NAME
FROM INSTRUCTOR
WHERE DIV = 'D020';
RESULT
L_NAM
----------LEE
STONE
FLYNN
M
---D
Y
C
F_NAME
-----------PATRICK
ALBERT
BRUCE
NOTE
In a WHERE Clause, values to be compared with text data
type must be enclosed in single quotation mark ( 'text' ).
In Access you can use both single quotes or double quotes.
©Minder Chen, 1996-2011
RDBMS&SQL - 68
SELECT
Finding NULL Columns
EXAMPLE
SELECT S_NO, NAME
FROM STUDENT
WHERE ADDRESS IS NULL;
RESULT
S_NO
--------5004
©Minder Chen, 1996-2011
NAME
---------JAMES
RDBMS&SQL - 69
SELECT Criteria -- Date format
Less Than, Greater Than, Equal: < > =
EXAMPLE
SELECT D_O_H, F_NAME, L_NAME
FROM INSTRUCTOR
WHERE D_O_H < '01-JAN-90'
In Access SQL
ORDER BY D_O_H;
#01/01/1990#
RESULT
D_O_H
--------------08-MAR-83
02-SEP-88
©Minder Chen, 1996-2011
F_NAME L_NAM
--------------- -------------PATRICK
LEE
JEAN
LOU
RDBMS&SQL - 70
SELECT Criteria
Less Than, Greater Than, Equal: < > =
EXAMPLE
SELECT L_NAME, I_NO, ED
FROM INSTRUCTOR
WHERE ED <= 10;
RESULT
L_NAM
I_NO
----------- --------FLYNN
21
LOU
52
©Minder Chen, 1996-2011
ED
----10
08
RDBMS&SQL - 71
SELECT Criteria
Multiple Conditions: AND
EXAMPLE
SELECT I_NO, D_O_H, SALARY
FROM INSTRUCTOR
WHERE D_O_H < '31-DEC-90' AND SALARY < 26000;
RESULT
AND
I_NO D_O_H
SALARY
--------- --------------- --------52 02-SEP-88
22000
©Minder Chen, 1996-2011
RDBMS&SQL - 72
SELECT Criteria
Multiple Conditions: OR
EXAMPLE
SELECT I_NO, D_O_H, SALARY
FROM INSTRUCTOR
WHERE D_O_H < '31-DEC-90' OR SALARY < 26000;
RESULT
I_NO
--------1
52
OR
D_O_H
-----------8-MAR-83
02-SEP-88
©Minder Chen, 1996-2011
SALARY
------------72000
22000
RDBMS&SQL - 73
SELECT Criteria
Multiple Conditions: AND, OR
EXAMPLE
SELECT I_NO, D_O_H, SALARY
FROM INSTRUCTOR
WHERE D_O_H < '01-JAN-90'
AND (SALARY < 40000 OR ED < 11);
RESULT
I_NO D_O_H
SALARY
--------- ------------- -----------52 02-SEP-88 22000
©Minder Chen, 1996-2011
RDBMS&SQL - 74
SELECT Criteria
Selecting All Rows Except ... : NOT, <>, ^=
EXAMPLE
SELECT D_ID, DIR
FROM DIVISION
WHERE NOT (D_ID <= 'D010');
RESULT
D_ID
DIR
-----------D020
1
D030
1
D040
1
D050
1
NOTE
– The NOT keyword must precede the condition.
©Minder Chen, 1996-2011
RDBMS&SQL - 75
Selecting Rows ...LIKE...
SELECT NAME, CITY
FROM STUDENT
WHERE CITY LIKE 'FAIR%';
- - match 'Fairlake'
SELECT NAME, CITY
FROM STUDENT
WHERE CITY LIKE 'FAIR_ _ _'; - - does not match 'Fairlake'
RESULT: Both query get same results.
NAME
---------MARY
KATHY
MIKE
CITY
---------FAIRFAX
FAIRFAX
FAIRFAX
SELECT NAME, CITY
FROM STUDENT
WHERE CITY LIKE '%FAIR%';
- - match 'Fairlake' and 'Myfaircity'
***NOTE***
– Use a percent sign (%) to indicate any string of zero or more
characters. Use * in Access instead of %
– Use an underscore (_) for any single character. (use ? In Access)
– Use the LIKE keyword only with data type CHAR, VARCHAR not with
NUMERIC or DATE.
©Minder Chen, 1996-2011
RDBMS&SQL - 76
SELECT Values Within a Range
EXAMPLE
SELECT D_ID, DIR
FROM DIVISION
WHERE D_ID BETWEEN 'D010' AND 'D030'
ORDER BY D_ID;
- - WHERE D_ID >= 'D10' AND D_ID <= 'D030'
RESULT
D_ID
DIR
------- --------D010
52
D020
1
D030
1
©Minder Chen, 1996-2011
RDBMS&SQL - 77
Selecting Values Within a Range
SELECT I_NO, SALARY
FROM INSTRUCTOR
WHERE SALARY NOT BETWEEN 30000 AND 50000;
- - WHERE SALARY < 30000 OR SALARY > 50000
RESULT
I_NO
SALARY
--------- -----------1
72000
19
60000
52
22000
©Minder Chen, 1996-2011
RDBMS&SQL - 78
SELECT ORDER BY: Showing Values in Sequence
ORDER BY: (ASC or DESC) Ascending Order is the default.
SELECT D_ID, DIR
FROM DIVISION
ORDER BY D_ID;
RESULT
D_ID
------D010
D020
D030
D040
D050
DIR
--------52
1
1
1
1
NOTE: All columns named in the ORDER BY clause must be named in
the SELECT clause.
(In Access the above statement is NOT TRUE.
Sorting columns don’t have to be selected for display.)
©Minder Chen, 1996-2011
RDBMS&SQL - 79
SELECT: ORDER BY clause
EXAMPLE: In Descending Order
SELECT D_ID, DIR FROM DIVISION
ORDER BY D_ID DESC;
- - ASC (Ascending) is the default sorting order
- - The default order for character fields is a case sensitive
ASCII sequence
RESULT
D_ID
------D050
D040
D030
D020
D010
DIR
--------1
1
1
1
52
©Minder Chen, 1996-2011
RDBMS&SQL - 80
SELECT: ORDER BY column number
EXAMPLE: Order by the first column
1
2
SELECT D_ID, DIR FROM DIVISION
ORDER BY 1;
RESULT
D_ID
DIR
------- --------D010
52
D020
1
D030
1
D040
1
D050
1
©Minder Chen, 1996-2011
RDBMS&SQL - 81
Top N Rows
• TOP n [PERCENT]: Returns a certain number of
records that fall at the top or the bottom of a
range specified by an ORDER BY clause.
• In Access you need to add TOP N qualifier in
SQL View
SELECT TOP 2 L_NAME,F_NAME, SALARY
FROM INSTRUCTOR
ORDER BY INSTRUCTOR.SALARY DESC;
©Minder Chen, 1996-2011
RDBMS&SQL - 82
SELECT: ORDER BY sort-list (multiple columns)
INSERT INTO
INSTRUCTOR (I_NO, L_NAME, M, F_NAME, SALARY)
VALUES (60, 'SMITH', 'F', 'BOB', 60000);
SELECT L_NAME, F_NAME, SALARY * 0.1 AS BONUS
FROM INSTRUCTOR ORDER BY Bonus DESC, L_NAME;
- - primary order column is BONUS
- - Secondary column is L_NAME
L_NAM
----LEE
SMITH
STONE
FLYNN
LOU
F_NAME
BONUS
-------- --------Access SQL: (cannot use alias column
PATRICK
7200
BOB
6000
name in Order By clause)
ALBERT
6000
SELECT L_NAME, F_NAME, SALARY * 0.1 AS BONUS
BRUCE
4800
FROM INSTRUCTOR
JEAN
2200
©Minder Chen, 1996-2011
ORDER BY 3 DESC, L_NAME;
RDBMS&SQL - 83
ORDER BY the Position of Columns
SELECT L_NAME, F_NAME, SALARY * 0.1
FROM INSTRUCTOR
ORDER BY 3 DESC, L_NAME;
Sort secondly by
L_NAME (asc)
L_NAM
----LEE
SMITH
STONE
FLYNN
LOU
Sort first by third column in
descending order
F_NAME
SALARY*0.1
-------- ---------PATRICK
7200
BOB
6000
ALBERT
6000
BRUCE
4800
JEAN
2200
©Minder Chen, 1996-2011
RDBMS&SQL - 84
SELECT
Concatenation Operations ||
EXAMPLE
SELECT L_NAME | | ', ' | | F_NAME | | ' ' | | M AS NAME
FROM INSTRUCTOR;
Access SQL: (use & instead of || for string concatenation)
SELECT L_NAME & ', ' & F_NAME & ' ' & M AS NAME
FROM INSTRUCTOR;
RESULT
NAME
--------------------------LEE, PATRICK D
STONE, ALBERT Y
FLYNN, BRUCE C
LOU, JEAN E
©Minder Chen, 1996-2011
A space
character
RDBMS&SQL - 85
SELECT: IN
Selecting Value in or not in a List:
EXAMPLE
SELECT D_ID, DIR
FROM DIVISION
WHERE D_ID IN ('D010', 'D020', 'D040')
ORDER BY D_ID;
RESULT
D_ID
DIR
------- --------SELECT D_ID, DIR
D010
52
FROM DIVISION
D020
1
WHERE D_ID NOT IN ('D010', 'D020', 'D040')
D040
1
ORDER BY D_ID;
©Minder Chen, 1996-2011
RDBMS&SQL - 86
SELECT
Expressions in Conditions:
EXAMPLE
SELECT I_NO, L_NAME, SALARY
FROM INSTRUCTOR
WHERE SALARY > 800 * 52;
RESULT
I_NO
--------1
19
21
L_NAM
SALARY
----- --------LEE
72000
STONE
60000
FLYNN
48000
©Minder Chen, 1996-2011
RDBMS&SQL - 87
INSERT
INSERT INTO [table-name | view-name]
[(column-name1, column-name2, …)]
VALUES (value1, value2, …);
Adding one or more row(s) into an existing table.
– Table with Foreign Keys
» Target (referenced column) of foreign key must exist
» NULL value of the foreign key (referencing column)
allowed
– CHECK Constraint
INSERT INTO DIVISION
VALUES('D010',52,'ADMINISTRATION');
©Minder Chen, 1996-2011
RDBMS&SQL - 88
INSERT Examples
INSERT INTO DIVISION
VALUES('D060',NULL,'MARKETING');
-- This does not work in Access
INSERT INTO DIVISION (DIV_NAME, D_ID)
VALUES('R&D', 'D070');
D_ID
DIR DIV_NAME
---- --------- ---------------D010
52 ADMINISTRATION
D020
1 COMPUTER SCIENCE
D030
1 ACCOUNTING
D040
1 STATISTICS
D050
1 MATHEMATICS
D060
MARKETING
D070
R&D
©Minder Chen, 1996-2011
RDBMS&SQL - 89
Access Append Query (Insert)
INSERT INTO DIVISION ( D_ID, DIV_NAME )
SELECT 'D060' AS Expr1, 'Marketing' AS Expr2;
INSERT INTO DIVISION ( D_ID, DIV_NAME )
VALUES ('D060', 'Marketing' );

Don’t select any tables
from the Show Table
dialog box




©Minder Chen, 1996-2011
RDBMS&SQL - 90
INSERT Multiple Rows
INSERT INTO [table-name | view-name]
[column-name1, column-name2, …)]
SELECT statement;
CREATE TABLE TEACHER1
(I_NO
NUMBER(4)
F_NAME
VARCHAR(8)
L_NAME
VARCHAR(5)
DIV
CHAR(4));
NOT NULL,
NOT NULL,
NOT NULL,
INSERT INTO TEACHER1 (I_NO, F_NAME, L_NAME, DIV)
SELECT I_NO, F_NAME, L_NAME, DIV FROM INSTRUCTOR
WHERE TITLE = 'TEACHER';
©Minder Chen, 1996-2011
RDBMS&SQL - 91
UPDATE: Modify One or More Columns
UPDATE [table-name | view-name]
SET column-name1 = expression1, column-name2 = expression2, ...
[WHERE clause];
- - Change the last name of the instructor (I_NO = 21) to 'WANG'
UPDATE INSTRUCTOR
SET L_NAME='WANG'
WHERE I_NO = 21;
--Give all the instructors in
--D010 department a 10% raise.
UPDATE INSTRUCTOR
SET SALARY = SALARY * 1.1
WHERE DIV = 'D010';
I_NO F_NAME M
L_NAM
SALARY
--------- ------------- --- ----------------------1
PATRICK D
LEE
72000
19
ALBERT Y
STONE
60000
21
BRUCE
C
WANG
48000
52
JEAN
E
LOU
24200
©Minder Chen, 1996-2011
Click Run
RDBMS&SQL - 92
UPDATE
• You cannot update a view if the view's
defining query contains one of the
following constructs:
–JOIN
–set operator
–GROUP BY clause
–group function
–DISTINCT operator
– Non-columns are used in SELECT clause.
©Minder Chen, 1996-2011
RDBMS&SQL - 93
DELETE: Deleting One or More Rows
DELETE FROM table-name [WHERE clause];
EXAMPLE
- - Delete instructor whose I_NO is 52
DELETE FROM INSTRUCTOR
WHERE I_NO = 52;
- - delete students with 301 area code
DELETE FROM STUDENT
WHERE TEL_NO LIKE = '301*';
- - delete the whole table
DELETE FROM DIVISION;
NOTES: DELETE command is a very powerful command, please
use it with care.
©Minder Chen, 1996-2011
RDBMS&SQL - 94
FUNCTIONS
AGGREGATE FUNCTIONS
– COUNT (*) or COUNT(DISTINT column)
– MAX
– MIN
– SUM
– AVG
Other Functions
– SUBSTR
– TRUNC
– Format
©Minder Chen, 1996-2011
RDBMS&SQL - 95
COUNT
PURPOSE: Counts the number of rows in each
group that satisfy the condition(s) specified.
EXAMPLE
SELECT COUNT(*) FROM STUDENT;
RESULT
COUNT(*)
--------------6
©Minder Chen, 1996-2011
RDBMS&SQL - 96
Count()
• How many students that we have from VA state?
SELECT Count(*) AS [VA-Student-count]
FROM STUDENT
WHERE STUDENT.STATE='VA';
©Minder Chen, 1996-2011
RDBMS&SQL - 97
Use Query Design View
Tool Bar
SELECT Count(STUDENT.S_NO)
AS CountOfS_NO
FROM STUDENT
WHERE (((STUDENT.STATE)="VA"));
©Minder Chen, 1996-2011
RDBMS&SQL - 98
COUNT
EXAMPLE
SELECT COUNT(ADDRESS)
FROM STUDENT;
RESULT
COUNT(ADDRESS)
--------------------------5
©Minder Chen, 1996-2011
RDBMS&SQL - 99
MAX
PURPOSE
Selects the maximum value in each group of a
column. Null values will be ignored.
EXAMPLE
SELECT MAX(SALARY)
FROM INSTRUCTOR;
RESULT
MAX(SALARY)
--------------------72000
©Minder Chen, 1996-2011
RDBMS&SQL - 100
MIN
PURPOSE
Selects the mininum value in each group of a
column. Null values will be ignored.
EXAMPLE
SELECT MIN(SALARY)
FROM INSTRUCTOR;
RESULT
MIN(SALARY)
-------------------22000
©Minder Chen, 1996-2011
RDBMS&SQL - 101
SUM
PURPOSE
Adds up all the values in each group of a column.
Null values will be ignored.
EXAMPLE
SELECT SUM(SALARY)
FROM INSTRUCTOR;
RESULT
SUM(SALARY)
--------------------202000
©Minder Chen, 1996-2011
RDBMS&SQL - 102
AVG
PURPOSE
Averages the values in each group of a column.
This function is for numeric columns only. Null
values will be ignored.
EXAMPLE
SELECT AVG(SALARY)
FROM INSTRUCTOR;
RESULT
AVG(SALARY)
-------------------50500
©Minder Chen, 1996-2011
RDBMS&SQL - 103
Use Several Functions Together
• More than one function can be used in a
command.
SELECT COUNT(I_NO), AVG(SALARY), MIN(SALARY),
MAX(SALARY)
FROM INSTRUCTOR;
COUNT(I_NO) AVG(SALARY) MIN(SALARY)
MAX(SALARY)
----------- ----------- ----------- ---------4
50500
22000
72000
©Minder Chen, 1996-2011
RDBMS&SQL - 104
SELECT with Distinct Values
Selecting values in a
column:
EXAMPLE
SELECT TITLE
FROM INSTRUCTOR;
RESULT
TITLE
-------------DEAN
TEACHER
TEACHER
CLERK
Selecting unique values in a
column:
EXAMPLE
SELECT DISTINCT ( TITLE )
FROM INSTRUCTOR;
RESULT You can only create this
query in Access SQL View
TITLE
-------------CLERK
DEAN
TEACHER
Create a query to tell me “How many distinct titles we have?”
©Minder Chen, 1996-2011
RDBMS&SQL - 105
Count and Distinct Statement Limitation
Oracle SQL:
SELECT COUNT(DISTINCT CITY)
FROM STUDENT;
-- Answer: 3 (exclude records that have NULL value
in their city column)
Access SQL (Include NULL value; cannot use distinct
and Count together) You have to use the SQL view to
enter the distinct keyword. It will not work in the
Design view.
SELECT DISTINCT (CITY)
FROM STUDENT;
©Minder Chen, 1996-2011
RDBMS&SQL - 106
How to count distinct values
1
Create the first query using
distinct and save it as
DistinctTitles
2
3
4
5
6
Create the second query against the
DistinctTitles query. Use count
©Minder Chen,
function
in1996-2011
this second query.
RDBMS&SQL - 107
Access Report Wizard
1
2
3
©Minder Chen, 1996-2011
RDBMS&SQL - 108
Group By  Control Break
The GROUP BY statement
is used in conjunction with
the aggregate functions to
group the result-set by one
or more columns.
4
Group By DIV
(Division ID)
Aggregate Functions
©Minder Chen, 1996-2011
RDBMS&SQL - 109
Group By
• SELECT clause contains both an aggregate function
and a column name are not allowed unless the result
set is GROUP BY the same column.
SELECT
TITLE,
GROUP BY
TITLE;
SUM (SALARY) FROM INSTRUCTOR
TITLE
SUM(SALARY)
-------- ----------CLERK
22000
DEAN
72000
TEACHER
108000
SELECT GB-column-name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY GB-column-name
©Minder Chen, 1996-2011
RDBMS&SQL - 110
SELECT: Grouping Rows
Summarize Group Values: Build-in functions (e.g., MAX, MIN)
can be applied to selected groups of rows in a table. GROUP
BY produces one row in the returned set for each different
value it finds in the GROUP BY column(s).
EXAMPLE
SELECT DIV, MAX(SALARY), MIN(SALARY)
FROM INSTRUCTOR
GROUP BY DIV
ORDER BY DIV;
RESULT
DIV
MAX(SALARY) MIN(SALARY)
------- --------------------------------------D010
22000
22000
D020
60000
48000
©Minder Chen, 1996-2011
RDBMS&SQL - 111
Group By … Having ...
Use HAVING instead of WHERE clause after GROUP to
qualify groups. Nested queries are permitted in the
HAVING clause.
SELECT TITLE, AVG(SALARY)
FROM INSTRUCTOR
GROUP BY TITLE
HAVING COUNT(*) > 1;
TITLE
AVG(SALARY)
-------- ----------TEACHER
54000
Show me each title and average salary of the title which has
more than one instructors with such title. This is for
privacy purpose.
©Minder Chen, 1996-2011
RDBMS&SQL - 112
SELECT Groups with Conditions
Groups with Conditions:
EXAMPLE
SELECT DIV, ROUND(AVG(SALARY)/52, 2)
AS AVG_SAL
ROUND(n[,m]):
FROM INSTRUCTOR
Returns n rounded
GROUP BY DIV
to m decimal places;
if m is omitted, to 0
HAVING COUNT(*) > 1
places.
ORDER BY DIV;
RESULT
DIV
AVG_SAL
---- --------D020
1153.85
Cannot use aggregate functions in WHERE clause!
©Minder Chen, 1996-2011
RDBMS&SQL - 113
SUBSTR(char, m [,n])
PURPOSE
Returns a portion of char, beginning at character m
for n characters long.
EXAMPLE
SELECT SUBSTR(DIV_NAME,1,4) "SUBS"
FROM DIVISION;
RESULT
SUBS
-------ADMI
COMP
ACCO
STAT
MATH
©Minder Chen, 1996-2011
Access SQL: (use LEFT string function)
SELECT Left(DIV_NAME,4) as SUBS
FROM DIVISION;
RDBMS&SQL - 114
TRUNC(n, d)
Truncates a number n and display d decimal positions.
EXAMPLE
SELECT I_NO,
TRUNC(MONTHS_BETWEEN(SYSDATE, D_O_B)/12, 0)
AS AGE
FROM INSTRUCTOR;
RESULT
I_NO AGE
Access SQL: (Date function)
--------- --------SELECT I_NO,
1
46
ROUND(DateDiff("m", D_O_B, Now)/12, 0)
19
43
AS AGE
FROM INSTRUCTOR;
21
39
52
33
•Assuming that the SYSDATE returns 10/4/1998.
Should you store “age” in the database instead of birthday?
©Minder Chen, 1996-2011
RDBMS&SQL - 115
Advanced SQL Programming
• JOIN
• Correlated subquery
• Non-correlated subquery
• UNION
JOIN: A SQL join is used to query data from two or
more tables, based on a relationship between
certain columns in these tables. The common
columns that are used to connect these tables
are usually a foreign key-primary key pair.
©Minder Chen, 1996-2011
RDBMS&SQL - 116
Join
• Show all instructors from the Instructor table with the
following information:
– I_NO, L_Name, F_Name, plus DIV and DIV_Name where
the instructor belongs.
©Minder Chen, 1996-2011
RDBMS&SQL - 117
JOIN
• Join condition: Join operation must based on one or
more columns from each of the two tables whose data
values share a common domain.
• To get data from two or more tables resulting in a
wide table, you need to name all tables in the FROM
clause.
EXAMPLE
SELECT D_ID, L_NAME, F_NAME, DIV_NAME
FROM DIVISION, INSTRUCTOR
WHERE Division.DIR = Instructor.I_NO
ORDER BY D_ID;
Show me the Division ID, Last name, First Name of its Director, and the
Division Name of all the Division and sorted it by Division ID
©Minder Chen, 1996-2011
RDBMS&SQL - 118
JOIN
RESULT
D_ID L_NAM F_NAME
------D010
D020
D030
D040
D050
------------LOU
LEE
LEE
LEE
LEE
--------------JEAN
PATRICK
PATRICK
PATRICK
PATRICK
DIV_NAME
------------------------------ADMINISTRATION
COMPUTER SCIENCE
ACCOUNTING
STATISTICS
MATHEMATICS
NOTE
– The FROM DIVISION, INSTRUCTOR clause shows this is
a join.
– The WHERE DIR = I_NO clause makes the connection
between the two tables.
©Minder Chen, 1996-2011
RDBMS&SQL - 119
JOIN: Access Query and SQL
SELECT INSTRUCTOR.I_NO, INSTRUCTOR.F_NAME,
INSTRUCTOR.L_NAME, DIVISION.DIV_NAME
FROM DIVISION INNER JOIN INSTRUCTOR
ON DIVISION.D_ID = INSTRUCTOR.DIV;
INSTRUCTOR
P
I_NO
F_NAME
M
L_NAME
F
DIV
D_O_H
TITLE
ED
SEX
D_O_B
SALARY
©Minder Chen, 1996-2011
DIVISION
P
D_ID
F
DIR
DIV_NAME
RDBMS&SQL - 120
Qualifying Column Names
• If the same column name is used in more than one table, you
must qualify its name to show which table or view you mean.
You can also qualify column names by correlation variables
SELECT TITLE, COURSE.C_ID, LOCATION, START_DATE
FROM COURSE, OFFERING
WHERE COURSE.C_ID = OFFERING.C_ID
ORDER BY TITLE;
or
COURSE
P
C_ID
TITLE
F
DIV
FEE
SELECT TITLE, X.C_ID, LOCATION, START_DATE
OFFERING
P,F
FROM COURSE X, OFFERING Y
C_ID
P
O_NO
WHERE X.C_ID = Y.C_ID
START_DATE
ORDER BY TITLE;
LOCATION
F
I_NO
X, Y are correlation variables or aliases.
©Minder Chen, 1996-2011
RDBMS&SQL - 121
Qualifying Column Names
RESULT
TITLE
------------------INTRO C++
INTRO C++
INTRO C++
INTRO PB
INTRO PB
INTRO PB
RDBMS SQL
WINDOWS NT
©Minder Chen, 1996-2011
C_ID
------C010
C010
C010
C020
C020
C020
C030
C040
LOCATION
------------------LOUDOUN
LOUDOUN
ROCKVILLE
LOUDOUN
LOUDOUN
ROCKVILLE
LOUDOUN
LOUDOUN
START_DAT
-----------------01-APR-97
01-OCT-97
01-OCT-97
01-APR-97
01-OCT-97
01-OCT-97
01-OCT-97
01-OCT-97
RDBMS&SQL - 122
JOIN and Aggregation Function
Show students ID, name, and GPA (assuming all courses are 3 credits)
SELECT STUDENT.S_NO, STUDENT.NAME,
Round(Avg(REGISTRATION.GRADE)*100)/100 AS AvgOfGRADE
FROM STUDENT.S_NO = REGISTRATION.S_NO
GROUP BY STUDENT.S_NO, STUDENT.NAME;
Don’t choose
any table here!
SQL: Format(Avg(GRADE), "###.00") AS GPA
Design View:
GPA: Format(Avg(GRADE), "###.00")
Change this
to expression
©Minder Chen, 1996-2011
RDBMS&SQL - 123
Examples
• How many students in each section of the fall 1997
class assuming the START_DATE of the fall 1997
semester is #10/1/1997#?
©Minder Chen, 1996-2011
RDBMS&SQL - 124
JOIN and Aggregation Function
• Show the total fees generated by each course (with
multiple offering)
SELECT COURSE.C_ID, COURSE.TITLE, Sum(COURSE.FEE) AS SumOfFEE
FROM (COURSE INNER JOIN OFFERING ON COURSE.C_ID = OFFERING.C_ID)
INNER JOIN REGISTRATION ON (OFFERING.O_NO = REGISTRATION.O_NO)
AND (OFFERING.C_ID = REGISTRATION.C_ID)
GROUP BY COURSE.C_ID, COURSE.TITLE
ORDER BY Sum(COURSE.FEE) DESC;
– How about fees generated in fall 1997 semester?
_ How about top 2 courses that generate most fees
©Minder Chen, 1996-2011
RDBMS&SQL - 125
Alternative Join Syntax
Oracle: (This works in Access as well)
SELECT TITLE, COURSE.C_ID, LOCATION, START_DATE
FROM COURSE, OFFERING
WHERE COURSE.C_ID = OFFERING.C_ID
ORDER BY TITLE;
Access SQL and SQL-92 Standard: (May not work in Oracle
SQL*Plus)
SELECT Title, COURSE.C_ID, LOCATION, START_DATE
FROM COURSE INNER JOIN OFFERING
ON COURSE.C_ID = OFFERING.C_ID
ORDER BY TITLE;
©Minder Chen, 1996-2011
RDBMS&SQL - 126
JOIN
Inner Join:
SELECT INSTRUCTOR.I_NO, L_NAME, F_NAME, C_ID, O_NO
FROM INSTRUCTOR, OFFERING
WHERE INSTRUCTOR.I_NO = OFFERING.I_NO;
I_NO
--------21
21
19
19
19
21
19
19
L_NAM
----FLYNN
FLYNN
STONE
STONE
STONE
FLYNN
STONE
STONE
F_NAME
-------BRUCE
BRUCE
ALBERT
ALBERT
ALBERT
BRUCE
ALBERT
ALBERT
C_ID
O_NO
---- --------C010
1
C010
10
C010
20
C020
1
C020
10
C020
20
C030
10
C040
10
• With inner join operation, only rows that satisfy the
comparison used to create the join are included in the
result. Instructors who do not have any offering entry in
the OFFERING table will not be retrieved.
©Minder Chen, 1996-2011
RDBMS&SQL - 127
Inner & Outer Joins
Inner Join
Left Outer Join
Right Outer Join
©Minder Chen, 1996-2011
RDBMS&SQL - 128
Left Outer Join
SELECT INSTRUCTOR.I_NO, L_NAME, F_NAME, C_ID, O_NO FROM
INSTRUCTOR, OFFERING
WHERE INSTRUCTOR.I_NO = OFFERING.I_NO (+);
I_NO
--------1
19
19
19
19
19
21
21
21
52
L_NAM
----LEE
STONE
STONE
STONE
STONE
STONE
FLYNN
FLYNN
FLYNN
LOU
F_NAME
-------PATRICK
ALBERT
ALBERT
ALBERT
ALBERT
ALBERT
BRUCE
BRUCE
BRUCE
JEAN
C_ID
O_NO
---- --------C010
C020
C030
C020
C040
C010
C010
C020
20
1
10
10
10
1
10
20
NULL Values
• When instructors from INSTRUCTOR table do not have any offering,
they will be listed with NULL values in columns from OFFERING table.
• (+) qualifier in the WHERE clause indicate which table's columns
should contain entries with NULL values when there is no matching
value in the other column. (Oracle only)
©Minder Chen, 1996-2011
RDBMS&SQL - 129
Outer Join
Right Outer Join:
• SELECT INSTRUCTOR.I_NO, L_NAME, F_NAME, C_ID, O_NO
FROM INSTRUCTOR, OFFERING
WHERE INSTRUCTOR.I_NO (+) = OFFERING.I_NO;
• Alternative Notation (SQL-92 standard)
SELECT INSTRUCTOR.I_NO, L_NAME, F_NAME, C_ID, O_NO
FROM INSTRUCTOR, OFFERING,
INSTRUCTOR RIGHT OUTER JOIN OFFERING
ON INSTRUCTOR.I_NO = OFFERING.I_NO;
Left Outer Join
• SELECT INSTRUCTOR.I_NO, L_NAME, F_NAME, C_ID, O_NO
FROM INSTRUCTOR, OFFERING
WHERE INSTRUCTOR.I_NO = OFFERING.I_NO (+);
• Alternative Notation (SQL-92 standard)
SELECT INSTRUCTOR.I_NO, L_NAME, F_NAME, C_ID, O_NO
FROM INSTRUCTOR, OFFERING,
INSTRUCTOR LETF OUTER JOIN OFFERING
ON INSTRUCTOR.I_NO = OFFERING.I_NO;
©Minder Chen, 1996-2011
RDBMS&SQL - 130
Multiway Join
This query joins six tables.
EXAMPLE
SELECT A.C_ID, B.TITLE, C.LOCATION, C.START_DATE,
D.DIV_NAME, E.L_NAME AS INSTR, F.NAME AS STUDENT
FROM REGISTRATION A, COURSE B,
OFFERING C, DIVISION D,
INSTRUCTOR E, STUDENT F
WHERE A.C_ID = 'C010'
AND A.C_ID = B.C_ID
AND(A.C_ID = C.C_ID AND A.O_NO = C.O_NO)
AND C.I_NO = E.I_NO
ANDB.DIV = D.D_ID
AND A.S_NO = F.S_NO;
©Minder Chen, 1996-2011
RDBMS&SQL - 131
Multiway Join
RESULT
C_ID
------C010
C010
C010
C010
C010
TITLE
---------------INTRO C++
INTRO C++
INTRO C++
INTRO C++
INTRO C++
LOCATION
--------------LOUDOUN
LOUDOUN
LOUDOUN
LOUDOUN
LOUDOUN
©Minder Chen, 1996-2011
START_DAT DIV_NAME
----------------- ------------------------------01-APR-97 COMPUTER SCIENCE
01-APR-97 COMPUTER SCIENCE
01-OCT-97 COMPUTER SCIENCE
01-OCT-97 COMPUTER SCIENCE
01-OCT-97 COMPUTER SCIENCE
INSTR
---------FLYNN
FLYNN
FLYNN
FLYNN
FLYNN
STUDENT
-------------MARY
TOM
LINDA
JAMES
KATHY
RDBMS&SQL - 132
Server Generated Sequence Numbers
• In some database servers such as Sybase, the system
maintains a sequence number for an identity column.
It is special data type called AutoNumber is Access.
Create table employee (
id
numeric(4, 0) identify,
name
char(20)
not null,
title
char(50)
not null);
Insert into employee values ('Minder', 'Dean');
Insert into employee values ('Justin', 'Instructor');
Result:
id
name
title
1
Minder
Dean
2
Justin
Instructor
©Minder Chen, 1996-2011
RDBMS&SQL - 133
Relationships among Access Database Objects
Table
Basic Database
Objects
Query
(View)
Database
Application
•
•
•
•
Form
Report
A saved SELECT query is officially called View in SQL.
QUERY in Access can be SELECT, INSERT, UPDATE, or DELETE.
You can create a query against a table or a query.
You can create a form or report against a table or a query.
©Minder Chen, 1996-2011
RDBMS&SQL - 134
Create Forms in Access
©Minder Chen, 1996-2011
RDBMS&SQL - 135
Create a Simple Form
©Minder Chen, 1996-2011
RDBMS&SQL - 136
Form Layout and Style
©Minder Chen, 1996-2011
RDBMS&SQL - 137
©Minder Chen, 1996-2011
RDBMS&SQL - 138
Detail Form in Datasheet Layout
©Minder Chen, 1996-2011
RDBMS&SQL - 139
©Minder Chen, 1996-2011
RDBMS&SQL - 140
©Minder Chen, 1996-2011
RDBMS&SQL - 141
©Minder Chen, 1996-2011
RDBMS&SQL - 142
LOCK
Locking mechanism is used to prevent loss of data integrity in
reading and writing data to the database. The lock controls
whether some other users can access (read or write) the
database. Transaction requests and releases for locks are
normally implicit.
Shared lock (S): Allow the lock owner to read the lock data, but
not to change it. You can acquire a shared lock on data after
other users also acquired a shared lock on the it.
Exclusive lock (X): Allow the lock owner to read and update the
lock data. No other user can access the locked data or
acquired locks on it while a exclusive lock is in effect.
Lock Compatibility
X
S
©Minder Chen, 1996-2011
X
NO
NO
S
NO
YES
RDBMS&SQL - 143
LOCK
Permits users to explicitly acquire table locks.
– Lock Objects
» Row
» Page
» Table
» Table Space
– Lock Modes
» Shared(S) Lock
» Exclusive(X) Lock
©Minder Chen, 1996-2011
RDBMS&SQL - 144
COMMIT & ROLLBACK
• COMMIT: Instructs the system to make all DML
commands executed by a transaction
permanent and releases the transaction's locks.
You can also use this command to manually
commit an in-doubt distributed transaction.
• ROLLBACK: Instructs the system to reverse all
DML commands executed by a transaction.
When the ROLLBACK command is issued, all
changes caused by INSERT, UPDATE and
DELETE commands of the transaction are
backed out. All locks acquired during a
transaction are released by the ROLLBACK
command.
©Minder Chen, 1996-2011
RDBMS&SQL - 145
Indexing & Performance: Retrieval vs. Update
Fastest
Retrieval speed
Trade off
between
retrieval
and update
Slowest
Update
speed
No index
All Indexed
Consider the creation of indexes when
faster retrieval performance is required.
©Minder Chen, 1996-2011
RDBMS&SQL - 146
APPENDIX A. Sample Database Tables
1. Instructor Table
I_NO
1
19
21
52
F_NAME
PATRICK
ALBERT
BRUCE
JEAN
M
D
Y
C
E
L_NAM
LEE
STONE
FLYNN
LOU
DIV
D020
D020
D020
D010
D_O_H
08-MAR-83
22-DEC-94
14-JUN-97
02-SEP-88
TITLE
DEAN
TEACHER
TEACHER
CLERK
ED
12
11
10
08
S
M
M
M
F
D_O_B
03-NOV-51
01-AUG-55
18-APR-59
01-JAN-65
SALARY
72000
60000
48000
22000
2. Student Table
S_NO
5001
5002
5003
5004
5005
5006
NAME
MARY
TOM
LINDA
JAMES
KATHY
MIKE
3. Division Table
©Minder Chen, 1996-2011
ADDRESS
102 1ST ST
39 5TH AVE
1 ROCK RD
CITY
FAIRFAX
ARLINGTON
ROCKVILLE
ST
VA
DC
MD
ZIP
12345
22313
20852
2 KING LA
P.O.BOX 1
FAIRFAX
FAIRFAX
VA
VA
12345
12345
D_ID
D010
D020
D030
D040
D050
DIR
52
1
1
1
1
TEL_NO
7037772311
2021112222
3019380975
3019784444
7037775534
7037771122
DIV_NAME
ADMINISTRATION
COMPUTER SCIENCE
ACCOUNTING
STATISTICS
MATHEMATICS
RDBMS&SQL - 147
4. Course Table
5. Offering Table
6. Registration Table
©Minder Chen, 1996-2011
C_ID
C010
C020
C030
C040
TITLE
INTRO C++
INTRO PB
ORACLE SQL
WINDOWS NT
C_ID
C010
C010
C010
C020
C020
C020
C030
C040
C_ID
C010
C010
C010
C010
C010
C020
C020
C020
C030
C040
C040
O_NO
1
10
20
1
10
20
10
10
DIV
D020
D020
D020
D020
START_DAT
01-APR-97
01-OCT-97
01-OCT-97
01-APR-97
01-OCT-97
01-OCT-97
01-OCT-97
01-OCT-97
O_NO
1
1
10
10
10
1
1
10
10
10
10
S_NO
5001
5002
5003
5004
5005
5001
5002
5004
5002
5001
5002
FEE
350
450
550
350
LOCATION
LOUDOUN
LOUDOUN
ROCKVILLE
LOUDOUN
LOUDOUN
ROCKVILLE
LOUDOUN
LOUDOUN
GRADE
4
4
4
4
3
3
3
4
4
3
4
I_NO
21
21
19
19
19
21
19
19
RDBMS&SQL - 148
Physical Data Model
©Minder Chen, 1996-2011
RDBMS&SQL - 149
Normalization
First Normal Form A table is in the first normal
form if it contains no repeating value.
Un-Normalized Relation:
STUDENT
Tom
Sam
Ralph
COURSE
C102
{C102, C103, C105}
{C102, C105}
Normalized Relation:
©Minder Chen, 1996-2011
STUDENT
Tom
Sam
Sam
Sam
Ralph
Ralph
COURSE
C102
C102
C103
C105
C102
C105
RDBMS&SQL - 150
Normalization
Second Normal Form A table is in the second
normal form if you remove any partial
functional dependencies.
Un-normalized Relation:
Registration
Partial Functional
Dependency
STUDENT_ID COURSE_ID Course Title Instructor_ID Grade
Normalized Relation:
Registration STUDENT_ID
Course
©Minder Chen, 1996-2011
Course_ID
COURSE_ID Cours Title
Grade
Instructor_ID
RDBMS&SQL - 151
Normalization
Third Normal Form A table is in the third normal
form if it contains no transitive functional
dependencies.
Transitive Functional
Dependencies
Un-normalized Relation:
Course COURSE Course Title
Normalized Relation:
COURSE_ID
Course
Instructor
©Minder Chen, 1996-2011
Instructor_ID InstructorTel
Course Title
Instructor_ID
INSTRUCTOR_ID Instructor Tel
RDBMS&SQL - 152
Download