Constraint

Embed Size (px)

DESCRIPTION

consr

Citation preview

SQL PRIMARY KEY ConstraintThe PRIMARY KEY constraint uniquely identifies each record in a database table.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.SQL PRIMARY KEY Constraint on CREATE TABLEThe following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons" table is created:MySQL:CREATE TABLE Persons(P_Id int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255),PRIMARY KEY (P_Id))SQL Server / Oracle / MS Access:CREATE TABLE Persons(P_Id int NOT NULL PRIMARY KEY,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255))To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:MySQL / SQL Server / Oracle / MS Access:CREATE TABLE Persons(P_Id int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255),CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName))Note: In the example above there is only ONE PRIMARY KEY (pk_PersonID). However, the value of the pk_PersonID is made up of two columns (P_Id and LastName).SQL PRIMARY KEY Constraint on ALTER TABLETo create a PRIMARY KEY constraint on the "P_Id" column when the table is already created, use the following SQL:MySQL / SQL Server / Oracle / MS Access:ALTER TABLE PersonsADD PRIMARY KEY (P_Id)To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:MySQL / SQL Server / Oracle / MS Access:ALTER TABLE PersonsADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)Note: If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created).To DROP a PRIMARY KEY ConstraintTo drop a PRIMARY KEY constraint, use the following SQL:MySQL:ALTER TABLE PersonsDROP PRIMARY KEYSQL Server / Oracle / MS Access:ALTER TABLE PersonsDROP CONSTRAINT pk_PersonID===================================================================================================foriegn key:CREATE TABLE Orders(O_Id int NOT NULL,OrderNo int NOT NULL,P_Id int,PRIMARY KEY (O_Id),FOREIGN KEY (P_Id) REFERENCES Persons(P_Id))SQL Server / Oracle / MS Access:CREATE TABLE Orders(O_Id int NOT NULL PRIMARY KEY,OrderNo int NOT NULL,P_Id int FOREIGN KEY REFERENCES Persons(P_Id))To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:MySQL / SQL Server / Oracle / MS Access:CREATE TABLE Orders(O_Id int NOT NULL,OrderNo int NOT NULL,P_Id int,PRIMARY KEY (O_Id),CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)REFERENCES Persons(P_Id))SQL FOREIGN KEY Constraint on ALTER TABLETo create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is already created, use the following SQL:MySQL / SQL Server / Oracle / MS Access:ALTER TABLE OrdersADD FOREIGN KEY (P_Id)REFERENCES Persons(P_Id)To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:MySQL / SQL Server / Oracle / MS Access:ALTER TABLE OrdersADD CONSTRAINT fk_PerOrdersFOREIGN KEY (P_Id)REFERENCES Persons(P_Id)To DROP a FOREIGN KEY ConstraintTo drop a FOREIGN KEY constraint, use the following SQL:MySQL:ALTER TABLE OrdersDROP FOREIGN KEY fk_PerOrdersSQL Server / Oracle / MS Access:ALTER TABLE OrdersDROP CONSTRAINT fk_PerOrders===========================================================================================Constraints are used to limit the type of data that can go into a table. two basic types of constraints are there 1.column level2.table levelthe difference is that column level constraints are applyonly to one column where as table level constraints areapply to whole table Constraints are declare at the time of table creation usingCREATE TABLE command there are 6 constraints used in oracle1.NOT NULL2.UNIQUE3.PRIMARY KEY4.CHECK5.DEFAULT6.REFERENCES=========================================================================================A Constraint is a property that we can assign to column in table. By assigning constraint property on column we can prevent the users to enter inconsistent of data in columns. We can assign these Constraint properties during the time of creation of table (By Using CREATE TABLE statement) or during the time of changing the existing table structure (By Using ALTER TABLE statement).In SQL we have different types of constraints are available those are1. Primary Key Constraint2. Unique Key Constraint3. Foreign Key Constraint4. Not Null Constraint5. Check ConstraintNow I will explain about each constraint clearlySQL Primary Key Constraint:Primary key constraint is used to uniquely identify each record in database table. It wont allow repetition or duplication of data. Each table is having only one primary key constraint and it contains only unique values. Primary key constraint doesnt accept null values.Example of creating Primary Key constraint during the time of CREATE TABLECreate Table SampleUserDetail(UserID integer PRIMARY KEY,UserName varchar(50),FirstName varchar(50),LastName varchar(50))Example of creating Primary Key constraint during the time of ALTER TABLEALTER TABLE SampleUserDetail ADD PRIMARY KEY (UserID)To Drop Primary Key constraint on table use the below statementALTER TABLE SampleUserDetail DROP Constraint UserIDSQL Unique Key ConstraintUnique key constraint is same as Primary key Constraint it doesnt allow duplication or repetition of data in column and we can uniquely identify records in table. The main difference is Primary Key constraint wont allow null values but unique key constraint allows null values. We have a chance to define only one primary key on table but we can define many unique key constraints on table.Example of creating Unique Key constraint during the time of table creationCreate Table SampleUserDetail(UserID integer ,UserName varchar(50),FirstName varchar(50),LastName varchar(50)CONSTRAINT us_UserId UNIQUE (UserID))Example of creating Unique Key constraint during the time of ALTER TABLEALTER TABLE SampleUserDetail ADD CONSTRAINT us_UserId UNIQUE (UserID)To Drop Unique Key constraint on table use the below statementALTER TABLE SampleUserDetail DROP Constraint us_UserIdSQL Foreign Key ConstraintA Foreign key in one table point to primary key in another table. The foreign key constraint is used to prevent the actions that would destroy the links between two tables.Example of Foreign key constraintCreate one table with primary key and give name as UserDetailsUserIDUserNameFirstNameLastName 1SureshDasariSureshDasari 2PrasanthiDonthiPrasanthiDonthi 3MaheshDasariMaheshDasariAfter create another table with Foreign Key and give name as SalaryDetailsSalIDSalaryUserID 1100001 2200002 3300003The column UserID is a primary key in UserDetails tableThe column SalID is a foreign key in SalaryDetails tablesIf you observe above two tables UserID in UserDetails table points to UserID in SalaryDetailsExample of creating Foreign Key constraint during the time of table creationCreate Table SalaryDetails(SalaryID integer ,Salary integer,UserID varchar(50),PRIMARY KEY (SalaryID),CONSTRAINT fk_SalaryID FOREIGN KEY(UserID)REFERENCES UserDetails(UserID))Example of creating Foreign Key constraint during the time of ALTER TABLEALTER TABLE SalaryDetails ADD CONSTRAINT fk_SalaryID FOREIGN KEY (UserID) REFERENCES UserDetails(UserID)To Drop Foreign Key constraint on table use the below statementALTER TABLE SampleUserDetail DROP Constraint fk_SalaryIDSQL Not NULL Constraint:If we set Not Null constraint property on any column in table that column wont accept NULL or Empty values. If you want enforce any column not to accept NULL or empty value just set Not NULL Constraint property for that particular columnExample of creating NOT NULL constraint during the time of table creationCreate Table SampleUserDetail(UserID integer NOT NULL,UserName varchar(50) NOT NULL,FirstName varchar(50),LastName varchar(50))SQL Check Constraint:The check constraint is used to limit the value range that can be placed in a column. If we set Check constraint property on particular column the values in particular column must satisfy condition set by check constraint. Example of creating Check constraint during the time of CREATE TABLECreate Table SampleUserDetail(UserID integer NOT NULL CHECK(UserID > 0),UserName varchar(50),FirstName varchar(50),LastName varchar(50))Example of creating Check constraint during the time of ALTER TABLEALTER TABLE SampleUserDetails ADD CONSTRAINT chk_UserID CHECK(UserID > 0)To Drop Check constraint on table use the below statementALTER TABLE SampleUserDetails DROP CONSTRAINT chk_UserID