Академический Документы
Профессиональный Документы
Культура Документы
Chapter 7
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel
1
How to use SQL for data manipulation (to add, modify, delete, and retrieve data)
How to use SQL to query a database to extract useful information
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Introduction to SQL
SQL functions fit into two broad categories:
Data definition language
SQL includes commands to:
Create database objects, such as tables, indexes, and views Define access rights to those database objects
Nonprocedural language
American National Standards Institute (ANSI) prescribes a standard SQL Several SQL dialects exist
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
10
First task:
RDBMS creates physical files that will hold database Tends to differ substantially from one RDBMS to another
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
11
Schema
Group of database objectssuch as tables and indexesthat are related to each other
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
12
Data Types
Data type selection is usually dictated by nature of data and by intended use
Pay close attention to expected use of attributes for sorting and data retrieval purposes
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
13
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
14
UNIQUE specification
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
15
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
16
SQL Constraints
NOT NULL constraint
Ensures that column does not accept nulls
UNIQUE constraint
Ensures that all values in column are unique
DEFAULT constraint
Assigns value to attribute when a new row is added to table
CHECK constraint
Validates data when attribute value is entered
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
17
SQL Indexes
When primary key is declared, DBMS automatically creates unique index
Often need additional indexes
Using CREATE INDEX command, SQL indexes can be created on basis of any selected attribute
Composite index
Index based on two or more attributes Often used to prevent data duplication
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
18
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
19
20
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
21
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Syntax:
COMMIT [WORK];
Will permanently save any changes made to any table in the database
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
23
24
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
25
If more than one attribute is to be updated in row, separate corrections with commas
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
26
Syntax:
ROLLBACK;
COMMIT and ROLLBACK only work with data manipulation commands that are used to add, modify, or delete table rows
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
27
If WHERE condition is not specified, all rows from specified table will be deleted
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
28
Syntax:
INSERT INTO tablename SELECT columnlist FROM tablename;
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
29
Syntax:
SELECT columnlist FROM tablelist [ WHERE conditionlist ] ;
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
30
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
31
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
32
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
33
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
34
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
35
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
36
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
37
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
38
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
39
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
40
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
41
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
42
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
43
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
44
Special Operators
BETWEEN
Used to check whether attribute value is within a range
IS NULL
Used to check whether attribute value is null
LIKE
Used to check whether attribute value matches given string pattern
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
45
EXISTS
Used to check if subquery returns any rows
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
46
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
47
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
48
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
49
Adding a Column
Use ALTER to add column
Do not include the NOT NULL clause for new column
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
50
Dropping a Column
Use ALTER to drop column
Some RDBMSs impose restrictions on the deletion of an attribute
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
51
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
52
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
53
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
54
55
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
56
SQL allows user to limit queries to only those entries having no duplicates or entries whose duplicates may be grouped
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
57
Ordering a Listing
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
58
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
59
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
60
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
61
Aggregate Functions
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
62
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
63
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
64
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
65
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
66
Grouping Data
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
67
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
68
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
69
70
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
71
72
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
73
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
74
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
75
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
76
Recursive Joins
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
77
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
78
Outer Joins
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
79
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
80
Summary
SQL commands can be divided into two overall categories:
Data definition language commands Data manipulation language commands
The ANSI standard data types are supported by all RDBMS vendors in different ways
Basic data definition commands allow you to create tables, indexes, and views
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
81
Summary (continued)
DML commands allow you to add, modify, and delete rows from tables
The basic DML commands are SELECT, INSERT, UPDATE, DELETE, COMMIT, and ROLLBACK INSERT command is used to add new rows to tables SELECT statement is main data retrieval command in SQL
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
82
Summary (continued)
Many SQL constraints can be used with columns
The column list represents one or more column names separated by commas WHERE clause can be used with SELECT, UPDATE, and DELETE statements to restrict rows affected by the DDL command
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
83
Summary (continued)
Aggregate functions
Special functions that perform arithmetic computations over a set of rows
ORDER BY clause
Used to sort output of SELECT statement Can sort by one or more columns and use either an ascending or descending order
84
Summary (continued)
Natural join uses join condition to match only rows with equal values in specified columns
Right outer join and left outer join used to select rows that have no matching values in other related table
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
85