0% found this document useful (0 votes)
49 views

Rdbms

The document describes creating tables, inserting data, and performing queries on a university registrar's office database. It creates tables to store information about courses, course offerings, students, and instructors. It inserts sample data and performs queries to select, update, delete and view the data. Indexes and views are also created. The aim of creating this database structure and performing queries was achieved successfully.
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)
49 views

Rdbms

The document describes creating tables, inserting data, and performing queries on a university registrar's office database. It creates tables to store information about courses, course offerings, students, and instructors. It inserts sample data and performs queries to select, update, delete and view the data. Indexes and views are also created. The aim of creating this database structure and performing queries was achieved successfully.
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/ 14

EXP.

NO:

A university registrar’s office maintains data about the following entities:(a)


courses, including number, credits, syllabus and prerequisites;(b) course
offerings, including course number, year, semester, section number,
instructor(s), timings, and classroom;(c) students, including student_id, name,
department, and title. Further, the enrollment of students in courses and grades
awarded to students in each course they are enrolled for must be appropriately
modeled and if anyone students left and add the university update it.
Aim
To create a table, views and indexes for given scenario.
Solution:
// create database
create database University_registrars_office;
________

//check my database was successfully created or not


show databases;
________

//use my created database


use University_registrars_office;
________
//create table
create table courses (number int, credits int, syllabus varchar(20),prerequisites
varchar(25));
create table course_offerings (course number int, year int, semester int,
section_number int, instructor varchar (24));
create table students (student_id int, name varchar (20), program varchar (23));
create table instructors (identification_number int, name varchar (23), program
varchar(26));
________

//insert values into table


insert into courses values(27,10,”rdbms”,”dbms”),(28,10,”rdbms”,dbms”);
insert into course_offerings values(1,2021,3,” Jayalakshmi”),(1,2021,3,”kala”);
insert into students values(28,”Lakshmi”,”RDBMS”),(29,”jaya”,”RDBMS”);;
insert into instructors values(208,”Jayalakshmi”,”RDBMS”),
(209,”kalai”,”RDBMS”);
________

//Display the tables


select * from course;
select * from course_offerings;
select* from students;
select* from instructors;
________
//Create index
create index Sname on students(Name);
________
//Create view
create view Stu_details as select student_id,name from students;
create view course_details as select number,syllabus from courses;
________
//Display view
select * from Stu_details;
select * from course_details;
____________
//Delete the left the students
delete from students where student_id=28;
//Update the students
update students set name=”Raj” where stu_id=29;

Result:
Thus, the creation of a database and sql queries to retrieve information from the
university registrar’s office database was implemented and verified
successfully.

EXP NO:
Aim :
To illustrate the working of stored procedure , functions and triggers

Background Knowledge of Stored Procedures:

 Stored procedures can help improve application performance and


reduce database access traffic.
 All database access must go across the network, which, in some cases,
can result in poor performance.
 For each SQL statement, a database application must initiate a
separate communication with DB2.
 To improve application performance, you can create stored
procedures that run on your database server.
Syntax for stored procedure:

DELIMITER \

CREATE PROCEDURE procedure_name [IN|OUT|INOUT]

BEGIN

Declaration_section

Executable_section

END \

DELIMITER;

The following syntax is used to call the stored procedure in MySQL:

CALL procedure_name(parameter(s))

Query

DELIMITER \  
CREATE PROCEDURE get_merit_student ()  
BEGIN  
SELECT * FROM student_info WHERE marks > 70;  
SELECT COUNT(stud_code) AS Total_Student FROM student_info;    
END \ 
DELIMITER; 
CALL get_merit_student();
Background Knowledge of Functions:

 User-defined functions (UDFs) and stored procedures have a similar


relationship in MySQL.
 Advantages of user-defined functions is their wide- ranging capability to
be specified over an externally written piece of code, such as C, and to
have MySQL reference that code to perform the functions operation.

Syntax for Function

CREATE FUNCTION function_name [ (parameter datatype [, parameter dataty
pe]) ]   
RETURNS return_datatype  
BEGIN  
Declaration_section  
Executable_section  
END;  
Query
DELIMITER \

CREATE FUNCTION AddTwoNos(a INT, b INT)

RETURNS INT DETERMINSTIC

