This document is a module on modifying data and managing databases in SQL. It covers SQL statements for inserting, updating, and deleting data. It also discusses managing database structures like creating and modifying tables, creating views and indexes. The last part covers basic security concepts in SQL like granting and revoking user privileges.
7. Data Modification Statements
• End-user rarely sees these
statements
• Application developer prepares
these statements “behind the
scenes” based on forms filled out
by user
Bookstore SQL200 Module 3 7
8. Insert
• Adds new rows to an existing table
• Two forms:
– Single Row
– Multi-Row
Bookstore SQL200 Module 3 8
13. Multi-row Insert
Basic Syntax:
Insert [into] <table-name>
Select <select-statement>
We will do this after creating a new
table later in this module
Bookstore SQL200 Module 3 13
14. Update
• Updates fields in an existing row
Basic Syntax:
Update <table-name>
Set <field1> = new value, <field2> = new value,
…
Where <selection-criteria>
Bookstore SQL200 Module 3 14
15. Update
• Increase Ingram prices by 10%
Example:
Update books
Set retail_price = retail_price
*1.10
Where source_numb = 1
Bookstore SQL200 Module 3 15
23. Delete and Referential Integrity
• Can affect referential integrity when deleting a
“parent” row
• Can do following with child…
– Cascade: delete the child row
– Set null: set the child’s foreign key null
– Set default: as above but to default value
– No action: don’t allow delete of parent row
• Referential integrity can be established when
creating or modifying table structures which we
will look at later in the class
Bookstore SQL200 Module 3 23
25. Schemas
• Logical view of a database; sort of a “sub-
database” – we will not cover these in this module
or…
– Catalogs
– Clusters
– Domains (somewhat like a user defined datatype)
• These topics are highly dependent upon the vendor
DBMS and installation practices
Bookstore SQL200 Module 3 25
26. Tables
• Base tables
• Temporary tables
– Local (or module scope)
– Global (session scope)
Bookstore SQL200 Module 3 26
27. Creating Tables
• Use create statement
• Specify:
– Columns with data types and column
constraints
– Table constraints
• Foreign key references
• Primary key designation
Bookstore SQL200 Module 3 27
28. Temporary Tables
Basic syntax:
Create [global] temporary table <table-name>
<rest of statement as for normal create>
Note: SQL Server uses a different syntax. Put a
#in front of the table name as in #mytable.
Access doesn’t have true temporary tables.
Bookstore SQL200 Module 3 28
29. Data Types
• Int – integers or whole numbers
– Ex: how_many int
• Char – fixed length fields
– Ex: state char(2)
• Varchar/Varchar2 – variable length fields
– Ex: address varchar(35)
• Money – money field; same as MS Access
currency
• Date/Datetime – date and time
• And many others – see documentation or Help
Bookstore SQL200 Module 3 29
30. Create Table
Basic syntax:
Create table <table-name>
<column1> <datatype> <constraints>
,.. <column1> <datatype> <constraints>
…
<table constraints>
Note: often preceded by a drop
Bookstore SQL200 Module 3 30
32. Table Constraints
• Primary Key
• Foreign Key
• Compare fields against each other. I.e.
ship_date >= order_date
Bookstore SQL200 Module 3 32
33. But first – the Drop Statement
• Deletes a database “object”
– Drop table <table-name>
– Drop view <view-name>
– Drop index <index-name>
– Drop domain <domain-name>
Bookstore SQL200 Module 3 33
34. Create Table
Example 1: Create a summary table
Create table summary(
isbn varchar(20) primary key,
How_many int check (how_many >= 0),
Constraint isbn_fk
Foreign key (isbn) references
books(isbn)
)
Bookstore SQL200 Module 3 34
38. Multi-row Insert
Basic Example: (store # times each
book ordered)
Insert into
summary(isbn, how_many)
Select isbn, count(*)
From orderlines
Group by isbn;
Bookstore SQL200 Module 3 38
41. SQL/200
SQL Programming
Part 3 – Creating Views and Indexes, Modifying
Structures
Bookstore SQL200 Module 3 41
42. Views
• Think of a view as a named query wherein
the definition is stored in the database
• Can be read like a table
• Some are updateable
Bookstore SQL200 Module 3 42
45. Using Views
• Can be used like a table subject to various
limitations
– Cannot insert into grouped queries, etc.
– Etc.
• Sample syntax:
select column-list
from employee_view
Bookstore SQL200 Module 3 45
47. Indexes
• Used to speed searches, joins, etc.
• Placed on:
– primary and foreign keys
– Secondary keys
• In commercial practice often managed by
DBA’s for large databases
Bookstore SQL200 Module 3 47
48. Indexes
Basic syntax:
Create [unique] index <index-name>
On <table-name> (field-name> [desc])
Note: can place index on a composite key; ex: state and
city
Bookstore SQL200 Module 3 48
49. Indexes
Basic example:
create index state_inx
on customers(customer_state)
Bookstore SQL200 Module 3 49
58. Security
• Specifics can vary by product
Access: workgroup administrator
•
• SQL Server: users, groups
• Oracle: users, roles
Bookstore SQL200 Module 3 58
59. Security
• Important DBA function
– Beyond scope of this course
– Typically controlled through the Enterprise
Managers
• In commercial practice application security
frequently controlled via own login and a
“users” table or similar
Bookstore SQL200 Module 3 59