Intro to tsql unit 7


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 1
  • 45
  • 47
  • Intro to tsql unit 7

    1. 1. Introduction To SQL Unit 7 Modern Business Technology Introduction To TSQL Unit 7 Developed by Michael Hotek
    2. 2. Integrity <ul><li>Integrity is the process by which data is validated and consistency is enforced </li></ul><ul><li>Databases were designed with integrity as a primary factor </li></ul><ul><li>Integrity can be enforced by a variety of means </li></ul><ul><ul><li>Rules </li></ul></ul><ul><ul><li>Defaults </li></ul></ul><ul><ul><li>Constraints </li></ul></ul><ul><ul><li>Primary keys </li></ul></ul><ul><ul><li>Foreign keys </li></ul></ul><ul><ul><li>Unique indexes </li></ul></ul><ul><ul><li>Triggers </li></ul></ul><ul><li>Integrity can also be programmatic or declarative </li></ul>
    3. 3. Declarative Integrity <ul><li>Defaults and constraints can be used directly in a create table statement, hence declarative integrity </li></ul><ul><li>Constraints include </li></ul><ul><ul><li>Check </li></ul></ul><ul><ul><li>Unique </li></ul></ul><ul><ul><li>Primary Key </li></ul></ul><ul><ul><li>Reference </li></ul></ul><ul><li>Constraints can be column or table level </li></ul><ul><li>Defaults are only column level </li></ul>
    4. 4. Defaults <ul><li>A default clause is used to supply a value for a column when one is not explicitly specified in an insert statement </li></ul><ul><li>For a DEFAULT constraint: </li></ul><ul><li>[CONSTRAINT constraint_name ] DEFAULT { constant _ expression | niladic-function | NULL} [FOR col_name ] </li></ul><ul><li>create table address </li></ul><ul><li>(CompID int not null, </li></ul><ul><li>Address varchar(50) not null, </li></ul><ul><li>City varchar(30) </li></ul><ul><li>default 'Chicago' , </li></ul><ul><li>State char(2) default 'IL' ) </li></ul>
    5. 5. Defaults <ul><li>Functions can also be used in place of constants as long as they return a single value </li></ul><ul><li>The value of the default must match the datatype of the column </li></ul><ul><li>Character and date values must be enclosed in quotes </li></ul><ul><li>A column can have only one default </li></ul><ul><li>sp_helpconstraint can be used to return constraint information about a table. </li></ul>
    6. 6. Check Constraints <ul><li>Check constraints are used to enforce domain integrity </li></ul><ul><li>Can be applied at a table and a column level </li></ul><ul><li>Constraints are used to specify: </li></ul><ul><ul><li>List or set of values </li></ul></ul><ul><ul><li>range of values </li></ul></ul><ul><ul><li>Format for data </li></ul></ul><ul><ul><li>Conditions on a value </li></ul></ul><ul><li>Enforced during inserts and updates </li></ul><ul><li>Must evaluate to a true or false </li></ul>
    7. 7. Column Constraints <ul><li>create table people </li></ul><ul><li>(SSN char(11) not null </li></ul><ul><li>constraint chk_ssn </li></ul><ul><li>check (SSN like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]' , </li></ul><ul><li>FirstName varchar(30) not null, </li></ul><ul><li>LastName varchar(50) not null, </li></ul><ul><li>… ) </li></ul><ul><li>or </li></ul><ul><li>create table people </li></ul><ul><li>(SSN char(11) not null </li></ul><ul><li>check (SSN like '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]' , </li></ul><ul><li>FirstName varchar(30) not null, </li></ul><ul><li>LastName varchar(50) not null, </li></ul><ul><li>… ) </li></ul>
    8. 8. Table Constraints <ul><li>Used for more than one column </li></ul><ul><li>create table discounts </li></ul><ul><li>(Type varchar(40) not null, </li></ul><ul><li>StoreID char(4) not null, </li></ul><ul><li>LowQty int not null, </li></ul><ul><li>HighQty int not null, </li></ul><ul><li>Discount float null, </li></ul><ul><li>constraint chk_low_high </li></ul><ul><li>check (LowQty <= HighQty) ) </li></ul>
    9. 9. Indexes <ul><li>Separate structure attached to a table </li></ul><ul><li>Contain pointers to the physical data </li></ul><ul><li>Used to increase performance when: </li></ul><ul><ul><li>Finding rows </li></ul></ul><ul><ul><li>Correlating data across tables </li></ul></ul><ul><ul><li>Ordering result sets </li></ul></ul><ul><ul><li>Inserting data in some cases </li></ul></ul><ul><li>Can enforce unique values in a column or table </li></ul><ul><li>CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX index_name ON [[ database . ] owner . ] table_name ( column_name [ , column_name ]... ) [WITH[FILLFACTOR = x ][[ , ] IGNORE_DUP_KEY] [[ , ] {SORTED_DATA | SORTED_DATA_REORG}] [[ , ] {IGNORE_DUP_ROW |ALLOW_DUP_ROW}]] [ON segment_name ] </li></ul>
    10. 10. Pages <ul><li>Data is stored in SQL Server is a set of structures called pages </li></ul><ul><li>Each page is 2K in size (8K in 7.0) </li></ul><ul><li>Many rows can be on a single page </li></ul><ul><li>A single row must be contained entirely on a page </li></ul><ul><li>Each page contains a header area that identifies the contents of each page </li></ul><ul><li>Pages are stored in a doubly linked list </li></ul>
    11. 11. Indexes <ul><li>As data gets added, a large number of pages are created </li></ul><ul><li>Indexes were devised to quickly navigate these pages </li></ul><ul><li>Indexes are also stored in pages </li></ul><ul><li>The index pages are stored in a B-Tree to support quick location of information </li></ul>
    12. 12. Indexes 177-32-1176 756-30-7391 756-30-7391 899-46-2035 177-32-1176 267-41-2394 409-56-7008 177-32-1176... 213-46-8915... 238-95-7766... 267-41-2394... 341-53-8472... 402-31-7808... 409-56-7008... 532-86-9471... 655-27-5281... 756-30-7391... 775-93-6481... 835-21-6639... Data Pages Index Pages
    13. 13. Indexes <ul><li>There are two types of indexes </li></ul><ul><ul><li>clustered </li></ul></ul><ul><ul><li>nonclusterd </li></ul></ul><ul><li>Only be 1 clustered index per table </li></ul><ul><li>Up to 249 nonclustered indexes </li></ul><ul><li>Order of data in the table is determined by the type of index </li></ul><ul><ul><li>clustered index </li></ul></ul><ul><ul><ul><li>Data in the same order as the index </li></ul></ul></ul><ul><ul><li>nonclustered index </li></ul></ul><ul><ul><ul><li>Data in the order it was inserted </li></ul></ul></ul>
    14. 14. Keys and Indexes <ul><li>Keys: </li></ul><ul><li>Logical </li></ul><ul><ul><li>Primary, foreign, and common </li></ul></ul><ul><li>Physical </li></ul><ul><ul><li>Single column or composite </li></ul></ul><ul><ul><li>This is an index </li></ul></ul><ul><li>Indexes are not necessarily logical keys </li></ul><ul><li>Indexes can be applied to columns that are not keys </li></ul><ul><li>Can contain up to 16 columns </li></ul><ul><li>Can have a maximum size of 256 bytes </li></ul>
    15. 15. Clustered Index <ul><li>Only one per table </li></ul><ul><li>This is your most powerful index </li></ul><ul><li>Physically orders the data in a table </li></ul><ul><li>Can be equated to the old card catalog </li></ul><ul><li>Good for range searches </li></ul><ul><li>Slow for inserts </li></ul><ul><ul><li>page splitting </li></ul></ul>
    16. 16. Clustered Indexes Bennet 1007 Page 1001 Karsen 1009 Smith 1062 Key ptr Karsen 1315 Page 1009 Key ptr Bennet 1132 Page 1007 Greane 1133 Hunter 1127 Key ptr Hunter Page 11127 Jenkins Greane Page 1133 Green Greene Bennet Page 1132 Chan Edwards Dull Data Pages Leaf Level Intermediate Root Page
    17. 17. Clustered Indexes <ul><li>The leaf level of the index is the data page of the table </li></ul><ul><li>Only one entry can point to a page in the next level </li></ul><ul><li>Require an additional 120% of space during creation </li></ul>
    18. 18. Nonclustered Indexes <ul><li>Data is stored in a random order at the data page level </li></ul><ul><li>Up to 249 nonclustered indexes can be defined per table </li></ul><ul><li>Good for searches of explicit values </li></ul><ul><li>Are much larger than a clustered index </li></ul>
    19. 19. Nonclustered Indexes Page 1001 Bennet 1007 1421,1 Karsen 1305 1876,1 Smith 1062 1242,1 Page 1007 Bennet 1132 1421,1 Greane 1133 1242,4 Hunter 1127 1242,1 Page 1305 Karsen 1311 1876,1 Page 1133 Greane 1242,4 Green 1409,2 Greene 1421,2 Page 1127 Hunter 1242,1 Jenkins 1241,4 Page 1421 18 Bennet 19 Greene 20 Ringer Page 1409 21 Dull 22 Green 23 White Page 1241 10 O'Leary 11 Ringer 12 White 13 Jenkins Page 1242 14 Hunter 15 Smith 16 Ringer 17 Greane Page 1132 Bennet 1421,1 Chan 1129,3 Dull 1409,1 Edwards 1018,5 Key Row ptr Page ptr Key Row ptr Page ptr Key Row ptr Root Page Intermediate Leaf Pages Data Pages
    20. 20. Nonclustered Indexes <ul><li>The root and intermediate levels work similarly for both clustered and nonclustered indexes </li></ul><ul><li>The leaf level of a nonclustered index contains a pointer to the row on each data page </li></ul><ul><li>The pointers at the leaf level are in index order </li></ul>
    21. 21. Unique Constraint <ul><li>Ensures no two rows have the same value </li></ul><ul><li>Allows one null value in a column </li></ul><ul><li>Creates a unique, nonclustered index by default </li></ul><ul><li>create table publishers </li></ul><ul><li>(pub_id char(4) null, </li></ul><ul><li>constraint u_pub_id unique , </li></ul><ul><li>pub_name varchar(30) not null) </li></ul>
    22. 22. Primary Key <ul><li>Ensures no two rows have the same value </li></ul><ul><li>Nulls are not allowed </li></ul><ul><li>Creates a unique, clustered index by default </li></ul><ul><li>create table publishers </li></ul><ul><li>(pub_id char(4) </li></ul><ul><li>constraint publishers_PK primary key , </li></ul><ul><li>pub_name varchar(30)) </li></ul><ul><li>create table sales </li></ul><ul><li>(stor_id char(4) not null, </li></ul><ul><li>ord_num varchar(20) not null, </li></ul><ul><li>date datetime nit null, </li></ul><ul><li>constraint sales_PK primary key </li></ul><ul><li>nonclustered (stor_id, ord_num) ) </li></ul>
    23. 23. Referential Integrity <ul><li>Used to maintain foreign keys when data is inserted or updated </li></ul><ul><li>Column </li></ul><ul><li>create table <table name> </li></ul><ul><li>(column datatype </li></ul><ul><li>[constraint constraint_name] </li></ul><ul><li>references ref_table [ref_column] </li></ul><ul><li>Table </li></ul><ul><li>[constraint constraint_name] </li></ul><ul><li>foreign key (column [{,column}…]) </li></ul><ul><li>references ref_table </li></ul><ul><li>[(column [{, column}…])] </li></ul>
    24. 24. Referential Integrity <ul><li>Use a column level constraint when only one column needs to be compared </li></ul><ul><li>Use a table level constraint when more than one column needs to be compared </li></ul><ul><li>The table in the references clause must already have a primary/unique constraint or a unique index defined on the columns </li></ul><ul><li>A roll back is issued if referential integrity is violated and a message is sent back </li></ul>
    25. 25. Referential Integrity <ul><li>Column Level </li></ul><ul><li>create table titles </li></ul><ul><li>(title_id tid not null, </li></ul><ul><li>title varchar(80) null, </li></ul><ul><li>pub_id char(4) null </li></ul><ul><li>constraint publishers_pub_id </li></ul><ul><li>references publishers(pub_id) , </li></ul><ul><li>notes varchar(200) null) </li></ul><ul><li>Restrictions </li></ul>
    26. 26. Referential Integrity <ul><li>create table salesdetail </li></ul><ul><li>(stor_id char(4) not null, </li></ul><ul><li>ord_num varchar(20) not null, </li></ul><ul><li>title_id tid not null, </li></ul><ul><li>qty int not null, </li></ul><ul><li>discount float not null, </li></ul><ul><li>constraint salesdetail_FK1 </li></ul><ul><li>foreign key (stor_id, ord_num) </li></ul><ul><li>references sales(stor_id, ord_num), </li></ul><ul><li>constraint salesdetail_FK2 </li></ul><ul><li>foreign key (title_id) </li></ul><ul><li>references titles(title_id) ) </li></ul>
    27. 27. Referential Integrity <ul><li>When primary keys are deleted and updated, three different option could be performed: </li></ul><ul><ul><li>Restrict </li></ul></ul><ul><ul><li>Cascade </li></ul></ul><ul><ul><li>Set null </li></ul></ul><ul><li>Declarative RI enforces a restrict </li></ul><ul><li>Cascade and set null can only be accomplished with triggers </li></ul><ul><li>In a perfect world, updates to a primary key are not allowed </li></ul><ul><li>In an imperfect world, these should be kept to a minimum </li></ul>
    28. 28. Error Messages <ul><li>Custom messages can be added with sp_addmessage </li></ul><ul><li>Drop a message with sp_dropmessage </li></ul><ul><li>Get a message with sp_getmessage </li></ul><ul><li>In Sybase, these messages can be bound to a constraint so that on a failure, a nice message to returned to the user </li></ul><ul><li>Bind messages using sp_bindmsg </li></ul><ul><li>Unbind messages using sp_unbindmsg </li></ul>
    29. 29. Alter Table <ul><li>Once a table is created, certain modifications to its structure can be performed </li></ul><ul><li>Allowed: </li></ul><ul><ul><li>Add columns </li></ul></ul><ul><ul><li>Add, drop, or change constraints </li></ul></ul><ul><li>Not allowed </li></ul><ul><ul><li>Dropping columns </li></ul></ul><ul><ul><li>Changing datatypes </li></ul></ul><ul><ul><li>Changing width of columns </li></ul></ul><ul><ul><li>Changing nullability options </li></ul></ul><ul><li>Constraints can only be modified with an alter table statement </li></ul><ul><li>Modifications to constraints do not affect existing data </li></ul>
    30. 30. Alter Table <ul><li>ALTER TABLE [ database . [ owner ] . ] table_name [WITH NOCHECK] [ADD { col_name column_properties [ column_constraints ] | [[ , ] table_constraint ]} [ , { next_col_name | next_table_constraint }]...] | [DROP [CONSTRAINT] constraint_name [ , constraint_name2 ]...] </li></ul>
    31. 31. Getting Help <ul><li>To obtain information about constraints and defaults defined for a table use </li></ul><ul><li>sp_helpconstraint table </li></ul>
    32. 32. Unit 7 Review <ul><li>Constraints are used to enforce integrity </li></ul><ul><li>A default will supply a value during an insert </li></ul><ul><li>Check constraints enforce valid data during inserts and updates </li></ul><ul><li>Data is stored in data pages that are 2K in size </li></ul><ul><li>A table can have 1 clustered index </li></ul><ul><ul><li>Physically orders the data </li></ul></ul><ul><ul><li>Leaf level of index is the data pages </li></ul></ul><ul><ul><li>Used for range searches </li></ul></ul><ul><li>A table can have up to 249 nonclusterd indexes </li></ul><ul><ul><li>Does not order the data </li></ul></ul><ul><ul><li>The leaf level of the index contains pointers to rows </li></ul></ul><ul><ul><li>Used for explicit searches </li></ul></ul><ul><li>Indexes can have up to 16 columns </li></ul><ul><li>Can be a maximum of 256 bytes </li></ul><ul><li>Unique constraint creates a unique, nonclustered index by default and allows one null </li></ul><ul><li>Primary key constraint creates a unique, clustered index by default and doe not allow nulls </li></ul>
    33. 33. <ul><li>Foreign keys are enforced via a references constraint </li></ul><ul><li>Referenced column(s) must have a primary/unique constraint or a unique index defined </li></ul><ul><li>Roll back is performed if RI is violated </li></ul><ul><li>The only type of RI that can be applied when modifying a primary key with constraints is restrict </li></ul><ul><li>You can add custom messages </li></ul><ul><li>Alter table can add columns </li></ul><ul><li>Alter can add, drop, or modify constraints </li></ul><ul><li>You can not drop a column </li></ul><ul><li>You can not change a datatype </li></ul><ul><li>You can not change the length of a column </li></ul><ul><li>You can not change the nullability </li></ul>Unit 7 Review
    34. 34. <ul><li>Time allotted for exercises is 30 minutes </li></ul>Unit 7 Exercises