M4 SQL partII
M4 SQL partII
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
2
To create a table, you
will need:
1. A table name
2. Names for all
columns
3. Data type for each
column
4. Applicable
constraints
3
Table 7.4 - Common SQL Data Types
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
4
5
CREATE TABLE
6
Or, name the constraint
CREATE TABLE VENDOR(
V_CODE integer NOT NULL,
V_NAME VARCHAR(35) NOT NULL,
…,
7
constraint constraintName PRIMARY KEY (V_CODE));
Delete this part if not supported 8
by your RDBMS.
Creating Table Structures
Use one line per column (attribute) definition
Use spaces to line up attribute characteristics and
constraints
Table and attribute names are fully capitalized
Features of table creating command sequence:
NOT NULL specification ensures data entry
UNIQUE specification avoids duplicated values
Table definition enclosed in parentheses
RDBMS automatically enforces referential integrity for
foreign keys.
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
9
SQL Constraints
NOT NULL
• Ensures that column does not accept nulls
UNIQUE
• Ensures that all values in column are unique
DEFAULT
• Assigns value to attribute when a new row is added to
table
CHECK
• Validates data when attribute value is entered
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
10
Constraint Examples
11
Data Manipulation Commands
• INSERT
• SELECT
• COMMIT
• UPDATE
• ROLLBACK
• DELETE
12
Adding Table Rows
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
13
14
Save and Restore
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
15
Saving Table Changes
• Changes made to table contents are not physically saved on
disk until:
– 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
16
Restoring Table Contents
• ROLLBACK
– Undoes changes since last COMMIT
– Brings data back to prechange values
• Syntax:
ROLLBACK;
• COMMIT and ROLLBACK only work with commands to add,
modify, or delete table rows
17
Updating Table Rows
UPDATE: Command to modify data
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
18
19
Deleting Table Rows
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
21
The Database Schema
Logical group of database objects – such as tables
and indexes - related to each other
Command:
CREATE SCHEMA AUTHORIZATION {creator};
Seldom used directly as command is usually optional
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
22
Additional Data Definition Commands
ALTER TABLE command: To make changes in the table structure
Keywords use with the command
ADD - Adds a column
MODIFY - Changes column characteristics
DROP - Deletes a column
Used to:
Add table constraints
Remove table constraints
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
23
Changing a Column’s Data Type and Data
Characteristics
ALTER used to change data type and characteristics
Some RDBMSs do not permit changes to data types unless
column is empty
Changes in characteristics are permitted if they do not alter
the existing data type
Syntax:
Data Type: ALTER TABLE tablename MODIFY
(columnname(datatype));
Data Characteristic: ALTER TABLE tablename MODIFY
(columnname(characteristic));
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
24
Adding and Dropping Columns
Adding a column
Use ALTER and ADD
Do not include the NOT NULL clause for new
column
Dropping a column
Use ALTER and DROP
Some RDBMSs impose restrictions on the deletion of
an attribute
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
25
Advanced Data Updates
UPDATE command updates only data in existing rows
If a relationship is established between entries and existing columns, the
relationship can assign values to appropriate slots
Arithmetic operators are useful in data updates
In Oracle, ROLLBACK command undoes changes made by last two
UPDATE statements
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
26
Copying Parts of Tables
SQL permits copying contents of selected table columns
Data need not be reentered manually into newly created table(s)
Table structure is created
Rows are added to new table using rows from another table
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
27
Adding Primary and Foreign Key
Designations
A created new table based on another table does not
include old table’s integrity rule (no primary key)
Can re-establish integrity rules using ALTER
command
Use ALTER TABLE command to ADD primary and
foreign keys
Composite primary keys and multiple foreign keys can
be designated in a single SQL command
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
28
Deleting a Table from the Database
DROP TABLE: Deletes table from database
Syntax - DROP TABLE tablename;
Can drop a table only if it is not the one side of any relationship
RDBMS generates a foreign key integrity violation error message if the
table is dropped
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
29