Sei sulla pagina 1di 6

Constraints

 It enforces limits to the data or type of data from a table


 whole purpose of constraints is to maintain the data integrity during an
update/delete/insert into a table.
 It can be performed at table level and column level
 Syntax: constraint constraint_name constraint_type(column_name);

TYPES

 Primary key
 Foreign key
 Unique
 Not null
 Check

NOT NULL

 NOT NULL constraint makes sure that a column does not hold NULL value.
 EX:

UNIQUE

 UNIQUE Constraint enforces a column or set of columns to have unique values.


 If a column has a unique constraint, it means that particular column cannot have
duplicate values in a table.
 EX:

CHECK

 This constraint is used for specifying range of values for a particular column of a table.
 EX:

PRIMARY KEY

 Primary key uniquely identifies each record in a table


 It must have unique values and cannot contain nulls
 EX:

FOREIGN KEY

 Foreign keys are the columns of a table that points to the primary key of another table
 They act as a cross-reference between tables.
 EX:

EXAMPLE PROGRAM ON CONSTRAINT

 create table INFOSYS


(
Employee_Id varchar2 (30),
Employee_Name char (20) NOT NULL,
Employee_Email_ID varchar2 (30),
Employee_Designation char (10),
Employee_Salary Number (10),
Employee_Hire_Date Date
);


insert into infosys
values('OM211','Daniel','danny@infosys.com','Programmer',5000,'21-May-2015');
insert into infosys
values('OM212','samuel','samuel@infosys.com','Programmer',8000,'27-Feb-2015');
insert into infosys
values('OM213','Arun','arun@infosys.com','Programmer',10000,'29-Mar-2015');
insert into infosys
values('OM214','Karthikeyan','karthi@infosys.com','clerk',8000,'11-JUN-2015');
insert into infosys values('OM215','Andrew','andrew@infosys.com','clerk',5000,'13-
sep-2015');
insert into infosys
values('OM216','Stewart','stewart@infosys.com','Programmer',15000,'19-JAN-
2015');


create table Infosys_employee_Details
(
emp_id varchar(2),
CITY char(20),
Country char(10),
EMAIL_ID varchar2(30),
Phone_number number(10),
Gender char(1),
constraint Infy_emp_id_fk FOREIGN KEY (emp_id) References (wrong don’t
follow )infosys(Employee_id) ON DELETE CASCADE,
constraint Infy_Email_FK FOREIGN KEY (Email_ID) References
infosys(Employee_email_id) ON DELETE SET NULL,
constraint Infy_emp_Gen CHECK (Gender IN ('F','M'))
);

insert into infosys_employee_details


values('OM211','chennai','india','danny@infosys.com',99484389,'M');
insert into infosys_employee_details
values('OM212','chennai','india','samuel@infosys.com',99484456,'M');
insert into infosys_employee_details
values('OM213','bangalore','india','arun@infosys.com',99484389,'M');
insert into infosys_employee_details
values('OM214','mysore','india','karthi@infosys.com',99484389,'M');
insert into infosys_employee_details
values('OM215','nagercoil','india','andrew@infosys.com',99484389,'M');
insert into infosys_employee_details
values('OM216','salem','india','stewart@infosys.com',99484389,'M');
ON DELETE CASCADE

 A foreign key with cascade delete means that if a record in the parent table is deleted,
then the corresponding records in the child table will automatically be deleted. This is
called a cascade delete in SQL Server
 A foreign key with cascade delete can be created using either a CREATE TABLE
statement or an ALTER TABLE statement

 EX: In the above table

 constraint Infy_id_FK FOREIGN KEY (Emp_id) References infosys(Employee_id) ON


DELETE CASCADE
 delete from infosys where employee_id='OM211';

( Infosys_employee_details table)

ON DELETE SET NULL

 A foreign key with "set null on delete" means that if a record in the parent table is
deleted, then the corresponding records in the child table will have the foreign key
fields set to null.
 The records in the child table will not be deleted.
 A foreign key with a "set null on delete" can be defined in either a CREATE TABLE
statement or an ALTER TABLE statement.

 EX: In the above table

 constraint Infy_id_FK FOREIGN KEY (Emp_id) References infosys(Employee_id) ON


DELETE SET NULL
 when a data from the parent table is deleted

Ex: delete from infosys where employee_id='OM212';


( Infosys_employee_details table)

2Write a PL/SQL block to declare a variable called sal to store the salary of an
employee. In the executable part of the program, do the following:

a. Store an employee name in a substitution variable:

DEFINE P_LASTNAME = Pataballa

b. Store his or her salary in the sal variable

c. If the salary is less than 3,000, give the employee a raise of 500 and
display the message “<Employee Name>’s salary updated” in the window.

d. If the salary is more than 3,000, print the employee’s salary in the format,
“<Employee Name> earns”

e. Test the PL/SQL block for the last names.

Note: “Undefine” the variable that stores the employee’s name at the end of the
script.

Potrebbero piacerti anche