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

Dbms edited file

The document outlines various practical exercises related to database management, specifically focusing on creating and manipulating a company database using ER and relational models. It includes SQL commands for creating tables, altering structures, implementing integrity constraints, updating records, and performing various join operations. Additionally, it covers the use of operators for data selection and aggregation in SQL.

Uploaded by

arpiarpi1663
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)
1 views

Dbms edited file

The document outlines various practical exercises related to database management, specifically focusing on creating and manipulating a company database using ER and relational models. It includes SQL commands for creating tables, altering structures, implementing integrity constraints, updating records, and performing various join operations. Additionally, it covers the use of operators for data selection and aggregation in SQL.

Uploaded by

arpiarpi1663
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/ 33

Type your text

ROLL NO: 11233003

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.

Company Database ER model:-

Figure 1.1: ER Model


ROLL NO: 11233003

Company Database ER Diagram Mapped into Relational Database

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

4. For creating table:-


create table table_name(
col1 int(size),
col2 varchar(size),
col3 date,
col4 time);
Following are the tables we create using SQL commands using command line client:-
Employee Table

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

5.For showing all tables we use the following command


show tables;

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

7.describe the structure of the table :-


Employee-

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);

b) To change coumn name in table:-


Alter table table_name
Change old_colname new_colname datatype(size);
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma Section:- D2
Roll No:-11233003
c) To modify column in table:-
Alter table table_name
Modify table_name data_type(size);

d) To change the table name:-


Alter table table_name
Rename to new_table_name;
Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:- Yadavi Verma Section:- D2
Roll No:-11233003

e) To delete column from table:-


Alter table table_name
Drop column_name;
Roll no:-11233003

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.

Constraints used in MySQL:-

a) NOT NULL Constraint:-

This constraint specifies that the column cannot have NULL or empty values. The below
statement creates a table with NOT NULL constraint.

➢ CREATE TABLE Products (

ProductID INT PRIMARY KEY,

ProductName VARCHAR(50) NOT NULL );

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.

➢ CREATE TABLE Employees (

EmployeeID INT PRIMARY KEY,

Salary DECIMAL(10, 2),

CHECK (Salary >= 0));


Roll No:-11233003

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:

➢ CREATE TABLE Students (

StudentID INT PRIMARY KEY,

Email VARCHAR(50) UNIQUE );

d) PRIMARY KEY 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.

➢ CREATE TABLE Employees (

EmployeeID INT PRIMARY KEY,


Roll No:-11233003

Name VARCHAR(50), ...);

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.

➢ CREATE TABLE Orders (

OrderID INT PRIMARY KEY,

OrderDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP );


Roll No:-11233003

f) Foreign key constraint:


The FOREIGN KEY constraint is used to prevent actions that would destroy
links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.
Syntax:-
ALTER TABLE orders
ADD CONSTRAINT fk_product
FOREIGN KEY (ProductID)
REFERENCES products(ProductID);
Roll No:-11233003

PRACTICAL-05
AIM: Write MySQL commands to update records in the table.

Syntax:-
a) To create table in database

Create table table_name(

Column_name1 datatype(size),

Column_name2 datatype(size));

b) To insert values in table

Insert into table_name(attributename1,attributename2)

Values(‘value1’, ‘value2’);

Update commands used in table:-

c) To update value of Sname attribute where sid =100 in Student table.


Update Student
Set Sname=”Rohit kumar”
Where Sid=100;
ROLL_NO: 11233003

d) To update value of DOB attribute where sid=100 in student table.


Update Student
Set DOB= “2003-06-22”
Where Sid=100;

e) To update value of marks and Address where Sid=100 in a Student table


Update Student
Set marks=”92”, Address= “State-bihar,pin-841219,india”
Where Sid=100;

f) To update value of Sname where Sid=101 in a Student table


Update Student
Set Sname=”Praful”
Where Sid=101;

Final table values:-


Maharishi Markandeshwar deemed to be University, Mullana,Ambala
Name:-Yadavi Verma
Roll no:- 11233003

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

Type your text

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);

Type your text

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

Less Than Equal To(<=):


Syntax:
select * from student
where marks <= 40;

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

Equality Operator (=):


This operator is used to match exact values.
Syntax:
select * from student
where marks = 60;

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

Fig. Full Join


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".

The GROUP BY statement is often used with aggregate functions


(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more
columns.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Creating table employee;

Inserting data into employee


ROLL NO: 11233003

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

The MySQL HAVING Clause


The HAVING clause was added to SQL because the WHERE keyword cannot be
used with aggregate functions.

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;

You might also like