1-Introduction To Data Integrity
1-Introduction To Data Integrity
Objectives:
Data Integrity.
Types of Data Integrity.
Integrity Constraint.
[:-1-:]
Data Integrity
• Need for updating data (data should be always
up-to-date).
[:-2-:]
Rules for Data Integrity
There are certain rules that help in Uniqueness
maintaining the accuracy and consistency Validity
Consistency
of data. These are:
• No two records in a table can have the
In addition to the general
exact same values in all columns. rules, maintaining
• Only valid data values can be inserted. business rules is
• The validity of data has to be maintained important.
Business rules are the
when data is modified. policies and standards
• When multiple tables are related through adhered to by an
a column, any changes to the data organization in running
its operation. For
values in that column in one table should example, the business
be appropriately reflected in the related rules of a bank can
table. specify the maximum
amount of loan.
[:-3-:]
Enforcing Data Integrity
SQL Server 2005 supports four mechanisms:
– Constraints: are properties that assigned to a
column in a table to prevent invalid data from being
entered into the column.
– Default Values: You can define default value for a
column which does not accept null value. This
default value will be inserted in records where this
column is left blank during insertion or modification.
– Rules: are constraints that can be applied in order to
control the data values being entered in a table.
Rules are independent of table definitions and can
be applied to multiple tables.
– Triggers: contains T-SQL statements that is
automatically executed when specified events occur.
[:-4-:]
Types of Data Integrity
• Entity Integrity
• Domain Integrity
• Referential Integrity
• User-defined Integrity
[:-5-:]
Entity Integrity
• A table in a database represents an entity whereas each record within
the table represents an instance of that entity. Table -> Entity
Record -> Instance of the entity
• A table is said to comply with entity integrity when no two rows in the
table have the exact same values in all the columns.
Entity integrity is ensured using:
– PRIMARY KEY constraint: does not allow duplicate or null values
to be inserted.
– UNIQUE constraint: does not allow duplicate but allows null values
to be inserted. However, this column allows a null value to be
inserted once.
– Indexes: prevents duplicate values from being entered in a
column.
– IDENTITY property: defines an identifier column that contains
system-generated sequential values for every record inserted.
[:-6-:]
Domain Integrity
• A domain defines a logical set of values that make up the valid values
in a column.
• Domain integrity is maintained using the following:
– FOREIGN KEY constraint: a FOREIGN KEY column can either
have a value that exists in the UNIQUE or PRIMARY KEY columns
of the referenced table or it can have a null value.
– CHECK constraint: specifies the range of valid data values that can
be entered into a column.
– DEFAULT definitions: specify default values for columns that do not
accept null values.
– NOT NULL definitions: specifies a column cannot accept NULL
values (unspecified or unknown).
– Data Types.
– Rules: specify the valid data formats or range for values in a
column.
[:-7-:]
Referential Integrity
• Referential integrity maintains consistency of data across tables that
are related through common columns.
• Referential integrity is implemented using the concept of FOREIGN
KEYS. FOREIGN KEYS columns reference UNIQUE or PRIMARY
KEY columns in other tables.
• Referential integrity is ensured by the following rules:
[:-9-:]
Integrity Constraints
• Constraints can be defined at two level: column and
table.
– A column-level constraint is a part of the column definition
and applies only on that particular column.
– A table-level constraint declaration is independent from a
column definition and can be simultaneously applied to
multiple columns in the the table. For example, when you
need to include multiple columns in the PRIMARY KEY, the
PRIMARY KEY constraint has to be defined at the table
level.
[:-10-:]
UNIQUE Constraint
• A UNIQUE constraint can be applied to a
column or a combination of columns to
ensure uniqueness of data values in these
columns.
• UNIQUE constraint can be defined on a
column or a combination of columns within
the CREATE TABLE or ALTER TABLE
statement.
[:-11-:]
CHECK Constraint
• CHECK constraint defines the range and
format for the values entered in a column.
• CHECK constraint can be specified on a
column within the CREATE TABLE or ALTER
TABLE statement.
[:-12-:]
PRIMARY KEY Constraint
• The purpose of a PRIMARY KEY column is to
uniquely identify each record within a table. A
PRIMARY KEY constraint checks for uniqueness of
data in the PRIMARY KEY column and disallows
duplicate values to be entered.
• A PRIMARY KEY can be specified either on a single
column or a combination of columns.
• A PRIMARY KEY constraint can be specified within
the CREATE TABLE or ALTER TABLE statement.
• A table can have only one PRIMARY KEY.
• A PRIMARY KEY column does not accept null values.
[:-13-:]
FOREIGN KEY Constraint
• A FOREIGN KEY constraint is used to create a link between the
data present in two tables. It helps to control the values of a
column to be subset of a pre-existing list of values. This pre-
existing list needs to exist as part of a PRIMARY KEY column or
a column defined by a UNIQUE constraint in another table.
• The column specified with the FOREIGN KEY constraint should
have reference to the PRIMARY KEY or the UNIQUE column.
• A FOREIGN KEY constraint can be specified within the
CREATE TABLE or ALTER TABLE statement.
• A table can have multiple FOREIGN KEY columns.
• The data type of the FOREIGN KEY column and of the referred
PRIMARY KEY or the UNIQUE column should be the same.
• FOREIGN KEY columns from multiple tables can reference the
same PRIMARY KEY column.
[:-14-:]
Cascading Options
[:-15-:]
Summary
• Data Integrity
– Data Integrity ensures accurate and up-to-date information at any
point in time.
– In SQL Server 2005, data integrity is enforced using Constraints,
Default Values, Rules and Triggers.
• Types of Data Integrity
– To maintain accuracy and consistency of data in a relational
database.
– Four types of integrity checks: Entity Integrity, Domain Integrity,
Referential Integrity, User-defined Integrity.
• Integrity Constraints
– To ensure validity and consistency of data in a database.
– SQL Server 2005 supports UNIQUE, CHECK, PRIMARY and
FOREIGN KEY constraints on columns in a table.
[:-16-:]