Adbms File
Adbms File
Gujral Punjab
Technical University
SUBMITTED TO
SUBMITTED BY
(Roll No.)
INDEX
1.
i)
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
TASK -1
2. Now click on the Windows(X86, 32 & 64 bit), ZIP Archive. And this will
redirect you to the next page which is shown below.
3. Then click on the Download button shown in the red box in the above video and
this will download the MySql installer. Then click and open the installer
to start the installation.
4. Read the License Agreement. Then click on I accept the license term and then
click on the next and following screen will be shown.
5. In the above image you will see some options. For customized server click on
the custom and hit the Next button. Then you will see the following screen.
Then click on the extension button to expand the given options. Select MySQL Server latest version and
click on the arrow button in the red box. Then it will look like the following. The option you choose will
come to the right side.
6. Then it will look like the image as above, we need three things to install. a)
MySql Server
b) MySql shell and c) MySql Workbench
And then click on the next button. Then click on the Execute and it will start
downloading all the things you have selected in this step.
7. Once it downloaded click on next button 3-4 times and you will see the
following page. Where it will ask you to create a password for your Sql server.
Then enter your password and confirm your password by re-entring it. Then
click on the next button. This password is used to open the MySql server.
8. Then you will see the following screen, click on the execute and it will apply
some configuration steps as shown in the image. Once it will completed, click
on next and then click on the finish.
9. MySQL has been installed successfully and now you can run SQL queries on
both command line and Workbench to store the data.
10. Read the License Agreement. Then click on I accept the license term and
then click on the next and following screen will be shown.
11.In the above image you will see some options. For customized server click on
the custom and hit the Next button. Then you will see the following screen.
Then click on the extension button to expand the given options. Select MySQL Server latest version and
click on the arrow button in the red box. Then it will look like the following. The option you choose will
come to the right side.
12. Then it will look like the image as above, we need three things to install. c)
MySql Server
d) MySql shell and
e) MySql Workbench
And then click on the next button. Then click on the Execute and it will start
downloading all the things you have selected in this step.
13. Once it downloaded click on next button 3-4 times and you will see the
following page. Where it will ask you to create a password for your Sql
server.
Then enter your password and confirm your password by re-entring it.
Then click on the next button. This password is used to open the MySql
server.
14. Then you will see the following screen, click on the execute and it will
apply some configuration steps as shown in the image. Once it will
completed, click on next and then click on the finish.
15. MySQL has been installed successfully and now you can run SQL queries
on both command line and Workbench to store the data.
TASK -2
create table student(rollNo int primary key, firstName varchar(25), lastName varchar(20), grade
varchar(3));
create table course(CId varchar(10) primary key, cName varchar(10), duration int
check(duration<6), rollNo int, foreign key(rollNo) references student(rollNo));
create table login(UserId int primary key, password varchar(25), foreign key(UserId) references
student(rollNo));
Select operations:
• insertinto CUSTOMERvalues(004,'Mr','Sham',null,null,'Sainki
Colony','Bandra','Maharashtra',120001,'EFG',5);
• create table ORDERS(Order_No int primary key, Customer_ID int ,Date, foreign
key(Customer_ID) references CUSTOMER(Customer_ID));
• insert into ORDERS values(100,001,'12-04-22');
• insert into ORDERS values(101,002,'12-04-22');
• insert into ORDERS values(102,001,'17-05-22');
• insert into ORDERS values(103,003,'22-05-22');
• insert into ORDERS values(104,005,'23-05-22');
• insert into ORDERS values(105,001,'23-05-22');
• insert into ORDERS values(106,004,'24-05-22');
• insert into ORDERS values(107,003,'12-06-22');
• insert into ORDERS values(108,002,'12-06-22');
• insert into ORDERS values(109,004,'12-06-22');
• create table STOCKS (Product_No int primary key, Description varchar (255), Catalog
varchar(255), Quantity_in_Pieces int ,Cost_per_unit int, Selling_Price_per_unit int , Order_No
int, foreign key(Order_No) references ORDERS(Order_No));
• insert into STOCKS values(213,'Dove Soap','MFG: 12-02-22 , COLOUR: Green, WEIGHT:
0.5g', 1279,75,75, 108);
• insert into STOCKS values(214,'PUMA Night Wear','MFG: 22-02-21 ,
COLOUR: Pink, Fabric: Cotton', 100,1280,1280, 107);
• insert into STOCKS values(215,'DELL Laptop','MFG: 13-04-21 , COLOUR:Steel Grey,
WEIGHT: 1899g', 7,71280,71280, 102);
• insert into STOCKS values(216,'TUPPERWARE Crockery SET','MFG: 02-01-22
, COLOUR8Green, WEIGHT: 0.5g', 50,279,279, 106);
• insert into STOCKS values(217,'Pears Soap','MFG: 12-12-21 , COLOUR:White, WEIGHT:
0.5g', 12,75,75, 103);
• insert into STOCKS values(218,'SHEENS Saree','MFG: 01-10-21 ,
COLOUR:Red, Fabric: Saturn', 1908,1200,1200, 104);
• insert into STOCKS values(219,'QUARTZ Watch','MFG: 29-08-21 ,
COLOUR:Steel Grey, WEIGHT: 0.5g', 127,2389,2389, 105);
• insert into STOCKS values(210,'AK Mens Wallet','MFG: 12-02-21 ,
COLOUR:Brown, WEIGHT: 0.9g', 279,710,710, 109);
• insert into STOCKS values(211,'MEGH Hair Accessories','MFG: 30-09-20
,
COLOUR:Magenta, WEIGHT: 0.5g', 129,75,75, 100);
• insert into STOCKS values(212,'ABC Hankercheifs','MFG: 17-12-21 , COLOUR: Green,
WEIGHT: 0.5g', 1279,75,75, 101);
1. Display the name of the customer who placed ordered more then 5 quantities of a given
productno.
TASK -4
From the following table rite a sql query to find the salesperson and customer
who live in the same city. Return customer name, salesperson name and
salesperson city.
SOLUTION:
From the following table write an sql query to find those customers who served by
salesperson and the salesperson works at the commission in the range
12% to 14% (Begin and end values included). Return cust_name AS
“Customer”, city AS “City”.
SOLUTION:
SELECT customer.cust_name as Customer, customer.city as City, salesman.name
FROM salesman as T1 INNER JOIN customer as T2 ON
T1.salesman_id=T2.salesman_id;
From the following table, write a SQL query to find those customer who made orders
on October 5, 2012. Return customer_id, cust_name, city, grade, salesman_id, ord_no,
purch_amt, ord_date, customer_id and salesman_id.
SOLUTION:
SELECT customer.customer_id, customer.cust_name, customer.city,
customer.grade, customer.salesman_id, orders.ord_no, orders.purch_amt,
orders.ord_date, orders.customer_id, orders.salesman_id
FROM salesman as T1 INNER JOIN orders as T2 ON
T1.salesman_id=T2.salesman_id
WHERE ord_date=’2012-10-5’;
Normalize the relation into different normal forms.Try and reach highest level of
normalization possible.
For example below we have one big table. Put the table in normalized form.
OID=Order ID, O_Date=Order Date,
TASK -5
Consider the following schema for a Library Database:
BOOK (Book_id, Title, Publisher_Name, Pub_Year) BOOK_AUTHORS (Book_id,
Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)
1 Write SQL queries to 1. Retrieve details of all books in the library_id, title, name
of publisher, authors, number of copies in each branch, etc .
Sol:
select BOOK.Book_id, BOOK.Title,
BOOK.Publisher_name,BOOK_AUTHOR.Author_Name, BOOK_COPIES.No_of_copies,
LIBRARY_BRANCH.Branch_id from BOOK, BOOK_AUTHORS, BOOK_COPIES,
LIBRARY_BRANCH where
BOOK.Book_id=BOOK_AUTHOR.Book_id and
BOOK.Book_id=BOOK_AUTHOR.Book_id
BOOK.Book_id=BOOK_COPIES.Book_id and
LIBRARY_BRANCH.Branch_id=BOOK_COPIES.Bracnh_id;
2 Get the particulars of borrowers who have borrowed more than three books jan
2018 to jun 2018. Sol:
Select Card_No from BOOK_LENDING where Date_Out between ‘01-JAN- 2018’ and ‘01-
jun-2018’ group by Card_No having count (*)>3;
3. Delete a book in BOOK table. Update the contents of other tables to reflect this
data manipulation operation.
Sol:
Delete from BOOK where Book_id =3;
4. Partition the BOOK table based on year of publication. Demonstrate its working
with a simple query.
Sol:
Create view V_Publication as select Pub_year from BOOK;
5. Create a view of all books and its number of copies that are currently available
in the Library.
Sol:
Create view V_BOOK as select BOOK.Book_id, BOOK.Title, BOOK_COPIES.No_of_copies
from BOOK, BOOK_COPIES, LIBRARY_BRANCH where
BOOK.Book_id=BOOK_COPIES.Book_id and
BOOK_COPIES.Branch_id=LIBRARY_BRANCH.Branch_id;
TASK -6
Consider the following schema for Order Database:
SALESMAN (Salesman_id, Name, City, Commission)
CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)
Sol:
SELECT grade, COUNT (*)
FROM customer
GROUP BY grade
HAVING grade > (SELECT AVG(grade)
FROM customer
(WHERE city = 'Amritsar');
2. Find the name and numbers of all salesmen who had more than one customer.
Sol:
SELECT salesman_id,name
FROM salesman a
WHERE 1 < (SELECT COUNT(*)
FROM customer
WHERE salesman_id=a.salesman_id);
3. List all salesmen and indicate those who have and don’t have customers in
their cities (Use UNION operation.)
Sol:
SELECT salesman.salesman_id, name, cust_name, commission
FROM salesman, customer
WHERE salesman.city = customer.city
UNION
(SELECT salesman_id, name, 'NO MATCH', commission
FROM salesman
WHERE NOT city = ANY
(SELECT city
FROM customer))
ORDER BY 2 DESC
4.Create a view that finds the salesman who has the customer with the highest order
of a day.
Sol:
CREATE VIEW elitsalesman
AS SELECT b.ord_date, a.salesman_id, a.name
FROM salesman a, orders b
WHERE a.salesman_id = b.salesman_id
AND b.purch_amt =
(SELECT MAX (purch_amt)
FROM orders c
WHERE c.ord_date = b.ord_date);
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.
Sol:
Use ON DELETE CASCADE at the end of foreign key definitions while creating child table
orders and then execute the following: Use ON DELETE SET NULL at the end of foreign key
definitions while creating child table customers and then executes the following:
DELETE FROM SALESMAN WHERE SALESMAN_ID=1000;
TASK -7
declare -- declare
variable x, y -- and z of
datatype number x
number(5); y
number(5); z
number(7);
begin
-- Assigning 20 into x
y:=20;
Output :
Sum is 30
Sql> CREATE TABLE STUDENTS (ID INT NOT NULL, NAME VARCHAR (20) NOT
NULL, PRIMARY KEY (ID) );
ID Name
1 AMAN
2 BHUPINDER
4) To display number of records updated using rowcount attribute in PL/SQL
DECLARE
i NUMBER;
BEGIN
UPDATE employees
SET status = 'fired'
WHERE name LIKE '%Bloggs';
i := SQL%rowcount;
--note that assignment has to precede COMMIT
COMMIT;
dbms_output.Put_line(i);
END;
DECLARE
CURSOR cur_emp
IS
SELECT * FROM doctor;
rec_emp cur_emp%rowtype;
BEGIN
OPEN cur_emp;
LOOP
FETCH cur_emp INTO rec_emp;
UPDATE employees
SET salary = salary + 1000
EXIT
WHEN cur_emp%notfound;
END LOOP;
CLOSE cur_emp;
END;
/
1. DECLARE
2. c_id customers.id%type := 8;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;
17. /
TASK 8
Create students table with ID and NAME and then rename the table to csestudents
Write the command to DROP the csestudents table.
CREATE TABLE STUDENTS (ID INT NOT NULL, NAME VARCHAR (20) NOT
NULL, PRIMARY KEY (ID) );
RENAME TABLE STUDENTS TO CSESTUDENTS;
DROP TABLE CSESTUDENTS;
TASK 9
Implement nested query
Write SQL query to display the names of all students who got more then average GPA
Sql> SELECT * FROM students WHERE GPA > (SELECT AVG(GPA) FROM students);
Consider employee table
Aggregate Function
TASK -10
Display the name of the company and count for that company only if count is greater than 2.
1. SELECT COMPANY, COUNT(*)
2. FROM PRODUCT_MAST
3. GROUP BY COMPANY
4. HAVING COUNT(*)>2;
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
TASK -11
Like function
Consider the following students table
id name class_id GPA
1 Jack Black 3 3.45
2 Daniel White 1 3.15
3 Kathrine Star 1 3.85
4 Helen Bright 2 3.10
5 Steve May 2 2.40
Write sql statement to display name of all students starting with ‘h’
Sql> SELECT * FROM students
WHERE name LIKE 'a%';
TASK -12
Practical- Join
Consider the following orders and customers table
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Write SQL statement using INNER JOIN that selects records that have matching values in both
tables
Write SQL statement using LEFT JOIN that will select all customers, and any orders they might
have:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM
Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
CREATE TABLE STUDENTS (ID INT NOT NULL, NAME VARCHAR (20) NOT
NULL, PRIMARY KEY (ID) ); CREATE
SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;
Create a user and then grant some rights to the user on student table and also revoke some of the
rights granted to user
BEGIN
VAR1:=200;
VAR2:=1;
WHILE (VAR2<=5)
LOOP
DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
VAR2:=VAR2+1;
END LOOP;
END;
OUTPUT:-
200
400
600
800
1000
Parameters Modes:-
IN mode- Default; Passes a constant value from the calling environment to the procedure
Create or replace procedure raise_salary
(p_id IN employees.emp_id%type)
IS
Begin
Update employees set
salary=salary*0.10
where emp_id=p_id
End raise_salary;
An exception is an error which disrupts the normal flow of program instructions. PL/SQL
provides us the
exception block which raises the exception thus helping the programmer to find out the fault
and resolve it.
There are two types of exceptions defined in PL/SQL
1. User defined exception.
2. System defined exceptions.
Syntax :-
WHEN exception THEN
Statement;
Consider the table STUDENT :-
ID NAME MARKS
1 AKSHAY 100
2 PRAVEEN 97
3 JESSIE 99
DECLARE
temp varchar(20);
BEGIN
SELECT ID into temp from STUDENT where NAME='RAHUL';
exception
WHEN no_data_found THEN
dbms_output.put_line('ERROR:');
dbms_output.put_line('there is no name as');
dbms_output.put_line(' RAHUL in STUDENT table');
end;
OUTPUT:-
ERROR: There is no name as RAHUL in STUDENT table
Task 18
Triggers and Cursor Management in PL/SQL.
1. Triggers:
o A trigger is a stored procedure in the database that is automatically
executed in response to specific events, such as INSERT, UPDATE, or
DELETE on a table.
o Triggers are useful for enforcing business rules, auditing changes, or
maintaining complex integrity constraints.
2. Cursors:
o A cursor is a pointer that allows you to retrieve, manipulate, and iterate
over query results row by row.
o Two types of cursors:
Implicit Cursor: Created automatically when a SELECT, INSERT,
UPDATE, or DELETE operation is executed.
Explicit Cursor: Defined explicitly by the programmer for complex
operations.
DECLARE
CURSOR cursor_name IS
SELECT column1, column2 FROM table_name;
variable1 table_name.column1%TYPE;
variable2 table_name.column2%TYPE;
BEGIN
OPEN cursor_name;
LOOP
FETCH cursor_name INTO variable1, variable2;
EXIT WHEN cursor_name%NOTFOUND;
-- Process fetched data
END LOOP;
CLOSE cursor_name;
END;
Create trigger to update logs to the EMPLOYEES table into an AUDIT_LOG table.
Tables:
1. EMPLOYEES(emp_id, emp_name, salary)
2. AUDIT_LOG(audit_id, emp_id, old_salary, new_salary, change_date)
Trigger Code:
sql
Create a cursor to calculate the average salary from the EMPLOYEES table and logs
employees earning above average into a HIGH_SALARY_EMPLOYEES table.
Tables:
1. EMPLOYEES(emp_id, emp_name, salary)
2. HIGH_SALARY_EMPLOYEES(emp_id, emp_name, salary)
Cursor Code:
sql
DECLARE
CURSOR emp_cursor IS
SELECT emp_id, emp_name, salary FROM EMPLOYEES;
avg_salary NUMBER;
emp_id EMPLOYEES.emp_id%TYPE;
emp_name EMPLOYEES.emp_name%TYPE;
salary EMPLOYEES.salary%TYPE;
BEGIN
-- Calculate average salary
SELECT AVG(salary) INTO avg_salary FROM EMPLOYEES;
-- Process each employee
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_id, emp_name, salary;
EXIT WHEN emp_cursor%NOTFOUND;