0% found this document useful (0 votes)
5 views47 pages

DBMS Printout 1 To 11 J 14 J15

The document outlines the creation and manipulation of several SQL tables related to persons, cars, accidents, employees, departments, salesmen, customers, and paintings. It includes commands for creating tables with primary and foreign keys, inserting data, updating records, and performing various SQL queries to retrieve and analyze data. The document also demonstrates data integrity constraints and relationships between different entities in a database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views47 pages

DBMS Printout 1 To 11 J 14 J15

The document outlines the creation and manipulation of several SQL tables related to persons, cars, accidents, employees, departments, salesmen, customers, and paintings. It includes commands for creating tables with primary and foreign keys, inserting data, updating records, and performing various SQL queries to retrieve and analyze data. The document also demonstrates data integrity constraints and relationships between different entities in a database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

PROGRAM 01 :

i) CREATE THE ABOVE TABLE USING BY PROPERLY SPECIFYING THE PRIMARY


KEYS AND FOREIGN KEYS
CREATE TABLE Person(
driver_id INT PRIMARY KEY,
name VARCHAR(50),
address VARCHAR(100),
phonenumber int UNIQUE
);
Table PERSON created.
DESC Person;
CREATE TABLE Car(
registration_no INT PRIMARY KEY,
model_year INT,
CHECK (model_year>2020)
);
Table CAR created.
DESC Car;
CREATE TABLE Accident(
report_number INT PRIMARY KEY,
accident_date DATE NOT NULL,
location VARCHAR(100)
);
Table ACCIDENT created.
DESC Accident;
CREATE TABLE Participated(
driver_id INT REFERENCES Person(driver_id),
registration_no INT REFERENCES Car(registration_no),
report_number INT REFERENCES Accident(report_number),
damage_amount INT
);
Table PARTICIPATED created.
DESC Participated;

ii)ENTER AT LEAST FIVE TUPLES FOR EACH RELATION :


INSERT INTO Person VALUES(10,'Susila','R.R.Nagar',8903183013);
1 row inserted.
INSERT INTO Person VALUES(20,'Arun','k.k.R.Nagar',9787121369);
1 row inserted.
INSERT INTO Person VALUES(11,'Susmitha','v.R.Nagar',8901383013);
1 row inserted.
INSERT INTO Person VALUES(25,'Ram','k.S.R.Nagar',9787121963);
1 row inserted.
INSERT INTO Person VALUES(15,'Ravi','MANI Nagar',9080243041);
1 row inserted.
INSERT INTO Person VALUES(16,'Vasu','R.S Nagar',9080293041);
1 row inserted.
Select * Person;
INSERT INTO Car VALUES(552,'TESLA',2050);
1 row inserted.
INSERT INTO Car VALUES(342,'BMW',2025);
1 row inserted.
INSERT INTO Car VALUES(52,'AUDI',2023);
1 row inserted.
INSERT INTO Car VALUES(222,'TATA',2035);
1 row inserted.
INSERT INTO Car VALUES(152,'SUZUKI',2024);
1 row inserted.
INSERT INTO Car VALUES(452,'HONDA',2024);
1 row inserted.
SELECT *FROM Car;
INSERT INTO Accident VALUES(54,'08-DEC-2025','Mathi Street');
1 row inserted.
INSERT INTO Accident VALUES(14,'18-MAY-2021','Periyar Street');
1 row inserted.
INSERT INTO Accident VALUES(26,'25-NOV-2031','Bharathi Street');
1 row inserted.
INSERT INTO Accident VALUES(05,'10-AUG-2028','Kamaraj Street');
1 row inserted.
INSERT INTO Accident VALUES(15,'28-SEP-2034','A.S.R Street');
1 row inserted.
INSERT INTO Accident VALUES(12,'28-JAN-2034','V.S.R Street');
1 row inserted.
SELECT *FROM Accident;
INSERT INTO Participated VALUES(10,552,54,30000);
1 row inserted.
INSERT INTO Participated VALUES(20,342,14,25000);
1 row inserted.
INSERT INTO Participated VALUES(11,52,26,12000);
1 row inserted.
INSERT INTO Participated VALUES(25,222,05,58000);
1 row inserted.
INSERT INTO Participated VALUES(15,152,15,25000);
1 row inserted.
INSERT INTO Participated VALUES(16,452,12,76000);
1 row inserted.
SELECT *FROM Participated;

iii) UPDATE THE DAMAGE_AMOUNT FOR THE CAR WITH SPECIFIC REGNO IN THE
ACCIDENT WITH REPORT_NUMBER 12 TO 25000
UPDATE Participated SET damage_amount=25000 WHERE report_number=12;
1row updated.
SELECT *FROM Participated;

iv) UPDATE THE MODEL_YEAR BASED ON REGISTRATION_NUMBER


UPDATE Car SET model_year=2050 where registration_no=152;
1 row updated.
SELECT * from Car;

v) ADD A NEW ACCIDENT TO THE DATABASE


INSERT INTO Accident VALUES(53,'7-JUL-2054','V.V.V Nager');
1 row inserted.
SELECT *FROM Accident;

vi) DELETE THE DETAILS OF ACCIDENT WITH THE REPORT NUMBER 15


DELETE FROM Participated WHERE report_number=15;
1row deleted.
DELETE FROM Accident WHERE report_number=15;
1row deleted.
SELECT*FROM Accident;

OUTPUT :

RESULT:
PROGRAM 02 :

i) Create the following tables by properly specfiying primay key and foreign key.
Employee_Details(Emp_no, Name, DOB, Address, DOJ, Mobile_Number, Department_no,
Salary)
Department_Details(Department_no,Department_name,Location)
CREATE TABLE department(
department_no int primary key,
department_name varchar(100),
location varchar(100));
Table DEPARTMENT created.
DESC department;
CREATE TABLE employee(
emp_no int primary key,
name varchar(100),
dob date,
address varchar(100),
doj date,
mobilenumber int,
department_no REFERENCES department(department_no) on DELETE CASCADE,
salary int
);
Table EMPLOYEE created.
DESC Employee;

