Relational Design
Relational Design
Deletion anomaly:
• leads to a situation in which deletion of data representing certain information results in losing data
representing some other information that is associated with it
Modification Anomaly:
• leads to a situation in which repeated data changed at one place results in inconsistency unless the
same data are also changed at other places
DELETE: Assume S24 is the only one student who opted Physics. When S24 leaves the course then
Student record is deleted which also result in loss of Course information.
UPDATE: If S21 changes his Address then in both records Address is to be modified otherwise it results
into inconsistency.
• Normalization is a process of decomposing the relations into multiple relations with fewer attributes each.
Example:
• Table with two columns Employee_Id and Employee_Name.
• {Employee_id, Employee_Name} → Employee_Id is a trivial functional dependency as
Example:
• ID → Name,
• Name → DOB
• Normalization divides the larger table into smaller and links them using relationships.
• The normal form is used to reduce redundancy from the database table.
• Normalization can be considered as fundamental to the modelling and design of a relational database
• Main purpose is to eliminate data redundancy and avoid data update anomalies
Advantages of Normalization
• Normalization helps to minimize data redundancy.
• Greater overall database organization.
• Data consistency within the database.
• Much more flexible database design.
• Enforces the concept of relational integrity.
Number of Tables
• Second Normal Form (2NF)
Complexity
Redundancy
• Third Normal Form (3NF)
• Boyce-Codd Normal Form (BCNF)
• Fourth Normal Form (4NF)
• Fifth Normal Form (5NF)
• Domain Key Normal Form (DKNF)
Most databases should be 3NF or BCNF in order to avoid the database anomalies.
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
TEACHER_ID SUBJECT
TEACHER_ID TEACHER_AGE 25 Chemistry
25 30 25 Biology
47 35 47 English
83 38 83 Math
83 Computer
• A transitive functional dependency is when changing a non-key column, might cause any of
other non-key columns to change
Example: Changing the non-key column Full Name may change Salutation.
• If there is no transitive dependency for non-prime attributes, then the relation must be in 3NF.
• A relation is in 3NF if it holds atleast one of the following conditions for every non-trivial function dependency X → Y.
• X is a super key.
• Y is a prime attribute, i.e., each element of Y is part of some candidate key.
EMPLOYEE_DETAIL table:
EMP_ID EMP_NAME EMP_ZIP EMP_STATE EMP_CITY
222 Harry 201010 UP Noida
333 Stephan 02228 US Boston
444 Lan 60007 US Chicago
555 Katharine 06389 UK Norwich
• Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP dependent on EMP_ID.
• Non-prime attributes (EMP_STATE, EMP_CITY) transitively dependent on super key(EMP_ID) → violates 3NF rule.
EMP_ID EMP_NAME EMP_ZIP EMP_STATE EMP_CITY
• Need to move EMP_CITY and EMP_STATE to 222 Harry 201010 UP Noida
new <EMPLOYEE_ZIP> table, with EMP_ZIP 333 Stephan 02228 US Boston
as a Primary key. 444 Lan 60007 US Chicago
555 Katharine 06389 UK Norwich
2NF
1NF
3NF
• Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the
WHERE clause.
• Used to return data that will be used in the main query as a condition to further restrict the data to be
retrieved.
• Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
SELECT *
FROM CUSTOMERS
WHERE ID IN ( SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
Find the average instructors’ salaries of those departments where the average salary is
greater than $42,000.
group by dept_name)
• INSERT statement uses data returned from subquery to insert into another table.
• Selected data in the subquery can be modified with any of the character, date or number functions.
• Subquery can be used in conjunction with the UPDATE and DELETE statements
• Fields in a view are fields from one or more real tables in the database.
• Some times, it is not desirable for all users to see entire logical
model . Thus Provides some kind of Security.
• A view provides a mechanism to hide certain data from the view of
certain users.
• A view is defined using the create view statement which has the form
VIEW
RENAMING COLUMN NAMES IN THE VIEW
VIEW
QUERYING A VIEW
Views can be queried in the same way like Base Tables
Example:
❖ %NOTFOUND: Evaluates to TRUE if the last fetch is failed i.e. no more rows are left. (single word)
Syntax: cursor_name %NOTFOUND
loop
end loop;
END;
/
SSS Shameem June-2022
D ATA B A S E T R I G G E R S
• Triggers get executed (fire) automatically when specified SQL DML operations – INSERT, UPDATE, or
DELETE affecting one or more rows of a table.
• A stored PL/SQL program unit that is associated with a specific database table, or with certain view
types – can also be associated with system event.
• Two sections:
• A named database event
• A PL/SQL block that will execute when event occurs
• Constraints affects all the rows i.e. Validates the even already existing data before defining the constraint.
• Triggers:
• are named PL/SQL blocks with declarative, executable, and exception handling sections.
• are stand-alone database objects
• do not accept arguments.
• To create/test a trigger, user must have appropriate access to all objects referenced by a trigger
action.
• Example: To create a BEFORE INSERT trigger for the employee table requires you to have INSERT
ROW privileges for the table.
[DECLARE
Declaration statements]
BEGIN
Executable statements
[EXCEPTION
Exception-handling statements]
END;
The trigger body must have at least the executable section.
The declarative and exception handling sections are optional.
When there is a declarative section, the trigger body must start with the DECLARE keyword.
The WHEN clause specifies the condition under which a trigger should fire.
SSS Shameem June-2022
TRIGGER TYPES
• BEFORE and AFTER Triggers – trigger fires before or after the triggering event. Applies only to
tables.
• INSTEAD OF Trigger – trigger fires instead of the triggering event. Applies only to views.
• Triggering event – a DML statement issued against the table or view named in the ON clause –
example: INSERT, UPDATE, or DELETE.
DML triggers are fired by DML statements and are referred to sometimes as row triggers.
• FOR EACH ROW clause – a ROW trigger that fires once for each modified row.
• STATEMENT trigger – fires once for the DML statement.
• Referencing_clause – enables writing code to refer to the data in the row currently being modified
by a different name.