CSE301 Lec6
CSE301 Lec6
1 2
3 4
PURPOSE OF SQL STANDARD BENEFITS OF A STANDARDIZED
RELATIONAL LANGUAGE
Specify syntax/semantics for data definition and
manipulation Reduced training costs
Define data structures and basic operations
Productivity
Enable portability of database definition and
application modules Application portability
Specify minimal (level 1) and complete (level 2)
standards Application longevity
Allow for later growth/enhancement to standard Reduced dependence on a single vendor
(referential integrity, transaction management,
user-defined functions, extended join Cross-system communication
operations, national character sets)
5 6
Figure 6-1
SQL ENVIRONMENT A simplified schematic of a typical SQL environment, as
described by the SQL: 2008 standard
Catalog
A set of schemas that constitute the description of a database
Schema
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
7 8
Figure 6-4
SQL DATA TYPES DDL, DML, DCL, and the database development process
9 10
11 12
Figure 6-5 General syntax for CREATE TABLE
statement used in data definition language THE FOLLOWING SLIDES CREATE TABLES
FOR THIS ENTERPRISE DATA MODEL
13 14
Figure 6-6 SQL database definition commands for Pine Valley Furniture
Company (Oracle 11g) Defining attributes and their data types
Overall table
definitions
15 16
Non-nullable specification Non-nullable specifications
Primary key
Default value
Primary key of
parent table
Domain constraint
19 20
CHANGING TABLES
DATA INTEGRITY CONTROLS
ALTER TABLE statement allows you to
Referential integrity–constraint that change column specifications:
ensures that foreign key values of a
table must match primary key values
of a related table in 1:M relationships Table Actions:
Restricting:
Deletes of primary records
Updates of primary records Example (adding a new column with a default value):
Inserts of dependent records
21 22
INSERT STATEMENT
REMOVING TABLES
Adds one or more rows to a table
Inserting into a table
DROPTABLE statement allows you to
remove tables from your schema: Inserting a record that has some null attributes requires
identifying the fields that actually get data
23 24
CREATING TABLES WITH IDENTITY COLUMNS DELETE STATEMENT
25 26
27 28
SELECT STATEMENT Figure 6-9
SQL statement
Used for queries on single or multiple tables processing
Clauses of the SELECT statement: order (based
SELECT on van der
List the columns (and expressions) to be returned from the query Lans, 2006
FROM p.100)
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
29 30
SELECT EXAMPLE
SELECT EXAMPLE USING ALIAS
Find products with standard price less than
Alias is an alternative column or table name
$275
Here,
CUST is a table alias and Name is a
Table 6-3: Comparison Operators in SQL column alias
31 32
SELECT EXAMPLE USING A FUNCTION SELECT EXAMPLE–BOOLEAN OPERATORS
AND, OR, and NOT Operators for customizing conditions
in WHERE clause
Using the COUNT aggregate function to find
totals
Figure 6-7 Boolean query A without use of parentheses SELECT EXAMPLE–BOOLEAN OPERATORS
With parentheses…these override the normal
precedence of Boolean operators
By default,
processing order
of Boolean
operators is NOT,
then AND, then
OR With parentheses, you can override normal precedence rules. In
this case parentheses make the OR take place before the AND.
35 36
Figure 6-8 Boolean query B with use of parentheses SORTING RESULTS WITH ORDER BY CLAUSE
Sort the results first by STATE, and within a state
by the CUSTOMER NAME
37 38
41 42
43 44