Professional Documents
Culture Documents
Chapter 4 - EC601 PDF
Chapter 4 - EC601 PDF
Chapter 4
1
Structured Query Language (SQL)
2
Introduction to SQL
3
Introduction to SQL (continued)
5
Introduction to SQL (continued)
6
Introduction to SQL (continued)
7
Data Definition Commands
8
The Database Model
9
The Database Model (continued)
10
Creating the Database
11
The Database Schema
Authentication
- Process through which DBMS verifies that
only registered users are able to access
database
- Log on to RDBMS using user ID and
password created by database administrator
Schema
- Group of database objectssuch as tables
and indexesthat are related to each other
12
Data Types
13
Data Types (continued)
14
Creating Table Structures
15
Creating Table Structures (continued)
16
SQL Constraints
17
SQL Indexes
18
SQL Indexes (continued)
19
Data Manipulation Commands
INSERT
- Used to enter data into table
- Syntax:
INSERT INTO columnname
VALUES (value1, value2, , valuen);
21
Adding Table Rows (continued)
22
Saving Table Changes
Changes made to table contents are not
physically saved on disk until, one of the
following occurs:
- Database is closed
- Program is closed
- COMMIT command is used
Syntax:
- COMMIT [WORK];
Will permanently save any changes made to any
table in the database
23
Listing Table Rows
SELECT
- Used to list contents of table
- Syntax:
SELECT columnlist
FROM tablename;
25
Updating Table Rows
UPDATE
- Modify data in a table
- Syntax:
UPDATE tablename
SET columnname = expression [, columname =
expression]
[WHERE conditionlist];
DELETE
- Deletes a table row
- Syntax:
DELETE FROM tablename
[WHERE conditionlist ];
28
Inserting Table Rows with a
Select Subquery
INSERT
- Inserts multiple rows from another table
(source)
- Uses SELECT subquery
Query that is embedded (or nested) inside
another query
Executed first
- Syntax:
INSERT INTO tablename SELECT
columnlist
FROM tablename;
29
Selecting Rows with
Conditional Restrictions
Select partial table contents by placing
restrictions on rows to be included in output
- Add conditional restrictions to SELECT
statement, using WHERE clause
Syntax:
- SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
30
Selecting Rows with
Conditional Restrictions (continued)
31
Selecting Rows with
Conditional Restrictions (continued)
32
7
Selecting Rows with
Conditional Restrictions (continued)
33
Selecting Rows with
Conditional Restrictions (continued)
34
Selecting Rows with
Conditional Restrictions (continued)
35
Selecting Rows with
Conditional Restrictions (continued)
36
Selecting Rows with
Conditional Restrictions (continued)
37
Selecting Rows with
Conditional Restrictions (continued)
38
Arithmetic Operators:
The Rule of Precedence
Perform operations within parentheses
Perform power operations
Perform multiplications and divisions
Perform additions and subtractions
40
Arithmetic Operators:
The Rule of Precedence (continued)
41
Logical Operators:
AND, OR, and NOT
42
Logical Operators:
AND, OR, and NOT (continued)
43
Logical Operators:
AND, OR, and NOT (continued)
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
45
7
IN
- Used to check whether attribute value
matches any value within a value list
EXISTS
- Used to check if subquery returns any rows
46
Advanced Data Definition Commands
47
Changing a Column's Data Type
48
Changing a Column's Data
Characteristics
Use ALTER to change data characteristics
If column to be changed already contains
data, changes in column's characteristics are
permitted if those changes do not alter the
data type
49
Adding a Column
50
Dropping a Column
51
Advanced Data Updates
52
Copying Parts of Tables
53
Copying Parts of Tables (continued)
54
Adding Primary and Foreign Key
Designations
When table is copied, integrity rules do not
copy, so primary and foreign keys need to be
manually defined on new table
User ALTER TABLE command
- Syntax:
ALTER TABLE tablename ADD
PRIMARY KEY(fieldname);
For foreign key, use FOREIGN KEY in place
of
PRIMARY KEY
55
Deleting a Table from the Database
DROP
- Deletes table from database
- Syntax:
DROP TABLE tablename;
56
Advanced Select Queries
57
Ordering a Listing
58
Ordering a Listing (continued)
59
Ordering a Listing (continued)
60
Listing Unique Values
61
Aggregate Functions
62
Aggregate Functions (continued)
63
Aggregate Functions (continued)
64
Aggregate Functions (continued)
65
Aggregate Functions (continued)
66
Grouping Data
67
Grouping Data (continued)
68
Grouping Data (continued)
69
Virtual Tables: Creating a View
70
Virtual Tables: Creating a View
(continued)
71
Joining Database Tables
72
Joining Database Tables (continued)
73
Joining Database Tables (continued)
74
Joining Database Tables (continued)
75
Joining Tables with an Alias
76
Recursive Joins
77
Recursive Joins (continued)
78
Outer Joins
79
Outer Joins (continued)
80
Summary
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
Join output of multiple tables with SELECT
statement
84
Summary (continued)
85