You are on page 1of 24

SQL COMPARISON OPERATORS

TYPES OF COMPARISON OPERATOR

• Equal =
• Not equal <>
• Less than <
• Greater than >
• Less than or equal to <=
• Greater than or equal to >=
EQUAL TO (=)
e_no e_name e_add
Data is retrieved from the tables based e1 Ravi Delhi
on equality operator.
e2 Manish Pune

e3 Ram Hyd
Q. Find the details of employee whose
name is Manish?
Ans. Select * from emp
where e_name='Manish';
NOT EQUAL TO (<>)
e_no e_name e_add
e1 Ravi Delhi
• Q. Find the details of employees e2 Manish Pune
who are not working in Delhi?
e3 Ram Hyd
• Ans. Select * from emp where
e_add <> 'Delhi';
LESS THAN (<)

E_no E_name E_add Salary


• Q Find the details of employees who are e1 Ravi Delhi 5000
earning less than 6000?
e2 Manish Pune 8000
• Ans. Select * from emp where salary<6000;
e3 Ram Hyd 6000
GREATER THAN
E_no E_name E_add Salary

• Q Find the details of employees who e1 Ravi Delhi 5000


are earning more than 5000? e2 Manish Pune 8000
• Ans. Select * from emp where e3 Ram Hyd 6000
salary>5000;
LESS THAN OR EQUAL
TO (<=)
E_no E_name E_add Salary
e1 Ravi Delhi 5000 • Q Find the details of employees
e2 Manish Pune 8000 who are earning maximum of 6000?

e3 Ram Hyd 6000 • Ans. Select * from emp where salary


<= 6000;
GREATER THAN OR
EQUAL TO (>=)
E_no E_name E_add Salary

• Q Find the details of e1 Ravi Delhi 5000


employees who are earning minimum e2 Manish Pune 8000
of 6000?
e3 Ram Hyd 6000
• Ans. Select * from emp where salary
>= 6000;
TRIGGERS

• A trigger is a stored procedure in database which


automatically invokes whenever DDL command gets
executed.
• For example, a trigger can be invoked when a row is
inserted into a specified table or when certain table
columns are being updated.
TRIGGER STRUCTURE

• There are two parts of trigger


1. Trigger Specification
2. Trigger Body
• Trigger specification (Syntax)
SYNTAX

• create trigger [trigger_name]


• [before | after]
• {insert | update | delete}
• on [table_name]
• [for each row] {For row level triggers}

[trigger_body]
EXPLANATION

• create trigger [trigger_name]:


Creates or replaces an existing trigger with the trigger_name.
• [before | after]:
This specifies when the trigger will be executed.
• {insert | update | delete}:
This specifies the DML operation.
• on [table_name]:
This specifies the name of the table associated with the trigger.
• [for each row]:
This specifies a row-level trigger, i.e., the trigger will be executed for each row
being affected.
• [trigger_body]:
This provides the operation to be performed when trigger is fired
BEFORE & AFTER TAGS

• Before
When we use before tag then trigger get invoked before execution of
DML command.
• After
When we use after tag then trigger get invoked after execution
of DML command.
EXAMPLE

• Student table

S_id Name Sub1 Sub2 Sub3 Total


- - - - - -

• Insert marks for student 1 Sub1 = 50, Sub2 = 50, Sub3 = 80.
• Requirement Before inserting marks we require addition of three subject and
set total.
• create trigger stud_marks
• before INSERT
• on
• Student
• for each row
• set Student.total = Student.subj1 + Student.subj2 +
Student.subj3;
E-R DIAGRAM

• The ER model defines the conceptual view of a database.


• Terms we cover in this are
1. Entity
2. Attributes
3. Relationship
ENTITY

• An entity can be a real-world object.


• All these entities have some properties that give them their identity.
• An entity set is a collection of similar types of entities.
• An entity set may contain entities with attribute may not.
ATTRIBUTES

• Entities are represented by means of their properties, called attributes.


• All attributes have values.
Example -A student entity may have name, class, and age as attributes.
Types of attributes-
1. Simple attribute
Simple attributes are atomic values, which cannot be divided further. Ex. Phone No.
2. Composite attribute −
Composite attributes are made of more than one simple attribute. Ex. Address.
3. Derived attribute
Derived attributes are the attributes that do not exist in the database, but
their values are derived from other attributes present in the database
Ex. Age
4. Multi-value attribute
Multi-value attributes may contain more than one values.
Ex. Multiple phone nos.
RELATIONSHIP

• The association among entities is called a relationship.


• Degree of relationship defines number of entities participating in relationship.
Binary – Two entities participate – Degree 2
Ternary – Three entities participate – Degree 3
N-ary - Degree
MAPPING CARDINALITIES

Cardinality
Defines no. Of entities in one set associated with
no. Of entities in other set.

1. One to one
One entity from entity set A can be associated
with at most one entity of entity set B and vice
versa.
2. One to many
One entity from entity set A can be
associated with more than one entities of
entity set B however an entity from
entity set B, can be associated with at
most one entity.

3. Many-to-one
More than one entities from entity set A
can be associated with at most one
entity of entity set B, however an entity
from entity set B can be associated with
more than one entity from entity set A.
4. Many-to-many
One entity from A can be associated with more than one entity from B and vice
versa.
REPRESENTATION OF
ER DIAGRAM
CARTESIAN PRODUCT

• On applying CARTESIAN PRODUCT on two sets of tuples, it will take every


row one by one from the left set and will pair it up with all the rows in the
right set.

SNO FNAME LNAME ROLLNO AGE


1 Albert Singh 5 18
2 Nora Fatehi 9 21

SNO FNAME LNAME ROLLNO AGE


1 Albert Singh 5 18
1 Albert Singh 9 21
2 Nora Fatehi 5 18
2 Nora Fatehi 9 21

You might also like