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