0% found this document useful (0 votes)
25 views29 pages

M4 SQL partII

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views29 pages

M4 SQL partII

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 7

Introduction to Structured Query


Language (SQL)
©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.
Creating the Database
 Create database structure
 RDBMS creates physical files that will hold database
 Differs from one RDBMS to another
 Authentication is the process DBMS uses to verify
that only registered users access the database
 Required for the creation tables
 User should log on to RDBMS using user ID and
password created by database administrator

©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

INSERT: Command to insert data into table

• Syntax : INSERT INTO tablename [(columnnames)]


VALUES (value1, value2, … , valueN);

• Used to add table rows with NULL and NOT NULL


attributes

©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

COMMIT: Command to save changes

• Syntax - COMMIT [WORK];


• Ensures database update integrity

ROLLBACK: Command to restore the database


• Syntax - ROLLBACK;
• Undoes the changes since last COMMIT 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.
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

• Syntax - UPDATE tablename SET columnname =


expression [, columnname = expression] [WHERE
conditionlist];

• If more than one attribute is to be updated in row,


separate corrections with commas

©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

DELETE: Command to delete

• Syntax - DELETE FROM tablename


• [WHERE conditionlist];

• WHERE condition is optional


• If WHERE condition is not specified, all rows from
specified table will be deleted
©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.
20
More SQL commands

©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

You might also like