Professional Documents
Culture Documents
DBMS4
DBMS4
For example, in employee relations, we assume that an employee may be assigned multiple
roles, and an employee may work on multiple projects simultaneously. So the primary key will
be composed of all three attributes, namely Emp_ID, Emp_role, and Proj_ID in combination.
So these attributes act as a composite key since the primary key comprises more than one
attribute.
Artificial key
The key created using arbitrarily assigned data are known as artificial keys.
These keys are created when a primary key is large and complex and has no relationship
with many other relations.
The data values of the artificial keys are usually numbered in a serial order.
For example, the primary key, which is composed of Emp_ID, Emp_role, and Proj_ID, is large in employee
relations.
So it would be better to add a new virtual attribute to identify each tuple in the relation uniquely.
Relational Algebra procedural query language
Language for expressing Relational DB queries
Rename Operation:
The rename operation is used to rename the output relation. It is
denoted by rho (ρ).
Example: We can use the rename operator to rename
STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
Union Operation:
• Suppose there are two tuples R and A union operation must hold the following
S. The union operation contains all condition:
• R and S must have the attribute of the same
the tuples that are either in R or S or
both in R & S. number.
• It eliminates the duplicate tuples. It • Duplicate tuples are eliminated automatically.
is denoted by ∪. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPO
1.Notation: R ∪ S SITOR) CUSTOMER_NAME
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
Cartesian product
• The Cartesian product is used to combine
each row in one table with each row in the
other table. It is also known as a cross
product. EMPLOYEE
• It is denoted by X. EMP_ID EMP_NAME EMP_DEPT
Notation: E X D
1 Smith A
1.EMPLOYEE X DEPARTMENT
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
2 Harry C B Sales
3 John B C Legal
1 Albert Singh
SNO FNAME LNAME ROLLNO AGE
2 Nora Fatehi 1 Albert Singh 5 18
1 Albert Singh 9 21
2 Nora Fatehi 5 18
ROLLNO AGE 2 Nora Fatehi 9 21
5 18
9 21
Join Operations:
1.Operation: (EMPLOYEE ⋈ SALARY)
A Join operation combines related tuples from different
relations, if and only if a given join condition is satisfied. It is
denoted by ⋈. EMP_CODE EMP_NAME SALARY
EMPLOYEE 101 Stephan 50000
102 Jack 30000
EMP_CODE EMP_NAME
103 Harry 25000
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Natural Join:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
Inner Join:
The outer join operation is an extension of the join operation. It
is used to deal with common information.
EMPLOYEE FACT_WORKERS
EMP_NAME STREET CITY EMP_NAME BRANCH SALARY
1.(EMPLOYEE ⋈ FACT_WORKERS)
EMPLOYEE ⟕ FACT_WORKERS
EMP_NAME STREET CITY EMP_NAME BRANCH SALARY
EMPLOYEE ⟖ FACT_WORKERS
EMPLOYEE ⟗ FACT_WORKERS
so while we have to find out the 1stname and lastname of employee whose Eid =! Mid
c =! z
• COMMIT
• SAVEPOINT
• ROLLBACK
COMMIT Command
• If everything is in order with all statements within a single transaction, all changes are
recorded together in the database is called committed.
• The COMMIT command saves all the transactions to the database since the last
COMMIT or ROLLBACK command.
Syntax:
COMMIT;
Example:
Sample Table1
• This command can only be used to undo transactions since the last COMMIT or
ROLLBACK command was issued.
Syntax for ROLLBACK command:
ROLLBACK;
Declaration DECLARE
declare A INT;
Block code a int B INT;
b int C INT;
c int BEGIN
A:= &A;
Executable code B:= &B;
Begin
C:= A+B;
End dbms_output.put_line('sum of a and b'||c);
end;
Exception Handling
Ant type of error ONLINE ORACLE &A AND &B wont work
DECLARE
A INT;
B INT;
C INT;
BEGIN
A:= 10;
B:= 20;
if(A>B)
THEN
dbms_output.put_line('A IS GREATER'||A);
ELSE
dbms_output.put_line('B IS GREATER'||B);
END IF;
end;
Trigger
A Trigger in Structured Query Language is a set of procedural statements which are executed
automatically when there is any response to certain events on the particular table in the database.
Triggers are used to protect the data integrity in the database.
In SQL, this concept is the same as the trigger in real life. For example, when we pull the gun
trigger, the bullet is fired.
Suppose Rishabh is the human resource manager in a multinational company. When the record of a new employee
is entered into the database, he has to send the 'Congrats' message to each new employee. If there are four or five
employees, Rishabh can do it manually, but if the number of new Employees is more than the thousand, then in
such condition, he has to use the trigger in the database.
Thus, now Rishabh has to create the trigger in the table, which will automatically send a 'Congrats' message to the
new employees once their record is inserted into the database.
The trigger is always executed with the specific table in the database. If we remove the table, all the triggers
associated with that table are also deleted automatically.
In Structured Query Language, triggers are called only either before or after the
below events:
1.INSERT Event: This event is called when the new row is entered in the table.
2.UPDATE Event: This event is called when the existing record is changed or modified
in the table.
3.DELETE Event: This event is called when the existing record is removed from the
table.
Types of Triggers in SQL
Following are the six types of triggers in SQL:
1.AFTER INSERT Trigger
This trigger is invoked after the insertion of data in the table.
2.AFTER UPDATE Trigger
This trigger is invoked in SQL after the modification of the data in
the table.
3.AFTER DELETE Trigger
This trigger is invoked after deleting the data from the table.
4.BEFORE INSERT Trigger
This trigger is invoked before the inserting the record in the table.
5.BEFORE UPDATE Trigger
This trigger is invoked before the updating the record in the table.
6.BEFORE DELETE Trigger
This trigger is invoked before deleting the record from the table.
• In the trigger syntax, firstly, we have
Syntax of Trigger in SQL
CREATE TRIGGER Trigger_Name to define the name of the trigger
[ BEFORE | AFTER ] [ Insert | Update | Delete] after the CREATE TRIGGER
ON [Table_Name] keyword.
• After that, we have to define the
[ FOR EACH ROW | FOR EACH COLUMN ]
AS BEFORE or AFTER keyword with
Set of SQL Statement anyone event.
• Then, we define the name of that
table on which trigger is to occur.
• After the table name, we have to
define the row-level or statement-
level trigger.
• And, at last, we have to write the
SQL statements which perform
actions on the occurring of event.
Example of Trigger in SQL CREATE TABLE Student_Trigger
To understand the concept of trigger in SQL, first, we have to (
create the table on which trigger is to be executed. Student_RollNo INT NOT NULL PRIMARY KEY,
The following query creates the Student_Trigger table in the Student_FirstName Varchar (100),
SQL database: Student_EnglishMarks INT,
Student_PhysicsMarks INT,
Student_ChemistryMarks INT,
Student_MathsMarks INT,
Student_TotalMarks INT,
Student_Percentage ); DESC Student_Trigger;
END;
/ the execution of the above code at SQL Prompt, it produces the following result.
After
Trigger created.
Check the salary difference by procedure:
Use the following code to get the old salary, new salary and salary
difference after the trigger created.
Output:
DECLARE
Old salary: 20000 Old salary: 25000
total_rows number(2);
New salary: 25000 New salary: 30000
BEGIN
Salary difference: 5000 Salary difference: 5000
UPDATE customers
Old salary: 22000 Old salary: 27000
SET salary = salary + 5000;
New salary: 27000 New salary: 32000
IF sql%notfound THEN
Salary difference: 5000 Salary difference: 5000
dbms_output.put_line('no customers updated');
Old salary: 24000 Old salary: 29000
ELSIF sql%found THEN
New salary: 29000 New salary: 34000
total_rows := sql%rowcount;
Salary difference: 5000 Salary difference: 5000
dbms_output.put_line( total_rows || ' customers updated ');
Old salary: 26000 Old salary: 31000
END IF;
New salary: 31000 New salary: 36000
END;
Salary difference: 5000 Salary difference: 5000
/
Old salary: 28000 Old salary: 33000
New salary: 33000 New salary: 38000
Salary difference: 5000 Salary difference: 5000
Old salary: 30000 Old salary: 35000
New salary: 35000 New salary: 40000
Salary difference: 5000 Salary difference: 5000
6 customers updated 6 customers updated
PL/SQL Cursor
• When an SQL statement is processed, Oracle creates a memory area known as context area. A cursor is a pointer
to this context area.
• It contains all information needed for processing the statement.
• In PL/SQL, the context area is controlled by Cursor. A cursor contains information on a select statement and the
rows of data accessed by it.
• A cursor is used to referred to a program to fetch and process the rows returned by the SQL statement, one at a
time.
• There are two types of cursors:
•Implicit Cursors
•Explicit Cursors
%NOTFOUND Its return value is TRUE if DML statements like INSERT, DELETE and
UPDATE affect no row, or a SELECT INTO statement return no rows.
Otherwise it returns FALSE. It is a just opposite of %FOUND.
%ISOPEN It always returns FALSE for implicit cursors, because the SQL cursor is
automatically closed after executing its associated SQL statements.
%ROWCOUNT It returns the number of rows affected by DML statements like INSERT,
DELETE, and UPDATE or returned by a SELECT INTO statement.
PL/SQL Implicit Cursor Example
Create customers table and have records:
ID NAME AGE ADDRESS SALARY DECLARE
total_rows number(2);
1 Ramesh 23 Allahabad 20000 BEGIN
2 Suresh 22 Kanpur 22000 UPDATE customers SET salary = salary + 5000;
IF sql%notfound THEN
3 Mahesh 24 Ghaziabad 24000
dbms_output.put_line('no customers updated');
4 Chandan 25 Noida 26000 ELSIF sql%found THEN
total_rows := sql%rowcount;
5 Alex 21 Paris 28000
dbms_output.put_line( total_rows || ' customers updat
6 Sunita 20 Delhi 30000 ed ');
END IF;
Let's execute the following program to update the table and
END;
increase salary of each customer by 5000. Here, SQL
/
%ROWCOUNT attribute is used to determine the number of
rows affected:
Output:
6 customers updated PL/SQL procedure
successfully completed.
select * from customers;
Steps:
You must follow these steps while working with an explicit cursor.
1.Declare the cursor to initialize in the memory.
2.Open the cursor to allocate memory.
3.Fetch the cursor to retrieve data.
4.Close the cursor to release allocated memory.
Declare the cursor:
It defines the cursor with a name and the associated SELECT
statement.
Syntax for explicit cursor decleration
CURSOR name IS
SELECT statement;
Open the cursor:
It is used to allocate memory for the cursor and make it easy to
fetch the rows returned by the SQL statements into it.
Syntax for cursor open:
OPEN cursor_name;
Fetch the cursor:
It is used to access one row at a time. You can fetch rows from
the above-opened cursor as follows:
FETCH cursor_name INTO variable_list;
Close the cursor:
It is used to release the allocated memory. The following syntax
is used to close the above-opened cursors.
Close cursor_name;
Create customers table and have records:
Execute the following program to retrieve the customer name
and address.
ID NAME AGE ADDRESS SALARY
DECLARE
1 Ramesh 23 Allahabad 20000 c_id customers.id%type;
c_name customers.name%type;
2 Suresh 22 Kanpur 22000
c_addr customers.address%type;
3 Mahesh 24 Ghaziabad 24000 CURSOR c_customers is
4 Chandan 25 Noida 26000 SELECT id, name, address FROM customers;
BEGIN
5 Alex 21 Paris 28000 OPEN c_customers;
6 Sunita 20 Delhi 30000 LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
1 Ramesh Allahabad
/
2 Suresh Kanpur
3 Mahesh Ghaziabad
4 Chandan Noida
5 Alex Paris
6 Sunita Delhi
PL/SQL procedure successfully completed.
Functional dependency
The functional dependency is a relationship that exists between two attributes.
It typically exists between the primary key and non-key attribute within a table.
X → Y
The left side of FD is known as a determinant, the right side of the production is
known as a dependent.
For example:
Assume we have an employee table with attributes: Emp_Id, Emp_Name,
Emp_Address.
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee
table because if we know the Emp_Id, we can tell that employee name associated
with it.
Functional dependency can be written as:
Emp_Id → Emp_Name
X determines Y • Functional dependency from X to Y
X → Y
Y is dependent on X • Here we are not calculating y for X like calculating Y
dependent • In DBMS you have given a table from X you would be
determinant determining the value of Y X Y
• Through X value determining Y value
• X -> ROLL NO , Y-> Name 1 2
If it is true then it is called as Functional dependency
2 1
3 1
4 3
1.ID → Name,
2.Name → DOB
1. Trivial functional dependency For Non trival FD only we can check valid / or not
Need of Normalization
• Decomposition of any table is not so simple because having the big table we cant do it
• Problems occur like lossless information & lossy information
Insert Anomaly
Update Anomaly Problem occurs when Normalization occurs
Delete Anomaly
Student Dept
PK FK PK