dbms unit3
dbms unit3
Example:
SQL> SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000 AND age < 25;
3.2.3 Aggregative Operators: The aggregative functions are used in the subqueries like
max(), min(), avg()…. It is used in the in the multiple row subqueries because in these
queries returns more than one row for the outer query.
Example: Display who are getting the minimum salary of each department.
select ename, sal from emp
where sal in (select min(sal) from emp group by deptid);
NULL VALUES:
SQL provides a special column value called null to use null when the column value is
unknown or inapplicable.
Example: create table student (sid integer constraint student_sid_nn not null,
name char(20));
In the above example sid doesn’t allow the null values in the column.
Impact on SQL Constructs: Boolean expressions arise in many contexts in SQL, and the
impact of null values must be recognized. In the presence of null value any row that
evaluates to false or unknown is eliminated so that there is an impact on the nested
queries.
The arithmetic operators all return null if one of their arguments is null. The aggregate
operators discard the null values.
3.2.4 Outer Joins:
Some interesting variants of the join operation that rely on null values called outer joins.
Left Outer Join:
To retrieve the missing records from the left side table of the join condition.
Example: Display name, dept no, dept name from tables
select e.ename, d.deptno, d.dname from emp e, dept d
where e.deptno = d.deptno(+);
Right Outer Join:
To retrieve the missing records from the right side table of the join condition.
Example: Display name, dept name, dept no from tables.
select e.ename,e.deptno,d.dname from emp e, dept d
where e.deptno(+) = d.deptno;
Full Outer Join:
To retrieve the missing records from the both side table of the join condition.
3.2.5 Complex Integrity Constraints in SQL:
Constraints over single table:
Constraints can specify over a single table using table constraints by CHECK
conditional expression.
Domain Constraints and distinct types:
A user can define a new domain using the CREATE DOMAIN statement which uses CHECK
constraints.
3.2.6 Triggers:
A trigger is a procedure that is automatically invoked by the DBMS in
response to special changes to the database and DBA. A database that has a set of
associated triggers is called an ACTIVE database.
Event: A changes to the data base that activates the trigger.
Condition: A query or test that is run when the trigger is activated.
Action: A procedure that is executed when the trigger is activated and the
condition is true.
A trigger can be thought of as a daemon that monitors a database and executed when the
database is modified in a way that matches the event specification
Eg: Write a trigger to display salary difference of old and new values.
set serveroutput on;
create or replace trigger emp_diff
after insert or delete or update on emp for each row
declare diff number;
begin
diff := :new.sal – :old.sal;
dbms_output.put_line(‘old salary’: || :old.sal);
dbms_output.put_line(‘new salary’: || :new.sal);
dbms_output.put_line(‘ salary difference’: || diffl);
end;
TEXT BOOK:
1. Raghurama Krishnan, Johannes Gehrke, Database Management Systems, 3rd Edition,
TATA McGraw hill.
2. Web pages