SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3

507 views

Published on

Accelerated Introduction to SQL using Microsoft SQL Server. Covers insert, update, delete, create, drop, alter statements. Includes views, indexes, tables, constraints.

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

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

No notes for slide

SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3

  1. 1. SQL/200 SQL Programming Workshop 3 – Modifying Data, Managing the Database Bookstore SQL200 Module 3 Based on SQL Clearly Explained by Jan Harrington
  2. 2. Note on SQL200 Slides <ul><li>These slides were originally designed to support the single SQL200 course which was used for any of MS Access, Oracle and SQL Server. </li></ul><ul><li>As such you may see here slides developed in any one of the above products. </li></ul><ul><li>We are in the process of migrating the Oracle slides and the MS Access slides out into their own slide sets. These SQL200 slides (used in SQL202 as well as SQL200) will focus on Microsoft SQL Server. </li></ul>Bookstore SQL200 Module 3
  3. 3. Warning! <ul><li>Below are some table name changes to be aware of in doing queries. We have created synonyms so either name should work. </li></ul>Bookstore2 SQL200 Module 2 New Name Old Name Orders Order_filled Order_Lines Orderlines
  4. 4. SQL200 Contact Information Bookstore SQL200 Module 3 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-2011. All rights reserved.
  5. 5. SQL200 Resources <ul><li>Bookstore database scripts found on box.net at </li></ul><ul><ul><li>http://tinyurl.com/SQLScripts </li></ul></ul><ul><li>Slides can be viewed on SlideShare… </li></ul><ul><ul><li>http://www.slideshare.net/OCDatabases </li></ul></ul><ul><li>Follow up questions? </li></ul><ul><ul><li>[email_address] </li></ul></ul>Bookstore SQL212 Module 1
  6. 6. SQL200 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 SQL200 Module 3
  7. 7. SQL/200 SQL Programming Part 1 – Modifying Data Bookstore SQL200 Module 3
  8. 8. Bookstore SQL200 Module 3 Relational Database with constraints (from text)
  9. 9. Data Modification Statements <ul><li>Insert </li></ul><ul><li>Update </li></ul><ul><li>Delete </li></ul>Bookstore SQL200 Module 3
  10. 10. 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 SQL200 Module 3
  11. 11. 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 SQL200 Module 3
  12. 12. Single Row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> Values (<value-list>)
  13. 13. Single Row Insert Bookstore SQL200 Module 3 Basic Example: insert into sources(source_numb, source_name, source_street) values (22,'Specialty Books', 'Canal Street')
  14. 14. Insert Statement Bookstore SQL200 Module 3
  15. 15. Sources table after Insert Bookstore SQL200 Module 3
  16. 16. Multi-row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> Select <select-statement> We will do this after creating a new table later in this module
  17. 17. Update <ul><li>Updates fields in an existing row </li></ul>Bookstore SQL200 Module 3 Basic Syntax: Update <table-name> Set <field1> = new value, <field2> = new value,… Where <selection-criteria>
  18. 18. Update <ul><li>Increase Ingram prices by 10% </li></ul>Bookstore SQL200 Module 3 Example: Update books Set retail_price = retail_price *1.10 Where source_numb = 1
  19. 19. Ingram Book Prices before Update Bookstore SQL200 Module 3
  20. 20. Ingram Book Prices after Update Bookstore SQL200 Module 3
  21. 21. Bookstore SQL200 Module 3 After update in MS Access
  22. 22. Delete <ul><li>Deletes one or more rows </li></ul>Bookstore SQL200 Module 3 Basic Syntax: Delete from <table-name> Where <selection-criteria>
  23. 23. Delete Bookstore SQL200 Module 3 Example: Delete from sources Where source_numb = 22
  24. 24. Delete Bookstore SQL200 Module 3
  25. 25. Sources table after Delete Bookstore SQL200 Module 3
  26. 26. 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 SQL200 Module 3
  27. 27. SQL/200 SQL Programming Part 2– Managing Database Structures Bookstore SQL200 Module 3
  28. 28. DDL <ul><li>Create </li></ul><ul><li>Alter </li></ul><ul><li>Drop </li></ul>Bookstore SQL200 Module 3
  29. 29. Schemas <ul><li>Logical view of a database; sort of a “sub-database” – we will not cover these in this module or… </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>Bookstore SQL200 Module 3
  30. 30. Tables <ul><li>Base tables </li></ul><ul><li>Temporary tables </li></ul><ul><ul><li>Local (or module scope) </li></ul></ul><ul><ul><li>Global (session scope) </li></ul></ul>Bookstore SQL200 Module 3
  31. 31. 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 SQL200 Module 3
  32. 32. 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 varchar(35) </li></ul></ul><ul><li>Money – money field; same as MS Access currency </li></ul><ul><li>Date/Datetime – date and time </li></ul><ul><li>And many others – see documentation or Help </li></ul>Bookstore SQL200 Module 3
  33. 33. Create Table Bookstore SQL200 Module 3 Basic syntax: Create table <table-name> <column1> <datatype> <constraints> ,.. <column1> <datatype> <constraints> … <table constraints> Note: often preceded by a drop
  34. 34. Temporary Tables Bookstore SQL200 Module 3 Basic syntax (SQL standard): 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.
  35. 35. 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 SQL200 Module 3
  36. 36. 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 SQL200 Module 3
  37. 37. 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 SQL200 Module 3
  38. 38. Create Table Bookstore SQL200 Module 3 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) )
  39. 39. Create Summary Table Bookstore SQL200 Module 3
  40. 40. Constraints on Summary Table Bookstore SQL200 Module 3
  41. 41. Multi-row Insert Bookstore SQL200 Module 3 Basic Syntax: Insert [into] <table-name> [(<column list>)] Select <select-statement>
  42. 42. Multi-row Insert Bookstore SQL200 Module 3 Basic Example: (store # times each book ordered) Insert into summary Select isbn, count(*) From orderlines Group by isbn;
  43. 43. Multi-row Insert Bookstore SQL200 Module 3
  44. 44. Bookstore SQL200 Module 3 After multi-row insert in MS Access
  45. 45. SQL/200 SQL Programming Part 3 – Creating Views and Indexes, Modifying Structures Bookstore SQL200 Module 3
  46. 46. 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 SQL200 Module 3
  47. 47. Views Bookstore SQL200 Module 3 Basic syntax: Create view <view-name> (<column-list>) As <select statement>
  48. 48. Creating a View Bookstore SQL200 Module 3
  49. 49. 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 SQL200 Module 3
  50. 50. Using a View Bookstore SQL200 Module 3
  51. 51. 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 SQL200 Module 3
  52. 52. Indexes Bookstore SQL200 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
  53. 53. Indexes Bookstore SQL200 Module 3 Basic example: create index state_inx on customers(customer_state)
  54. 54. Customers table with index Bookstore SQL200 Module 3
  55. 55. Dropping an index <ul><li>Basic Syntax: </li></ul><ul><ul><li>Drop index <table-name.index-name>; </li></ul></ul>Bookstore SQL200 Module 3
  56. 56. 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 SQL200 Module 3
  57. 57. Modifying a Table Design Bookstore SQL200 Module 3 Basic syntax: Alter <table-name> Add <field-name>, Add <table-constraint>, Modify <field-name> Etc.
  58. 58. Modify a Table Design Bookstore SQL200 Module 3 Example: add a phone number field alter table publishers add phone char(12);
  59. 59. Bookstore SQL200 Module 3 After alter publishers table
  60. 60. SQL/200 SQL Programming Part 4 – Security Bookstore SQL200 Module 3
  61. 61. 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 SQL200 Module 3
  62. 62. 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 SQL200 Module 3
  63. 63. SQL Security Statements <ul><li>Grant </li></ul><ul><li>Revoke </li></ul><ul><li>Deny </li></ul>Bookstore SQL200 Module 3
  64. 64. Grant Bookstore SQL200 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.
  65. 65. 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 SQL200 Module 3
  66. 66. Grant Bookstore SQL200 Module 3 Example: Grant update On employees to ddurso
  67. 67. Revoke <ul><li>Revokes the rights </li></ul><ul><li>Syntax similar to grant </li></ul>Bookstore SQL200 Module 3 [end module]
  68. 68. Notes Bookstore SQL200 Module 3

×