0% found this document useful (0 votes)
12 views84 pages

DBMS LAB FINAL.doc

The document outlines the implementation of DDL and DML commands for an e-shopping and student management database, including creating tables, altering structures, and populating data. It details various SQL queries for managing customer and purchase data, as well as student records and their marks, with examples of creating, updating, and deleting records. Additionally, it includes aggregate functions to analyze faculty data, such as calculating total salaries and average salaries per department.

Uploaded by

aathmikam51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views84 pages

DBMS LAB FINAL.doc

The document outlines the implementation of DDL and DML commands for an e-shopping and student management database, including creating tables, altering structures, and populating data. It details various SQL queries for managing customer and purchase data, as well as student records and their marks, with examples of creating, updating, and deleting records. Additionally, it includes aggregate functions to analyze faculty data, such as calculating total salaries and average salaries per department.

Uploaded by

aathmikam51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 84

Reg No: 912221104006

Ex No. 1 IMPLEMENTATION OF DDL COMMANDS,


CONSTRAINTS
Date:

1. Write queries to create tables for e-shopping database with the specified fields

Customer (custid,custname,address,state,gender)
Purchase(purchase_order_id, custid, purchase_date, purchase_amount)

mysql> create table customer_01(custid int,custname varchar(20),address


varchar(20),state varchar(20),gender varchar(20));
Query OK, 0 rows affected (0.11 sec)

mysql> create table purchase(purchase_order_id int,custid int,purchase_date


date,purchase_amount float);
Query OK, 0 rows affected (0.02 sec)

2. Write a query to display the structure of the table customer

mysql> desc customer_01;

3. Write a query to add the new column dateofbirth in the customer table

mysql> alter table customer_01 add date_of_birth date;


Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

4. Write a query to change the size of the address to 30 in the customer table

mysql> alter table customer_01 modify address varchar(30);


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

5. Write a query to delete the column gender in the customer


table mysql> alter table customer_01 drop column gender;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
Reg No: 912221104006

6. Write a query to add the primary key constraint for the fields custid, purchaseorderid

mysql> alter table customer_01 add constraint c1 primary key(custid);


Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

7. Write a query to create a new table purchaseorder from the existing table purchase

mysql> alter table purchase add constraint c2 primary key(purchase_order_id);


Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

8. Write a query to rename the purchase table as purchaseorder

mysql> rename table purchase to purchase_order;


Query OK, 0 rows affected (0.03 sec)

9. Write a query to drop the table customer

mysql> drop table customer_01;


Query OK, 0 rows affected (0.03 sec)

10. Write a query to create a table customer with following


constraints: c_id with primary key, c_name with NOT NULL
constraints

Field Name Type


C_id Int
C_name varchar
Address varchar
State varchar
Date of birth date

mysql> create table customer_01(c_id int primary key,c_name varchar(20) not


null,address varchar(20),state varchar(20),date_of_birth date);
Query OK, 0 rows affected (0.03 sec)
Reg No: 912221104006

11. Write a query to drop the primary key constraint for custid

mysql> alter table customer_01 drop primary key;


Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

12. Write a query to add the table level primary key constraint for the field custid

mysql> alter table customer_01 add primary key(c_id);


Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

13. Write a query to add the foreign key constraints for custid in the purchaseorder table
which refer the custid in the customer table

mysql> alter table purchase_order add constraint c3 foreign


key(purchase_order_id) references customer_01(c_id);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

14. Write a query to display the structure of the table customer

mysql> desc customer_01;

15. Write a query to display the structure of the table purchase_order.


Reg No: 912221104006
Reg No: 912221104006

EX NO:2 IMPLEMENTATION OF DML COMMANDS AND CONSTRAINTS

DATE:

Consider the following relations for Student Management System:

i) Student_XX (RegNo, Name, DoJ, Branch)

ii) Mark_XX (RegNo, Sub1, Sub2, Sub3, Total, Result)

[XX: Mention your Roll Number]

For the above schema, perform the following:

1. Create the above tables with the mapping given above and specify the primary key constraint
which is underlined and specify the foreign key.
MYSQL>create table student_41(regno varchar(90) primary key,name varchar(20),DOJ
date,branch varchar(20));

MYSQL> desc student_41;

MYSQL> create table mark_41(regno varchar(90),sub1 int,sub2 int,sub3 int,total


int,result varchar(20),foreign key(regno) references student_41(regno));

MYSQL> desc mark_41;


Reg No: 912221104006

Populate the tables with the values given.


