Lecture 04
Lecture 04
INTRODUCTION TO SQL
1
OBJECTIVES
Define terms
Define a database using SQL data
definition language
Write single and multi- table queries
using SQL
Establish referential integrity and
other constraints using SQL
5
SQL DATA TYPES
7
SQL DATABASE DEFINITION
Data Definition Language (DDL)
Major CREATE statements:
CREATE SCHEMA–defines a portion of the
database owned by a particular user
CREATE TABLE–defines a new table and
its columns
CREATE VIEW–defines a logical table from
one or more tables or views
Other CREATE statements: CHARACTER
SET, COLLATION, TRANSLATION, ASSERTION,
DOMAIN
Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 8
STEPS IN TABLE CREATION
1. Identify data types for 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
Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 9
General syntax for CREATE TABLE statement used in
data definition language
Overall table
definitions
13
Non-nullable specification
Primary keys
can never have
NULL values
Primary key
15
Controlling the values in attributes
Default value
Domain constraint
16
Identifying foreign keys and establishing relationships
Primary key of
parent table
17
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
Referential
integrity is
enforced via
the primary-
key to foreign-
key match
19
CHANGING TABLES
ALTER TABLE statement allows you to
change column specifications:
Table Actions:
28
SELECT EXAMPLE
SELECT CUST.CUSTOMERNAME AS
NAME,
CUST.CUSTOMERADDRESS
FROM CUSTOMER_V CUST
WHERE NAME = ‘Home
Furnishings’;
Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
30
SELECT EXAMPLE USING A FUNCTION
By default,
processing
order of
Boolean
operators is
NOT, then
AND, then OR