Database Management Systems (It 311) : Department of Information and Technology
Database Management Systems (It 311) : Department of Information and Technology
Database Management Systems (It 311) : Department of Information and Technology
SQL COMMANDS
6.LOCK
DDL also known as Data Definition Language defines commands for defining
relation schemas, deleting relations, creating indexes and modifying relation
schemas.
DQL statements are used for performing queries on the data within schema
objects.The purpose of the DQL Command is to get some schema relation
based on the query passed to it. We can define DQL as follows: it is a
component of SQL that allows getting data from the database and
imposing order upon it. SELECT - The SQL SELECT statement is used to
fetch the data from a database table which returns this data in the form of
a result table. These result tables are called result-sets.
Syntax:
SELECT
expressions
FROM TABLES
WHERE condition
TCL also known as transaction control language includes commands that deals
with the transaction within a database
THEORY:
MySQL, is one of the most popular Open Source SQL database management
systems.
MySQL is a fast, easy-to-use RDBMS being used for many small and big
businesses.
MySQL is developed, marketed, and supported by MySQL AB, which is a
Swedish company.
DATABASE QUERIES:
Before creating any tables, MySQL requires you to create a database by
executing the CREATE DATABASE command.
Create a database
CREATE DATABASE <database
USE <database
SHOW
databases; Rename a
database
TABLE QUERIES:
To Create a table
CREATE TABLE (<fieldname>< fieldtype>(<fieldsize) ,
…) ; List all tables in the current database
SHOW tables;
Show table format with column names and data
types DESCRIBE <table name>;
Modify the structure of table
ALTER TABLE <table name><alter specifications>;
ALTER TABLE < table name> DROP COLUMN <column name>;
ALTER TABLE <table name> ADD
COLUMN<column name><datatype>(<Size>);
Delete the table
PROCEDURE:
(i) CREATE DATABASE
command
(ii)USE DATABASE command
(iii) CREATE TABLE command
organization;
(iii)
(a) create table Department(dept_no int(5) primary
key not null unique,dept_name varchar(30) not
null,location varchar(30) not null);
PROBLEMS
1. Consider the database for a college and design an ER diagram.
Write the query for the following.
• Create the tables:
Student (sid, sname, sex, dob,dno)
Department (dno, dname)
Faculty (F_id, fname, designation, salary,dno)
Course (cid, cname, credits,dno)
Register (sid,cid,sem ) Teaching
(f_id,cid,sem)
Hostel(hid,hname,seats,)
• Include the necessary constraints NOT NULL,
DEFAULT, CHECK, and PRIMARY KEY, UNIQUE.
• Create a database college.
• Use college as the current database
• Display all the tables in college database
• Describe the structure of all tables
• Modify the student table to add a new field
‘grade’
ER Diagram
2) Consider the database for a banking enterprise. Write the
queries for the below questions.
Customer cid,cname,loc,sex,dob
Loan Lacno,ltype,ldate,lamt
Account in Bcode,cid
Depositor cid,dacno
Borrower cid,lacno
● Include necessary constraints.
● Tables are created under the database ‘bank’
● Display all the tables in bank database
● Describe the structure of all tables
Output:
EXPERIMENT NO 3
AIM:
Consider the database for an organization. Write the queries
for the following
OBJECTIVES:
To understand how to insert, update and delete data from within a table.
To learn how to retrieve data from a table using
the SELECT statement.
THEORY
INSERT
INSERT INTO tablename VALUES (value1, value2, ..., valuen).
UPDATE
UPDATE <table> SET <field1> = <value1> AND <field2> =
<value2> WHERE <conditions>
DELETE
DELETE FROM <table> WHERE <condition>
SELECT
• Retrieve from all columns
SELECT * FROM <table>
• Retrieve from selected columns
SELECT <column 1>, <column 2> FROM <table>
• Retrieve unique values
SELECT DISTINCT <column name> FROM <table>
• Retrieve data satisfying a given condition
SELECT <columns> FROM <tables> WHERE <condition>
PROCEDURE:
(i) Use insert command
(ii) Use Select command
(iii) Use Select command with where condition
(iv) Use Select command with order by clause
(v) Use Select command with operators
(vi) Use Select command with DISTINCT keyword
(vii) Use Select command with group by clause
(viii) Use Update command
(ix) Use Update command
(x) Use Delete command
(xi) Use select command with group by and having clause
RESULT:
The DML commands are executed successfully.
OUTPUT:
Problems
1. Consider the database for a college. Write the query for the
following. (i)Insert at least 5 tuples into each table.
(ii) List the details of students in the ascending
order of date of birth
(iii) Display the details of students from
computer department
(iv) List the faculties in the descending order of salary
(v)Display the total number of students in each
department
(vi)Display the total number of faculties in each department
with salary greater than 2500
2. Consider the database for a banking enterprise. Write the queries for the
below questions.
(i)Insert at least 5 tuples in each table
(ii)Display the branch details
(iii)List the customers of ‘Mumbai’ city
(iv)List the male customers of ‘Kolkata City
(v) List the state having more than one branch.
AIM:
Consider the database for the organization and Write the queries for the following
(i) display the empno, name, and salaries for employees whose
average salary is higher than the average salary of the
organization
(ii) Display the details of employees whose salary is equal to the
minimum salary of the organisation.
(iii) Display all the employees whose designation is same as that of ‘Arun’
(iv) display the empno and name of employees who
earn more than any Employee in dept 1.
(v)Display the empno, name, departments that the
departments are same in both the emp and dept
(vi) Display the employee details by
implementing left inner join
(vii)Display employee details by implementing a right outer join
OBJECTIVES
To understand Subqueries and Join in Mysql.
THEORY
NESTED QUERIES:
A subquery is a query within a query. These subqueries can
reside in the WHERE clause, the FROM clause, or the SELECT
clause. The first query in the SQL statement is known as the
outer query. The query inside the SQL statement is known as
the inner query. The inner query is executed first. The output
of an inner query is used as the input for the outer query.
The entire SQL statement is sometimes referred to as a nested
query.
JOINS:
MySQL JOINS are used to retrieve data from multiple tables. A
MySQL JOIN is performed whenever two or more tables are
joined in a SQL statement.
There are three types of MySQL joins:
MySQL INNER JOIN (or sometimes called simple join)
MySQL LEFT OUTER JOIN (or sometimes called LEFT
JOIN)
MySQL RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
INNER JOIN (simple join)
MySQL INNER JOINS return all rows from multiple tables where the join condition
is met.
Syntax
Select columns from
table2
On table1.column=table2.column;
Another type of join is called a MySQL LEFT OUTER JOIN. This type
of join returns all rows from the LEFT-hand table specified in the
ON condition and only those rows from the other table where the
joined fields are equal.
Syntax
Select columns from
On table1.column=table2.column;
Syntax
Select columns from table 1
Right join table2
On table1.column=table2.column;
RESULT:
The nested queries and joins are executed successfully.
Code and Output:
● SELECT emp_no,emp_name,salary from
employee WHERE salary>(SELECT
avg(salary)FROM employee);
1. Consider the database for a banking enterprise. Write the queries for the
below questions.
(i) List the deposit account number and amount in
which the deposit scheme having maximum deposit is
opened
(ii) List the account number and amount of that savings
bank deposit scheme in which a minimum amount is
deposited.
(iii) List the customers having accounts in
(v) List the customers having the same name but different account numbers.
(vi) List the customer name that is having maximum
deposit account in bank
(vii) List the customer who has borrowed highest
amount of home
● select Dacno,damt from deposit where damt=(select min(damt) from deposit where
dtype=”savings”);
● select cname,customer.cid,dob from customer inner join accountin on
customer.cid=accountin.cid inner join bankbrn on
accountin.Bcode=bankbrn.Bcode where bankbrn.bloc="Chennai";
2. Consider the database for a college. Write the queries for the below
questions.
(iii) List the names of faculties who teach for a specific course.
(iv)Display the student details by implementing left inner
join
VIEWS
AIM:
Write the queries for the following
(i) Create a view emp from employee such that it
contains only emp_no and emp_name and department.
(ii) Create a view dept from department with only dept_no and location.
(iii) Create a view that contains the details of employees who are managers only.
(iv) drop the views.
OBJECTIVES
To understand views in Mysql
THEORY
A view is the tailored presentation of data contained in one or more
tables and can also be said as a restricted view to the data in the
tables. A view is a “virtual table” or a “stored query” which takes the
output of a query and treats it as a table. The table upon which a
view is created is called a base table. A view is a logical table based
on a table or another view. A view contains no data of its own but
is like a window through which data from tables can be viewed or
changed. The tables on which a view is based are called base tables.
The view is stored as a SELECT statement in the data dictionary.
Advantages of a view:
a. Additional level of table security.
b. Hides data complexity.
c. Simplifies the usage by combining multiple
tables into a single table
Creating and dropping view:
Syntax:
Create or replace view view_name AS SELECT
column_name(s) FROM table_name
WHERE condition;
Drop view <view name>
PROCEDURE:
● Create the employee table
● Create the view
● Display the content of view
Result
Thus the views are created successfully.
Problems
1. Create and drop views on student table
2. Ans:-
create view Customer_View as (select cid,cname,dob,loc from customer
where
loc="Delhi");
create view Deposit_type as (select * from deposit where dtype="Savings"); select * from
Deposit_type;
EXPERIMENT NO:6
PROCEDURE
AIM:
Write a procedure which increases the salary of an employee. It accepts
an employee number and salary increase amount. It uses the employee
number to find the current salary from the EMPLOYEE table and update
the salary.
OBJECTIVES
To understand procedure in Mysql
THEORY
PROCEDURE:
Syntax
Create procedure procedure name (parameter data type, parameter
data type...) Begin
Declaration
section
Executable
_section End;
Procedure name
The name to assign to this procedure in MySQL.
Parameter
When creating a procedure, there are three types of parameters that can be declared:
1. IN - The parameter can be referenced by the procedure. The value
of the parameter cannot be overwritten by the procedure.
2. OUT - The parameter cannot be referenced by the procedure, but
the value of the parameter can be overwritten by the procedure.
3. IN OUT - The parameter can be referenced by the procedure and
the value of the parameter can be overwritten by the procedure.
Declaration section
The place in the procedure where you declare local variables.
Executable section
The place in the procedure where you enter the code for the procedure.
PROCEDURE:
Problems
DELIMITER ;
call getbalance(23212);
OUTPUT:
2.
Ans-
use college;
DELIMITER //
END //
DELIMITER ;
call getdept(3);
OUTPUT
EXPERIMENT NO:7
CURSORS
AIM
Write a cursor to display the list of employees who are working as managers.
OBJECTIVES
To implement cursor
THEORY
A cursor is a SELECT statement that is defined within the declaration section
of your stored program in MySQL.
1. Declare a cursor
Declare cursor name cursor for select statement;
2. Open the cursor.
Open cursor name;
3. Fetch cursor
The purpose of using a cursor, in most cases, is to retrieve the rows from your
cursor so that some type of operation can be performed on the data. Fetch cursor
name into variable list;
4 . Close the cursor
Close cursor name;
PROCEDURE
PROGRAMS
1 . Create a cursor to modify the salary of ‘Professors’ belonging to all
departments by 150%.
2. Consider the college database. Retrieve all students who have registered
for a specific course and store their details into another table using cursors.
3 . Consider the bank database. Retrieve all customers who have a loan at a
particular branch using a cursor.
EXPERIMENT NO:8
TRIGGER
AIM
Write a Trigger for employee table it will store the updated salary into
another
table SALARY while updating salary.
OBJECTIVES
To understand triggers in Mysql
THEORY.
A trigger is a set of actions that are run automatically when a specified
change operation
( INSERT, UPDATE, or DELETE statement) is performed on a specified table.
The syntax to
create an AFTER UPDATE Trigger in MySQL is:
Create trigger trigger_nane
After update On table name for each row
Begin Variable declarations
Trigger code
End;
PROCEDURE
(i) Create the employee table
(ii) Insert values
(iii) Write the after update trigger
(iv) Update the salary in employee table
(v) Display the SALARY table
CODE & OUTPUT
RESULT
The trigger procedure has been executed successfully.
Problems 1. Write an update trigger on the Account table. The system should
keep track of the records that are being updated.
Problem 2. Write a before delete trigger on the Student table.
Experiment No: 9
CONCEPTS OF NORMALIZATION
AIM
Checking Normalization of a database table (First Normal form)
Problem Statement
An exercise to check whether the given database table is normalized or not.
If yes, find out the status of normalization and reasoning .
Objective
To study the concept of various levels of normalization and understand how
to convert
into normalized forms.
Requirements
Mysql database software
Design/Theory
Create a database table in SQL with a few rows and columns. Analyze the
table
and determine to which normal form it belongs to according to the rules and
regulations of each normal form.
Procedure
Consider a student table as given below.
We can easily verify that this table satisfies 1NF: viz, it has duplicate rows;
each cell is a single-valued(i.e there is no repeating group of arrays); and all
the entries in the given column are of the same kind. In this table we can see
that the key, SSN, functionality determines the other attributes; i.e
FirstName, LastName and Major.
Experiment No: 10
AIM
Checking Normalization of a database table (Third normal form).
Problem Statement
An exercise to check whether the given database table is normalized or not.
If yes ,find out the status of normalization and reasoning.
Objective
To study the concept of various levels of normalization and understand how
to convert into normalized forms.
Requirements
Mysql database software
Design/Theory
Create a database table in SQL with a few no of rows and columns.Analyze
the table
and determine to which normal form it belongs to according to the rules and
regulations of each normal form.
Procedure
Consider a book database table as given below.
By examining the table, we can infer that books dealing with history,
cognitive psychology, and folksong are assigned to the PCL General Stacks
collection; that books dealing with legal procedures are assigned to the Law
Library; that books dealing with Greek literature are assigned to the Classics
Library; that books dealing with library biography are assigned to the Library
and Information Science Collection (LISC); and that books dealing with
music literature are assigned to the Fine Arts Library.
Moreover, we can infer that the PCL General Stacks collection and the LISC
are both housed in the Perry-Castañeda Library (PCL) building; that the
Classics Library is housed in Waggener Hall; and that the Law Library and
Fine Arts Library are housed, respectively, in Townes Hall and the Fine Arts
Building.
Thus we can see that a transitive dependency exists in the above table : any
book that deals with history, cognitive psychology, or library biography will
be physically housed in the PCL building (unless it is temporarily checked
out to a borrower); any book dealing with legal procedures will be housed in
Townes Hall; and so on. In short, if we know what subject a book deals with,
we also know not only what library or collection it will be assigned to but
also what building it is physically housed in.