Intro to tsql unit 8

362 views
325 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

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

No notes for slide
  • 1
  • Intro to tsql unit 8

    1. 1. Introduction To SQL Unit 8 Modern Business Technology Introduction To TSQL Unit 8 Developed by Michael Hotek
    2. 2. Defaults <ul><li>A default can exist as a separate object </li></ul><ul><li>A default still follows the same rules as a default constraint, but gives the flexibility of reuse </li></ul><ul><li>create default default_name as </li></ul><ul><li>constant|expression </li></ul><ul><li>Once a default is created, it can be bound to multiple columns using sp_bindefault </li></ul>
    3. 3. Defaults <ul><li>create default D_city as 'Chicago' </li></ul><ul><li>exec sp_bindefault D_city,authors.city </li></ul><ul><li>exec sp_bindefault D_city,employee.city </li></ul><ul><li>To unbind a default, use sp_unbindefault </li></ul><ul><li>To drop a default, use drop default </li></ul><ul><li>A default must be unbound from all columns before it can be dropped </li></ul>
    4. 4. Rule <ul><li>A rule is the same as a check constraint, except it exists as an independent object </li></ul><ul><li>Just like a default object, this gives the flexibility to use the rule in many places </li></ul><ul><li>The name of the varibale used inside a rule is arbitrary and can not be directly accessed </li></ul><ul><li>create rule rule_name as </li></ul><ul><li>conditional_expression </li></ul><ul><li>create rule R_state as </li></ul><ul><li>@state in ('IL', 'MI', 'MA') </li></ul><ul><li>create rule R_salary as </li></ul><ul><li>@salary < 1000000 </li></ul>
    5. 5. Rule <ul><li>create rule R_state as @state in ('IL', 'MI', 'MA') </li></ul><ul><li>exec sp_bindrule R_state,authors.state </li></ul><ul><li>exec sp_bindrule R_state,employee.state </li></ul><ul><li>To unbind a rule, use sp_unbindrule </li></ul><ul><li>To drop a rule, use drop rule </li></ul><ul><li>A rule must be unbound from all columns before it can be dropped </li></ul><ul><li>Only one rule can be active on a column at a time </li></ul><ul><li>The last rule bound to a column is the one in effect </li></ul><ul><li>If the column also has a check constriant, then the only acceptable values are those that match both </li></ul>
    6. 6. User Datatypes <ul><li>The true power of rules, defaults, and user datatypes comes when they are combined. </li></ul><ul><li>A rule and a default can be bound directly to a user datatype. </li></ul><ul><li>Since the datatype can be used in many columns, this allows you to enforce a rule or default from a single location with a single command </li></ul><ul><li>Any changes to the rule or default are automatically reflected in all columns where that datatype is used </li></ul><ul><li>A rule or default bound directly to a column override the rule or default bound to a column </li></ul>
    7. 7. Precedence <ul><li>During an insert, a default is checked first and then any rule </li></ul><ul><li>During an update, the rule is checked first </li></ul><ul><li>A rule or default bound directly to a column overrides the rule or default bound to the user datatype </li></ul><ul><li>The last rule or default bound is the only one that is effective </li></ul><ul><li>If a rule is bound to a column that has a check constraint, the only allowed values are those that meet both the rule and constraint </li></ul>
    8. 8. Definitions <ul><li>Just like every other object in the database, information concerning a rule or default can be obtained from the system procedure sp_helptext </li></ul><ul><li>sp_helptext object_name </li></ul>
    9. 9. Unique Index <ul><li>Unique indexes are used to enforce uniqueness within a column or group of columns </li></ul><ul><li>A unique index is created in three cases: </li></ul><ul><ul><li>Primary key constraint is defined </li></ul></ul><ul><ul><li>Unique constraint is defined </li></ul></ul><ul><ul><li>A unique index is explicitly created </li></ul></ul><ul><li>When the index is created, the data is checked for uniqueness. If duplicates are present, the creation of the index fails </li></ul><ul><li>The index is then enforced on all subsequent inserts and updates </li></ul>
    10. 10. Implementing RI <ul><li>Which method to choose? </li></ul>
    11. 11. Unit 8 Review <ul><li>Defaults can be created as separate objects </li></ul><ul><li>sp_bindefault binds a default to a column or datatype </li></ul><ul><li>Rules can also be created as separate objects </li></ul><ul><li>sp_bindrule binds a rule to a column or datatype </li></ul><ul><li>Rules and defaults promote reusablity </li></ul><ul><li>They can be combined with user datatypes for increased power and flexibility </li></ul><ul><li>The last rule or default bound to a datatype takes effect </li></ul><ul><li>A rule or default bound directly to a column overrides a rule or default bound to the column's datatype </li></ul><ul><li>A unique index is used to enforce uniqueness within a column or group of columns </li></ul>
    12. 12. Unit 8 Exercises <ul><li>Time allotted for exercises is 30 minutes </li></ul>

    ×