Data Integrity
 Data integrity refers to the accuracy,
consistency, and reliability of data that is
stored in the database.
 Enforcing data integrity ensures the quality of
the data in the data
Why Data Integrity??
 if an employee is entered with an employee_id
value of 123, the database should not allow
another employee to have an ID with the same
value. If you have an employee_rating column
intended to have values ranging from 1 to 5, the
database should not accept a value of 6. If the
table has a dept_id column that stores the
department number for the employee, the
database should allow only values that are valid
for the department numbers in the company.
 Data integrity falls into these categories:
 Entity integrity
 Domain integrity
 Referential integrity
1.Entity integrity
 Entity integrity refers to the requirement that
all rows in a table must have a unique
identifier that can be used to tell apart each
record.
 This unique identifier is normally known as
Primary Key of the table.
 A Primary Key can be formed by a single
column or a combination of multiple
columns.
2.Domain Integrity
 Domain integrity is the validity of entries for a
given column.
 It refers to the requirement that data stored
in a column must adhere to the same format
and definition.
 This includes data type, data length, default
value of data, range of possible values,
whether duplicate values are allowed, or
whether null values are allowed.
3. Referential Integrity
 Referential integrity is based on relationships
between foreign keys and primary keys.
 Referential integrity ensures that key values
are consistent across tables. Such consistency
requires that there be no references to
nonexistent values and that if a key value
changes, all references to it change
consistently throughout the database.
After enforcing referential
integrity
 Users cannot Add records to a related table if
there is no associated record in the primary table.
 User cannot Change values in a primary table
that result in orphaned records in a related table.
 Users cannot Delete records from a primary table
if there are matching related records.
Importance of integrity
 Maintaining integrity is of itmost importance
for a database, so much so that we cannot
trust users and applications to enforce these
rules by themselves. Once integrity is lost,
you may find customers are double billed,
payments to the supplier are missing, and
everyone loses faith in your application.
Database Constraints
 The primary job of a constraint is to enforce a
rule in the database.
 The constraints in a database maintain the
integrity of the database.
 Entity integrity is enforced by
 Primary key constraint
 Unique constraint
 Domain integrity is enforced by
 Foreign key constraint
 Check constraint
 Default constraint
 Data type constraint
 Nullability constraint
 Referential integrity is enforced by
 Foreign key constraint
 User-defined integrity
 Check constraint
 Constraints can be defined in two ways
 The constraints can be specified immediately after
the column definition.This is called column-level
definition.
 The constraints can be specified after all the
columns are defined.This is called table-level
definition.
1.Primary key constraints:
 This constraint defines a column or
combination of columns which uniquely
identifies each row in the table.
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
or
CREATETABLE employee
( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
Syntax to define a Primary key at column level:
column name datatype [CONSTRAINT constraint_name] PRIMARY KEY
Syntax to define a Primary key at table level:
[CONSTRAINT constraint_name] PRIMARY KEY(column_name)
CREATE TABLE employee
( id number(5),
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT emp_id_pk PRIMARY KEY (id)
);
ALTERTABLE employee
ADD CONSTRAINT emp_id_pk PRIMARY KEY (id);
2.UNIQUE constraint
 This constraint ensures that a column or a group
of columns in each row have a distinct value. A
column(s) can have a null value but the values
cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) UNIQUE
);
or
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) CONSTRAINT loc_un UNIQUE
);
Syntax to define a Unique key at table level:
[CONSTRAINT constraint_name] UNIQUE(column_name)
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT loc_un UNIQUE(location)
);
ALTERTABLE employee
ADD CONSTRAINT loc_un UNIQUE (location);
3. Foreign key constraint:
 This constraint identifies any column referencing the
PRIMARY KEY in another table.
 It establishes a relationship between two columns in the
same table or between different tables.
 For a column to be defined as a Foreign Key, it should be