mysql> insert into student_41(regno,name,doj,branch)values(‘06c01’,Aarthi','2011-02-
07','CSE');
Query OK, 1 row affected (0.01 sec)

mysql> insert into student_41(regno,name,doj,branch)values(‘06c02’,'Bala','2011-02-


08','CSE');
Query OK, 1 row affected (0.00 sec)
1 row created.
mysql> insert into student_41(regno,name,doj,branch)values(‘06c03’,‘krithika’,'2011-
02-09','CSE');
Query OK, 1 row affected (0.00 sec)
1 row created.
mysql> insert into student_41(regno,name,doj,branch)values(‘06it01’,‘Amutha’,'2011-
02-11','IT');
Query OK, 1 row affected (0.00 sec)
1 row created.
mysql> insert into student_41(regno,name,doj,branch)values(‘06it02’,'Bama','2011-02-
10','IT');
Query OK, 1 row affected (0.00 sec)
1 row created.
mysql> insert into student_41(regno,name,doj,branch)values(‘06it03’,'Bala','2011-02-
10','IT');
Query OK, 1 row affected (0.00 sec)
1 row created.

mysql> insert into mark_41(regno,sub1,sub2,sub3)values(‘06c01’,68,89,90);


Query OK, 1 row affected (0.02 sec)

mysql> insert into mark_41(regno,sub1,sub2,sub3)values(‘06c02’,45,89,68);


Query OK, 1 row affected (0.02 sec)

mysql> insert into mark_41(regno,sub1,sub2,sub3)values(‘06c03’,96,92,90);


Query OK, 1 row affected (0.02 sec)

mysql> insert into mark_41(regno,sub1,sub2,sub3)values(‘06it01’,70,80,90);


Query OK, 1 row affected (0.02 sec)

mysql> insert into mark_41(regno,sub1,sub2,sub3)values((‘06it02’,70,22,93);


Query OK, 1 row affected (0.02 sec)

2. Write a query to display the values of STUDENT_XX and MARK_XX table.


mysql> select * from student_41;
Reg No: 912221104006

mysql> Select * from mark_41;

3. Write a query to display the name and branch from STUDENT_XX table.
mysql> select name,branch from student_41;

4. Write a query to display the unique value of branch from STUDENT_XX table.
mysql> select distinct branch from student_41;

5. Write a query to list the student detail who belongs to the CSE department in STUDENT_XX
table.
mysql> select * from student_41 where branch='CSE';
Reg No: 912221104006

6. Write a query to display the student detail wherebranch=’CSE’ and regno=’06c01’.


mysql> > select * from student_41 where branch='cse' and regno='06c01';

7. Write a query to display the student details where regno=’06c02’ and name=’bala’.
Mysql>select * from student_41 where branch='CSE' and regno=’06c02;

8. Write a query to display the student details order by (ascending) name in STUDENT_XX table.
Mysql>select * from student_41 order by name asc;

9. Write a query to update the column name=’bhuvana’ where regno=’06it02’ in STUDENT_XX


table and display the STUDENT_XX details.
mysql>update student_41 set name='bhuvana' where regno = ‘06it02’;
Reg No: 912221104006

mysql> select * from student_41;

10. Write a query to display the regno, name from STUDENT_XX table where doj is 09-feb-11.

MYSQL> select regno,name from student_41 where doj='2011-02-09’;

11. Write a query to retrieve all the details from the STUDENT_XX table whose name starts with A.

MYSQL> select * from student_41 where name like 'b%';

12. Write a query to retrieve all the details from the STUDENT_XX table whose name ends with A.

MYSQL> select * from student_41 where name like '%a';


Reg No: 912221104006

13. Write a query to retrieve all the records from the STUDENT_XX table which does not have
‘VA’.

MYSQL> select * from student_41 where name not like '%va%';

14. Write a query to display the months between DOJ and till date.

MYSQL> select * from student_41 where doj between '2011-01-11' and current_date();

15. Write a query to update the total marks in MARK_XX table and display the MARK_XX values.
MYSQL> update mark_41 set total=sub1+sub2+sub3;

MYSQL> select * from mark_41;

16. Write a query to display only those rows whose total ranges between 150 and 200.
MYSQL> select * from mark_41 where total between 150 and 200;
Reg No: 912221104006

17. Write a query to update the result column in MARK_XX table which holds the following
condition: sub1>=50,sub2>=50&sub3>=50 (PASS) Else (FAIL). Display the table values after
gets updated.
MYSQL> update mark_41 set result=case
2 when(sub1>=50 and sub2>=50 and sub3>=50)
3 then 'pass'
4 else 'fail'
5 end;

mysql> select * from mark_41;

18. Write a query to display the mark details where total is greater than 200.
MYSQL> select * from mark_41 where total>200;

19. Write a query to display the mark details where total not in 240.
MYSQL> select * from mark_41 where total<>240;

20. Write a query to delete the row from STUDENT_XX table where regno=’06it02’.
MYSQL> delete from mark_41 where regno='06it02';
MYSQL> delete from student_41 where regno='06it02';
Reg No: 912221104006

MYSQL> select * from mark_41;

MYSQL> select * from student_41;


Reg No: 912221104006

Ex No. 3 IMPLEMENTATION OF AGGREGATE FUNCTIONS


DATE :

Consider the following relation Faculty

Faculty (faculty_id,Faculty_Name,Dept_Name,Position,Email,Ph_number, Salary)

1. Create a table faculty and populate the values given above.


Mysql > create table faculty(faculty_id varchar(90),faculty_name
varchar(90),Dept_name varchar(90),position varchar(90),email varchar(90),ph_number
big int,salary int);
Mysql>insert into
faculty(faculty_id,faculty_name,Dept_name,position,email,ph_number,salary)values(“C
S01”, “Aarthi”, “CSE”, “Assistant professor”, “[email protected]”, 9874562310,40000);
Mysql>insert into
faculty(faculty_id,faculty_name,Dept_name,position,email,ph_number,salary)values(“C
S02”, “Aadhavan”, “CSE”, “Assistant professor”, “[email protected]”,
8974852250,40000);
Mysql>insert into
faculty(faculty_id,faculty_name,Dept_name,position,email,ph_number,salary)values(“C
S03”, “Bala”, “CSE”, “HOD”, “[email protected]”, 7074854150,60000);
Mysql>insert into
faculty(faculty_id,faculty_name,Dept_name,position,email,ph_number,salary)values(“IT
01”, “Balamurugan”, “IT”, “Assistant professor”, “[email protected]”,
7074852546,35000);
Mysql>insert into
faculty(faculty_id,faculty_name,Dept_name,position,email,ph_number,salary)values(“IT
02”, “Sneha”, “IT”, “HOD”, “[email protected]”, 8904852546,60000);
Mysql>insert into
faculty(faculty_id,faculty_name,Dept_name,position,email,ph_number,salary)values(“IT
03”, “Dinesh”, “IT”, “Assistant professor”, “[email protected]”, 8945218246,40000);
Reg No: 912221104006

2. Write a query to list the number of departments available in the faculty table.
mysql> select count(distinct dept_name) from faculty;

3. Write a query to get the total salaries payable to


faculties. mysql> select sum(salary) from faculty;

4. Write a query to get the minimum salary from faculty table.


mysql> select min(salary) from faculty;

5. Write a query to get the maximum salary of a faculty table.


mysql> select max(salary) from faculty;

6. Write a query to get the average salary and number of faculties working in the CSE
department.
mysql> select avg(salary),count(*) from faculty where dept_name='CSE';
Reg No: 912221104006

7. Write a query to get the maximum, minimum, sum, average salary of all faculties.
mysql> select max(salary),min(salary),sum(salary),avg(salary) from faculty;

8. Write a query to get the maximum, minimum, sum, average salary of all faculties
working in the IT department.
mysql> select max(salary),min(salary),sum(salary),avg(salary) from faculty where
dept_name='IT';

9. Write a query to get the number of faculties with the same


position. mysql> select position,count(*) from faculty group by
position;

10. Write a query to get the number of faculties with the same position in CSE
department.
mysql> select position,dept_name,count(*) from faculty group by position,dept_name
having dept_name='CSE';

11. Write a query to get the department name and the total salary payable in each
department.
Reg No: 912221104006

mysql> select dept_name,sum(salary) from faculty group by dept_name;

12. Write a query to get the job id and maximum salary of the faculties where maximum
salary is greater than or equal to $5000.
mysql> select faculty_id,max(salary) from faculty group by faculty_id having
max(salary)>=5000;
Reg No: 912221104006
Reg No: 912221104006

Ex No. 4 IMPLEMENTATION OF JOINS, SUB QUERIES


DATE:

1. Consider the following relations for Student Management System:

Student_XX (RegNo, Name, DoJ, Branch)

Mark_XX (RegNo, Sub1, Sub2, Sub3, Total, Result)

[XX: Mention your Roll Number]

1. Write a query to display the regno, name, result from student_xx and
mark_xx table.
mysql> select a.regno,name,result from student01 a,mark01 b where a.regno=b.regno;

2. Write a query to display the regno, name, result of CSE & IT from
student_xx and mark_xx table.
mysql> select a.regno,name,result from student01 a,mark01 b where a.regno=b.regno
and branch in('CSE');

3. Write a query to select regno, name, branch, total from mark_xx and not
in student_xx using both the tables.
Reg No: 912221104006

mysql> select a.regno,name,branch,total from student01 a right outer join mark01 b


on a.regno=b.regno;

4. Write a query to select regno, name, branch, total from student_xx and not
in mark_xx tables.
Mysql>select a.regno,name,branch,total from student01 a left outer join mark01
b on a.regno=b.regno;

5. Write a query to select regno, name, branch, total from both the
tables. Mysql>select a.regno,name,branch,total
From student01 a
Cross join mark01 b
Where a.regno=b.regno;

I. Create a table book and author

Book
ISBN Title Author_name Publisher_name Pub_Year Unit_Price
1001 Oracle Arora PHI 2004 399
1002 DBMS Silberschatz McGrawHill 2000 400
2001 Unix Kapoor SciTech 2002 270
2002 ADBMS Berson Pearson 2004 550

2003 FDBMS E.Navathe Pearson 2003 750


Reg No: 912221104006

Author

Author_name Country
Arora U.s
Silberschatz Asia
Kapoor Canada
Berson Germany
E.Navathe England
Banu India
1. Create the above-mentioned tables and populate the values given.
Mysql>create table book01(isbn int,title varchar(90),author_name
varchar(90),pub_name varchar(90),pub_year date,unit_price int);
Mysql>insert into
book01(isbn,title,author_name,pub_name,pub_year,unit_price)values(1001,
‘oracle’,‘Arora’, ‘PHI’, ‘2004’, 399);
Mysql>insert into
book01(isbn,title,author_name,pub_name,pub_year,unit_price)values(1002,
‘DBMS’, ‘silberschatz’,‘McGrawHill’, ‘2000’,400);
Mysql>insert into
book0@(isbn,title,author_name,pub_name,pub_year,unit_price)values(2001,
‘unix’, ‘Kapoor’, ‘scitech’, 2002,270);
Mysql>insert into
book01(isbn,title,author_name,pub_name,pub_year,unit_price)values(2002,
‘ADBMS’, ‘Bearson’, ‘Pearson’, ‘2004’,550);
Mysql>insert into
book01(isbn,title,author_name,pub_name,pub_year,unit_price)values(2003,
‘FDBMS’ , ‘navatha,’pearson’, ‘2003’,750);
Myqsl> create table author01(authorname varchar(90),country varchar(90));
mysql> insert into author01(authorname,country)values('arora','U.S');
mysql> insert into
author01(authorname,country)values('silberschatz','Asia'); mysql> insert
into author01(authorname,country)values('kapoor','Canada');
mysql> insert into author01(authorname,country)values('Pearson','Germany');
mysql> insert into author01(authorname,country)values('E-navatha','England');
mysql> insert into author01(authorname,country)values('banu','India');
mysql>select *from author01;
Reg No: 912221104006

2. Display the title, author, and publisher name of all books published in 2000,

2002, 2004.

mysql> select title,authorname,pub_name from book01 where pub_year


in(2000,2002,2004);

3. Display the maximum price isbn number.

mysql> select isbn from book01where unit_price =(select max(unit_price)from


book01);

4. Display the minimum price isbn number.

mysql> select isbn from book01where unit_price =(select max(unit_price)from


book01);
Reg No: 912221104006

5. Get the details of author who have published book in year 2004.

mysql> select * from author01 where authorname in(select authorname from


book01 where pub_year=2004);

6. Get the titles of all books written by authors not living in U.S

mysql> select title from book01 where authorname not in(select authorname
from author where country='U.S');

7.Display the titles of books that have price greater than all the books publisher
in year 2004.

Mysql> select title from book01 where unit_price>all(select unit_price from


book01 where pub_year = 2004);
Reg No: 912221104006
Reg No: 912221104006

Ex No. 5 IMPLEMENTATION OF DCL & TCL COMMANDS


Date :

1. Create a sample student table and insert a following records


ID Name
1 John
2 Joe
3 Abhi
SQL> create table student01(id number ,name varchar2(10));
Table created.
SQL>select * from student_41;

2. Update the above table with id=4 where name=’abhi’.


Mysql>update student_41 set id=4 where name= “Abhi”;

Mysql>select * from student_41;


Reg No: 912221104006

3. Commit the changes.


Mysql> commit;

4. Insert the following records into the existing table.


ID Name
5 Kelvin
6 Chris
Mysql>insert into student_41(ID,Name)values(5, “Kelvin”);
Mysql>insert into student_41(ID,Name)values(6,“Chris”);

Mysql>select * from student_41;

5. Create a savepoint s1.


Mysql>Start transaction

Mysql>Savepoint s1;
Reg No: 912221104006

6. Insert into the above table for id=7 and name=’ram’.


Mysql>Insert into student_41(ID,Name)values(7,“ram”);
Myqsl>select * from student_41;

7. Rollback to savepoint s1.


Mysql>Rollback to savepoint s1;
Mysql> select * from savepoints1;

8. Commit the changes.


Mysql>Commit;
Reg No: 912221104006

9. Display the student table by viewing records.


Mysql>Select * from student_41;

10. Change the above table with id=8 where name=’chris’.


Mysql>update student_41 set id =8 where name= “chris”;

Mysql> select * from student_41;


Reg No: 912221104006

11.Create a savepoint s2.


Mysql> Savepoint s2;

12.Delete the record whose id=6.


Myqsl>delete from student_41 where i=8;

13.Rollback to savepoint s2.


Mysql>rollback to savepoint s2;

14.Display the student table using select command and observe the
changes.
Mysql>Select * from student_41;

15.Commit the changes.


Mysql>commit;
Reg No: 912221104006
Reg No: 912221104006

Ex No. 6 IMPLEMENTATION OF TCL COMMANDS


DATE:

1. Create a student table in DBA login and insert following records


Studid Studname Address State

1 John Madurai Tamil


Nadu

2 Joe Madurai Tamil


Nadu

3 Abhi Virudhunagar Tamil


Naru

Mysql>create table student(Stud_id int,Studname varchar(90),Address


Varchar(90),State varchar(90));
Mysql>insert into
student(Stud_id,Studname,Address,State)values(1,"john","Madurai","TamilNa
du");
Mysql>insert into
student(Stud_id,Studname,Address,State)values(2,"joe","Madurai","TamilNad
u");
Mysql>insert into
student(Stud_id,Studname,Address,State)values(3,"Abhi","Virudhunagar","Ta
milNadu");
2. Create a user with user name as ‘student’ and password as ‘intel’.

Mysql>create user ammu@'localhost' identified by 'mypass';

3. Write a query to grant select privilege to a table named “student”, the user name
is “XXX”.

Mysql>GRANT SELECT
>ON student
>TO 'ammu'@'localhost';
Reg No: 912221104006

4. Write a query to grant multiple privileges to a user.

Mysql>GRANT SELECT, INSERT, DELETE, UPDATE


> ON student
> TO 'ammu'@'localhost';

5. Write a query to grant all the privileges to a user


Mysql>GRANT ALL
>ON student
>TO 'ammu'@'localhost';

6 .Write a query to grant the privileges to all users.


Mysql> GRANT SELECT
> ON student
>TO '*'@'localhost';

7. Write a query to revoke delete permission for the ‘student’ table from user name
‘xxxxx’.

Mysql>GRANT SELECT

>ON student

>TO '*'@'localhost';

5. Write a query to remove every permission

Mysql>REVOKE ALL
Reg No: 912221104006

>ON student

>FROM ammu;
Reg No: 912221104006

Ex No:7 IMPLEMENTATION OF VIEWS AND INDEXES


DATE:

1. Write a query to create the following table


Customer(c_id,cname,city) ###c_id with AUTOINCREMENT
Mysql>create table customer02(c_id int auto_increment,c_name varchar(20),city
varchar(20),primary key(cid));

2. Insert atleast 5 tuples in the above mentioned table by creating sequences


for c_id column.
Mysql>insert into customer02(c_name,city)values('kanni','madurai');
mysql> insert into customer02(c_name,city)values('kamal','Chennai');
mysql> insert into customer02(c_name,city)values('Dharun','Mumbai');
mysql> insert into customer02(c_name,city)values('karthi','coimbatore');
mysql> insert into customer02(c_name,city)values('keerthana','Bangalore');
3. Create a view cust_view from customer table.
Mysql>create view cust_view(c_name,city)as select c_name,city from customer02;

4. Write a query to view the structure of customer and cust_view


table. Mysql>desc customer;

Mysql>desc cust_view;

5. Write a query to create a view cust_view2 from customer table such that
it contains only c_name=’XXX’
CREATE OR REPLACE VIEW cust_view2 AS
SELECT c_name, city
FROM customer
WHERE c_name = 'keerthana';
Reg No: 912221104006

6. Write a query to drop a view cust_view2


Mysql>drop view cust_view2;

7. Write a query to create an index on particular column in customer table.


Mysql> CREATE INDEX idx_customer_c_id ON customer (c_id);

8. Write a query to create an index on multiple columns in customer table.


Mysql> CREATE INDEX idx_customer_multi ON customer (c_id, custname, state);

9. Write a query to create an unique index on table customer.


Mysql>CREATE UNIQUE INDEX idx_customer_unique ON customer
(c_id);

10. Write a query to drop the index.


Mysql> ALTER TABLE customer DROP INDEX idx_customer_multi;
Mysql> ALTER TABLE customer DROP INDEX idx_customer_unique;
Reg No: 912221104006

Ex No: 8 PROCEDURES AND FUNCTIONS


DATE:

1. Develop a PL/SQL procedure that accepts the user variable

DELIMITER $$
CREATE PROCEDURE User_Variables( )
BEGIN
SET @x=15;
SET @y=10;
SELECT @x,@y,@x-@y;
END$$
mysql> CALL User_Variables( );
-> $$

2. Develop a PL/SQL procedure that accepts the local variables.


mysql> CREATE PROCEDURE Local_Variables( )
-> BEGIN
-> DECLARE a,b,c INT;
-> SET a=20;
-> SET b=5;
-> SET c=a*b;
-> SELECT a,b,c;
-> END $$
Reg No: 912221104006

3. Develop a PL/SQL procedure that display the entire table ‘student’.


id name class
1 Naga MBA
2 Anil MCA
3 Mith MBBS
4 Sujitha BCA
5 Raga MCA
mysql> DELIMITER //
mysql> CREATE PROCEDURE show_students()
-> BEGIN
-> SELECT * FROM student;
-> END //

mysql> CALL show_students();


Reg No: 912221104006

4. Develop a PL/SQL procedure to display the MCA count.


mysql> DELIMITER //
mysql>
mysql> CREATE PROCEDURE display_MCA_count()
-> BEGIN
-> SELECT COUNT(*) AS MCA_count FROM student WHERE class =
'MCA';
-> END //

5. Develop a PL/SQL program to display all records of this table whose marks are
greater than 70 and count all the table rows.
mysql> DELIMITER //
mysql> CREATE PROCEDURE display_marks()
-> BEGIN
-> DECLARE total_rows INT DEFAULT 0;
Reg No: 912221104006

-> SELECT COUNT(*) INTO total_rows FROM student;


-> SELECT * FROM student WHERE marks > 70;
-> SELECT CONCAT('Total rows: ', total_rows) AS output;
-> END;
-> //

1. mysql> CALL display_marks();

6. Develop a PL/SQL procedure to display only the top 4 rows from studenttable.
mysql> DELIMITER //
mysql> CREATE PROCEDURE display_top_4_students()
-> BEGIN
-> SELECT * FROM student_info
-> ORDER BY marks DESC
-> LIMIT 4;
-> END;
-> //
Reg No: 912221104006

1. mysql> CALL display_top_4_students();

7. Develop a PL/SQL procedure to display the maximum mark from student table.
mysql> DELIMITER //
mysql> CREATE PROCEDURE display_max_mark()
-> BEGIN
-> DECLARE max_mark INT;
-> SELECT MAX(marks) INTO max_mark FROM student;
-> SELECT CONCAT('The maximum mark is ', max_mark) AS result;
-> END //

mysql> CALL display_max_mark();


Reg No: 912221104006

8. Develop a PL/SQL procedure to get the value from the user and display the
appropriate user mark.
mysql> DELIMITER //
mysql> CREATE PROCEDURE display_user_mark(IN p_stud_id INT)
-> BEGIN
-> DECLARE v_marks INT;
->
-> SELECT marks INTO v_marks FROM student WHERE stud_id = p_stud_id;
->
-> IF v_marks IS NOT NULL THEN
-> SELECT CONCAT('Marks for user with stud_id ', p_stud_id, ': ', v_marks) AS
result;
-> ELSE
-> SELECT CONCAT('No record found for user with stud_id ', p_stud_id) AS result;
-> END IF;
-> END //
Query OK, 0 rows affected (0.02 sec)
mysql> DELIMITER ;

mysql> CALL display_user_mark(1);


Reg No: 912221104006

FUNCTIONS:

9. Develop a PL/SQL function that returns the customer occupation based on the

age mysql> DELIMITER //


mysql> CREATE FUNCTION get_customer_occupation(p_age INT)
-> RETURNS VARCHAR(50)
-> DETERMINISTIC
-> BEGIN
-> DECLARE v_occupation VARCHAR(50);
-> SELECT occupation INTO v_occupation FROM customer_info WHERE age =
p_age;
-> RETURN v_occupation;
-> END //

mysql> SELECT get_customer_occupation(25);


Reg No: 912221104006

Ex No. 9 IMPLEMENTATION OF TRIGGERS


DATE:
1. Create the following table

ID NAME OCCUPATION WORKING


HOURS
1 AAA Actor 12
2 BBB Teacher 14

mysql> CREATE TABLE employee(id int, name varchar(20), occupation


varchar(20), working_hours int);
mysql>insert into
employee(id,name,occupation,working_hours)values(1,”AAA”,”Actor”,12);
mysql>insert into
employee(id,name,occupation,working_hours)values(2,”BBB”,”Teacher”,14);
mysql>insert into
employee(id,name,occupation,working_hours)values(3,”CCC”,”professor”,10);

2. Write the PL/SQL to trigger the event before inserting the working hour less than 0.

mysql> DELIMITER $$
mysql> Create Trigger before_inserthour
-> BEFORE INSERT ON employee FOR EACH ROW
-> BEGIN
-> IF NEW.working_hours<0 THEN SET NEW.working_hours=0;
-> END IF;
-> END $$
Reg No: 912221104006

mysql> insert into employee values(4,"DDD","Nurse",12);


-> $$

mysql> select * from employee;


-> $$

mysql> show triggers;


-> $$

3. Write the PL/SQL to trigger the event after inserting the details

mysql> create table emp_details1(id int,name varchar(25),occupation


varchar(25),working_hours int, Lastinserted Time);

mysql> DELIMITER $$
mysql> Create Trigger after_insert_details2
-> AFTER INSERT ON employee FOR EACH ROW
-> BEGIN
-> INSERT INTO emp_details1 VALUES (NEW.id, NEW.name, NEW.occupation,
NEW.working_hours, CURTIME( ));
-> END$$
Reg No: 912221104006

mysql> INSERT into employee values(8,'MMM','Doctor',23);


-> $$

mysql> select * from employee;


-> $$

4. Write the PL/SQL to perform before update trigger.

mysql> DELIMITER $$
mysql> CREATE TRIGGER before_update
-> BEFORE UPDATE ON employee FOR EACH ROW
-> BEGIN
-> DECLARE error_msg VARCHAR(200);
-> SET error_msg=('The working hours cannot be greater than 30');
-> IF new.working_hours>30 THEN
-> SIGNAL SQLSTATE '45000'
-> SET MESSAGE_TEXT=error_msg;
-> END IF;
-> END $$
Reg No: 912221104006

mysql> update employee set working_hours=28 where id=3;


-> $$

5. Write the PL/SQL to perform after update trigger.

mysql> create table student(id int, name varchar(20), class int);

mysql> insert into student values(101,'AAA',5),(102,'BBB',8),(103,'CCC',4);

mysql> select * from student;

mysql> CREATE TABLE studentlog (user_name VARCHAR(10) NOT


NULL,description VARCHAR(222) NOT NULL);
Reg No: 912221104006

mysql> select * from studentlog;

mysql> CREATE TRIGGER after_updatestu1


-> AFTER UPDATE ON student FOR EACH ROW
-> BEGIN
-> INSERT into studentlog VALUES (user( ), CONCAT('update', OLD.name,'Prev:'
,OLD.class, 'Pres:', NEW.class));
-> END$$

mysql> UPDATE student SET class = class + 1;


-> $$

mysql> select * from student;


-> $$

mysql> select * from studentlog;


Reg No: 912221104006

6. Write the PL/SQL to perform before delete trigger.

mysql> select * from employee;


-> $$

mysql> create table delemp1(id int PRIMARY KEY AUTO_INCREMENT, name


varchar(30), occupation varchar(20), deleted_time TIMESTAMP DEFAULT NOW(
));

mysql> CREATE TRIGGER before_delete


-> BEFORE DELETE ON employee FOR EACH ROW
-> BEGIN
-> INSERT INTO delemp1(name,occupation) VALUES(OLD.name,OLD.occupation);
-> END$$

mysql> delete from employee where id=8;


-> $$
Reg No: 912221104006
mysql> select * from employee;
Reg No: 912221104006

mysql> select * from delemp1;


-> $$

7. Write the PL/SQL to perform after delete trigger.

mysql> create table empsalary(empid int, amount int);


-> $$

mysql> insert into empsalary values(101,5000),(102,4000),(103,3000),(104,8000);


-> $$

mysql> select * from empsalary;


-> $$
Reg No: 912221104006

mysql> create table totalsalary(total_budget int);


-> $$

mysql> insert into totalsalary(total_budget)


-> SELECT SUM(amount) from empsalary;
-> $$

mysql> select * from totalsalary;


-> $$

mysql> DELIMITER $$
mysql> CREATE TRIGGER after_delete
-> AFTER DELETE ON empsalary FOR EACH ROW
-> BEGIN
-> UPDATE totalsalary SET total_budget=total_budget-OLD.amount;
-> END $$
Reg No: 912221104006

mysql> delete from empsalary where empid=103;


-> $$

mysql> select * from empsalary;


-> $$

mysql> select * from totalsalary;


-> $$

mysql> drop trigger after_delete;


-> $$
Reg No: 912221104006

Ex. No. 10 DESIGN XML SCHEMA FOR COMPANY DATABASE


DATE:

AIM

To Design XML Schema for the given company database and to implement the following
queries using XQuery.
Requirements:

 Notepad
 BaseX 7.8.2

Procedure

 Start the program.


 Create the xml schema in notepad.
 Create a xml schema for Department using xml file with deptName, deptNo, deptManagerSSN,
deptManagerStartDate, deptLocation.
 Create a xml schema for Employee using xml file with empName, empSSN, empSex,
empSalary, empBirthDate, empDeptNo, empSupervisorSSN, empAddress, empWorksOn.
 Create a xml schema for Project using xml file with projName, projNo,
projLocation, projDeptNo, projWorker.
 Implement the following queries using XQuery in BaseX 7.8.2
 Retrieve the details of the queries from the database.
 Show the result.
 Stop the program.

To Design XML Schema for the given company database:


XML
//department.xml

<?xml version="1.0"?>
<departments>
<department>
<deptName>Research</deptName>
<deptNo>1</deptNo>
<deptMgrSSN>11</deptMgrSSN>
<deptMgrStartDate>1/1/2000</deptMgrStartDate>
<deptLocation>Chennai</deptLocation>
Reg No: 912221104006

</department>
<department>
<deptName>Outsourcing</deptName>
<deptNo>2</deptNo>
<deptMgrSSN>22</deptMgrSSN>
<deptMgrStartDate>1/1/2001</deptMgrStartDate>
<deptLocation>Hyderabad</deptLocation>
</department>
</departments>

//employee.xml
<?xml version="1.0"?>
<employees>
<employee>
<empName>Arthi</empName>
<empSSN>11</empSSN>
<empSex>Female</empSex>
<empSalary>900000</empSalary>
<empBirthDate>1-3-89</empBirthDate>
<empDeptNo>1</empDeptNo>
<empAddress>kknagar,chennai</empAddress>
<empWorksOn>Web Mining</empWorksOn>
</employee>
<employee>
<empName>Malliga</empName>
<empSSN>12</empSSN>
<empSex>Female</empSex>
<empSalary>300000</empSalary>
<empBirthDate>2-3-89</empBirthDate>
<empDeptNo>1</empDeptNo>
Reg No: 912221104006

<empSupSSN>11</empSupSSN>
<empAddress>Anna nagar,chennai</empAddress>
<empWorksOn>Cloud Computing</empWorksOn>
</employee>
<employee>
<empName>Sindharth</empName>
<empSSN>13</empSSN>
<empSex>Male</empSex>
<empSalary>300000</empSalary>
<empBirthDate>4-9-89</empBirthDate>
<empDeptNo>1</empDeptNo>
<empSupSSN>11</empSupSSN>
<empAddress>Anna nagar,chennai</empAddress>
<empWorksOn>Cloud Computing</empWorksOn>
</employee>
<employee>
<empName>Ganesh</empName>
<empSSN>14</empSSN>
<empSex>Male</empSex>
<empSalary>300000</empSalary>
<empBirthDate>6-9-88</empBirthDate>
<empDeptNo>1</empDeptNo>
<empSupSSN>11</empSupSSN>
<empAddress>Anna nagar,chennai</empAddress>
<empWorksOn>Web Mining</empWorksOn>
</employee>
<employee>
<empName>Sruthi</empName>
<empSSN>22</empSSN>
<empSex>Female</empSex>
Reg No: 912221104006

<empSalary>900000</empSalary>
<empBirthDate>3-3-89</empBirthDate>
<empDeptNo>2</empDeptNo>
<empAddress>T nagar,chennai</empAddress>
<empWorksOn>Business Process</empWorksOn>
</employee>
<employee>
<empName>Dhanush</empName>
<empSSN>23</empSSN>
<empSex>Male</empSex>
<empSalary>300000</empSalary>
<empBirthDate>7-9-1992</empBirthDate>
<empDeptNo>2</empDeptNo>
<empSupSSN>22</empSupSSN>
<empAddress>T nagar,chennai</empAddress>
<empWorksOn>Business Process</empWorksOn>
</employee>
<employee>
<empName>Anushya</empName>
<empSSN>24</empSSN>
<empSex>Female</empSex>
<empSalary>400000</empSalary>
<empBirthDate>3-3-90</empBirthDate>
<empDeptNo>2</empDeptNo>
<empSupSSN>22</empSupSSN>
<empAddress>kknagar,chennai</empAddress>
<empWorksOn>Knowledge Process</empWorksOn>
</employee>
<employee>
<empName>Manu</empName>
Reg No: 912221104006

<empSSN>25</empSSN>
<empSex>Female</empSex>
<empSalary>300000</empSalary>
<empBirthDate>3-5-90</empBirthDate>
<empDeptNo>2</empDeptNo>
<empSupSSN>22</empSupSSN>
<empAddress>annanagar,chennai</empAddress>
<empWorksOn>Knowledge Process</empWorksOn>
</employee>
</employees>

//project.xml
<?xml version="1.0"?>
<projects>
<project>
<projName>Web Mining</projName>
<projNo>111</projNo>
<projLoc>Chennai</projLoc>
<projDeptNo>1</projDeptNo>
<projWorkers>
<projWorker>
<name>Manu</name>
<name>Jeyaraman</name>
</projWorker>
</projWorkers>
</project>
<project>
<projName>Cloud Computing</projName>
<projNo>112</projNo>
<projLoc>Chennai</projLoc>
Reg No: 912221104006

<projDeptNo>1</projDeptNo>
<projWorkers>
<projWorker>
<name>Arthi</name>
<name>Jeyaraman</name>
</projWorker>
</projWorkers>
</project>
<project>
<projName>Business Process</projName>
<projNo>221</projNo>
<projLoc>Chennai</projLoc>
<projDeptNo>2</projDeptNo>
<projWorkers>
<projWorker>
<name>Anushya</name>
<name>J</name>
</projWorker>
</projWorkers>
</project>
<project>
<projName>Knowledge Process</projName>
<projNo>222</projNo>
<projLoc>Chennai</projLoc>
<projDeptNo>2</projDeptNo>
<projWorkers>
<projWorker>
<name>Manu</name>
</projWorker>
</projWorkers>
Reg No: 912221104006

</project>
</projects>

XML SCHEMA
department.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="departments">
<xs:complexType>
<xs:sequence>
<xs:element name="department" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="deptName"/>
<xs:element type="xs:byte" name="deptNo"/>
<xs:element type="xs:byte" name="deptMgrSSN"/>
<xs:element type="xs:string" name="deptMgrStartDate"/>
<xs:element type="xs:string" name="deptLocation"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

employee.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employees">
<xs:complexType>
Reg No: 912221104006

<xs:sequence>
<xs:element name="employee" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="empName"/>
<xs:element type="xs:byte" name="empSSN"/>
<xs:element type="xs:string" name="empSex"/>
<xs:element type="xs:int" name="empSalary"/>
<xs:element type="xs:string" name="empBirthDate"/>
<xs:element type="xs:byte" name="empDeptNo"/>
<xs:element type="xs:byte" name="empSupSSN" minOccurs="0"/>
<xs:element type="xs:string" name="empAddress"/>
<xs:element type="xs:string" name="empWorksOn"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

project.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="projects">
<xs:complexType>
<xs:sequence>
<xs:element name="project" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="projName"/>
Reg No: 912221104006

<xs:element type="xs:short" name="projNo"/>


<xs:element type="xs:string" name="projLoc"/>
<xs:element type="xs:byte" name="projDeptNo"/>
<xs:element name="projWorkers">
<xs:complexType>
<xs:sequence>
<xs:element name="projWorker">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name" maxOccurs="unbounded"
minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
To implement the following queries using XQuery
XQUERY
1. Retrieve the department name, manager name, and manager salary for every department

let $d1 := doc("E:\Durga\XmL\department.xml")


let $d2 := doc("E:\Durga\XmL\employee.xml")
for $p1 in $d1/departments/department
Reg No: 912221104006

for $p2 in $d2/employees/employee


where $p1/deptMgrSSN = $p2/empSupSSN
return<Result><dept>{$p1/deptName}</dept><mgrName>{$p2/empName}</mgrName><mgrSal>
{$p2/empSalary}</mgrSal></Result>

Output

<Result>
<dept>
<deptName>Research</deptName>
</dept>
<mgrName>
<empName>Malliga</empName>
</mgrName>
<mgrSal>
<empSalary>300000</empSalary>
</mgrSal>
</Result>
<Result>
<dept>
<deptName>Research</deptName>
</dept>
<mgrName>
<empName>Sindharth</empName>
</mgrName>
<mgrSal>
<empSalary>300000</empSalary>
</mgrSal>
</Result>
<Result>
<dept>
<deptName>Research</deptName>
Reg No: 912221104006

</dept>
<mgrName>
<empName>Ganesh</empName>
</mgrName>
<mgrSal>
<empSalary>300000</empSalary>
</mgrSal>
</Result>
<Result>
<dept>
<deptName>Outsourcing</deptName>
</dept>
<mgrName>
<empName>Dhanush</empName>
</mgrName>
<mgrSal>
<empSalary>300000</empSalary>
</mgrSal>
</Result>
<Result>
<dept>
<deptName>Outsourcing</deptName>
</dept>
<mgrName>
<empName>Anushya</empName>
</mgrName>
<mgrSal>
<empSalary>400000</empSalary>
</mgrSal>
</Result>
Reg No: 912221104006

<Result>
<dept>
<deptName>Outsourcing</deptName>
</dept>
<mgrName>
<empName>Manu</empName>
</mgrName>
<mgrSal>
<empSalary>300000</empSalary>
</mgrSal>
</Result>

2.Retrieve the employee name, supervisor name and employee salary for each employee
who works in the Research Department.

let $d1 := doc("E:\Durga\XmL\department.xml")


let $d2 := doc("E:\Durga\XmL\employee.xml")
let $r := $d1/departments/department[deptName="Research"]
let $sup := $d2/employees/employee[empSSN=$r/deptMgrSSN]
for $p1 in $d2/employees/employee
where $p1/empDeptNo=$r/deptNo
return<Result><eName>{$p1/empName}</eName><supName>{$sup/empName}</supName><em
pSal>{$p1/empSalary}</empSal></Result>

Output

<Result>
<eName>
<empName>Arthi</empName>
</eName>
<supName>
<empName>Arthi</empName>
Reg No: 912221104006

</supName>
<empSal>
<empSalary>900000</empSalary>
</empSal>
</Result>
<Result>
<eName>
<empName>Malliga</empName>
</eName>
<supName>
<empName>Arthi</empName>
</supName>
<empSal>
<empSalary>300000</empSalary>
</empSal>
</Result>
<Result>
<eName>
<empName>Sindharth</empName>
</eName>
<supName>
<empName>Arthi</empName>
</supName>
<empSal>
<empSalary>300000</empSalary>
</empSal>
</Result>
<Result>
<eName>
<empName>Ganesh</empName>
Reg No: 912221104006

</eName>
<supName>
<empName>Arthi</empName>
</supName>
<empSal>
<empSalary>300000</empSalary>
</empSal>
</Result>

3. Retrieve the project name, controlling department name, number of employees and
total hours worked per week on the project for each project.

let $d1 := doc("E:\Durga\XmL\department.xml")


let $d2 := doc("E:\Durga\XmL\project.xml")
for $p1 in $d2/projects/project
let $dep:=$d1/departments/department[deptNo=$p1/projDeptNo]
return<Result><projName>{$p1/projName}</projName><depName>{$dep/deptName}
</depName><workers>{count($p1/projWorkers/projWorker/name)}</workers></Result>

Output

<Result>
<projName>
<projName>Web Mining</projName>
</projName>
<depName>
<deptName>Research</deptName>
</depName>
<workers>2</workers>
</Result>
<Result>
Reg No: 912221104006

<projName>
<projName>Cloud Computing</projName>
</projName>
<depName>
<deptName>Research</deptName>
</depName>
<workers>2</workers>
</Result>
<Result>
<projName>
<projName>Business Process</projName>
</projName>
<depName>
<deptName>Outsourcing</deptName>
</depName>
<workers>2</workers>
</Result>
<Result>
<projName>
<projName>Knowledge Process</projName>
</projName>
<depName>
<deptName>Outsourcing</deptName>
</depName>
<workers>1</workers>
</Result>
4. Retrieve the project name, controlling department name, number of employees and total
hours worked per week on the project for each project with more than one employee working
on it.
let $d1 := doc("E:\Durga\XmL\department.xml")
let $d2 := doc("E:\Durga\XmL\project.xml")
Reg No: 912221104006

for $p1 in $d2/projects/project


let $dep:=$d1/departments/department[deptNo=$p1/projDeptNo]
where count($p1/projWorkers/projWorker/name)>1
return<Result><projName>{$p1/projName}</projName><depName>{$dep/deptName}
</depName><workers>{count($p1/projWorkers/projWorker/name)}</workers></Result>
Output
<Result>
<projName>
<projName>Web Mining</projName>
</projName>
<depName>
<deptName>Research</deptName>
</depName>
<workers>2</workers>
</Result>
<Result>
<projName>
<projName>Cloud Computing</projName>
</projName>
<depName>
<deptName>Research</deptName>
</depName>
<workers>2</workers>
</Result>
<Result>
<projName>
<projName>Business Process</projName>
</projName>
<depName>
<deptName>Outsourcing</deptName>
Reg No: 912221104006

</depName>
<workers>2</workers>
</Result>

Result
Thus the XML Schema for the given database has been executed and implemented successfully.
Reg No: 912221104006

Ex. No. 11 MONGODB CRUD OPERATIONS


DATE:

AIM:

The aim of this experiment is to test the performance of MongoDB when executing CRUD (Create, Read,
Update, and Delete) operations on a single collection, without using any external dataset.

DESCRIPTION:
In this lab experiment, we will use MongoDB to execute CRUD operations on a single collection,
without using any external dataset. We will create a sample schema for the collection and insert a small
amount of data to perform the CRUD operations on.

To perform the CRUD operations, we will use the MongoDB shell, a command-line interface for interacting
with MongoDB. We will execute various queries to create, read, update, and delete data in the MongoDB
CREATE:
To create a new document in MongoDB using the shell, use the insertOne() method as follows:
db.collection.insertOne({field1: value1, field2: value2, ...})
READ:
To read data from a MongoDB collection using the shell, use the find() method as follows:
db.collection.find(query)
UPDATE:
To update an existing document in MongoDB using the shell, use the updateOne() method as
follows:
db.collection.updateOne(filter, update)
DELETE:
To delete a document from a MongoDB collection using the shell, use the deleteOne() method as
follows:
db.collection.deleteOne(filter)
to delete a collection from MongoDB using this shell,use the drop() method as follows:
db.collectionsname.drop()
we will query the data using various filters and criteria. For the Update operation, we will update the data in
the collection. Finally, for the Delete operation, we will remove the data from the collection.

How to open MongoDB:


1. Open the command prompt.
Reg No: 912221104006
2. Type mongod and open another terminal new window and type mongosh

Create database
Create database k7
And use database
Use k7

Queries:
1 .Using the MongoDB shell, create a new document in the "students" collection with the following
attributes:
 Name: John Doe
 Age: 21
 Gender: Male
 Major: Computer Science
 GPA: 3.8

k7> db.students.insertOne({
name: "John Doe",
age: 21,
gender: "Male",
major: "Computer Science",
gpa: 3.8
})
Reg No: 912221104006

2 .Read the collection

k7>db.students.find()

3 . Using the MongoDB shell, update the document in the "students" collection with the name "John
Doe" to have a GPA of 4.0.
K7> db.students.updateOne(
{ name: "John Doe" },
{ $set: { gpa: 4.0 } }

k7>db.students.find()
Reg No: 912221104006

4 .Using the MongoDB shell, delete the document in the "students" collection with the name "John
Doe".
K7> db.students.deleteOne({ name: "John Doe" })

RESULT
Thus, CRUD operations (create, read, update, delete) on a MongoDB collection was executed
successfully.
Reg No: 912221104006

Ex. No. 12 DATABASE DESIGN USING ER MODELING


DATE:

Aim:
To create Database Design Using ER Modeling, Normalization and Implementation for Any Application
Step 1: Create table using mysqlclient
mysql> use Purchase
Database changed
mysql> create table Customer1(Cid int primary key,Cname varchar(20),Caddress varchar(20),Cphone
int); mysql> create table Order1(Orderid int primary key, Orderdate DATE, Quantity int, Amount int, Cid
int,foreign key(Cid) references Customer1(Cid));
mysql> create table Payments1(Pid int primary key,PaymentDate DATE, Amount int, Cid int,Orderid int,
foreign key(Cid) references Customer1(Cid),foreign key(Orderid) references Order1(Orderid));

Step 2: Open Mysql Workbench


Reg No: 912221104006

Step 3: Open Reverse Engineer from Database

Step 4: Click Next


Reg No: 912221104006

Step 5: Enter password and press ok then click next

Step 6: Select database purchase from the list and press next
Reg No: 912221104006

Step 7: Press Execute and then Next


Reg No: 912221104006

RESULT
Thus the Database Design Using ER Modeling, Normalization and Implementation for Any Application was
executed successfully.
Reg No: 912221104006

Ex No. 13 DATABASE CONNECTIVITY WITH MYSQL USING PYTHON


DATE:

Aim:
To create a student’s database system by establishing MySQL connectivity with
Python as a graphical front-end interface.

Description:
This System focuses on creating a student database system that enables
database connectivity with MySQL using Python. The program provides a user-
friendly front-end interface for interacting with the database. It allows users to
perform essential operations such as inserting data into the database, retrieving
data from the database, updating existing data, and deleting data.

By establishing a connection to the MySQL database and utilizing Python


libraries, users can seamlessly interact with the database and observe the results
through the intuitive graphical interface. The program enhances the
understanding of database connectivity concepts and equips users with the
ability to perform common database operations efficiently.

Program:
i) Python code
from tkinter import *

import tkinter.messagebox as MessageBox

import mysql.connector as mysql

def insert():

id = e_id.get()

name = e_name.get();
Reg No: 912221104006

phone = e_phone.get();

if(id=="" or name=="" or phone==""):

MessageBox.showinfo("Insert Status", "All Fields are required")

else:

con = mysql.connect(host="localhost", user="root",


password="root",database="k7")

cursor = con.cursor()

cursor.execute("insert into students values('"+ id +"','"+ name +"','"+ phone +"')")

cursor.execute("commit");

e_id.delete(0,'end')

e_name.delete(0,'end')

e_phone.delete(0,'end')

show()

MessageBox.showinfo("Insert status", "Inserted Successfully");

con.close();

def delete():

if(e_id.get() == ""):

MessageBox.showinfo("Delete Status", "ID is compulsary for delete")

else:

con = mysql.connect(host="localhost", user="root",


password="root",database="k7")

cursor = con.cursor()

cursor.execute("delete from students where id='"+ e_id.get() +"'")

cursor.execute("commit");

e_id.delete(0,'end')

e_name.delete(0,'end')
Reg No: 912221104006

e_phone.delete(0,'end')

show()

MessageBox.showinfo("Delete status", "Deleted Successfully");

con.close();

def update():

id = e_id.get()

name = e_name.get();

phone = e_phone.get();

if(id=="" or name=="" or phone==""):

MessageBox.showinfo("Update Status", "All Fields are required")

else:

con = mysql.connect(host="localhost", user="root",


password="root",database="k7")

cursor = con.cursor()

cursor.execute("update students set name='"+ name +"', phone='"+ phone +"'


where id='"+ id +"'")

cursor.execute("commit");

e_id.delete(0,'end')

e_name.delete(0,'end')

e_phone.delete(0,'end')

show()

MessageBox.showinfo("Update status", "Updated Successfully");

con.close();

def get():

if(e_id.get() == ""):

MessageBox.showinfo("Fetch Status", "ID is compulsary to Fetch")


Reg No: 912221104006

else:

con = mysql.connect(host="localhost", user="root",


password="root",database="k7")

cursor = con.cursor()

cursor.execute("select * from students where id='"+ e_id.get() +"'")

rows = cursor.fetchall()

for row in rows:

e_name.insert(0, row[1])

e_phone.insert(0, row[2])

con.close();

def show():

con = mysql.connect(host="localhost", user="root",


password="root",database="k7")

cursor = con.cursor()

cursor.execute("select * from students")

rows = cursor.fetchall()

list.delete(0, list.size())

for row in rows:

insertData = str(row[0])+ ' '+ row[1]

list.insert(list.size()+1, insertData)

con.close()

root = Tk()

root.geometry("600x300");

root.title("Pyhton+Tkinter+MySql")

Label(root, text = "Student Registration",font = "arial


15",width=10,height=2).pack(side=TOP,fill=X)

id = Label(root,text='Enter ID',font=('bold', 10))


Reg No: 912221104006

id.place(x=20,y=50)

name = Label(root,text='Enter Name',font=('bold',10))

name.place(x=20,y=80)

phone = Label(root,text='Enter Phone',font=('bold',10))

phone.place(x=20,y=110);

e_id = Entry()

e_id.place(x=150, y=50)

e_name = Entry()

e_name.place(x=150, y=80)

e_phone = Entry()

e_phone.place(x=150, y=110)

insert = Button(root, text="Insert",font=("italic", 10), bg="white", command=insert)

insert.place(x=20, y=160)

delete = Button(root, text="Delete",font=("italic", 10), bg="white", command=delete)

delete.place(x=70, y=160)

update = Button(root, text="Update",font=("italic", 10), bg="white",


command=update)

update.place(x=130, y=160)

get = Button(root, text="Get",font=("italic", 10), bg="white", command=get)

get.place(x=190, y=160)

list = Listbox(root)

list.place(x=320, y=50)

show()

root.mainloop()
Reg No: 912221104006

ii) MySQL Code


Create Database
MYSQL>Create database k7;

Create Table
mysql>create table students(id,name,phno);

Show After You Insert the Values In GUI

mysql>select *from students;

OUTPUT:
Result:
The provided program demonstrates a student’s database system by
establishing connectivity with MySQL using Python, allowing users to insert,
retrieve, update, and delete data from the MySQL database through a
graphical user interface(GUI).

You might also like