26
Integrity Constraint and Referential Integrity Made By: Suman Kumar 0802cs133D11 Guided By : Mr Dinesh Chandra Jain

Entigrity constraint

Embed Size (px)

Citation preview

Page 1: Entigrity constraint

Integrity Constraintand

Referential Integrity

Made By:

Suman Kumar 0802cs133D11

Guided By:Mr Dinesh Chandra Jain

Page 2: Entigrity constraint

OUT LINE

SQL Constraint Meaning SQL Constraint Meaning

Constraint Type Constraint Type

Syntax Of Create Constraint Syntax Of Create Constraint

Page 3: Entigrity constraint

Constraint

Meaning :-

Constraints can be specified when a table is created (with the

CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement)

Constraints are used to prohibit illegal work in to a

database

Page 4: Entigrity constraint

Constraint Implementation

Way :--

You can define constraints in two ways:

1. As part of the definition of an individual column or attribute. This

is called inline specification Or called column level definition

2. As part of the table definition. This is called out-of-

line specification Or called table level definition

Page 5: Entigrity constraint

Syntax For Create Constraint

Sql> CREATE TABLE <table name>( <attribute name> DATA TYPE (<size>) [constraint], <attribute name> DATA TYPE (<size>) [constraint], <attribute name> DATA TYPE (<size>) [constraint], ……………………. ……………………

);NOTE: [ ]->Optional,( )->Required, < >->Required & depend on programmer;

Page 6: Entigrity constraint

1 .Not Null

2 .Unique Key

3 .Primary Key

4 .Foreign Key

5 .Check Key

6 .Default Key

Constraint Type:-

Page 7: Entigrity constraint

Constraint Type:-

Not Null

constraint enforces a column to NOT accept NULL values.

This means that you cannot insert a new record, or update a record without adding a value to

this field.Expression of Not Null we use short cut NN

Page 8: Entigrity constraint

Constraint Type:-

1 .Not Null Example-:

Sql>CREATE TABLE customer ( c_code CHAR (3) PRIMERY KEY, c_name CHAR (20) NOT NULL,

c_city CHAR (10) DEFAULT “patna”,gender CHAR (6) CHECK (gender=“Male”

OR gender=“Female”), mob_no CHAR (14) UNIQUE, );

Page 9: Entigrity constraint

Constraint Implementation

Way :-- NOT NULL Constraints

& Default Constraint Must be declared inline.

Page 10: Entigrity constraint

Constraint Implementation

Way :--

All other constraints can be declared either inline or out of line.

Page 11: Entigrity constraint

Constraint Type:-

2 .Unique Key

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.

Page 12: Entigrity constraint

Constraint Type:-

Example 2. Unique Key

Sql>CREATE TABLE customer ( c_code CHAR (3) PRIMERY KEY, c_name CHAR (20) NOT NULL,

c_city CHAR (10) DEFAULT “patna”,gender CHAR (6) CHECK

(gender=“Male” OR gender=“Female”) mob_no CHAR (14) UNIQUE, );

Page 13: Entigrity constraint

Constraint Type:-

3 .Primary Key

Primary keys must contain unique values

A primary key column cannot contain NULL values.

Each table should have a primary key, and each table can

have only ONE primary key

This constraint defines a column or combination of columns which uniquely identifies each row in the

table.

Page 14: Entigrity constraint

Constraint Type:-

3 .Primary Key Example

Sql>CREATE TABLE product ( p_code CHAR (3) PRIMERY KEY, p_name CHAR (20) NOT NULL, p_rate NUMBER (5,2) CHECK (c_rate>0), qoh NUMBER(2),);

Page 15: Entigrity constraint

Unique Key Unique Key

1.Unique key use many times in a table

2Unique key accept only one null value

Primary Key

1.Primary key use only one times in a table

2 Primary key does not accept null value

Unique Key Vs Primary Key

Page 16: Entigrity constraint

Constraint Type:-

Unique Key

4 .Foreign Key or Referential Integrity

A FOREIGN KEY in one table points to a PRIMARY KEY in another tableThe FOREIGN KEY constraint is

used to prevent actions that would destroy links between

tables.

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.

Page 17: Entigrity constraint

Constraint Type:-

Unique Key

4 .Foreign Key Example

Sql>CREATE TABLE sales ( c_code CHAR(3) REFERENCES

customer (c_code), p_code CHAR(3) REFERENCES product (p_code),qty NUMBER (4) CHECK (qty>=0),

);

Page 18: Entigrity constraint

Constraint Type:-

Unique Key

Structure of Customer table

The “Costomer" table:

Column name Data type Size Constraint

C_code Char/text 3 PRIMARY KEY

C_name Char/text 20 NOT NULL

C_city Char/text 20 DEFAULT(Patna)

gender Char/text 6 CHECK(male,female)

Mob_no Char/text 14 UNIQUE,NOT NULL

Page 19: Entigrity constraint

Constraint Type:-

Unique Key

Structure of Product table

The “Product" table:

Column name Data type Size Constraint

p_code Char/text 3 PRIMARY KEY

p_name Char/text 20 NOT NULL

P_rate Number 5,2 CHECK(>0),NOT NULL

Qoh Number 5 CHECK(>=0),NOT NULL

Page 20: Entigrity constraint

Constraint Type:-

Unique Key

Structure of Sales table

The “Sales" table:

Column name

Data type Size Constraint

c_code Char/text 3 FOREIGN KEY,(RFFERENCES CUSTOMER (c_code))

p_code Char/text 3 FOREIGN KEY,(RFFERENCES PRODUCT (p_code))

qty Number 5 CHECK(>0),NOT NULL

Page 21: Entigrity constraint

Constraint Type:-

5 .Check Key

This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be applied for a single column or a group of columns.

Page 22: Entigrity constraint

Constraint Type:-

5 .Check Key Example

Sql>CREATE TABLE product ( p_code CHAR(3) PRIMERY KEY, p_name CHAR(20) NOT NULL, p_rate NUMBER(5,2) CHECK (c_rate>0), qoh NUMBER(2),);

Page 23: Entigrity constraint

Constraint Type:-

5 .Check Key 6 .Default Key

The DEFAULT constraint is used to insert a default value into a column The default value will be

added to all new records if no other value is specified.

Page 24: Entigrity constraint

Constraint Type:-

5 .Check Key 6 .Default Key Example

Sql>CREATE TABLE customer ( c_code CHAR (3) PRIMERY KEY, c_name CHAR (20) NOT NULL,

c_city CHAR (10) DEFAULT “patna”,gender CHAR (6) CHECK

(gender=“Male” OR gender=“Female”) mob_no CHAR (14) UNIQUE, );

Page 25: Entigrity constraint

Composite Key : A composite key is a combination of more than one column to identify a unique row in a table. 

Candidate Key: All keys in a table that become unique called as candidate key. Ex-email id,mob no

Alternate Key: Among of candidate keys if any single key or combination of keys made as primary key then rest candidate key called as alternate key.  

Some Other Keys

Page 26: Entigrity constraint

Thank you for listen