Front PAGE-1 - Merged
Front PAGE-1 - Merged
CAMPUS
RATHINAM TECHZONE
POLLACHI MAIN ROAD, EACHANARI, COIMBATORE-641021.
NAME :
REGISTER NUMBER :
YEAR/SEMESTER :
ACADEMIC YEAR :
RATHINAM TECHNICAL CAMPUS
RATHINAM TECHZONE
POLLACHI MAIN ROAD, EACHANARI, COIMBATORE-641021.
BONAFIDE CERTIFICATE
NAME :
ACADEMIC YEAR :
YEAR/SEMESTER :
BRANCH :
Certified that this is the bonafide record of work done by the above student in the
ER diagram for a
Movie_id
membership_id
full_names
title
gender
director
Members Movies
date_of_birth
Year_released
physical_ Has
address category
postal_
Address
Do Has
membership_id
Category_id
payment_id
paymentd
ate
reference_no remarks
description
Amt_paid
Result:
Thus the Database has been designed for the movie management using
Conceptual modeling (ER-EER).
EX.NO:2 Database Implementation using SQL Data definition and
Data manipulation Language with constraints
DATE:
Aim:
Create a database table, add constraints (primary key, unique, check, Not null), insert rows,
update and delete rows using SQL DDL and DML commands.
Create table student_36(Name varchar(30) NOT NULL,Roll_no int primary key,Age int
CHECK(Age>18),stud_id varchar(20) UNIQUE,Marks int);
Result:
Thus the Database has been implemented using SQL DDL and DML commands with constraints.
EX.NO: 03 Implementing referential integrity
constraints on tables
DATE:
Aim:
To create a set of tables, add foreign key constraints and incorporate referential integrity
\d Employee_36;
Result:
Thus, the tables with referential integrity constraints has been implemented.
EX.NO:4
Query the database using SQL Where Clause
DATE:
Aim:
To query the database using where clause.
CUSTOMER:
create table customer_36 ( custid varchar(20) primary key, fname varchar(20),mname
varchar(20), ltname varchar(20), city varchar(20),mobileno bigint , occupation varchar(20),
dob date);
BRANCH:
create table branch_36(bid int primary key,bname varchar(30),bcity varchar(30));
ACCOUNT:
Create table account_36(acnumber varchar(6) primary key,custid varchar(10), foreign
key(custid) references customer_36(custid),bid int, foreign key(bid) references
branch_23(bid),opening_balance int,aod date,atype varchar(10),astatus varchar(10));
LOAN:
Create table loan_36(loan_number varchar(10) primary key, custid varchar(10),bid int,
loan_amount int, foreign key (custid) references customer_36(custid),foreign key(bid)
references branch_36(bid));
a) Write a query to display the customer number, firstname, customer’s date of birth.
Display in sorted order of date of birth year and within that sort by firstname.
c) Display the Customer_id and Customer_name who have taken less than two loans.
Delete from account_36 where bid=(select bid from branch_36 where bcity =
'Delhi');
e) Display all the Customer names whose come from either Mumbai or Hydrabad
f) Find all loan (numbers) for loans made at the ‘Adyar’ branch with loan amount
greater than 50000.
Select loan_number from loan_36 where loan_amount>50000 and bid = (select bid
from branch_36 where bname = ‘Adyar’);
g) Find loan( numbers )of those loans with loan amount between 10000 and 20000.
select loan_number from loan_36 where loan_amount between 10000 and 20000;
create table employee_36( empno int primary key, empfname varchar(20), emplname
varchar(20), designation varchar(10), date_of_join date, jobid varchar(20), salary int not null,
depno int, foreign key(depno) references department_36(depno));
c) Create a query to display the last name and salary of employees whose salary is not in
the range of 2000 and 80000. (hints: not between )
Select emplname,salary from employee_36 where salary not between 2000 and
80000;
d) Display the employee last name, job ID, and start date of employees hired between
01-MAR-1995 and May 1,2010. (hints: between)
select emplname , JobId,date_of_join from employee_36 where date_of_join between
('01-03-1995') and ('01-05-2010');
e) Display the last name and hire date of every employee who was hired in 1994.(hints:
like)
a) Display the number of employees in each department with the department numbers in
descending order
e) Insert 5 records into the table. Don’t input values for total field and calculate total
field.
Result:
Thus the Database has been queried using SQL Where Clause.
EX.NO:5
Implementing Aggregate functions in database tables
DATE:
Aim:
To implement aggregate functions in database tables.
Customers:
Vendors:
vend_id vend_name vend_address vend_city vend_state vend_zip vend_country
123 Main Bear
BRS01 Bears R Us Street Town MI 44444 USA
Bear 500 Park
BRE02 Emporium Street Anytown OH 44333 USA
Doll House 555 High
DLL01 Inc. Street Dollsville CA 99999 USA
1000 5th New
FRB01 Furball Inc. Avenue York NY 11111 USA
Fun and 42 Galaxy
FNG01 Games Road London NULL N16 6PS England
1 Rue
JTS01 Jouets et ours Amusement Paris NULL 45678 France
OrderItems :
Orders :
order_num order_date cust_id
20005 01-05-2004 00:00 1000000001
20006 12-01-2004 00:00 1000000003
20007 30-01-2004 00:00 1000000004
20008 03-02-2004 00:00 1000000005
20009 08-02-2004 00:00 1000000001
Products:
prod_id vend_id prod_name prod_price prod_desc
BR01 BRS01 8 inch teddy bear 5.99 8 inch teddy bear, comes with cap and
jacket
12 inch teddy 12 inch teddy bear, comes with cap and
BR02 BRS01 bear 8.99 jacket
18 inch teddy 18 inch teddy bear, comes with cap and
BR03 BRS01 bear 11.99 jacket
Fish bean bag Fish bean bag toy, complete with bean
BNBG01 DLL01 toy 3.49 bag worms with which to feed it
Bird bean bag
BNBG02 DLL01 toy 3.49 Bird bean bag toy, eggs are not included
Rabbit bean bag Rabbit bean bag toy, comes with bean
BNBG03 DLL01 toy 3.49 bag carrots
RGAN01 DLL01 Raggedy Ann 4.99 18 inch Raggedy Ann doll
RYL01 FNG01 King doll 9.49 12 inch king doll with royal garments and
crown
RYL02 FNG01 Queen doll 9.49 12 inch queen doll with royal garments and
crown
Problems:
1. How many bean bag toys are in the Products table? select count(*) from products_36
where prod_desc like '%bean bag%';
4. How much would it cost to order 100 units of the most expensive product?
5. How much would it cost to order 10 quantities of the 3 most expensive products?
Result:
Thus, the aggregate functions has been implemented in database tables.
EX.NO:6
AIM:
SQL – Sequence:
Syntax:
CREATE SEQUENCE sequence_name
[ AS { data_type | DEFAULT integer_literal } ]
[ INCREMENT BY increment ]
[ START WITH start ]
[ MINVALUE minvalue | NOMINVALUE ]
[ MAXVALUE maxvalue | NOMAXVALUE ]
[ CYCLE | NOCYCLE ]
Queries:
1. Query to create a sequence in ascending order.
CREATE SEQUENCE ascending_36 START WITH 1 INCREMENT BY 1;
4. Altering a Sequence.
ALTER SEQUENCE ascending_36 INCREMENT BY 2;
5. Dropping a Sequence.
drop sequence ascending_36;
SQL – Identity:
SQL, an identity column is a column in a table that automatically generates numeric values. It's
commonly used for primary key columns to ensure each row has a unique identifier. Different
database systems have slightly different syntax for defining an identity column.
Syntax:
CREATE TABLE table_name(column_name data_type PRIMARY KEY GENERATED
ALWAYS AS IDENTITY (start_value, increment_value));
Queries:
1. Creating a table with the Identity generated always.
CREATE TABLE Tab1_36(ID INT GENERATED ALWAYS AS IDENTITY,Name VARCHAR(50));
Result:
Thus the database created, Sequences and Identity has been applied successfully.
EX.NO:7 QUERYING THE DATABASE USING SQL SUB
DATE: QUERIES AND JOINS
Aim:
To query or manage the database using SQL Sub queries and Joins.
Table: Employees:
EMP_I EMP_NA JOB_NA MANAGER HIRE_DA SALA COMMISSI DEP_I
D
D ME ME _ID TE RY ON
Table: Department:
DEP_ID DEP_NAME DEP_LOCATION
Queries:
1. write a SQL query to find the managers. Return complete information about the
managers.
select * from employees_36 where job_name= 'MANAGER';
2. write a SQL query to compute the experience of all the managers. Return employee ID,
employee name, job name, joining date, and experience.
Select emp_id, emp_name, job_name, hire_date, (current_date – hire_date)/365 as
experience from employees_36 where job_name = 'MANAGER';
3. write a SQL query to find those employees who work as 'MANAGERS' and 'ANALYST'
and working in ‘SYDNEY’ or ‘PERTH’ with an experience more than 5 years without
receiving the commission. Sort the result-set in ascending order by department location.
Return employee ID, employee name, salary, and department name.
select e.emp_id, e.emp_name, e.salary, d.dep_name from employees_36 e join
departments_36 d on e.dep_id = d.dep_id where e.job_name in
(‘MANAGER’,’ANALYST’) AND d.dep_location in (‘SYDNEY’,’PERTH’) AND
(current_date-e.hire_date)/365 >5 and e.commission is null order by d.dep_location asc ;
4. write a SQL query to find those employees of department 1001 and whose salary is more
than the average salary of employees in department 2001. Return complete information
about the employees.
select * from employees_36 where dep_id = 1001 and salary > (select
avg(salary) from employees_36 where dep_id = 2001);
5. write a SQL query to find the employees of grade 2 and 3.Return all the information of
employees and salary details.
Select e.*, s.min_salary, s.max_salary from employees_36 e join salarygrades_36 s on e.salary
between s.min_salary and s.max_salary where s.salary_grades in (2,3);
a. Write a SQL query to find those employees who work as same designation of
FRANK. Return complete information about the employees.
select * from employees_36 where job_name = (select job_name from employees_36
where emp_name = 'FRANK');
6. write a SQL query to find those employees of department ID 2001 and whose designation
is same as of the designation of department ID 1001. Return complete information about
the employees.
Select * from employees_36 where dep_id = 2001 and job_name in (select
job_name from employees_36 where dep_id = 1001);
7. write a SQL query to find those employees whose salary is the same as the salary of
FRANK or SANDRINE. Sort the result-set in descending order by salary. Return
complete information about the employees.
Select * from Employees_36 where salary in (select salary from Employees_36
where emp_name in ('FRANK','SANDRINE')) order by salary desc;
8. write a SQL query to find those employees whose salary is more than the total
remuneration (salary + commission) of the designation SALESMAN. Return complete
information about the employees.
Select * from Employees_36 where salary > (SELECT MAX(salary + commission)
from Employees_36 where job_name in ('SALESMAN'));
9. write a SQL query to find those employees who are senior to BLAZE and working at
PERTH or BRISBANE. Return complete information about the employees.
select * from Employees_36 where hire_date < (select hire_date from Employees_36
where emp_name = 'BLAZE') and dep_id in (select dep_id from Departments_36 where
dep_location in('PERTH','BRISBANE'));
10. write a SQL query to list any job of department ID 1001 which are not found in
department ID 2001. Return job name.
select distinct job_name from employees_36 where dep_id = 1001 and job_name not
in (select job_name from employees_36 where dep_id = 2001);
11. From the following table, write a SQL query to find the highest paid employee. Return
complete information about the employees.
Select * from employees_36 where salary = (select max(salary) from employees_36);
12. write a SQL query to find the highest paid employees in the department MARKETING.
Select * from Employees_36 where dep_id = (select dep_id from Departments_36
where dep_name = 'MARKETING') order by salary desc;
13. write a SQL query to find those employees who are senior employees as of year 1991.
Return complete information about the employees.
select * from employees_36 where hire_date < '1992-01-01';
Result:
Thus the Database has been queried using SQL Sub Queries and Joins.
EX.NO:8 QUERYING THE DATABASE USING SQL
DATE: PROGRAMMING
AIM:
To query or manage the database using SQL Stored Procedures/Functions.
SQL PROGRAMMING – STORED PROCEDURES/FUNCTIONS:
1. Write a PL/PGSQL code block to find sum and average of three numbers
CREATE PROCEDURE calculation_36 (a int, b int, c int)
AS $$
BEGIN
RAISE NOTICE 'SUM:%',(a+b+c);
RAISE NOTICE 'AVERAGE:%',(a+b+c)/3;
END;
$$ LANGUAGE plpgsql;
BEGIN
area := PI() * radius * radius;
RAISE NOTICE 'Area of circle with radius %: %', radius, area;
END;
$$ LANGUAGE plpgsql;
AS $$
DECLARE
result int;
BEGIN
IF n = 0 THEN
$$ LANGUAGE plpgsql;
remainder INTEGER;
BEGIN
WHILE n > 0 LOOP
remainder = n % 10;
reversed = reversed * 10 + remainder;
n = n / 10;
END LOOP;
RAISE NOTICE 'Reversed number:%',reversed;
END;
$$ LANGUAGE plpgsql;
greatest INTEGER;
BEGIN
IF a >= b AND a >= c THEN
greatest = a;
ELSIF b >= a AND b >= c THEN
greatest = b;
ELSE
greatest = c;
END IF;
RAISE NOTICE 'Greatest:%',greatest;
END;
$$ LANGUAGE plpgsql;
IF n <= 0 THEN
RAISE NOTICE '0';
END IF;
IF n = 1 THEN
RAISE NOTICE '1';
END IF;
FOR i IN 3..n LOOP
temp = a + b;
a = b;
b = temp;
END LOOP;
RAISE NOTICE 'fibonacci:%',temp;
END;
$$ LANGUAGE plpgsql;
8. Write a PL/PGSQL function to calculate the income tax for the given employee:
Conditions:
If pay < 2,50,000, no tax is charged.
If pay is >= 2,50,001 and <= 5,00,000, 5% of pay is charged as tax.
If pay is >= 5,00,001 and <= 10,00,000, 20% of pay is charged as tax.
If pay is >= 10,00,001, 30% of pay is charged as tax.
9. An Electricity Board charges the following rates to domestic users to discourage large
consumption of energy:
For the first 1 - 200 units { 1 - 100 units = Rs. 1.00, 101 - 200 units = Rs.1.50 }
From 201 - 500 units{ 1 - 200 units = Rs. 2.00 and 201 - 500 units = Rs. 3.00 }
Above 500 units{ 1 - 200 = Rs. 3.50, 201- 500 unit = Rs. 4.60 &above 500 unit
Rs.6.60}
Write a PL/PGSQL program to read the connection_id of users and number of units
consumed, display the charges with names.
10. Write a PL/PGSQL Procedure to print the grade of the Student. Obtain student marks as
input from the main program.
Check for the following condition:
if the input mark ranges 91-100 then Grade A,
81-90 then Grade B,
71-80 then Grade C,
60-70 then Grade D and <60 Grade E.
11. Write a PL/PGSQL program to display the salary of an employee when an empId is given.
SELECT display_employee_salary_36(101);
RETURN employee_count;
END;
$$ LANGUAGE plpgsql;
SELECT count_employees_in_department_36(1);
Result:
Thus the database has been queried using the stored procedures and functions.
EX.NO:9
DATE: VIEWS
Aim:
To create a database and apply views using DDL and DML commands.
StudentDetails:
SID NAME ADDRESS CONTACTNO
1 Harsh Kolkata 9876543891
2 Ashish Durgapur 7890654321
3 Pratik Delhi 9977665541
4 Dhanraj Bihar 9087654321
5 Ram Rajasthan 9876543210
StudentMarks
1 Harsh 90 19
2 Suresh 50 20
3 Pratik 80 19
4 Dhanraj 95 21
5 Ram 85 18
SQL – Views:
Creating View from a single table:
1. Create a View named DetailsView from the table StudentDetails with the details of
students whose ID is less than 4.
1. create a View named MarksView from two tables StudentDetails and StudentMarks.
Return ID,Name ,Address, ContactNo,Marks.
1. Create a View SampleView from StudentDetails Table in which student name should not
be null
Create view sampleviews_36 as select * from studentdetails_36 where name is not null;
Result:
Thus, the database has been created using views and DDL, DML commands has been
applied successfully.
EX.NO:10
QUERYING/MANAGING THE DATABASE USING SQL
DATE: PROGRAMMING - TRIGGERS
AIM:
To query or manage the database using SQL Triggers.
SQL – TRIGGERS:
1. Create the following tables with given attributes having appropriate data type and specify
the necessary primary and foreign key constraints:
Customer (Custid, Custname, Addr, phno,panno)
Create table customer_36 (custid int primary key, custname varchar(30), addr
varchar(100), phno bigint, panno bigint);
Create a trigger that checks for the minimum balance in the account.
CREATE OR REPLACE FUNCTION check_min_balance_36() RETURNS
TRIGGER
AS $$
BEGIN
IF NEW.Accbal < 100 THEN -- Assuming the minimum balance is 100
RAISE EXCEPTION 'Minimum balance constraint violated';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER check_min_balance_trigger_36
BEFORE INSERT OR UPDATE ON Account_36
FOR EACH ROW
EXECUTE FUNCTION check_min_balance_36();
2. Create the student database with the following tables and do the following:
Create database student10a_36;
Write a PL/SQL Trigger to verify the data before insertion on the assessment table.
4. Create a trigger to maintain an employee audit whenever an employee’s first name changes.
CREATE OR REPLACE FUNCTION employee_first_name_audit_36() RETURNS
TRIGGER
AS $$
BEGIN
IF OLD.first_name IS DISTINCT FROM NEW.first_name THEN
INSERT INTO employee_audit(audit_id, employee_id, old_first_name, new_first_name,
change_date)
VALUES (DEFAULT, OLD.employee_id, OLD.first_name, NEW.first_name,
CURRENT_TIMESTAMP);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER employee_first_name_audit_trigger
BEFORE UPDATE OF empname ON employee_36
FOR EACH ROW
EXECUTE FUNCTION employee_first_name_audit_36();
6. Write a database trigger before delete for each row on table course, delete corresponding
course students from Student table
1.Create a table ‘Student’ with the following details (table level constraints)
create table student_36(rollno int primary key, name varchar(20), coursecode int,
mark1 int check (mark1 between 0 and 100),mark2 int check(mark2 between 0 and 100), constraint
fk_course foreign key(coursecode) references course_36(ccode));
2. Create a table ‘Course’ with the following details (table level constraints)
Ccode number(2) Primary key
Course varchar2(10)
7. Create the following tables with given attributes having appropriate data type and specify
the necessary primary and foreign key constraints:
Customer (Custid, Custname, phno,pan,DOB)
create table customer5_36(custid int primary key, custname varchar(30), phno bigint, pan bigint,
dob date);
Create another table called ‘Department’ with the following structure(Column level
constraints)
Depno Number(2) Primary key
Depname Varchar2(15)
Deplocation Varchar2(10)
RESULT: