Dbms edited file
Dbms edited file
PRACTICAL – 01
AIM: To Draw ER Model for company database.
Consider the company ER model as shown in Figure 1.1, which keeps track of employees,
departments, and projects.
• The company is organized into departments. Each department has a unique
name, a unique number, and a particular employee who manages the
department. A department may have several locations.
• A department controls a number of projects; each of which has a unique
name, a unique number, and a single location.
• We store each employee’s name, emp_id, address, salary, sex, and
birth_date. An employee is assigned to one department, but may work on
several projects, which are not necessarily controlled by the same
department. We also keep track of the direct supervision of each
employee.
• We want to keep track of the dependants of each employee for insurance
purpose. We keep each dependant’s name, sex, birth_date, and relationship
to the employee.
Figure 1.2 Schema Diagram for the company relational database schema
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:- D2
Roll no:- 11233003
PRACTICAL: - 02
Aim: - To Draw Relational model corresponding to ER model.
Following are the commands for creating tables in database.
Table name:-
1. Employee 2. Department 3. Dept_Locations 4. Project 5. Works_On 6. Dependent
1. Creating database:-
create database my_database;
2.For using Database:-
use my_database;
Fig.1.1
3.For showing databases we use the following syntax
show databases;
Fig. 1.2
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:- Yadavi Verma Section:- D2
Roll no:- 11233003
Fig. 1.3
Department table
Fig. 1.4
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:- D2
Roll no:- 11233003
Dept_Locations
Fig.1.5
Project
Fig. 1.6
Works_On
Fig.1.7
Dependent
Fig.1.8
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:- D2
Roll no:- 11233003
Fig.1.9
6.For delete a table we use Drop command and the syntax for deleting is
drop table dependent;
Fig.1.10
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:- Yadavi Verma Section:- D2
Roll no:-11233003
Department-
Dept_locations-
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:- Yadavi Verma Section:- D2
Roll no:- 11233003
Project-
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:- Yadavi Verma Section:- D2
Roll No:- 11233003
PRACTICAL-03
AIM: Write MySQL commands to alter the structure of tables in the database.
• SYNTAX-
a) To add new column in tables:-
Alter table table_name
Add column_name datatype(size);
PRACTICAL-04
AIM: Write MySQL commands to implement integrity constraints.
MySQL Constraints:-
The constraint in MySQL is used to specify the rule that allows or restricts what values/data
will be stored in the table. They provide a suitable method to ensure data accuracy and integrity
inside the table. It also helps to limit the type of data that will be inserted inside the table. If
any interruption occurs between the constraint and data action, the action is failed.
This constraint specifies that the column cannot have NULL or empty values. The below
statement creates a table with NOT NULL constraint.
b) CHECK Constraint:-
It controls the value in a particular column. It ensures that the inserted value in a column
must be satisfied with the given condition. In other words, it determines whether the value
associated with the column is valid or not with the given condition.
c) UNIQUE Constraint:-
This constraint ensures that all values inserted into the column will be unique. It means a
column cannot stores duplicate values. MySQL allows us to use more than one column with
UNIQUE constraint in a table. The below statement creates a table with a UNIQUE
constraint:
This constraint is used to identify each record in a table uniquely. If the column contains
primary key constraints, then it cannot be null or empty. A table may have duplicate
columns, but it can contain only one primary key. It always contains unique value into a
column.
e) DEFAULT Constraint:-
This constraint is used to set the default value for the particular column where we have not
specified any value. It means the column must contain a value, including NULL.
PRACTICAL-05
AIM: Write MySQL commands to update records in the table.
Syntax:-
a) To create table in database
Column_name1 datatype(size),
Column_name2 datatype(size));
Values(‘value1’, ‘value2’);
PRACTICAL: - 06
AIM: -To select data from Student table using various Operators.
Introduction:
AND operator:
The AND operator displays a record if all the conditions separated by AND are TRUE.
Syntax:
select * from student
where s_id=112227678 and s_name=”Bijay”;
Fig.6.1
OR operator:
The OR operator displays a record if any of the conditions separated by OR is TRUE.
Syntax:
select * from student
where s_id=112227678 or s_name=”Ajay”;
Fig.6.2
NOT operator:
The NOT operator displays a record if the condition(s) is NOT TRUE.
Syntax:
select s_id,s_name,c_id from student
where marks not in(27,74,60);
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:- D2
Roll No:-11233003
Fig.6.3
LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
The percent sign (%) represents zero, one, or multiple characters
Syntax:
select * from student
where s_name like “A%”;
Fig.6.4
The underscore sign (_) represents one, single character
The percent sign and the underscore can also be used in combinations!
Syntax:
select s_id,s_name,c_id from student
where s_name like “_%y”;
Fig.6.5
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:-D2
Roll No:-11233003
IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
Syntax:
select s_id,s_name,c_id from student
where marks in(27,74,60);
Fig.6.6
BETWEEN Operator
The BETWEEN operator selects values within a given range.
Syntax:
select s_id,s_name,c_id from student
where marks between 40 and 60;
Fig.6.7
Comparison Operators (<, >, <=, >=):
These operators are used to compare values.
Greater than(>):
Syntax:
select * from student
where marks > 40;
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:- D2
Roll no:- 11233003
Fig.6.8
less than(<):
Syntax:
select * from student
where marks < 40;
Fig.6.9
Greater Than Equal To(>=):
Syntax:
select * from student
where marks >= 40;
Fig.6.10
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:-D2
Roll No:-11233003
Fig.6.11
Not Equal to(<>):
Syntax:
select * from student
where marks <> 27;
Fig.6.12
IS NULL Operator:
This operator is used to check for NULL values.
Syntax:-
SELECT * FROM Student WHERE Address IS NULL;
Fig.6.13
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:-D2
Roll no:- 11233003
Fig.6.14
Arithmetic Operators
ADD (+)
Syntax:
select s_name,marks+10 from student
where s_name in (“Ajay”,”Crees”);
Fig.6.15
Subtract(-)
Syntax:
select s_name,marks-10 from student
where s_name in (“Ajay”,”Crees”);
Fig.6.16
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:- Yadavi Verma Section:- D2
Roll No:-11233003
Multiply(*)
Syntax:
select s_name,marks*20 from student
where s_name in (“Ajay”,”Crees”);
Fig.6.17
Divide(/)
Syntax:
select s_name,marks/10 from student
where s_name in (“Ajay”,”Crees”);
Fig.6.18
Modulo(%)
Syntax:
select s_name,marks%10 from student
where s_name in (“Ajay”,”Crees”);
Fig.6.19
Roll No:-11233003
PRACTICAL:07
AIM: To implement various JOIN Operations.
1) Natural join:-
Natural join is an SQL join operation that creates a join on the base of the common
columns in the tables.
Syntax:-
SELECT Employee.Dnumber, Employee.Fname, Employee.Ssn
FROM Employee
NATURAL JOIN department;
2) Inner Join:
The INNER JOIN keyword selects all rows from both the tables as long as the condition
is satisfied. This keyword will create the result-set by combining all rows from both the
tables where the condition satisfies i.e value of the common field will be the same.
Syntax:-
SELECT Employee.Dnumber, Employee.Fname, Employee.Ssn
FROM Employee
INNER JOIN department ON Employee.Dnumber = department.Dnumber;
Roll No:-11233003
3) Left Join:
This join returns all the rows of the table on the left side of the join and matches rows for
the table on the right side of the join. For the rows for which there is no matching row on
the right side, the result-set will contain null. LEFT JOIN is also known as LEFT
OUTER JOIN.
Syntax:-
SELECT Employee.Dnumber, Employee.Fname, Employee.Ssn
FROM Employee
LEFT JOIN department ON Employee.Dnumber = department.Dnumber;
Roll No:-11233003
4) Right Join:
RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the
right side of the join and matching rows for the table on the left side of the join. For the
rows for which there is no matching row on the left side, the result-set will contain null.
RIGHT JOIN is also known as RIGHT OUTER JOIN.
Syntax:-
SELECT Employee.Dnumber, Employee.Fname, Employee.Ssn
FROM Employee
RIGHT JOIN department ON Employee.Dnumber = department.Dnumber;
5) Full Join:
FULL JOIN creates the result-set by combining results of both LEFT JOIN and RIGHT
JOIN. The result-set will contain all the rows from both tables. For the rows for which
there is no matching, the result-set will contain NULL values.
Syntax:-
SELECT Employee.Dnumber, Employee.Fname, Employee.Ssn
FROM Employee
LEFT JOIN department ON Employee.Dnumber = department.Dnumber
UNION
SELECT department.Dnumber, Employee.Fname, Employee.Ssn
FROM department
LEFT JOIN Employee ON Employee.Dnumber = department.Dnumber;
Roll No:-11233003
PRACTICAL:-08
GROUP BY COMMANDS
The GROUP BY statement groups rows that have the same values into summary
rows, like "find the number of customers in each country".
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Creating table employee;
Queries:
Query 1:
SELECT COUNT(Empid), Deptid
FROM Employee
GROUP BY Deptid;
Query 2:
SELECT Deptid, Ename
FROM Employee
ORDER BY Ename DESC;
Query 3:
SELECT COUNT(Empid), Deptid
FROM Employee
GROUP BY Deptid
ORDER BY COUNT(Empid) DESC;
ROLL NO: 11233003
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Queries:
Query 4:
SELECT COUNT(Empid), Deptid
FROM Employee
GROUP BY Deptid
HAVING COUNT(Empid) > 1;
ROLL NO: 11233003
Query 5:
SELECT COUNT(Empid), Deptid
FROM Employee
GROUP BY Deptid
HAVING COUNT(Empid) > 1
ORDER BY COUNT(Empid) DESC;
Query 6:
SELECT Max(salary), Deptid
FROM Employee
GROUP BY Deptid
HAVING Max(salary) > 35000;
Query 7:
SELECT Min(salary), Deptid
FROM Employee
GROUP BY Deptid
HAVING Min(salary) < 30000;
ROLL NO: 11233003
Query 8:
SELECT AVG(salary), Deptid
FROM Employee
GROUP BY Deptid
HAVING AVG(salary) > 30000;
Query 9:
SELECT Sum(salary), Deptid
FROM Employee
GROUP BY Deptid
HAVING Sum(salary) > 50000;