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

It Enforces Limits To The Data or Type of Data From A Table

Here is the PL/SQL block with the logic to check the salary of an employee stored in the substitution variable and perform the required actions: DECLARE sal NUMBER; BEGIN -- Store employee name DEFINE P_LASTNAME = 'Pataballa'; -- Store salary IF P_LASTNAME = 'Pataballa' THEN sal := 2500; END IF; -- Check salary and perform required actions IF sal < 3000 THEN sal := sal + 500; DBMS_OUTPUT.PUT_LINE(P_LASTNAME ||'''s salary updated'); ELSE DBMS_OUTPUT.PUT_LINE(P_LASTNAME ||' earns '|| sal); END

Uploaded by

daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

It Enforces Limits To The Data or Type of Data From A Table

Here is the PL/SQL block with the logic to check the salary of an employee stored in the substitution variable and perform the required actions: DECLARE sal NUMBER; BEGIN -- Store employee name DEFINE P_LASTNAME = 'Pataballa'; -- Store salary IF P_LASTNAME = 'Pataballa' THEN sal := 2500; END IF; -- Check salary and perform required actions IF sal < 3000 THEN sal := sal + 500; DBMS_OUTPUT.PUT_LINE(P_LASTNAME ||'''s salary updated'); ELSE DBMS_OUTPUT.PUT_LINE(P_LASTNAME ||' earns '|| sal); END

Uploaded by

daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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','[email protected]','Programmer',5000,'21-May-2015');
insert into infosys
values('OM212','samuel','[email protected]','Programmer',8000,'27-Feb-2015');
insert into infosys
values('OM213','Arun','[email protected]','Programmer',10000,'29-Mar-2015');
insert into infosys
values('OM214','Karthikeyan','[email protected]','clerk',8000,'11-JUN-2015');
insert into infosys values('OM215','Andrew','[email protected]','clerk',5000,'13-
sep-2015');
insert into infosys
values('OM216','Stewart','[email protected]','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','[email protected]',99484389,'M');
insert into infosys_employee_details
values('OM212','chennai','india','[email protected]',99484456,'M');
insert into infosys_employee_details
values('OM213','bangalore','india','[email protected]',99484389,'M');
insert into infosys_employee_details
values('OM214','mysore','india','[email protected]',99484389,'M');
insert into infosys_employee_details
values('OM215','nagercoil','india','[email protected]',99484389,'M');
insert into infosys_employee_details
values('OM216','salem','india','[email protected]',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.

You might also like