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

Constraints

The document discusses different types of constraints in SQL including primary keys, unique keys, not null, check, and foreign keys. It provides examples of creating a table with different constraints on columns and examples of inserting data that violates these constraints. The key points covered are: - There are 5 main types of constraints: primary key, unique key, not null, check, and foreign key. - Primary keys enforce uniqueness and do not allow nulls. Unique keys are also unique but can allow nulls. - Not null constraints do not allow null values. Check constraints ensure values meet certain conditions. - Examples are provided of creating a table with different constraints and inserting data that violates the constraints.

Uploaded by

Senthil Raj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Constraints

The document discusses different types of constraints in SQL including primary keys, unique keys, not null, check, and foreign keys. It provides examples of creating a table with different constraints on columns and examples of inserting data that violates these constraints. The key points covered are: - There are 5 main types of constraints: primary key, unique key, not null, check, and foreign key. - Primary keys enforce uniqueness and do not allow nulls. Unique keys are also unique but can allow nulls. - Not null constraints do not allow null values. Check constraints ensure values meet certain conditions. - Examples are provided of creating a table with different constraints and inserting data that violates the constraints.

Uploaded by

Senthil Raj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CONSTRAINTS

IT ENFORCE RULE ON A TABLE PRIMARY KEY UNIQUE KEY NOT NULL CHECK FOREGIN KEY

Document prepared by: AN.Murugappan

CONSTRAINT IT ENFORCE A RULE ON A TABLE TYPES OF CONSTRAINT: PRIMARY KEY UNIQUE KEY NOT NULL CHECK FOREGIN KEY PRIMARY KEY: IT IS A UNIQUE IDENTIFIER AND IT WILL NOT ACCEPT NULL VALUE ONLY ONE PRIMARY KEY IS ALLOWED IN THE TABLE IT AUTOMATICALLY CREATES A UNIQUE INDEX IT IS A ENTITY INTAGRITY UNIQUE KEY: IT IS A UNIQUE IDENTIFIER AND IT WILL ACCEPT NULL VALUE. IT AUTOMATICALLY CREATES A UNIQUE INDEX. IT IS A ENTITY INTAGRITY. NOT NULL: IT WILL NOT ACCEPT NULL VALUE. IT IS A DOMAIN INTAGRITY. CHECK: IT SHOULD SATISFY THE CONDITION. IT IS A DOMAIN INTAGRITY. FOREGIN KEY: IT IS THE REFERIANTIAL INTAGRITY IT REFERS A PRIMARY KER OR UNIQUE CONSTRAINT OF THE ANOTHER TABLE ENTITY INTAGRITY DOMAIN INTAGRITY REFERENTIAL INTAGRITY

IT WILL ACCEPT NULL VALUE AND DUPLICATES

MORE THEN ONE CONSTRAINT CAN BE APPLIED TO THE COLUMN.

HOEW TO CREATE CONSTRAINTS:

CREATE TABLE INFO ( ID NUMBER(3), NAME VARCHAR(20) NOT NULL, EMAIL VARCHAR(30), MOBILE NUMBER(10), GENDER CHAR(1), CONSTRAINT PK_ID_INFO PRIMARY KEY(ID), CONSTRAINT UK_EMAIL_INFO UNIQUE(EMAIL), CONSTRAINT CHK_MOBILE_INFO CHECK(MOBILE LIKE '__________'), CONSTRAINT CHK_GENDER_INFO CHECK(GENDER IN('M','F')) );

INSERT INTO INFO VALUES (100,'MURUGAPPAN','[email protected]',9843688568,'M');

INSERT INTO INFO VALUES(100,'MURUGAPPAN','[email protected]',9843688568,'M');

[ ALREADY ID 100 IS INSERTED ]

INSERT INTO INFO VALUES(101,NULL,'[email protected]',9843688568,'M');

[THE COLUMN NAME IS SET AS A NOT NULL CONSTRAINT SO IT WILL NOT ACCEPT NULL VALUE] NULL, NULL VALUE NULL CHARACTER

INSERT INTO INFO VALUES(101,'JOY','[email protected]',9843688568,'M');

[MAIL IS SET AS A UNIQUE CONSTRAINT SO IT WILL NOT ACCEPT DUPLICATE VALUE]

INSERT INTO INFO VALUES(101,'JOY','[email protected]',9843688568,'M');

INSERT INTO INFO VALUES(103,'RAJESH','[email protected]',953672812,'M');

[ MOBILE NO MUST CONTAIN 0NLY NINE CHARACTER ]

INSERT INTO INFO VALUES(103,'RAJESH','[email protected]',9536728120,'M');

INSERT INTO INFO VALUES(104,'karthi','[email protected]',9444190830,'S');

[ GENDE ONLY ACCEPT M OF F BUT HERE S IAS GIVE AS A INPUT ]

INSERT INTO INFO VALUES(104,'karthi','[email protected]',9444190830,'M');

SELECT * FROM INFO;

TO VIEW THE CREATED CONSTRAINTS SELECT * FROM USER_CONSTRAINTS;

SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME='INFO';

MORE THEN ONE CONSTRAINT CAN BE APPLIED TO THE COLUMN

ONLY ONE PRIMARY KEY IS ALLOWED IN THE TABLE. IF YOU WANT TO CREATE MORE THAN ONE PRIMERY KEY SET UNIQUE + NOT NULL.

IF YOU DROP THE TABLE CONSTRAINT AUTOMATICALLY DROPED.

You might also like