ii) Insert five tuples in each table.


INSERT INTO department VALUES(101,'marketing','coimbatore');
1 row inserted.
INSERT INTO department VALUES(102,'human resource','Chennai');
1 row inserted.
INSERT INTO department VALUES(103,'Production','coimbatore');
1 row inserted.
INSERT INTO department VALUES(104,'finance','pondicherry');
1 row inserted.
INSERT INTO department VALUES(105,'management','chennai');
1 row inserted.
SELECT * From Department ;
INSERT INTO employee VALUES(01,'diya','3-dec-1989','gandhi street','1-
jan2009',1234567869,101,25000);
1 row inserted.
INSERT INTO employee VALUES(02,'santhosh','23-jun-1989','mayor street','2-
feb2005',7878646472,102,30000);
1 row inserted.
INSERT INTO employee VALUES(03,'mani','9-aug-1990','kamaraj street','14-
sep2001',9988776655,103,45000);
1 row inserted.
INSERT INTO employee VALUES(04,'vivek','16-apr-1992','k.k.street','15-
may2005',7565554535,105,55000);
1 row inserted.
INSERT INTO employee VALUES(05,'jeya','5-may-1980','ck nagar','30-
jun2007',8903183013,106,70000);
INSERT INTO employee VALUES(05,'jeya','5-may-1980','ck nagar','30-
jun2007',8903183013,106,70000)
Error report -
ORA-02291: integrity constraint (NANTHINI.SYS_C007097) violated - parent key not found
SELECT * FROM employee;

iii) Select department_no from department_details and not in Employee_details using both
tables.

SELECT department_no from department minus select department_no FROM


employee;
iv) Find Location,Employee_name from department and employee table whose department_no
is same in both table.
SELECT department.location,employee.name FROM department inner join
employee on department.department_no=employee.department_no;

v) Delete the details of department_no=101 .


delete from department where department_no=101;
1 row deleted.
SELECT * FROM department;
SELECT * FROM employee;
OUTPUT :

RESULT :
PROGRAM 03 :

CREATION & INSERTION OF VALUES INTO THE TABLE


a. Salesman{salesman_id,name,city,commission}
b. Customer{customer_id,cust_name,city,grade,salesman_id}
c. Orders{ ord_no,purch_amt,ord_date,customer_id,salesman_id}
CREATE TABLE salesman(
salesman_id int primary key,
sname varchar(100),
city VARCHAR(100),
commission decimal);
Table SALESMAN created.
DESC salesman;
CREATE TABLE customer(
customer_id int primary key,
cust_name varchar(100),
city varchar(100),
grade varchar(100),
salesman_id int references salesman(salesman_id));
Table CUSTOMER created.
DESC customer;
CREATE TABLE orders(
ord_no int,
purch_amt int,
ord_date date,
customer_id int references customer(customer_id),
salesman_id int references salesman(salesman_id));
Table ORDERS created.
DESC orders;
INSERT INTO Salesman VALUES(15,'anu','Paris',0.15);
1 row inserted.
INSERT INTO Salesman VALUES(22,'lathika','Rome',0.12);
1 row inserted.
INSERT INTO Salesman VALUES(53,'vasuki','New york',0.53);
1 row inserted.
INSERT INTO Salesman VALUES(23,'vathanie','Landon',0.13);
1 row inserted.
INSERT INTO Salesman VALUES(07,'dharani','Paris',0.07);
1 row inserted
SELECT * from Salesman;
INSERT INTO Customer VALUES(30,'Nivi','Rome',100,15);
1 row inserted.
INSERT INTO Customer VALUES(31,'Harini','Paris',300,22);
1 row inserted.
INSERT INTO Customer VALUES(32,'Briya','London',200,53);
1 row inserted.
INSERT INTO Customer VALUES(33,'Vishu','New york',200,23);
1 row inserted.
INSERT INTO Customer VALUES(34,'Kaviya','Mexican',300,07);
1 row inserted.
SELECT * FROM Customer;
INSERT INTO Orders VALUES(101,10.1,'01-aug-2020',30,15);
1 row inserted.
INSERT INTO Orders VALUES(102,10.2,'10-jun-2021',31,22);
1 row inserted.
INSERT INTO Orders VALUES(103,10.5,'27-sep-2020',32,53);
1 row inserted.
INSERT INTO Orders VALUES(104,10.6,'25-apr-2020',33,23);
1 row inserted.
INSERT INTO Orders VALUES(105,10.7,'17-dec-2021',34,07);
1 row inserted.
SELECT * from Orders;
i. Write a SQL statement to find those salesmen with all information who come from the city
either Paris or Rome
SELECT * FROM salesman WHERE city IN ('Paris','Rome');
ii. Write a query to produce a list of salesman_id, name, city and commision of each salesman
who live in cities other than Paris and Rome
SELECT * FROM salesman WHERE city NOT IN ('Paris','Rome');
iv. Write a SQL statement to find that customer with all information whose names begin with
the letter 'B'.
SELECT * FROM customer WHERE cust_name LIKE 'B%';
v. Write a SQL statement to prepare a list with salesman name, customer name and their cities
for the salesmen and customer who belongs to the same city.
SELECT salesman.sname AS "Salesman",customer.cust_name, customer.city FROM
salesman,customer WHERE salesman.city=customer.city;
vi. Write a SQL statement to make a list in ascending order for the salesmen who works either
for one or more customer or not yet join under any of the customers.
SELECT a.cust_name,a.city,a.grade, b.sname AS "Salesman", b.city FROM customer a RIGHT
OUTER JOIN salesman b ON b.salesman_id=a.salesman_id ORDER BY b.salesman_id DESC;
vii. Write a query to display the orders according to the order number arranged by ascending
order.
SELECT * FROM orders ORDER BY ord_no ;
book(book_name,author_name,price,quantity).
CREATE TABLE Book(
Book_name VARCHAR(100),
Author_name VARCHAR(100),
Price INT,
Quantity INT);
Table BOOK created.
DESC book;
INSERT INTO Book VALUES('TOC','Arivu',2500,60);
1 row inserted.
INSERT INTO Book VALUES('DBMS','Xamen',5000,37);
1 row inserted.
INSERT INTO Book VALUES('AI','Mark',2000,50);
1 row inserted.
INSERT INTO Book VALUES('ALGORITHM','Crypto',500,47);
1 row inserted.
INSERT INTO Book VALUES('EVS','Giri',250,35);
1 row inserted.
SELECT * from Book;
i. Write a query to find out the minimum quantity of books available in the table book.
SELECT MIN(Quantity) FROM Book;
ii. Write a query to find the total price of all the books present in the table.
SELECT SUM (Price) FROM Book;
iii. Write a query to find the average amount of all the books.
SELECT AVG(Price) FROM Book;
iv. Write a query to find out the maximum quantity of books available in the table book.
SELECT MAX(Quantity) FROM Book;
v. Write a query to find out the number of books in the table book.
SELECT SUM(Quantity) FROM Book;
vi. List all the book_name whose price is greater than Rs.400.
SELECT Book_name FROM Book WHERE Price > 400;
vii. Retrieve the list of author_name whose first letter is ’a’ along with the book_name and price.
SELECT * FROM Book WHERE Author_name LIKE 'A%';

OUTPUT :
RESULT :
PROGRAM 04 :

Create the following tables by specifying proper primary key and foreign key
Paintings(id,name,artist_id,listed_price)
Artists(id,first_name,last_name)
Collectors(id,first_name,last_name)
Sales(id,date,painting_id,artist_id,collector_id,sales_price)
CREATE TABLE Artists(
id INT PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255)
);
Table ARTISTS created.
DESC Artists;
CREATE TABLE Collectors(
id INT PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255) );
Table COLLECTORS created.
DESC Collectors;
CREATE TABLE Paintings(
id INT PRIMARY KEY,
name VARCHAR(255),
artist_id INT,
listed_price DECIMAL(10,2),
FOREIGN KEY (artist_id) REFERENCES Artists(id)
);
Table PAINTINGS created.
DESC Paintings;
CREATE TABLE Sales(
id INT PRIMARY KEY,
dates DATE,
painting_id INT,
artist_id INT,
collector_id INT,
sales_price DECIMAL(10,2),
FOREIGN KEY (painting_id) REFERENCES Paintings(id) on delete cascade,
FOREIGN KEY (artist_id) REFERENCES Artists(id)on delete cascade,
FOREIGN KEY (collector_id) REFERENCES Collectors(id) on delete cascade
);
Table SALES created.
DESC Sales;
insert into Artists values(1,'Sri','vidhya');
1 ROW INSERTED.
insert into Artists values(2,'Laura','madhavan');
1 ROW INSERTED.
insert into Artists values(3,'Nanthini','devi');
1 ROW INSERTED.
insert into Artists values(4,'Vasuki','elango');
1 ROW INSERTED.
insert into Artists values(5,'Mathesh','kumar');
1 ROW INSERTED.
select *from Artists;
insert into Paintings values(11,'nature',1,300.00);
1 ROW INSERTED.
insert into Paintings values(12,'dragons',1,700.00);
1 ROW INSERTED.
insert into Paintings values(13,'landscape',2,2800.00);
1 ROW INSERTED.
insert into Paintings values(14,'potrait',2,2300.00);
1 ROW INSERTED.
insert into paintings values(19,'cartoon',5,3000.00);
1 ROW INSERTED.
insert into Paintings values(15,'anime',3,250.0);
1 ROW INSERTED.
insert into Paintings values(16,'clouds',3,5000.00);
1 ROW INSERTED.
insert into Paintings values(17,'chess',3,50.00);
1 ROW INSERTED.
insert into Paintings values(18,'shakespeare',4,1300.00);
1 ROW INSERTED.
select *from Paintings;
insert into Collectors values(101,'Jay','witson');
1 ROW INSERTED.
insert into Collectors values(102,'Ferin','mark');
1 ROW INSERTED.
insert into Collectors values(103,'Senif','camone');
1 ROW INSERTED.
insert into Collectors values(104,'laura','fisher');
1 ROW INSERTED.
select *from collectors;
insert into Sales values(1001,'11-01-2021',13,2,104,12500.00);
1 ROW INSERTED.
insert into Sales values(1002,'12-02-2023',14,2,104,2300.00);
1 ROW INSERTED.
insert into Sales values(1003,'03-04-2021',11,1,102,300.00);
1 ROW INSERTED.
insert into Sales values(1004,'04-12-2020',16,3,103,14000.00);
1 ROW INSERTED.
insert into Sales values(1005,'12-02-2023',15,3,103,11200.00);
1 ROW INSERTED.
insert into Sales values(1006,'30-03-2022',17,3,103,50.00);
1 ROW INSERTED.
select *from Sales;
i. List paintings that are priced higher then the average.
SELECT name,listed_price FROM paintings WHERE listed_price > (select
avg(listed_price) from paintings);
ii. List all collectors who purchased paintings from our gallery.
SELECT first_name,last_name from collectors where id in (select collector_id from
sales);
iii. Total number of sales for each artists who has sold at least one painting in our gallery.
select a.first_name,sum(s.sales_price)
from artists a
join sales s
on a.id=s.artist_id
group by a.first_name
having count(s.sales_price)> 0;
iv. Calculate the number of paintings purchased enough our gallery for each
collector.
SELECT first_name,last_name,(SELECT count(*) AS paintings FROM sales
WHERE collectors.id = sales.collector_id) as count_painting FROM collectors;
v. Show the first name and the last name of the artists who had zero sales with our gallery.
SELECT first_name, last_name FROM artists WHERE NOT EXISTS (SELECT *
FROM sales WHERE sales.artist_id = artists.id);
vi. Find the total number of paintings which were and drawn the artists with the id=12.
SELECT COUNT(*) as Drawn_by_12 FROM paintings p JOIN artists a ON
p.artist_id = a.id WHERE a.id = 12;
vii. Display the first_name and last_name of collectors in both collectors and sales table.
SELECT Collectors.first_name, Collectors.last_name FROM Collectors JOIN Sales
ON Collectors.id = Sales.collector_id;
viii. Find the paintings which were drawn by “Laura” on the date “12.02.2023”.
SELECT * FROM sales s JOIN artists a ON s.artist_id = a.id WHERE a.first_name =
'Laura' AND s.dates = '12.02.2023';
ix. List the artists whose painting’s sales price is more then 10000.
SELECT artists.first_name,artists.last_name, sales.sales_price FROM artists INNER
JOIN sales ON artists.id = sales.artist_id WHERE sales.sales_price > 10000;
x. Find the Average sales price of artists where the average sales price is greater then 12000.
SELECT artists.id,AVG(sales.sales_price)
FROM sales
JOIN artists ON artists.id=sales.artist_id
GROUP BY artists.id
HAVING AVG(sales.sales_price)>1200;
xi. Display artists name,painting name and it’s sales price.
SELECT artists.first_name,artists.last_name, paintings.name, paintings.listed_price
FROM artists INNER JOIN paintings ON artists.id = paintings.artist_id;
xii. Display Paintings id,Sales id and Sales price.
SELECT painting_id, sales_price,sales.id as sales_id FROM sales INNER JOIN
paintings ON sales.painting_id = paintings.id;
xiii. Delete all the records of paintings whose sales price is less then 1000.
DELETE paintings,sales from paintings inner join sales on
paintings.id=sales.painting_id where sales.sales_price<1000;
4 rows deleted.
SELECT * FROM Paintings;
xiv. Create a duplicate copy of artists table with the name artist details.
CREATE table artist_details as select * from artists;
Table ARTIST_DETAILS created.
SELECT * from artist_details;
xv. Give a 2percent listed price raise to the paintings whose listed price is less than the average.
UPDATE Paintings SET listed_price = listed_price * 1.02 WHERE listed_price <
(SELECT AVG(listed_price) FROM Paintings);
3rows updated.
SELECT * from paintings;

OUTPUT :
RESULT :
PROGRAM 05 :
Create Table DEPARTMENT(DEPT_NAME Varchar(20),MANAGER_NAME Varchar(255));
Create Table EMPLOYEE(EMP_ID int,EMP_NAME Varchar(20),DEPT_NAME Varchar(255));
Create table student(reg_no int(3) primary key,sname varchar(20));
Create table course(cid int(3) primary key, cname varchar(20),s_id int(3), foreign key (s_id)
references student(reg_no));
INSERT INTO DEPARTMENT VALUES ( "IT", "ROHAN"),( "SALES", "RAHUL"),( "HR",
"TANMAY"),( "FINANCE", "ASHISH"),("MARKETING", "SAMAY");
INSERT INTO EMPLOYEE VALUES (1, "SUMIT", "HR"),(2, "JOEL", "IT"),(3, "BISWA",
"MARKETING"),(4, "VAIBHAV", "IT"),(5, "SAGAR", "SALES");
INSERT INTO STUDENT values(1,’AAA’),(2,’BBB’),(3,’CCC’),(4,’DDD’),(5,’EEE’);
INSERT INTO COURSE VALUES (101,’Intro to JAVA’,3),(102,’Data Science’,1),(103,’AI &
ML’,4), (104,’Python’,3);

1. Natural Join
SELECT * FROM EMPLOYEE NATURAL JOIN DEPARTMENT;
2. Equi join
select * from student s join course c where s.reg_no = c.s_id;
3. Outer join
i. Left outer join
SELECT * FROM DEPARTMENT d LEFT OUTER JOIN EMPLOYEE e ON d.DEPT_NAME =
e.DEPT_NAME;
ii. Right outer join
SELECT * FROM DEPARTMENT d RIGHT OUTER JOIN EMPLOYEE e ON d.DEPT_NAME =
e.DEPT_NAME;
iii. Full outer join
SELECT * FROM DEPARTMENT d LEFT OUTER JOIN EMPLOYEE e ON d.DEPT_NAME =
e.DEPT_NAME UNION SELECT * FROM DEPARTMENT d RIGHT OUTER JOIN EMPLOYEE e
ON d.DEPT_NAME
OUTPUT :

RESULT :
PROGRAM 06 :
Procedure Implementation
Creating the table ‘product_items’ and displaying the contents
SQL> create table product_items(itemid number(3), actualprice number(5), ordid number(4), prodid
number(4));
Table created.
SQL> insert into product_items values(101, 2000, 500, 201);
1 row created.
SQL> insert into product_items values(102, 3000, 1600, 202);
1 row created.

SQL> insert into product_items values(103, 4000, 600, 202); 1 row created.
SQL> select * from product_items;
Program for general procedure – selected record’s price is incremented by 500, executing the
procedure created and displaying the updated table
SQL> create procedure itsum(identity number, total number) is price number;
2 null_price exception;
3 begin
4 select actualprice into price from product_items where itemid=identity;
5 if price is null then
6 raise null_price;
7 else
8 update product_items set actualprice=actualprice+total where itemid=identity;
9 end if;
10 exception when null_price then
12 dbms_output.put_line('price is null');
13 end;
14 /
Procedure created.

Procedure for ‘in’ parameter – creation, execution


SQL> set serveroutput on;
SQL> create procedure yyy (a IN number) is price number;
2 begin
3 select actualprice into price from product_items where itemid=a;
4 dbms_output.put_line('Actual price is ' || price);
5 if price is null then
6 dbms_output.put_line('price is null');
7 end if;
8 end;
9/
Procedure created.
SQL> exec yyy(103);
Procedure for ‘out’ parameter – creation, execution
SQL> set serveroutput on;
SQL> create procedure zzz (a in number, b out number) is identity number;
2 begin
3 select ordid into identity from product_items where itemid=a;
4 if identity<1000 then
5 b:=100;
6 end if;
7 end;
8/
Procedure created.
SQL> declare
2 a number;
3 b number;
4 begin
5 zzz(101,b);
6 dbms_output.put_line('The value of b is '|| b);
7 end;
8/
Procedure for ‘inout’ parameter – creation, execution
SQL> create procedure itit ( a in out number) is
2 begin
3 a:=a+1;
4 end;
5/
Procedure created.
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
5 dbms_output.put_line(„The updated value is „||a);
6 end;
7/
Function Implementation
Create the table ‘train’ to be used for functions
SQL>create table train ( tno number(10), tfare number(10));
Table created.
SQL>insert into train values (1001, 550);
1 row created.
SQL>insert into train values (1002, 600);
1 row created.
SQL>select * from train;
To create the table ‘itempls’
SQL> create table employee (ename varchar2(10), eid number(5), salary number(10));
Table created.
SQL> insert into employee values('xxx',11,10000);
1 row created.
SQL> insert into employee values('yyy',12,10500);
1 row created.
SQL> insert into employee values('zzz',13,15500);
1 row created.
SQL> select * from employee;
Program for function and it’s execution
SQL> create function aaa (trainnumber number) return number is
2 Train function train.tfare % type;
3 begin
4 select tfare into trainfunction from train where tno=trainnumber;
5 return(trainfunction);
6 end;
7/
Function created.
SQL> set serveroutput on;
SQL> declare
2 total number;
3 begin
4 total:=aaa (1001);
5 dbms_output.put_line('Train fare is Rs. '||total);
6 end;
7/
Train fare is Rs.550
PL/SQL procedure successfully completed.
Factorial of a number using function — program and execution
SQL> create function fact (a number) return number is
2 fact number:=1;
3 b number;
4 begin
5 b:=a;
6 while b>0
7 loop
8 fact:=fact*b;
9 b:=b-1;
10 end loop;
11 return(fact);
12 end;
13 /
Function created.
SQL> set serveroutput on;
SQL> declare
2 a number:=7;
3 f number(10);
4 begin
5 f:=fact(a);
6 dbms_output.put_line(„The factorial of the given number is‟||f);
7 end;
8/
OUTPUT :

ITEMID ORDID PRODID


ACTUALPRIC
E
101 2000 500 201
102 3000 1600 202
103 4000 600 202

SQL> exec itsum(101, 500); PL/SQL procedure


successfully completed.
ITEMID ACTUAL ORDID PRODID
PRICE
101 2500 500 201
102 3000 1600 202
103 4000 600 202
SQL> exec yyy(103);
Actual price is 4000
PL/SQL procedure successfully completed.
The value of b is 100
PL/SQL procedure successfully completed.
The updated value is 8
PL/SQL procedure successfully completed.

ENAME EID SALARY


---------- --------- ---------
Xxx 11 10000
yyy 12 10500
zzz 13 15500
Train fare is Rs.550
PL/SQL procedure successfully completed
The factorial of the given number is 5040
PL/SQL procedure successfully completed.

RESULT :
PROGRAM 07 :
DCL COMMANDS
mysql> create database dcl;
Query OK, 1 row affected (0.03 sec)
mysql> use dcl;
Database changed
mysql> show databases;
mysql> create user tom;
Query OK, 0 rows affected (0.07 sec)
mysql> create user john;
Query OK, 0 rows affected (0.02 sec)
mysql> create user allison;
Query OK, 0 rows affected (0.03 sec)
mysql> create table student(roll_no int(255),name varchar(100),branch varchar(100),age int(255));
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> insert into student values(10,'anitha','CSE',14);
Query OK, 1 row affected (0.01 sec)
mysql> insert into student values(14,'bavani','ECE',15);
Query OK, 1 row affected (0.03 sec)
mysql> insert into student values(16,'vikranth','EEE',16);
Query OK, 1 row affected (0.01 sec)
mysql> insert into student values(17,'yamini','civil',18);
Query OK, 1 row affected (0.03 sec)
mysql> insert into student values(19,'krishna','mech',20);
Query OK, 1 row affected (0.03 sec)
mysql> select * from student;
mysql> GRANT all on student to allison;
Query OK, 0 rows affected (0.02 sec)
mysql> show grants for allison;
mysql> GRANT select,insert,update on student to john;
mysql> show grants for john;
mysql> GRANT select,insert,update,delete on student to tom;
Query OK, 0 rows affected (0.03 sec)
mysql> show grants for tom;
mysql> REVOKE select, update on student from john;
Query OK, 0 rows affected (0.03 sec)
mysql> show grants for john;
mysql> REVOKE delete on student from tom;
Query OK, 0 rows affected (0.02 sec)
mysql> show grants for tom;
mysql> REVOKE all on student from allison;
Query OK, 0 rows affected (0.03 sec)
mysql> show grants for allison;
TCL COMMANDS
mysql> drop database account_details;
Query OK, 1 row affected (0.02 sec)
mysql> create database account_details;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
mysql> use account_details;
Database changed
mysql> create table account_details(account_no int(50),customer_name varchar(100),address
varchar(100),phone_no int(50),amount decimal(50));
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> insert into account_details values(101,'raj','kk nagar',56789,20000);
Query OK, 1 row affected (0.01 sec)
mysql> insert into account_details values(102,'ravi','mgr nagar',59713,10000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into account_details values(103,'selvam','royal nagar',89436,17500);
Query OK, 1 row affected (0.00 sec)
mysql> insert into account_details values(104,'ananthi','transport nagar',94682,35000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into account_details values(105,'jansi','padma nagar',67823,10900);
Query OK, 1 row affected (0.00 sec)
mysql> select*from account_details;
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into account_details values(106,'rajaboopathi','jk nagar',90807,28000);
Query OK, 1 row affected (0.00 sec)
mysql> savepoint stu1;
Query OK, 0 rows affected (0.00 sec)
mysql> select*from account_details;
mysql> update account_details set amount=19000 where account_no=104;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> savepoint stu2;
Query OK, 0 rows affected (0.00 sec)
mysql> select*from account_details;
mysql> delete from account_details where account_no=105;
Query OK, 1 row affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from account_details where account_no=105;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint stu3;
Query OK, 0 rows affected (0.00 sec)
mysql> select*from account_details;
mysql> ROLLBACK TO SAVEPOINT stu3;
Query OK, 0 rows affected (0.00 sec)
mysql> select*from account_details;

OUTPUT :
+--------------------+
| Database |
+--------------------+
| dcl |
| user1 |
| user2 |
| user3 |
| world |
+--------------------+
mysql> select * from student;
+---------+----------+--------+------+
| roll_no | name | branch | age |
+---------+----------+--------+------+
| 10 | anitha | CSE | 14 |
| 14 | bavani | ECE | 15 |
| 16 | vikranth | EEE | 16 |
| 17 | yamini | civil | 18 |
| 19 | krishna | mech | 20 |
mysql> show grants for allison;
+------------------------------------------------------+
| Grants for allison@% |
+------------------------------------------------------+
| GRANT USAGE ON *.* TO `allison`@`%` |
| GRANT ALL PRIVILEGES ON `dcl`.`student` TO `allison`@`%` |
+------------------------------------------------------+
+---------------------------------------------------------------+
| Grants for john@% |
+---------------------------------------------------------------+
| GRANT USAGE ON *.* TO `john`@`%` |
| GRANT SELECT, INSERT, UPDATE ON `dcl`.`student` TO `john`@`%` |
+---------------------------------------------------------------+
2 rows in set (0.02 sec)
+-----------------------------------------------------------------------+
| Grants for tom@% |
+-----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `tom`@`%` |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `dcl`.`student` TO ` tom `@`%` |
+-----------------------------------------------------------------------+
2 rows in set (0.00 sec)
+----------------------------------+
| Grants for john@% |
+----------------------------------+
| GRANT USAGE ON *.* TO `john`@`%` |
| GRANT INSERT ON `dcl`.`student` TO `john`@`%` |
+----------------------------------+
1 row in set (0.00 sec)
+----------------------------------+
| Grants for tom@% |
+----------------------------------+
| GRANT USAGE ON *.* TO ` tom`@`%` |
| GRANT SELECT, INSERT, UPDATE ON `dcl`.`student` TO ` tom `@`%` |
+----------------------------------+
1 row in set (0.00 sec)
+------------+---------------+-----------------+----------+--------+
| account_no | customer_name | address | phone_no | amount |
+------------+---------------+-----------------+----------+--------+
| 101 | raj | kk nagar | 56789 | 20000 |
| 102 | ravi | mgr nagar | 59713 | 10000 |
| 103 | selvam | royal nagar | 89436 | 17500 |
| 104 | ananthi | transport nagar | 94682 | 19000 |
| 106 | rajaboopathi | jk nagar | 90807 | 28000 |
+------------+---------------+-----------------+----------+--------+
5 rows in set (0.00 sec)
+------------+---------------+-----------------+----------+--------+
| account_no | customer_name | address | phone_no | amount |
+------------+---------------+-----------------+----------+--------+
| 101 | raj | kk nagar | 56789 | 20000 |
| 102 | ravi | mgr nagar | 59713 | 10000 |
| 103 | selvam | royal nagar | 89436 | 17500 |
| 104 | ananthi | transport nagar | 94682 | 19000 |
| 105 | jansi | padma nagar | 67823 | 10900 |
| 106 | rajaboopathi | jk nagar | 90807 | 28000 |
+------------+---------------+-----------------+----------+--------+
6 rows in set (0.00 sec)
+------------+---------------+-----------------+----------+--------+
| account_no | customer_name | address | phone_no | amount |
+------------+---------------+-----------------+----------+--------+
| 101 | raj | kk nagar | 56789 | 20000 |
| 102 | ravi | mgr nagar | 59713 | 10000 |
| 103 | selvam | royal nagar | 89436 | 17500 |
| 104 | ananthi | transport nagar | 94682 | 19000 |
| 106 | rajaboopathi | jk nagar | 90807 | 28000 |
+------------+---------------+-----------------+----------+--------+
5 rows in set (0.00 sec)

RESULT :
PROGRAM 08 :
To create a simple trigger that does not allow insert update and delete operations on the table
SQL> create trigger trigg1 before insert or update or delete on employee for each row
2 begin
3 raise_application_error(-20010,'You cannot do manipulation');
4 end;
5/
Trigger created.
SQL> insert into employee values('aaa',14,34000);
insert into employee values('aaa',14,34000)
To drop the created trigger
SQL> drop trigger trigg1;
Trigger dropped
To create a trigger that raises an user defined error message and does not allow updation and
insertion
SQL> create trigger trigg2 before insert or update of salary on employee for each row
2 declare
3 triggsal employee.salary%type;
4 begin
5 select salary into triggsal from employee where eid=12;
6 if(:new.salary>triggsal or :new.salary<triggsal) then
7 raise_application_error(-20100,'Salary has not been changed');
8 end if;
9 end;
10 /
Trigger created.
SQL> insert into employee values ('bbb',16,45000);
insert into employee values ('bbb',16,45000)

OUTPUT :
* ERROR at line 1:
ORA-20010: You cannot do manipulation
ORA-06512: at "STUDENT.TRIGG1", line 2
ORA-04088: error during execution of trigger 'STUDENT.TRIGG1'
SQL> delete from employee where ename='xxx';
delete from employee where ename='xxx'
*
ERROR at line 1:
ORA-20010: You cannot do manipulation
ORA-06512: at "STUDENT.TRIGG1", line 2
ORA-04088: error during execution of trigger 'STUDENT.TRIGG1'
SQL> update employee set eid=15 where ename='yyy';
update employee set eid=15 where ename='yyy'
*
ERROR at line 1:
ORA-20010: You cannot do manipulation
ORA-06512: at "STUDENT.TRIGG1", line 2
ORA-04088: error during execution of trigger 'STUDENT.TRIGG1'

*
ERROR at line 1:
ORA-04098: trigger 'STUDENT.TRIGG2' is invalid and failed re-validation
SQL> update employee set eid=18 where ename='zzz';
update employee set eid=18 where ename='zzz'
*
ERROR at line 1:
ORA-04298: trigger 'STUDENT.ITTRIGGS' is invalid and failed re-validation

RESULT :
PROGRAM 09 :
VIEW:
create table Instructor(
id number Primary Key,
name varchar(90),
dept_name varchar(80),
salary number);
Table INSTRUCTOR created.
DESC Instructor;
create table Department(
department_name varchar(80),
building varchar(70),
budget number);
Table DEPARTMENT created.
DESC Department;
SELECT * FROM Instructor;
SELECT * FROM Department;
CREATING A VIEW ‘sample_instructor’ FOR THE TABLE ‘Instructor’
CREATE VIEW sample_instructor as select id,name,dept_name from Instructor;
View SAMPLE_INSTRUCTOR created.
SELECT DISTINCT * FROM sample_instructor;
PERFORMING INSERT OPERATION
INSERT INTO sample_instructor VALUES(401,'Allison','mech');
1 row inserted.
SELECT * FROM sample_instructor;
PERFORMING UPDATE OPERATION
UPDATE sample_instructor SET name='scott' WHERE id=106;
1 row updated.
SELECT * FROM sample_instructor;
PERFORMING DELETE OPERATION
DELETE FROM sample_instructor WHERE ID=400;
1 row deleted.
SELECT * FROM sample_instructor;
CREATING A VIEW ‘instructor_department’ USING BOTH THE TABLES ‘Instructor’ AND
‘Department’
create view instructor_department as
select id,name,building from instructor,department
where department.deptment_name=instructor.dept_name;
View INSTRUCTOR_DEPARTMENT created.
SELECT DISTINCT * FROM instructor_department;

OUTPUT :

RESULT :
PROGRAM 10 :
Register an XML Schema
DECLARE
l_schema CLOB; BEGIN
l_schema := '<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MAILS">
<xs:complexType>
<xs:sequence>
<xs:element name="MAIL" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="TO_"/>
<xs:element type="xs:string" name="FROM_"/>
<xs:element type="xs:string" name="HEADING_"/>
<xs:element type="xs:string" name="BODY_"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
DBMS_XMLSCHEMA.registerSchema(schemaurl=>'student.xsd', schemadoc => l_schema,
local => TRUE,
gentypes =>FALSE,
gentables => FALSE,
enablehierarchy => DBMS_XMLSCHEMA.enable_hierarchy_none);
END;
/
We can check the schema details using the USER_XML_SCHEMAS view.
SELECT schema_url FROM user_xml_schemas;
SCHEMA_URL
student.xsd SQL>
Validate XML Document (SCHEMAVALIDATE)
DECLARE
l_xml CLOB; l_xmltype XMLTYPE; BEGIN
l_xml := '<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<MAILS>
<MAIL>
<TO_>STU10</TO_>
<FROM_>STU01</FROM_>
<HEADING_>REMAINDER</HEADING_>
<BODY_>Don't forget the Exam</BODY_>
</MAIL>
<MAIL>
<TO_>STU20</TO_>
<FROM_>STU02</FROM_>
<HEADING_>INVITATION</HEADING_>
<BODY_>Come to my Brother's Marriage</BODY_>
</MAIL>
<MAIL>
<TO_>STU30</TO_>
<FROM_>STU03</FROM_>
<HEADING_>REQUEST</HEADING_>
<BODY_>Please give me a pen</BODY_>
</MAIL>
</MAILS>
l_xmltype := XMLTYPE(l_xml, 'student.xsd'); l_xmltype.schemavalidate;
END;
/
OUTPUT
PL/SQL procedure successfully completed.
SQL>
DECLARE
l_xml CLOB; l_xmltype XMLTYPE; BEGIN
l_xml := '<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<MAILS>
<MAIL>
<TO_>STU10</TO_>
<FROM_1>STU01</FROM_>
<HEADING_>REMAINDER</HEADING_>
<BODY_>Don't forget the Exam</BODY_>
</MAIL>
<MAIL>
<TO_>STU20</TO_>
<FROM_>STU02</FROM_>
<HEADING_>INVITATION</HEADING_>
<BODY_>Come to my Brother's Marriage</BODY_>
</MAIL>
<MAIL>
<TO_>STU30</TO_>
<FROM_>STU03</FROM_>
<HEADING_>REQUEST</HEADING_>
<BODY_>Please give me a pen</BODY_>
</MAIL>
</MAILS>
l_xmltype := XMLTYPE(l_xml, 'student.xsd');
l_xmltype.schemavalidate;
END;
/

OUTPUT :
DECLARE
*
ERROR at line 1:
ORA-30937: No schema definition for 'FROM_1' (namespace '##local') in parent '/MAILS/MAIL'
ORA-06512: at "SYS.XMLTYPE", line 354
ORA-06512: at line 10
SQL>

RESULT :
PROGRAM 11 :
DOCUMENT DATABASE
use ABC
db
db.createCollection("Student")
show collections
db.Student.insertMany([{ID:101,Name:"AAA"},{ID:102,Name:"BBB"},{ID:103,Name:"CCC"},{ID
:104,Name:"DDD"},{ID:105,Name:"EEE"}])
db.Student.find()
db.Student.updateOne({Name:"EEE"},{$set:{ID:106}})
db.Student.find()
db.Student.deleteOne({ID:106})
db.Student.find()
db.Student.remove({})
db.Student.find()
db.Student.drop()
show collections
db.dropDatabase()
COLUMN ORIENTED DATABASE
Create keyspace nosql;
desc keyspaces;
use nosql;
desc nosql;
create table Student(ID int primary key, Name text);
desc Student;
Select * from Student;
insert into Student(id,name) values (1,'AAA');
insert into Student(id,name) values (2,'BBB');
insert into Student(id,name) values (3,'CCC');
insert into Student(id,name) values (4,'DDD');
insert into Student(id,name) values (5,'EEE');
select * from Student;
update Student set name='FFF' where id=5;
select * from Student;
delete from Student where id=5;
select * from Student;
truncate Student;
select * from Student;
Desc Student;

OUTPUT :
RESULT :
CONTENT BEYOND SYLLABUS
Ex. No. : 14 Date :
PostgreSQL
AIM :
To write query to create schema table, insert value, update value, delete value and drop table in
PostgreSQL..
DESCRIPTION :
1. Command: Create schema

Description: To create schema in PostgreSQL.


Syntax: Create schema schema_name;
2. Command: Create table

Description: To create a table in PostgreSQL


Syntax: Create table table_name;
3. Command: Insert into

Description: To insert values in an table in PostgreSQL


Syntax: Insert into table_name (column1,column2…) Value (value1,value2…);
4. Command: Update

Description: To Update the existing value from the table in PostgreSQL


Syntax: Update table_name set column=value where condition;
5. Command: Delete from

Description: To delete records from the table in PostgresSQL


Syntax: Delete from table_name where condition;

Description: To drop a table from the schema in PostgresSQL


Syntax: drop table table_name;
7. Command: drop schema;

Description: To drop schema from PostgresSQL


Syntax: drop schema schema_name;
8. Command: \dt

Description: To show the records in the PostgresSQL


9. Command: \dn

Description: To show the schemas in the Postgres SQL


COMMANDS :
Create a Schema
postgres=# create schema Office;
postgres=# \dn;
Create Table
postgres=# CREATE TABLE department (
postgres(# dept_ID serial PRIMARY KEY,
postgres(# Dept_name VARCHAR (255) NOT NULL,
postgres(# description VARCHAR (255),
postgres(# location VARCHAR(50)
postgres(# );
CREATE TABLE :

Insert into table


postgres=# INSERT INTO department (dept_name, location)
postgres-# VALUES ('ACCOUNTING', 'Boston'),
postgres-# ('OPERATIONS','Florida'),
postgres-# ('SALES','Chicago');
INSERT 03postgres=# SELECT * FROM department;
Alter table
postgres=# ALTER TABLE department ADD COLUMN last_update Date;
postgres=# select * from department;
postgres=# select * from department;
Update table
postgres=# UPDATE department
postgres-# SET location = 'U.S.A';
postgres=# select * from department;

Delete from table


postgres=# DELETE FROM department
postgres-# WHERE dept_id = 2;
postgres=# select * from department;

Delete from table


postgres=# DELETE FROM department
postgres-# WHERE dept_id = 2;

RESULT :
Thus the query to create schema table, insert value, update value, delete value and drop table in
PostgreSQL has been executed and output was verified.
Ex. No. : 15 Date :
DbVisualizer
AIM :
To create EER diagram and use filter for showing results of a database table using DbVisualizer.
DESCRIPTION :
Creating a schema
To create a new schema:
1. Locate the Schemas node in the Databases tab tree.
2. Open the Create Schema dialog from the right-click menu.
3. Enter all required information (database dependent).
4. Click Execute to create the schema.

Viewing Entity Relationships


To see how a table is related to other tables through Foreign Keys:
1. Locate the Tables node in the Databases tab tree,
2. Double-click the node to open its Object View tab,
3. Select the References sub tab.
● We can select among different graph layouts in the layout drop-down list in the toolbar: Hierarchic,
Organic, Orthogonal, or Circular.
● Other layout settings can be changed in the Graph Control area, which is shown or hidden with the
settings toggle button in the toolbar. For instance, we can select how much information to include for
each table in the graph: just the Table Name, the Primary Key column(s) or all Columns.

Using the Filter feature


1. Connect to the Database and Open Table Editor:

● Start DbVisualizer and establish a connection to our database by selecting the appropriate driver,
specifying the connection details, and providing login credentials.
● Once connected, expand the database node in the Database Objects view and navigate to the table
we want to filter.
● Right-click on the table and choose "Open Table" to open the Table Editor.

2. Enable Filter Mode:

● In the Table Editor, we'll see the table data displayed in a grid format.
● Look for the "Filter Mode" button in the toolbar. It usually looks like a funnel icon.
● Click on the "Filter Mode" button to enable the filter mode for the table.
3. Define Filters:

● Once the filter mode is enabled, we'll notice that each column header now has a filter icon (usually
a funnel or a small arrow).
● Click on the filter icon of the column we want to apply a filter to. A filter dialog or dropdown will
appear.
● Depending on the column's data type, the available filter options may vary. Common filter options
include:

● Equals (for text and numeric columns)


● Contains (for text columns)
● Greater Than, Less Than, Between (for numeric columns)
● Date-based filters (e.g., Before, After, Between) for date columns.

4. Apply Filters:

● Define our filter criteria within the filter dialog for the selected column.
● Click "OK" or "Apply" to apply the filter.
● We can apply filters to multiple columns simultaneously to perform more complex filtering.

5. Remove Filters:

● To remove a filter and show all data again, click on the filter icon of the filtered column and choose
"Clear Filter."

6. Disable Filter Mode:

● If we want to disable the filter mode and display all data again without filters, click on the "Filter
Mode" button in the toolbar to turn it off.
OUTPUT :
Generating Database EER Diagram
Using Filter for Database Table

Result of Above Filter

RESULT :
Thus creating EER diagram and using filter for showing results of a database table using DbVisualizer
has been written, executed successfully and the output was verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy