Sql server ___________session_15(data integrity)

Preview:

Citation preview

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_idvalue 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.

CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) );

or

CREATE TABLE 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)

);

ALTER TABLE employeeADD 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

CREATE TABLE employee( id number(5) PRIMARY KEY,name char(20),dept char(10),age number(2),salary number(10),location char(10) UNIQUE );

or

CREATE TABLE 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)

CREATE TABLE 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) );

ALTER TABLE employeeADD 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".

CREATE TABLE product ( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY, product_name char(20),supplier_name char(20),unit_price number(10));

CREATE TABLE order_items( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY,pid number(5) CONSTRAINT pd_id_fkREFERENCES,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);

CREATE TABLE 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,

CREATE TABLE 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.

CREATE TABLE Employees_2 (

EmployeeID int PRIMARY KEY,FirstName varchar(50) NULL,LastName varchar(50) NOT NULL,

)

CREATE TABLE 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.

CREATE TABLE 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: 1EmployeeID : 1 OrderDate : 2003-01-02Freight : NULLShipAddress : NO SHIPPING ADDRESS

alter table table_nameadd 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)

CREATE TABLE Products_2(

ProductID int PRIMARY KEY,UnitPrice money CHECK(UnitPrice > 0 AND UnitPrice < 100)

)

OR

CREATE TABLE 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 ALTER TABLE syntax.

CREATE TABLE Employees_2(

EmployeeID int,HireDate datetime

)

ALTER TABLE Employees_2ADD CONSTRAINT CK_HireDate CHECK(hiredate < GETDATE())

Dropping constraints:

ALTER TABLE Table_nameDROP CONSTRAINT name_of_constraint

Recommended