BEGIN

DECLARE c INT;

SET c=(a+b);

RETURN c;

END\

DELIMITER;
Select AddTwoNos(3,4);

Background Knowledge of Triggers

A trigger is a piece of code that is automatically executed or fired in response to


a data modification event on a table.

The data modification event include insert, update, delete driven by an INSERT,
DELETE, UPDATE, and MERGE statement.

Triggers are stored in the database at once. There are 2 types of triggers-

BEFORE triggers(executed before any SQL operation) and

AFTER triggers(executed after any SQL operation).

Syntax of Triggers

CREATE TRIGGER trigger_name

(AFTER|BEFORE) (INSERT|UPDAATE|DELETE)

ON table_name FOR EACH ROW

BEGIN

--variable declarations

--trigger code

END;

Query

CREATE TRIGGER t1

BEFORE INSERT

ON sales FOR EACH ROW


BEGIN

SET new.total=new.qtysold*new.price;

End\

DELIMITER;

select * from sales;

Result:

Thus, the stored procedure, function and triggers was successfully executed and
verified.
EXP.NO:
Write a query for given table,
a) Find common city
b) Join two tables

Sample table: Salesman

Aim:
To write a query for given tables using UNION and INTERSECT.

Solution:
Syntax for UNION:

SELECT column_list FROM table1  
UNION  
SELECT column_list FROM table2;  
Query:

Syntax for DISTINCT operator and INNER JOIN clause:


SELECT DISTINCT column_list FROM table_name1 INNER JOIN
table_name2 USING (join_condition);
Solution
Query
SELECT DISTINCT city FROM salesman INNER JOIN Customer USING
(city);

Result
Thus, the UNION and INTERSECT operator was executed from given table
and verified.

EXP.NO.
Read data using SELECT statement
The table shown below shows data in movierentals table

we want to get the number of times that the movie with id 2 has been rented out
and omit the duplicates id’s, we want to know the year in which the oldest
movie in our library was released, we want to get the year that the latest movie
in our database was released, we want total number of memberships, we want
to find the average of membership_number, I want to sort movierentals by
membership_number in descending order

Aim:
To perform various Aggregate function and clauses using SELECT statement
for given scenario.
Syntax:
1.Count
SELECT COUNT(Field_name) FROM table_name where conditions;
2.Distinct
SELECT DISTINCT (Field_name) FROM table_name;
3.MIN
SELECT MIN (Field_name) FROM table_name;
4.MAX
SELECT MAX(Field_name) FROM table_name;
5.SUM
SELECT SUM(Field_name) FROM table_name;
6.AVG
SELECT AVG(Field_name) FROM table_name;
7.ORDER BY
SELECT Field_list FROM table_name where conditions ORDER BY
expression[ASC|DESC]
8.GROUP BY
SELECT Field_list aggregate_function (Field_name) FROM table_name where
conditions GROUP BY Field_list
Solution
Query
SELECT COUNT (movie_id) FROM movierentals WHERE movie_id = 2;
SELECT DISTINCT movie_id FROM movierentals;
SELECT MIN (year_released) FROM movies;
SELECT MAX (year_released) FROM movies;
SELECT SUM (membership_number) FROM movierentals;
SELECT AVG (membership_number) FROM movierentals;
SELECT reference_number, membership_number FROM movierentals ORDER BY
membership_number DESC;
SELECT movie_id, COUNT (movie_id) FROM movierentals GROUP BY movie_id;

Result:
Thus, the aggregate functions and clauses was executed from given scenario and
implemented.
Exp.NO
Query Multiple tables with join statements

we have two tables — one called students, and one called advisors.


The students table contains a field called advisor_id that references an id
within the advisor’s table:

If we wanted to pull information on students and their


advisors, excluding students who are not assigned to an advisor , if we will
show a list of all students, even those who do not have advisors  ,ifwe will
show a list of all students who are assigned to advisors
Aim
To perform join statements for given scenario(tables).

Solution:
Syntax for Simple join:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;

Syntax for Right join:


SELECT columns FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column =
table2.column;

Syntax for Left join:


SELECT columns FROM table1 LEFT [OUTER] JOIN table2 ON table1.column =
table2.column;
Result:
Thus, the join statements were executed from given scenario and implemented.

You might also like