SQL_Unit II-2
SQL_Unit II-2
SQL_Unit II-2
SYSTEMS
CS2801T DBMS LAB
•TEXTBOOK:
•Abraham Silberschatz, Henry F. Korth, S. Sudharshan,
“Database System Concepts”,Sixth Edition, Tata McGraw
Hill, 2011.
•Ramez Elmasri, Shamkant B. Navathe, “Fundamentals of
Database Systems”, Sixth Edition, Pearson, 2011.
Syllabus
CONCEPTUAL DATA MODELING
Database environment – Database system development lifecycle – Requirements collection –Database
design - Entity-Relationship model – Enhanced-ER model – UML class diagrams.
RELATIONAL MODEL AND SQL
Relational model concepts - Integrity constraints - SQL Data manipulation – SQL Data definition –
Views - SQL programming.
RELATIONAL DATABASE DESIGN AND NORMALIZATION
ER and EER-to-Relational mapping – Update anomalies – Functional dependencies – Inference rules –
Minimal cover – Properties of relational decomposition – Normalization (upto BCNF).
TRANSACTIONS
Transaction Concepts – ACID Properties – Schedules – Serializability – Concurrency Control – Need for
Concurrency – Locking Protocols – Two Phase Locking – Deadlock – Transaction Recovery - Save Points
– Isolation Levels – SQL Facilities for Concurrency and Recovery.
NO-SQL DATABASES
No-SQL: CAP theorem – Document-based: MongoDB data model and CRUD operations; Column-
based: Hbase data model and CRUD operations.
UNIT II
UPDATE STUDENT
SET NAME = ‘Sherlock’
WHERE NAME = ‘Jolly’;
COMMIT;
UPDATE NAME
SET NAME= ‘Rossie’
WHERE marks= 70;
SAVEPOINT A;
Savepoint B;
Savepoint C;
SELECT *
FROM STUDENT;
• Rollback to B;
• Rollback to A;
• Rollback
• UPDATE STUDENT
• SET NAME = ‘Sherlock’
• WHERE STUDENT_NAME = ‘Jolly’;
• Rollback;
SQL Integrity Constraints
• SQL constraints are used to specify rules for the data in a table.
• Constraints are used to limit the type of data that can go into a
table
•NOT NULL - Ensures that a column cannot have a NULL value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
•FOREIGN KEY - Prevents actions that would destroy links between
tables
•CHECK - Ensures that the values in a column satisfies a specific
condition
•DEFAULT - Sets a default value for a column if no value is specified
NOT NULL
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
UNIQUE
• Union
• Union all
• Intersection
• Minus
SELECT * FROM First
UNION
SELECT * FROM Second;
SELECT * FROM First
UNION ALL
SELECT * FROM Second;
SELECT * FROM First
INTERSECT
SELECT * FROM Second;
SELECT * FROM First
MINUS
SELECT * FROM Second;
Aggregate Function
• An aggregate function in SQL returns one value after calculating multiple values
of a column
• Count()
• Sum()
• Avg()
• Min()
• Max()
SELECT AVG (salary) FROM employees;
Rename attribute
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
E.g)
ALTER TABLE student RENAME COLUMN sname TO name;
• SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
• ORDER BY
• used to sort the result-set in ascending or descending order.
• SELECT * FROM Customers
ORDER BY Country
GROUP BY
groups rows that have the same values into summary rows
often used with aggregate functions
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
• HAVING
• WHERE keyword cannot be used with aggregate functions
• lists the number of customers in each country. Only include countries
with more than 5 customers?
• SELECT COUNT(CustomerID), Country FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
• lists the number of customers in each country, sorted high to low (Only
include countries with more than 5 customers)
• SELECT COUNT(CustomerID), Country FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
• IN
• allows you to specify multiple values in a WHERE clause
• selects all customers that are located in "Germany", "France" or "UK?
• SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK’);
• Natural Join joins two tables based on same attribute name and datatypes.
• The resulting table will contain all the attributes of both the table but keep only
one copy of each common column.
• The INNER JOIN keyword selects all rows from both the tables as long as the
condition is satisfied
• The resulting table will contain all the attributes from both the tables including
common column also
• NATURAL JOIN
• SELECT * FROM Employee NATURAL JOIN Department;
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
VIEW
• A View can either have all the rows of a table or specific rows based on certain condition
• Security
dbms_output.put_line(var); //output
END;
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
BEGIN
c := a + b ;
dbms_output.put_line('Sum of '||a||' and '||b||' is = '||c);
END;
/
IF THEN Statement
IF condition THEN
statements
END IF;
IF THEN ELSIF Statement
IF condition-1 THEN
statements;
ELSIF condition-2 THEN
statements;
ELSIF condition-3 THEN
statements;
ELSE
statements;
END IF;
DECLARE
grade CHAR(1);
BEGIN
grade := 'B';
DBMS_OUTPUT.PUT_LINE('Excellent');
DBMS_OUTPUT.PUT_LINE('Very Good');
DBMS_OUTPUT.PUT_LINE('Good');
DBMS_OUTPUT. PUT_LINE('Fair');
DBMS_OUTPUT.PUT_LINE('Poor');
ELSE
END IF;
END;
/
CASE selector
WHEN selector_value_1 THEN
statements_1
WHEN selector_value_1 THEN
statement_2
...
ELSE
else_statements
END CASE;
DECLARE
grade CHAR(1);
BEGIN
grade := 'B';
CASE grade
END CASE;
END;
/
FOR index IN [ REVERSE ] lower_bound..upper_bound LOOP
statements
END LOOP;
E.G)
BEGIN
FOR i IN 1..3 LOOP
DBMS_OUTPUT.PUT_LINE (i);
END LOOP;
DECLARE
a number(2);
BEGIN
FOR a in 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/
DECLARE
a number(2) ;
BEGIN
FOR a IN REVERSE 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/
DECLARE
i number(1);
j number(1);
BEGIN
FOR i IN 1..3 LOOP
FOR j IN 1..3 LOOP
dbms_output.put_line('i is: '|| i || ' and j is: ' || j);
END;
END;
END;
/
WHILE <condition>
LOOP statements;
END LOOP;
DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
LOOP
statements;
EXIT;
{or EXIT WHEN condition;}
END LOOP;
DECLARE
i NUMBER := 1;
BEGIN
LOOP
EXIT WHEN i>10;
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
• Break
• Continue
Function
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END ;
create or replace function adder(n1 in number, n2 in number)
return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;
/
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
/
CREATE OR REPLACE FUNCTION totalCustomers
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customers;
RETURN total;
END;
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
Procedure
P_UPDATE_SALARY(a, b);
END;
/
Cursor
OPEN c_customers;
CLOSE c_customers;
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
BEGIN
OPEN c_customers;
LOOP
END LOOP;
CLOSE c_customers;
END;
/
Trigger
Triggers are stored programs, which are automatically executed or fired when
some events occur
• Scalar subqueries return a single value, or exactly one row and exactly one
column.
• Multirow subqueries return either:
• One column with multiple rows (i.e. a list of values), or
• Multiple columns with multiple rows (i.e. tables).
• Correlated subqueries, where the inner query relies on information obtained
from the outer query.
• Sub Quries
• We want to list paintings that are priced higher than the average
1) Problem Identification
2) Requirement Gathering
3) Design Using ER Diagram
4) Design Using UML Diagram
5) Working with DDL and DML Commands
6) Working with Joins, Set operations and Aggregate Functions
7) Working with queries related to Projects
8) Working with Basic PL/SQL Programming
9) Working with PL/SQL Procedures
10) Working with PL/SQL Functions
11) Working with PL/SQL Cursors
12) Working with PL/SQL Triggers
13) Project Report
THANK YOU !!!