a defined as a Primary Key in the table which it is
referring. One or more columns can be defined as
Foreign key.
Syntax to define a Foreign key at column level:
[CONSTRAINT constraint_name] REFERENCES
Referenced_Table_name(column_name)
Lets use the "product" table and "order_items".
CREATETABLE product
( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY,
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
CREATETABLE order_items
( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY,
pid number(5) CONSTRAINT pd_id_fk
REFERENCES,product(product_id),
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
Syntax to define a Foreign key at table level:
[CONSTRAINT constraint_name] FOREIGN KEY(column_name)
REFERENCES referenced_table_name(column_name);
CREATETABLE order_items
( order_id number(5) ,
p_id number(5),
product_name char(20),
supplier_name char(20),
unit_price number(10)
CONSTRAINT od_id_pk PRIMARY KEY(order_id),
CONSTRAINT pd_id_fk FOREIGN KEY(p_id)
REFERENCES product(product_id)
);
If the employee table has a 'mgr_id' i.e, manager id as a
foreign key which references primary key 'id' within the
same table, the query would be like,
CREATETABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
mgr_id number(5) REFERENCES employee(id),
salary number(10),
location char(10)
);
4.Null constraint:
 This defines that if a column is NOT NULL or
allow NULL values to be stored in it.
 The decision to allow NULL values in a
column or not is a type of rule enforcement
for domain integrity.
CREATETABLE Employees_2
(
EmployeeID int PRIMARY KEY,
FirstName varchar(50) NULL,
LastName varchar(50) NOT NULL,
)
CREATETABLE employee
(
id number(5),
name char(20) CONSTRAINT nm_nn NOT NULL,
dept char(10),
age number(2),
salary number(10),
location char(10)
);
5.Default constraint:
 This defines what value the column should use when no
value has been supplied explicitly when inserting a record
in the table.
 A default can assign a constant value, the value of a
system function, or NULL to a column.
 You can use a default on any column except IDENTITY
columns.
CREATETABLE Orders_2
(
OrderID int ,
EmployeeID int ,
OrderDate datetime NULL DEFAULT(GETDATE()),
Freight money NULL DEFAULT (0),
ShipAddress nvarchar (60) NULL DEFAULT('NO SHIPPING ')
);
INSERT INTO Orders_2 (OrderId ,EmployeeID, Freight)VALUES(1,1, NULL)
OrderID: 1
EmployeeID : 1
OrderDate : 2003-01-02
Freight : NULL
ShipAddress : NO SHIPPING ADDRESS
alter table table_name
add constraint name _of _constraint
default value for column_name;
6. Check constraint
 Check constraints contain an expression the
database will evaluate when you modify or
insert a row. If the expression evaluates to
false, the database will not save the row.
 You can use many of the same operators (>, <,
<=, >=, <>, =) in additional to BETWEEN, IN,
LIKE, and NULL.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name]CHECK (condition)
CREATETABLE Products_2
(
ProductID int PRIMARY KEY,
UnitPrice money CHECK(UnitPrice > 0 AND UnitPrice < 100)
)
OR
CREATETABLE Products_2
(
ProductID int PRIMARY KEY,
UnitPrice money,
CONSTRAINT CK_UnitPrice2 CHECK(UnitPrice > 0 AND UnitPrice < 100)
)
You can also add check constraints to a table after a table exists using the
ALTERTABLE syntax.
CREATETABLE Employees_2
(
EmployeeID int,
HireDate datetime
)
ALTERTABLE Employees_2
ADD CONSTRAINT CK_HireDate CHECK(hiredate < GETDATE())
Dropping constraints:
ALTERTABLE Table_name
DROP CONSTRAINT name_of_constraint

