SQL200.3 Module 3

741 views

Published on

Workshop 3 – Modifying Data, Managing the Database

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

No Downloads
Views
Total views
741
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

SQL200.3 Module 3

  1. 1. SQL/200 SQL Programming Workshop 3 – Modifying Data, Managing the Database Bookstore SQL200 Module 3 1
  2. 2. SQL200 Contact Information P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com slides.1@dhdursoassociates.com Copyright 2001-2009. All rights reserved. Bookstore SQL200 Module 3 2
  3. 3. SQL200 Module 3 • Part 1 – Modifying Data Part 2 – Managing Database Structures • • Part 3 – Creating Views and Indexes • Part 4 -- Security Bookstore SQL200 Module 3 3
  4. 4. SQL/200 SQL Programming Part 1 – Modifying Data Bookstore SQL200 Module 3 4
  5. 5. Relational Database with constraints (from text) Bookstore SQL200 Module 3 5
  6. 6. Data Modification Statements • Insert • Update • Delete Bookstore SQL200 Module 3 6
  7. 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. 8. Insert • Adds new rows to an existing table • Two forms: – Single Row – Multi-Row Bookstore SQL200 Module 3 8
  9. 9. Single Row Insert Basic Syntax: Insert [into] <table-name> Values (<value-list>) Bookstore SQL200 Module 3 9
  10. 10. Single Row Insert Basic Example: insert into sources(source_numb, source_name, source_street) values(22,'Specialty Books', 'Canal Street') Bookstore SQL200 Module 3 10
  11. 11. Insert Statement Bookstore SQL200 Module 3 11
  12. 12. Sources table after Insert Bookstore SQL200 Module 3 12
  13. 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. 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. 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
  16. 16. Ingram Book Prices before Update Bookstore SQL200 Module 3 16
  17. 17. Ingram Book Prices after Update Bookstore SQL200 Module 3 17
  18. 18. After update in MS Access Bookstore SQL200 Module 3 18
  19. 19. Delete • Deletes one or more rows Basic Syntax: Delete from <table-name> Where <selection-criteria> Bookstore SQL200 Module 3 19
  20. 20. Delete Example: Delete from sources Where source_numb = 22 Bookstore SQL200 Module 3 20
  21. 21. Delete Bookstore SQL200 Module 3 21
  22. 22. Sources table after Delete Bookstore SQL200 Module 3 22
  23. 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
  24. 24. SQL/200 SQL Programming Part 2– Managing Database Structures Bookstore SQL200 Module 3 24
  25. 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. 26. Tables • Base tables • Temporary tables – Local (or module scope) – Global (session scope) Bookstore SQL200 Module 3 26
  27. 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. 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. 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. 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
  31. 31. Column Constraints • Primary key Not NULL • • CHECK clause • Default Unique • Bookstore SQL200 Module 3 31
  32. 32. Table Constraints • Primary Key • Foreign Key • Compare fields against each other. I.e. ship_date >= order_date Bookstore SQL200 Module 3 32
  33. 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. 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
  35. 35. Create Summary Table Bookstore SQL200 Module 3 35
  36. 36. Constraints on Summary Table Bookstore SQL200 Module 3 36
  37. 37. Multi-row Insert Basic Syntax: Insert [into] <table-name> Select <select-statement> Bookstore SQL200 Module 3 37
  38. 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
  39. 39. Multi-row Insert Bookstore SQL200 Module 3 39
  40. 40. After multi-row insert in MS Access Bookstore SQL200 Module 3 40
  41. 41. SQL/200 SQL Programming Part 3 – Creating Views and Indexes, Modifying Structures Bookstore SQL200 Module 3 41
  42. 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
  43. 43. Views Basic syntax: Create view <view-name> (<column-list>) As <select statement> Bookstore SQL200 Module 3 43
  44. 44. Creating a View Bookstore SQL200 Module 3 44
  45. 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
  46. 46. Using a View Bookstore SQL200 Module 3 46
  47. 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. 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. 49. Indexes Basic example: create index state_inx on customers(customer_state) Bookstore SQL200 Module 3 49
  50. 50. After indexing on customer_state Bookstore SQL200 Module 3 50
  51. 51. Customers table with index Bookstore SQL200 Module 3 51
  52. 52. State_inx in Oracle Bookstore SQL200 Module 3 52
  53. 53. Modifying a Table Design • Applies to tables • Use ALTER statement – Add columns – Delete columns – Rename columns – Add column constraints – Add table constraints Bookstore SQL200 Module 3 53
  54. 54. Modifying a Table Design Basic syntax: Alter <table-name> Add <field-name>, Add <table-constraint>, Modify <field-name> Etc. Bookstore SQL200 Module 3 54
  55. 55. Modify a Table Design Example: add a phone number field alter table publishers add phone char(12); Bookstore SQL200 Module 3 55
  56. 56. After alter publishers table Bookstore SQL200 Module 3 56
  57. 57. SQL/200 SQL Programming Part 4 – Security Bookstore SQL200 Module 3 57
  58. 58. Security • Specifics can vary by product Access: workgroup administrator • • SQL Server: users, groups • Oracle: users, roles Bookstore SQL200 Module 3 58
  59. 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
  60. 60. SQL Security Statements • Grant • Revoke • Deny Bookstore SQL200 Module 3 60
  61. 61. Grant Syntax: Grant <access-right> [with grant option] On <object> to <user> Bookstore SQL200 Module 3 61
  62. 62. Access Rights • Select Update • • Insert • Delete References • • All privileges Bookstore SQL200 Module 3 62
  63. 63. Grant Example: Grant update On employees to ddurso Bookstore SQL200 Module 3 63
  64. 64. Revoke • Revokes the rights • Syntax similar to grant [end module] Bookstore SQL200 Module 3 64

×