0% found this document useful (0 votes)
12 views11 pages

CSE301 Lec6

The document provides an overview of SQL, including its history, purpose, and benefits as a standardized relational language. It covers key concepts such as data definition language (DDL), data manipulation language (DML), and data control language (DCL), as well as the creation and management of tables, data integrity controls, and querying techniques. Additionally, it discusses the use of views for controlled access to data and the advantages and disadvantages associated with them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

CSE301 Lec6

The document provides an overview of SQL, including its history, purpose, and benefits as a standardized relational language. It covers key concepts such as data definition language (DDL), data manipulation language (DML), and data control language (DCL), as well as the creation and management of tables, data integrity controls, and querying techniques. Additionally, it discusses the use of views for controlled access to data and the advantages and disadvantages associated with them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

OBJECTIVES

CHAPTER 6:  Define terms


INTRODUCTION TO SQL  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:2008 standards

1 2

SQL OVERVIEW HISTORY OF SQL


 Structured Query Language  1970–E. F. Codd develops relational database concept
 1974-1979–System R with Sequel (later SQL) created at
 The standard for relational database IBM Research Lab
management systems (RDBMS)  1979–Oracle markets first relational DB with SQL
 1981 – SQL/DS first available RDBMS system on DOS/VSE
 Others followed: INGRES (1981), IDM (1982), DG/SGL
 RDBMS: A database management system that (1984), Sybase (1986)
manages data as a collection of tables in which  1986–ANSI SQL standard released
all relationships are represented by common  1989, 1992, 1999, 2003, 2006, 2008–Major ANSI
values in related tables standard updates
 Current–SQL is supported by most major database vendors

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

SQL DATABASE DEFINITION STEPS IN TABLE CREATION


 Data Definition Language (DDL) 1. Identify data types for attributes
 Major CREATE statements: 2. Identify columns that can and cannot be null
 CREATE SCHEMA–defines a portion of the
3. Identify columns that must be unique (candidate keys)
database owned by a particular user
 CREATE TABLE–defines a new table and its 4. Identify primary key–foreign key mates
columns 5. Determine default values
 CREATE VIEW–defines a logical table from one or
more tables or views 6. Identify constraints on columns (domain
specifications)
 Other CREATE statements: CHARACTER SET,
COLLATION, TRANSLATION, ASSERTION, DOMAIN 7. Create the table and associated indexes

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

(from Chapter 1, Figure 1-3)

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

Primary keys Some primary keys are composite–


can never have
NULL values composed of multiple attributes
Identifying primary key
17 18

Identifying foreign keys and establishing relationships


Controlling the values in attributes

Default value
Primary key of
parent table

Domain constraint

Foreign key of dependent table

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

DROP TABLE CUSTOMER_T


 Inserting from another table

23 24
CREATING TABLES WITH IDENTITY COLUMNS DELETE STATEMENT

 Removes rows from a table


Introduced with SQL:2008
 Delete certain rows
 DELETE
FROM CUSTOMER_T WHERE
CUSTOMERSTATE = ‘HI’;
 Delete all rows
Inserting into a table does not require explicit customer ID entry or  DELETE FROM CUSTOMER_T;
field list

INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’,


‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);

25 26

UPDATE STATEMENT SCHEMA DEFINITION


 Control processing/storage efficiency:
 Choice of indexes
 Modifies data in existing rows  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(CUSTOMERNAME)
 This makes an index for the CUSTOMERNAME field of the
CUSTOMER_T table

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

SELECT COUNT(*) FROM ORDERLINE_T


WHERE ORDERID = 1004;

Note: with aggregate functions you can’t have single-


valued columns included in the SELECT clause, Note: the LIKE operator allows you to compare strings using
unless they are included in the GROUP BY clause. wildcards. For example, the % wildcard in ‘%Desk’ indicates
that all strings that have any number of characters preceding
the word “Desk” will be allowed.
33 34

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

Note: the IN operator in this example allows you to include rows


whose CustomerState value is either FL, TX, CA, or HI. It is
more efficient than separate OR conditions.

37 38

CATEGORIZING RESULTS USING GROUP QUALIFYING RESULTS BY CATEGORIES


BY CLAUSE USING THE HAVING CLAUSE
 For use with aggregate functions  For use with GROUP BY
 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)

Like a WHERE clause, but it operates on groups


(categories), not on individual rows. Here, only
You can use single-value fields with aggregate functions those groups with total numbers greater than 1
if they are included in the GROUP BY clause. will be included in final result.
39 40
USING AND DEFINING VIEWS SAMPLE CREATE VIEW
 Views provide users controlled access to tables Query: What are the data elements necessary
 Base Table–table containing the raw data to create an invoice for a customer?
 Virtual Table–constructed automatically as needed; not Save this query as a view named Invoice_V.
maintained as real 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
 Contents materialized as a result of a query

41 42

ADVANTAGES OF VIEWS DISADVANTAGES OF VIEWS

 Simplify query commands  Use processing time each time view is


 Assist with data security (but don't rely on referenced
views for security, there are more important
security measures)  May or may not be directly updateable

 Enhance programming productivity


 Contain most current base table data
 Use little storage space
 Provide customized view for user
 Establish physical data independence

43 44

You might also like