Sql server ___________session_15(data integrity)

  • 1.
  • 2.
     Data integrityrefers to the accuracy, consistency, and reliability of data that is stored in the database.  Enforcing data integrity ensures the quality of the data in the data
  • 3.
    Why Data Integrity?? if an employee is entered with an employee_id value of 123, the database should not allow another employee to have an ID with the same value. If you have an employee_rating column intended to have values ranging from 1 to 5, the database should not accept a value of 6. If the table has a dept_id column that stores the department number for the employee, the database should allow only values that are valid for the department numbers in the company.
  • 4.
     Data integrityfalls into these categories:  Entity integrity  Domain integrity  Referential integrity
  • 5.
    1.Entity integrity  Entityintegrity refers to the requirement that all rows in a table must have a unique identifier that can be used to tell apart each record.  This unique identifier is normally known as Primary Key of the table.  A Primary Key can be formed by a single column or a combination of multiple columns.
  • 6.
    2.Domain Integrity  Domainintegrity is the validity of entries for a given column.  It refers to the requirement that data stored in a column must adhere to the same format and definition.  This includes data type, data length, default value of data, range of possible values, whether duplicate values are allowed, or whether null values are allowed.
  • 7.
    3. Referential Integrity Referential integrity is based on relationships between foreign keys and primary keys.  Referential integrity ensures that key values are consistent across tables. Such consistency requires that there be no references to nonexistent values and that if a key value changes, all references to it change consistently throughout the database.
  • 8.
    After enforcing referential integrity Users cannot Add records to a related table if there is no associated record in the primary table.  User cannot Change values in a primary table that result in orphaned records in a related table.  Users cannot Delete records from a primary table if there are matching related records.
  • 9.
    Importance of integrity Maintaining integrity is of itmost importance for a database, so much so that we cannot trust users and applications to enforce these rules by themselves. Once integrity is lost, you may find customers are double billed, payments to the supplier are missing, and everyone loses faith in your application.
  • 10.
  • 11.
     The primaryjob of a constraint is to enforce a rule in the database.  The constraints in a database maintain the integrity of the database.
  • 12.
     Entity integrityis enforced by  Primary key constraint  Unique constraint  Domain integrity is enforced by  Foreign key constraint  Check constraint  Default constraint  Data type constraint  Nullability constraint  Referential integrity is enforced by  Foreign key constraint  User-defined integrity  Check constraint
  • 13.
     Constraints canbe defined in two ways  The constraints can be specified immediately after the column definition.This is called column-level definition.  The constraints can be specified after all the columns are defined.This is called table-level definition.
  • 14.
    1.Primary key constraints: This constraint defines a column or combination of columns which uniquely identifies each row in the table.
  • 15.
    CREATETABLE employee ( idnumber(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) ); or CREATETABLE employee ( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) ); Syntax to define a Primary key at column level: column name datatype [CONSTRAINT constraint_name] PRIMARY KEY
  • 16.
    Syntax to definea Primary key at table level: [CONSTRAINT constraint_name] PRIMARY KEY(column_name) CREATE TABLE employee ( id number(5), name char(20), dept char(10), age number(2), salary number(10), location char(10), CONSTRAINT emp_id_pk PRIMARY KEY (id) ); ALTERTABLE employee ADD CONSTRAINT emp_id_pk PRIMARY KEY (id);
  • 17.
    2.UNIQUE constraint  Thisconstraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated.
  • 18.
    Syntax to definea Unique key at column level: [CONSTRAINT constraint_name] UNIQUE CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) UNIQUE ); or CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) CONSTRAINT loc_un UNIQUE );
  • 19.
    Syntax to definea Unique key at table level: [CONSTRAINT constraint_name] UNIQUE(column_name) CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10), CONSTRAINT loc_un UNIQUE(location) ); ALTERTABLE employee ADD CONSTRAINT loc_un UNIQUE (location);
  • 20.
    3. Foreign keyconstraint:  This constraint identifies any column referencing the PRIMARY KEY in another table.  It establishes a relationship between two columns in the same table or between different tables.  For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key.
  • 21.
    Syntax to definea Foreign key at column level: [CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name) Lets use the "product" table and "order_items". CREATETABLE product ( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY, product_name char(20), supplier_name char(20), unit_price number(10) ); CREATETABLE order_items ( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY, pid number(5) CONSTRAINT pd_id_fk REFERENCES,product(product_id), product_name char(20), supplier_name char(20), unit_price number(10) );
  • 22.
    Syntax to definea Foreign key at table level: [CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES referenced_table_name(column_name); CREATETABLE order_items ( order_id number(5) , p_id number(5), product_name char(20), supplier_name char(20), unit_price number(10) CONSTRAINT od_id_pk PRIMARY KEY(order_id), CONSTRAINT pd_id_fk FOREIGN KEY(p_id) REFERENCES product(product_id) );
  • 23.
    If the employeetable has a 'mgr_id' i.e, manager id as a foreign key which references primary key 'id' within the same table, the query would be like, CREATETABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), mgr_id number(5) REFERENCES employee(id), salary number(10), location char(10) );
  • 24.
    4.Null constraint:  Thisdefines that if a column is NOT NULL or allow NULL values to be stored in it.  The decision to allow NULL values in a column or not is a type of rule enforcement for domain integrity. CREATETABLE Employees_2 ( EmployeeID int PRIMARY KEY, FirstName varchar(50) NULL, LastName varchar(50) NOT NULL, )
  • 25.
    CREATETABLE employee ( id number(5), namechar(20) CONSTRAINT nm_nn NOT NULL, dept char(10), age number(2), salary number(10), location char(10) );
  • 26.
    5.Default constraint:  Thisdefines what value the column should use when no value has been supplied explicitly when inserting a record in the table.  A default can assign a constant value, the value of a system function, or NULL to a column.  You can use a default on any column except IDENTITY columns.
  • 27.
    CREATETABLE Orders_2 ( OrderID int, EmployeeID int , OrderDate datetime NULL DEFAULT(GETDATE()), Freight money NULL DEFAULT (0), ShipAddress nvarchar (60) NULL DEFAULT('NO SHIPPING ') ); INSERT INTO Orders_2 (OrderId ,EmployeeID, Freight)VALUES(1,1, NULL) OrderID: 1 EmployeeID : 1 OrderDate : 2003-01-02 Freight : NULL ShipAddress : NO SHIPPING ADDRESS
  • 28.
    alter table table_name addconstraint name _of _constraint default value for column_name;
  • 29.
    6. Check constraint Check constraints contain an expression the database will evaluate when you modify or insert a row. If the expression evaluates to false, the database will not save the row.  You can use many of the same operators (>, <, <=, >=, <>, =) in additional to BETWEEN, IN, LIKE, and NULL.
  • 30.
    Syntax to definea Check constraint: [CONSTRAINT constraint_name]CHECK (condition) CREATETABLE Products_2 ( ProductID int PRIMARY KEY, UnitPrice money CHECK(UnitPrice > 0 AND UnitPrice < 100) ) OR CREATETABLE Products_2 ( ProductID int PRIMARY KEY, UnitPrice money, CONSTRAINT CK_UnitPrice2 CHECK(UnitPrice > 0 AND UnitPrice < 100) )
  • 31.
    You can alsoadd check constraints to a table after a table exists using the ALTERTABLE syntax. CREATETABLE Employees_2 ( EmployeeID int, HireDate datetime ) ALTERTABLE Employees_2 ADD CONSTRAINT CK_HireDate CHECK(hiredate < GETDATE())
  • 32.