0% found this document useful (0 votes)
2 views

SQL_6

The document outlines the implementation of data integrity in MS SQL Server, focusing on various constraints such as primary keys, unique keys, foreign keys, check constraints, and default constraints. It explains the importance of data integrity, types of integrity, and provides guidance on creating rules and defaults. The content serves as a comprehensive guide for ensuring correct and reliable data in a database.

Uploaded by

nidhit102646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL_6

The document outlines the implementation of data integrity in MS SQL Server, focusing on various constraints such as primary keys, unique keys, foreign keys, check constraints, and default constraints. It explains the importance of data integrity, types of integrity, and provides guidance on creating rules and defaults. The content serves as a comprehensive guide for ensuring correct and reliable data in a database.

Uploaded by

nidhit102646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

MS SQL SERVER

SESSION # : 6
IMPLEMENTING
DATA INTEGRITY
Focus Points :

Add Primary key constraints to tables.


Add unique constraints to tables.
Add foreign key constraints to tables.
Add check constraints to tables.
Add default constraints to tables.
Create rules.
Create defaults.
Contents

 Data Integrity and types.


 Primark Key Constraint.
 Unique Key Constraint.
 Foreign Key Constraint.
 Check Constraint.
 Default Constraint.
 Creating A rule.
 Creating A Default
Data Integrity and Types

 When you examine data integrity, you are trying to ensure


that the data in your database is correct—both from a literal
standpoint (without errors) and from a business
standpoint.
 The mechanisms to enforce integrity are declared as part of
the definition of the objects (tables) in your database.
 They become an integral part of these objects.
 There are three types of integrity: domain, referential, and
entity.
Data Integrity and Types

 Domain Integrity
 Data types help determine what values are valid for a particular column.
This is known as domain integrity. The domain is simply the set of valid
values for a particular column. Nullability is another option to determine
which values are valid in the domain; in this case, whether the unknown
value (null) is valid.
 Entity Integrity
 This means that you can uniquely identify every row in a table.
 You can do this with primary-key or unique constraints.
 Referential Integrity
 Referential integrity refers to the maintenance of relationships between
data rows in multiple tables.i.,e Foreign key of one table matches the
values in the Primary key of one table
Primary Key

 SQL PRIMARY KEY CONSTRAINT is a combination of a


NOT NULL constraint and a UNIQUE constraint.
 This constraint ensures that the specific column or
combination of two or more columns for a table have an
unique identity which helps to find a particular record in
a table more easily and quickly.

Add constraint [name] Primary key (col1,col2,..)


OR
Columnname type size [constraint] [name] Primary key
Unique Key

 Unique constraints enable you to create unique indexes,


just as primary keys can, but with a bit more flexibility.
 Unique constraints can also be created on columns that
allow nulls.
 You can also have more than one unique constraint on a
table.

Add constraint [name] unique (col1) for columnname


OR
Columnname type size [constraint] [name] unique
Foreign Key

 Foreign-key constraints protect referential integrity between tables.


You create a foreign key on a table, which references another table's
primary-key or unique constraint.
 This restricts data modifications against the table with the primary
key as long as there are related rows in the tables with the foreign
keys.
 It also prevents data from being added (or updated) on the table
with the foreign-key constraint that would not contain valid data
from the referenced tables

Add constraint [name] foreign key (col1) references tablename


(colname)
OR
Columnname type size [constraint] [name] foreign key references
tablename (columnname)
Check Key

 Check constraints function very much like rules. They provide a


mechanism to enforce domain integrity for your columns.
 You can have as many check constraints as you want on a single
column.
 They are checked during inserts and updates, just as rules are.
 However, check constraints can do something that rules cannot. Check
constraints can refer to other columns as part of their enforcement of
conditions. You can do this only with table-level constraints, however.

Add constraint [name] check (col1) expression for columnname


OR
Columnname type size [constraint] [name] check expression
OR
[CONSTRAINT constraint_name] CHECK (expression)
Default Key

 A Default constraint can be used to assign a constant value


to a column, and the user need not insert value for such a
column.
 Only one default is allowed for a column

create default defaultname


as
expression

Sp_bindefault

sp_bindefault ‘defaultname ‘, ‘target ‘, [futureonly]


Default Key

Sp_bindefault

sp_bindefault ‘defaultname ‘, ‘target ‘, [futureonly]

 defaultname is the rule you'd like to bind to.


 target is, as before, either a tablename.columnname combination or a user-defined data
type.
 futureonly has the same meaning as before. The rule is not applied to existing columns
declared with the user-defined data type you're binding the rule to. As before, I don't
suggest you use the futureonly option because administration can become very confusing.
Everything must be in quotes.
Create Rules

 Rules further enforce domain integrity by providing more sophisticated


checking of valid values. Rules are used to ensure that values
 Match a pattern (much like a like clause)
 Match a list of values (much like an in clause)
 Fall within a range of values (much like a between clause)
 Rules are created using the CREATE RULE statement.

CREATE RULE rule_name AS condition_expression


Create Rules

sp_bindrule ‘rulename’, ‘objname’,[ futureonly]

 rulename is the rule you'd like to bind to.


 objname is, as before, either a tablename.columnname combination or
a user-defined data type.
 futureonly has the same meaning as before. The rule is not applied to
existing columns declared with the user-defined data type you're
binding the rule to. As before, I don't suggest you use
the futureonly option because administration can become very
confusing. Everything must be in quotes.
Create A Default

 Defaults are used to specify a value to add to a column when


you don't want to directly insert a value into that column.
 There are two kinds of defaults in SQL server: constraint
defaults and standalone objects called defaults.

CREATE DEFAULT default_name


AS constant_expression
Create A Default

sp_bindefault defname, objname [, futureonly]


where
 defname is the name of the default you have already created.
 objname is the name of the object to which you want to bind your
default and must be in quotes. You can bind to either a column or a
user-defined data type. SQL Server knows you are binding to a column
if your quoted text is in the formattablename.columnname .
 futureonly applies only to user-defined data types. If specified, the
default does not apply anywhere that the user-defined data type has
already been used. Each time you use the user-defined data type in the
future, the default will be bound.
Examples
Summary

 Data Integrity and types.


 Primark Key.
 Unique Key.
 Foreign Key.
 Check Constraint.
 Default Constraint.
 Creating A rule.
 Creating A Default.

You might also like