SQL/212 SQL Programming Workshop 3 – Modifying Data, Managing the Database Bookstore SQL212 Module 3
SQL212 Contact Information Bookstore SQL212 Module 3 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2assoc...
SQL212 Module 3 <ul><li>Part 1 – Modifying Data </li></ul><ul><li>Part 2 – Managing Database Structures </li></ul><ul><li>...
SQL/212 SQL Programming Part 1 – Modifying Data Bookstore SQL212 Module 3
Bookstore SQL212 Module 3 Relational Database with constraints (from text)
Data Modification Statements <ul><li>Insert </li></ul><ul><li>Update </li></ul><ul><li>Delete </li></ul>Bookstore SQL212 M...
Data Modification Statements <ul><li>End-user rarely sees these statements </li></ul><ul><li>Application developer prepare...
Insert <ul><li>Adds new rows to an existing table </li></ul><ul><li>Two forms: </li></ul><ul><ul><li>Single Row </li></ul>...
Single Row Insert Bookstore SQL212 Module 3 Basic Syntax: Insert [into] <table-name> Values (<value-list>)
Single Row Insert Bookstore SQL212 Module 3 Basic Example: insert into  sources(source_numb, source_name, source_street) v...
Insert Statement Bookstore SQL212 Module 3
Sources table after Insert Bookstore SQL212 Module 3
Multi-row Insert Bookstore SQL212 Module 3 Basic Syntax: Insert [into] <table-name> Select <select-statement> We will do t...
Update <ul><li>Updates fields in an existing row </li></ul>Bookstore SQL212 Module 3 Basic Syntax: Update <table-name> Set...
Update <ul><li>Increase Ingram prices by 10% </li></ul>Bookstore SQL212 Module 3 Example: Update books Set retail_price = ...
Ingram Book Prices before Update Bookstore SQL212 Module 3
Ingram Book Prices after Update Bookstore SQL212 Module 3
Delete <ul><li>Deletes one or more rows </li></ul>Bookstore SQL212 Module 3 Basic Syntax: Delete from <table-name> Where <...
Delete Bookstore SQL212 Module 3 Example: delete the source we added Delete from sources Where source_numb = 22
Delete Bookstore SQL212 Module 3
Sources table after Delete Bookstore SQL212 Module 3
Delete and Referential Integrity <ul><li>Can affect referential integrity when deleting a “parent” row </li></ul><ul><li>C...
SQL/212 SQL Programming Part 2– Managing Database Structures Bookstore SQL212 Module 3
Schemas <ul><li>Logical view of a database; sort of a “sub-database” – we will not cover these in this module… </li></ul><...
Tables <ul><li>Two types </li></ul><ul><ul><li>Base tables </li></ul></ul><ul><ul><li>Temporary tables  </li></ul></ul><ul...
Creating Tables <ul><li>Use create statement </li></ul><ul><li>Specify: </li></ul><ul><ul><li>Columns with data types and ...
Data Types <ul><li>Int – integers or whole numbers </li></ul><ul><ul><li>Ex: how_many int </li></ul></ul><ul><li>Char – fi...
Create Table Bookstore SQL212 Module 3 Basic syntax: Create table <table-name> <column1> <datatype> <constraints> ,.. <col...
Temporary Tables Bookstore SQL212 Module 3 Basic syntax: Create [global] temporary table <table-name> <rest of statement a...
Column Constraints <ul><li>Primary key </li></ul><ul><li>Not NULL </li></ul><ul><li>CHECK clause </li></ul><ul><li>Default...
Table Constraints <ul><li>Primary Key </li></ul><ul><li>Foreign Key </li></ul><ul><li>Compare fields against each other. I...
But first – the Drop Statement <ul><li>Deletes a database “object” </li></ul><ul><ul><li>Drop table <table-name> </li></ul...
Create Table Bookstore SQL212 Module 3 Example 1: Create  a summary table Create  table summary( isbn varchar2(20)  primar...
Create Summary Table Bookstore SQL212 Module 3
Constraints on Summary Table Bookstore SQL212 Module 3
Multi-row Insert Bookstore SQL212 Module 3 Basic Syntax: Insert [into] <table-name> Select <select-statement>
Multi-row Insert Bookstore SQL212 Module 3 Basic Example:  (store # times each book ordered) Insert into summary(isbn, how...
Multi-row Insert Bookstore SQL212 Module 3
Bookstore SQL212 Module 3 After multi-row insert in MS Access
SQL/212 SQL Programming Part 3 – Creating Views and Indexes, Modifying Structures Bookstore SQL212 Module 3
Views <ul><li>Think of a view as a named query wherein the definition is stored in the database </li></ul><ul><li>Can be r...
Views Bookstore SQL212 Module 3 Basic syntax: Create view <view-name> (<column-list>) As <select statement>
Creating a View Bookstore SQL212 Module 3 <ul><li>Example: show inventory for each book </li></ul>
Using Views <ul><li>Can be used like a table subject to various limitations </li></ul><ul><ul><li>Cannot insert into group...
Using a View Bookstore SQL212 Module 3
Indexes <ul><li>Used  to speed searches, joins, etc. </li></ul><ul><li>Placed on: </li></ul><ul><ul><li>primary and foreig...
Indexes Bookstore SQL212 Module 3 Basic syntax: Create [unique] index <index-name>  On <table-name> (field-name> [desc]) N...
Indexes Bookstore SQL212 Module 3 Basic example: create index state_inx on customers(customer_state) <ul><li>Add an index ...
Bookstore SQL212 Module 3 After indexing on customer_state
State_inx in Oracle Bookstore SQL212 Module 3
Modifying a Table Design <ul><li>Applies to tables </li></ul><ul><li>Use ALTER statement </li></ul><ul><ul><li>Add columns...
Modifying a Table Design Bookstore SQL212 Module 3 Basic syntax: Alter <table-name> Add <field-name>, Add <table-constrain...
Modify a Table Design Bookstore SQL212 Module 3 Example: add a phone number field alter table publishers add phone char(12);
Bookstore SQL212 Module 3 After alter publishers table
SQL/212 SQL Programming Part 4 – Security Bookstore SQL212 Module 3
Security <ul><li>Important DBA function </li></ul><ul><ul><li>Beyond scope of this course </li></ul></ul><ul><ul><li>Typic...
Security <ul><li>Specifics can vary by product </li></ul><ul><li>Access: workgroup administrator </li></ul><ul><li>SQL Ser...
SQL Security Statements <ul><li>Grant </li></ul><ul><li>Revoke </li></ul><ul><li>Deny </li></ul>Bookstore SQL212 Module 3
Grant Bookstore SQL212 Module 3 Syntax: Grant <access-right> [with grant option] On <object> to <user>   Note: by default ...
Access Rights <ul><li>Select </li></ul><ul><li>Update </li></ul><ul><li>Insert </li></ul><ul><li>Delete </li></ul><ul><li>...
Grant Bookstore SQL212 Module 3 Example: Give user ddurso the permission to update the employees table. Grant update On em...
Revoke <ul><li>Revokes the rights </li></ul><ul><li>Syntax similar to grant </li></ul>Bookstore SQL212 Module 3 [end module]
Notes Bookstore SQL212 Module 3
Notes Bookstore SQL212 Module 3
Upcoming SlideShare
Loading in...5
×

SQL212.3 Introduction to SQL using Oracle Module 3

454

Published on

Modifying data and data structures.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
454
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

SQL212.3 Introduction to SQL using Oracle Module 3

  1. 1. SQL/212 SQL Programming Workshop 3 – Modifying Data, Managing the Database Bookstore SQL212 Module 3
  2. 2. SQL212 Contact Information Bookstore SQL212 Module 3 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-2009. All rights reserved.
  3. 3. SQL212 Module 3 <ul><li>Part 1 – Modifying Data </li></ul><ul><li>Part 2 – Managing Database Structures </li></ul><ul><li>Part 3 – Creating Views and Indexes </li></ul><ul><li>Part 4 -- Security </li></ul>Bookstore SQL212 Module 3
  4. 4. SQL/212 SQL Programming Part 1 – Modifying Data Bookstore SQL212 Module 3
  5. 5. Bookstore SQL212 Module 3 Relational Database with constraints (from text)
  6. 6. Data Modification Statements <ul><li>Insert </li></ul><ul><li>Update </li></ul><ul><li>Delete </li></ul>Bookstore SQL212 Module 3
  7. 7. Data Modification Statements <ul><li>End-user rarely sees these statements </li></ul><ul><li>Application developer prepares these statements “behind the scenes” based on forms filled out by user </li></ul>Bookstore SQL212 Module 3
  8. 8. Insert <ul><li>Adds new rows to an existing table </li></ul><ul><li>Two forms: </li></ul><ul><ul><li>Single Row </li></ul></ul><ul><ul><li>Multi-Row </li></ul></ul>Bookstore SQL212 Module 3
  9. 9. Single Row Insert Bookstore SQL212 Module 3 Basic Syntax: Insert [into] <table-name> Values (<value-list>)
  10. 10. Single Row Insert Bookstore SQL212 Module 3 Basic Example: insert into sources(source_numb, source_name, source_street) values (22,'Specialty Books', 'Canal Street')
  11. 11. Insert Statement Bookstore SQL212 Module 3
  12. 12. Sources table after Insert Bookstore SQL212 Module 3
  13. 13. Multi-row Insert Bookstore SQL212 Module 3 Basic Syntax: Insert [into] <table-name> Select <select-statement> We will do this after creating a new table later in this module
  14. 14. Update <ul><li>Updates fields in an existing row </li></ul>Bookstore SQL212 Module 3 Basic Syntax: Update <table-name> Set <field1> = new value, <field2> = new value,… Where <selection-criteria>
  15. 15. Update <ul><li>Increase Ingram prices by 10% </li></ul>Bookstore SQL212 Module 3 Example: Update books Set retail_price = retail_price *1.10 Where source_numb = 1
  16. 16. Ingram Book Prices before Update Bookstore SQL212 Module 3
  17. 17. Ingram Book Prices after Update Bookstore SQL212 Module 3
  18. 18. Delete <ul><li>Deletes one or more rows </li></ul>Bookstore SQL212 Module 3 Basic Syntax: Delete from <table-name> Where <selection-criteria>
  19. 19. Delete Bookstore SQL212 Module 3 Example: delete the source we added Delete from sources Where source_numb = 22
  20. 20. Delete Bookstore SQL212 Module 3
  21. 21. Sources table after Delete Bookstore SQL212 Module 3
  22. 22. Delete and Referential Integrity <ul><li>Can affect referential integrity when deleting a “parent” row </li></ul><ul><li>Can do following with child… </li></ul><ul><ul><li>Cascade: delete the child row </li></ul></ul><ul><ul><li>Set null: set the child’s foreign key null </li></ul></ul><ul><ul><li>Set default: as above but to default value </li></ul></ul><ul><ul><li>No action: don’t allow delete of parent row </li></ul></ul><ul><li>Referential integrity can be established when creating or modifying table structures which we will look at later in the class </li></ul>Bookstore SQL212 Module 3
  23. 23. SQL/212 SQL Programming Part 2– Managing Database Structures Bookstore SQL212 Module 3
  24. 24. Schemas <ul><li>Logical view of a database; sort of a “sub-database” – we will not cover these in this module… </li></ul><ul><ul><li>Catalogs </li></ul></ul><ul><ul><li>Clusters </li></ul></ul><ul><ul><li>Domains (somewhat like a user defined datatype) </li></ul></ul><ul><li>These topics are highly dependent upon the vendor DBMS and installation practices </li></ul><ul><ul><li>In Oracle you are logged in to your schema </li></ul></ul>Bookstore SQL212 Module 3
  25. 25. Tables <ul><li>Two types </li></ul><ul><ul><li>Base tables </li></ul></ul><ul><ul><li>Temporary tables </li></ul></ul><ul><ul><ul><li>Local (or module scope) </li></ul></ul></ul><ul><ul><ul><li>Global (session scope) </li></ul></ul></ul><ul><li>We will create base tables in this class </li></ul>Bookstore SQL212 Module 3
  26. 26. Creating Tables <ul><li>Use create statement </li></ul><ul><li>Specify: </li></ul><ul><ul><li>Columns with data types and column constraints </li></ul></ul><ul><ul><li>Table constraints </li></ul></ul><ul><ul><ul><li>Foreign key references </li></ul></ul></ul><ul><ul><ul><li>Primary key designation </li></ul></ul></ul>Bookstore SQL212 Module 3
  27. 27. Data Types <ul><li>Int – integers or whole numbers </li></ul><ul><ul><li>Ex: how_many int </li></ul></ul><ul><li>Char – fixed length fields </li></ul><ul><ul><li>Ex: state char(2) </li></ul></ul><ul><li>Varchar/Varchar2 – variable length fields </li></ul><ul><ul><li>Ex: address varchar2(35) </li></ul></ul><ul><li>Money – money field </li></ul><ul><li>Date/Datetime – date and time </li></ul><ul><li>And many others – see documentation or Help </li></ul>Bookstore SQL212 Module 3
  28. 28. Create Table Bookstore SQL212 Module 3 Basic syntax: Create table <table-name> <column1> <datatype> <constraints> ,.. <column1> <datatype> <constraints> … <table constraints> Note: often preceded by a drop
  29. 29. Temporary Tables Bookstore SQL212 Module 3 Basic syntax: Create [global] temporary table <table-name> <rest of statement as for normal create> Note: SQL Server uses a different syntax. Just put a #in front of the table name as in #mytable. Access doesn’t have true temporary tables.
  30. 30. Column Constraints <ul><li>Primary key </li></ul><ul><li>Not NULL </li></ul><ul><li>CHECK clause </li></ul><ul><li>Default </li></ul><ul><li>Unique </li></ul>Bookstore SQL212 Module 3
  31. 31. Table Constraints <ul><li>Primary Key </li></ul><ul><li>Foreign Key </li></ul><ul><li>Compare fields against each other. I.e. ship_date >= order_date </li></ul>Bookstore SQL212 Module 3
  32. 32. But first – the Drop Statement <ul><li>Deletes a database “object” </li></ul><ul><ul><li>Drop table <table-name> </li></ul></ul><ul><ul><li>Drop view <view-name> </li></ul></ul><ul><ul><li>Drop index <index-name> </li></ul></ul><ul><ul><li>Drop domain <domain-name> </li></ul></ul>Bookstore SQL212 Module 3
  33. 33. Create Table Bookstore SQL212 Module 3 Example 1: Create a summary table Create table summary( isbn varchar2(20) primary key , How_many int check (how_many >= 0), Constraint isbn_fk Foreign key (isbn) references books(isbn) )
  34. 34. Create Summary Table Bookstore SQL212 Module 3
  35. 35. Constraints on Summary Table Bookstore SQL212 Module 3
  36. 36. Multi-row Insert Bookstore SQL212 Module 3 Basic Syntax: Insert [into] <table-name> Select <select-statement>
  37. 37. Multi-row Insert Bookstore SQL212 Module 3 Basic Example: (store # times each book ordered) Insert into summary(isbn, how_many) Select isbn, count(*) From orderlines Group by isbn;
  38. 38. Multi-row Insert Bookstore SQL212 Module 3
  39. 39. Bookstore SQL212 Module 3 After multi-row insert in MS Access
  40. 40. SQL/212 SQL Programming Part 3 – Creating Views and Indexes, Modifying Structures Bookstore SQL212 Module 3
  41. 41. Views <ul><li>Think of a view as a named query wherein the definition is stored in the database </li></ul><ul><li>Can be read like a table </li></ul><ul><li>Some are updateable </li></ul>Bookstore SQL212 Module 3
  42. 42. Views Bookstore SQL212 Module 3 Basic syntax: Create view <view-name> (<column-list>) As <select statement>
  43. 43. Creating a View Bookstore SQL212 Module 3 <ul><li>Example: show inventory for each book </li></ul>
  44. 44. Using Views <ul><li>Can be used like a table subject to various limitations </li></ul><ul><ul><li>Cannot insert into grouped queries, etc. </li></ul></ul><ul><ul><li>Etc. </li></ul></ul><ul><li>Sample syntax: </li></ul><ul><ul><li>select column-list </li></ul></ul><ul><ul><li>from employee_view </li></ul></ul>Bookstore SQL212 Module 3
  45. 45. Using a View Bookstore SQL212 Module 3
  46. 46. Indexes <ul><li>Used to speed searches, joins, etc. </li></ul><ul><li>Placed on: </li></ul><ul><ul><li>primary and foreign keys </li></ul></ul><ul><ul><li>Secondary keys </li></ul></ul><ul><li>In commercial practice often managed by DBA’s for large databases </li></ul>Bookstore SQL212 Module 3
  47. 47. Indexes Bookstore SQL212 Module 3 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
  48. 48. Indexes Bookstore SQL212 Module 3 Basic example: create index state_inx on customers(customer_state) <ul><li>Add an index on state to speed searches and reporting </li></ul>
  49. 49. Bookstore SQL212 Module 3 After indexing on customer_state
  50. 50. State_inx in Oracle Bookstore SQL212 Module 3
  51. 51. Modifying a Table Design <ul><li>Applies to tables </li></ul><ul><li>Use ALTER statement </li></ul><ul><ul><li>Add columns </li></ul></ul><ul><ul><li>Delete columns </li></ul></ul><ul><ul><li>Rename columns </li></ul></ul><ul><ul><li>Add column constraints </li></ul></ul><ul><ul><li>Add table constraints </li></ul></ul>Bookstore SQL212 Module 3
  52. 52. Modifying a Table Design Bookstore SQL212 Module 3 Basic syntax: Alter <table-name> Add <field-name>, Add <table-constraint>, Modify <field-name> Etc.
  53. 53. Modify a Table Design Bookstore SQL212 Module 3 Example: add a phone number field alter table publishers add phone char(12);
  54. 54. Bookstore SQL212 Module 3 After alter publishers table
  55. 55. SQL/212 SQL Programming Part 4 – Security Bookstore SQL212 Module 3
  56. 56. Security <ul><li>Important DBA function </li></ul><ul><ul><li>Beyond scope of this course </li></ul></ul><ul><ul><li>Typically controlled through Enterprise Manager or Studio GUI’s </li></ul></ul><ul><li>In commercial practice application security frequently controlled via own login and a “users” table or similar </li></ul>Bookstore SQL212 Module 3
  57. 57. Security <ul><li>Specifics can vary by product </li></ul><ul><li>Access: workgroup administrator </li></ul><ul><li>SQL Server: users, roles </li></ul><ul><li>Oracle: users, roles </li></ul>Bookstore SQL212 Module 3
  58. 58. SQL Security Statements <ul><li>Grant </li></ul><ul><li>Revoke </li></ul><ul><li>Deny </li></ul>Bookstore SQL212 Module 3
  59. 59. Grant Bookstore SQL212 Module 3 Syntax: Grant <access-right> [with grant option] On <object> to <user> Note: by default only tables owners and admins can access a table. Others must be granted the relevant rights.
  60. 60. Access Rights <ul><li>Select </li></ul><ul><li>Update </li></ul><ul><li>Insert </li></ul><ul><li>Delete </li></ul><ul><li>References </li></ul><ul><li>All privileges </li></ul>Bookstore SQL212 Module 3
  61. 61. Grant Bookstore SQL212 Module 3 Example: Give user ddurso the permission to update the employees table. Grant update On employees to ddurso
  62. 62. Revoke <ul><li>Revokes the rights </li></ul><ul><li>Syntax similar to grant </li></ul>Bookstore SQL212 Module 3 [end module]
  63. 63. Notes Bookstore SQL212 Module 3
  64. 64. Notes Bookstore SQL212 Module 3

×