0% found this document useful (0 votes)
9 views20 pages

Apurva Ag. Dbms

The document outlines a series of experiments related to database management, specifically focusing on MySQL operations such as creating tables, executing DDL and DML statements, and performing various SQL queries. It includes practical examples of creating databases, inserting, updating, and deleting records, as well as using aggregate functions and joins. The experiments cover both theoretical concepts and practical implementations in scenarios like college and company databases.

Uploaded by

arunntw2004
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)
9 views20 pages

Apurva Ag. Dbms

The document outlines a series of experiments related to database management, specifically focusing on MySQL operations such as creating tables, executing DDL and DML statements, and performing various SQL queries. It includes practical examples of creating databases, inserting, updating, and deleting records, as well as using aggregate functions and joins. The experiments cover both theoretical concepts and practical implementations in scenarios like college and company databases.

Uploaded by

arunntw2004
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/ 20

Apurva Agarwal 22SCSE1012878 Section26

Page Date Date Signature Remarks


S.No Experiment name no performed Submitted
DRAW AN E-R DIAGRAM AND CONVERT
1. ENTITIES AND RELATIONSHIPS TO A
RELATION TABLE FOR A GIVEN
SCENARIO

IMPLEMENT DDL STATEMENTS AND


DML STATEMENTS IN MYSQL FOR
THE FOLLOWINGS COMMANDS a)
CREATING SIMPLE TABLES b)
2.
CREATING SIMPLE TABLES
c)INSERTING/UPDATING/DELETING
RECORDS IN TABLE d)VIEWING ALL
TABLES IN A DATABASE
ALTERING A TABLE,
3. DROPPING/TRUNCATING/RENAMING
TABLES, ADDING A COLUMN
CHANGING COLUMN DATA TYPE,
SIZE,DROPPING A COLUMN
PERFORM THE FOLLOWING: a)
4. CREATING TABLES (WITH AND
WITHOUT CONSTRAINTS
(KEY/DOMAIN) b) CREATING TABLES
(WITH REFERENTIAL INTEGRITY
CONSTRAINTS).

FOR A GIVEN SET OF RELATION


5. SCHEMES, CREATE TABLES AND
PERFORM THE FOLLOWING
AGGREGATE FUNCTIONS
(MAX/MIN/SUM/AVG/COUNT).

FOR A GIVEN SET OF


6. RELATIONSCHEMES, CREATE TABLES
AND PERFORM THE FOLLOWING
QUERIES: a) INNER JOIN b) OUTER
JOIN SUBQUERIES- WITH IN CLAUSE,
WITH EXISTS CLAUSE

7. EXECUTE THE SELECT COMMAND WITH


DIFFERENT CLAUSES.
Apurva Agarwal 22SCSE1012878 Section26

EXPERIMENT 1

1.DRAW AN E-R DIAGRAM AND CONVERT ENTITIES AND


RELATIONSHIPS TO A RELATION
TABLE FOR A GIVEN SCENARIO. (TWO ASSIGNMENTS SHALL BE
CARRIED OUT I.E. CONSIDER
TWO DIFFERENT SCENARIOS (EG. BANK, COLLEGE)
1. COLLEGE DATABASE:

STUDENT (USN, SName, Address, Phone, Gender)


SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)

1. COMPANY DATABASE:

EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)


DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
Apurva Agarwal 22SCSE1012878 Section26
Apurva Agarwal 22SCSE1012878 Section26
Apurva Agarwal 22SCSE1012878 Section26

EXPERIMENT 2

2. IMPLEMENT DDL STATEMENTS AND DML STATEMENTS IN MYSQL


FOR THE FOLLOWINGS COMMANDS
a) CREATING DATABASE
Create database college;

b) CREATING SIMPLE TABLES

CREATE TABLE `college`.`student` (


`stu_id` INT NOT NULL,
`stu_name` VARCHAR(30) NOT NULL,
`stu_ph_no` BIGINT NOT NULL,
`stu_dob` DATE NOT NULL,
PRIMARY KEY (`stu_id`));

CREATE TABLE `college`.`branch` (


`brn_id` INT NOT NULL,
`brn_name` VARCHAR(45) NULL,
`brn_ctg` VARCHAR(45) NULL,
PRIMARY KEY (`brn_id`));

c) INSERTING/UPDATING/DELETING RECORDS IN TABLE

INSERTING
INSERT INTO `college`.`student` (`stu_id`, `stu_name`, `stu_ph_no`, `stu_dob`) VALUES
('911', 'Apurva Agarwal', '7012253752', '2005-03-20');
INSERT INTO `college`.`student` (`stu_id`, `stu_name`, `stu_ph_no`, `stu_dob`) VALUES ('912',
'Prachi Negi', '8549976278', '2005-01-10');
INSERT INTO `college`.`student` (`stu_id`, `stu_name`, `stu_ph_no`, `stu_dob`) VALUES ('913',
'Vinay Singh', '9972548677', '2004-05-10');
INSERT INTO `college`.`student` (`stu_id`, `stu_name`, `stu_ph_no`, `stu_dob`) VALUES ('914',
'Shreya Kumari', '8754987217', '2004-05-07');
INSERT INTO `college`.`student` (`stu_id`, `stu_name`, `stu_ph_no`, `stu_dob`) VALUES ('915',
' Priyanshu Yadav', '7842187549', '2005-04-20');
INSERT INTO `college`.`student` (`stu_id`, `stu_name`, `stu_ph_no`, `stu_dob`) VALUES ('916',
'Shruti Agarwal', '8754861747', '2004-10-12');
Apurva Agarwal 22SCSE1012878 Section26

INSERT INTO `college`.`branch` (`brn_id`, `brn_name`, `brn_ctg`) VALUES ('1001',


'Heisengton', 'Science');
INSERT INTO `college`.`branch` (`brn_id`, `brn_name`, `brn_ctg`) VALUES ('1002',
'JumbleNumbers', 'Mathematics');
INSERT INTO `college`.`branch` (`brn_id`, `brn_name`, `brn_ctg`) VALUES ('1003',
'HelloWorld', 'IT');

UPDATING

UPDATE student

SET stu_name='Priyanshu Raj'

WHERE stu_id='915';

UPDATE branch
SET brn_name=’Statistics’
WHERE brn_id=’1002’;
DELETING
DELETE FROM student
WHERE stu_id='914';
Apurva Agarwal 22SCSE1012878 Section26

DELETE FROM branch


WHERE brn_id='1001';

d)VIEWING ALL TABLES IN A DATABASE


SELECT * FROM college.student;

SELECT * FROM college.branch;


Apurva Agarwal 22SCSE1012878 Section26

EXPERIMENT 3
3.PERFORM THE FOLLOWING:
a) Altering a Table, Dropping/Truncating/Renaming Tables,
Altering a Table:
ALTER TABLE Course
ADD COLUMN semester VARCHAR(20);

Dropping a Table:
DROP TABLE Student;

Truncating a Table:
TRUNCATE TABLE STUDENT;

Renaming a Table:
ALTER TABLE branch
RENAME TO branches;

b) Adding a column
ALTER TABLE student
ADD COLUMN year_of_study INT;

Changing column data type, size


ALTER TABLE student
MODIFY COLUMN stu_ph_no
BIGINT;
c)Dropping a column
ALTER TABLE students
DROP COLUMN stu_dob;
Apurva Agarwal 22SCSE1012878 Section26

EXPERIMENT 4

4. PERFORM THE FOLLOWING:


a. Creating Tables (With and Without Constraints (Key/Domain)
=>table with constraints:
CREATE TABLE College (
college_id INT PRIMARY KEY,
college_code VARCHAR(20),
college_country VARCHAR(20) DEFAULT 'US'
);

Here, the default value of the college_country column is US.


If we try to store the NULL value in the college_country column, its value will be US.

=>table without constraints:


Creating a simple 'Employee' table without constraints
CREATE TABLE Employee (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary DECIMAL(10, 2),
hire_date DATE
);

b. Creating Tables (With Referential Integrity Constraints)

The syntax of the Master Table or Referenced table is:

1. CREATE TABLE Student (Roll int PRIMARY KEY, Name varchar(25) , Course varc
har(10) );

Here column Roll is acting as Primary Key, which will help in deriving the value of
foreign key in the child table.

The syntax of Child Table or Referencing table is:

2. CREATE TABLE Subject (Roll int references Student, SubCode int, SubName va
rchar(10) );
In the above table, column Roll is acting as Foreign Key, whose values are derived using the
Roll value of Primary key from Master table.
Apurva Agarwal 22SCSE1012878 Section26

EXPERIMENT 5
5. FOR A GIVEN SET OF RELATION SCHEMES, CREATE TABLES AND PERFORM THE
FOLLOWING AGGREGATE FUNCTIONS (MAX/MIN/SUM/AVG/COUNT).

The following SQL statement finds the sum of the "unit price" fields in
the "products" table:
Apurva Agarwal 22SCSE1012878 Section26

The following SQL command calculates the average quantity in stock.

This will produce the following result.

The above code will give us the minimum quantity in stock in the
products table.

The code depicted below will give us the maximum quantity in stock in
the products table.

This will produce the following result.


Apurva Agarwal 22SCSE1012878 Section26

EXPERIMENT 6

6. FOR A GIVEN SET OF RELATION SCHEMES, CREATE TABLES AND


PERFORM THE
FOLLOWING QUERIES:

CREATE TABLE professor(


ID int,
Name varchar(20),
Salary int
);
CREATE TABLE teaches(
course_id int,
prof_id int,
course_name varchar(20)
);

Inserting rows inside professor table –


INSERT INTO professor VALUES (1, 'Rohan', 57000);
INSERT INTO professor VALUES (2, 'Aryan', 45000);
INSERT INTO professor VALUES (3, 'Arpit', 60000);
INSERT INTO professor VALUES (4, 'Harsh', 50000);
INSERT INTO professor VALUES (5, 'Tara', 55000);

Inserting rows inside teaches table –


INSERT INTO teaches VALUES (1, 1, 'English');
INSERT INTO teaches VALUES (1, 3, 'Physics');
INSERT INTO teaches VALUES (2, 4, 'Chemistry');
INSERT INTO teaches VALUES (2, 5, 'Mathematics');
SELECT * FROM professor;
Output :
Apurva Agarwal 22SCSE1012878 Section26

ID Name Salary

1 Rohan 57000

2 Aryan 45000

3 Arpit 60000

4 Harsh 50000

5 Tara 55000
SELECT * FROM teaches;
Output :
course_id prof_id course_name

1 1 English

1 3 Physics

2 4 Chemistry

2 5 Mathematics

INNER JOIN Query :


SELECT teaches.course_id, teaches.prof_id, professor.Name,
professor.Salary
FROM professor INNER JOIN teaches ON professor.ID = teaches.prof_id;

Output
Using the Inner Join we are able to combine the information in the two tables
based on a condition and the tuples in the Cartesian product of the two tables
that do not satisfy the required condition are not included in the resulting
table.
course_id prof_id Name Salary

1 1 Rohan 57000
Apurva Agarwal 22SCSE1012878 Section26

course_id prof_id Name Salary

1 3 Arpit 60000

2 4 Harsh 50000

2 5 Tara 55000

b) Outer Join Subqueries- With IN clause, With EXISTS clause


Creating Tables:
Let's define two tables - 'Employees' and 'Departments':

CREATE TABLE Employees (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT
);

CREATE TABLE Departments (


department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);

Inserting Data:

INSERT INTO Departments VALUES (1, 'Sales');


INSERT INTO Departments VALUES (2, 'Marketing');
INSERT INTO Departments VALUES (3, 'Finance');
INSERT INTO Employees VALUES (1, 'John', 'Doe', 1);
INSERT INTO Employees VALUES (2, 'Jane', 'Smith', 2);
INSERT INTO Employees VALUES (3, 'David', 'Williams', 1);

Outer Join with IN Clause:


Retrieve employee names from the 'Employees' table who belong to
departments listed in the 'Departments' table:

SELECT first_name, last_name


FROM Employees
WHERE department_id IN (SELECT department_id FROM Departments);
Apurva Agarwal 22SCSE1012878 Section26

Outer Join with EXISTS Clause:


Retrieve employee names from the 'Employees' table who belong to
departments existing in the 'Departments' table:
SELECT first_name, last_name
FROM Employees e
WHERE EXISTS (
SELECT 1
FROM Departments d
WHERE e.department_id = d.department_id );

EXPERIMENT 7

7. EXECUTE THE SELECT COMMAND WITH DIFFERENT CLAUSES

CREATE TABLE:

CREATE TABLE Customer(

CustomerID INT PRIMARY KEY,


CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Apurva Agarwal 22SCSE1012878 Section26

Age int(2),
Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country,
Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain',
'Spain','22','xxxxxxxxxx');
Output:

SELECT Statement in SQL


SELECT CustomerName, LastName FROM Customer;
Output:

SELECT Statement with WHERE Clause:


Query:
SELECT CustomerName FROM Customer where Age = '21';
Output:
Apurva Agarwal 22SCSE1012878 Section26

SQL SELECT Statement with GROUP BY Clause


Query:
SELECT COUNT (item), Customer_id FROM Orders GROUP BY order_id;
Output:

SELECT Statement with HAVING Clause


Consider the following database for Having Clause:

Query:
SELECT Department, sum(Salary) as Salary
FROM employee
GROUP BY department
HAVING SUM(Salary) >= 50000;
Apurva Agarwal 22SCSE1012878 Section26

Output:

SELECT Statement with ORDER BY clause in SQL


Query:
SELECT * FROM Customer ORDER BY Age DESC;
Output:

DISTINCT Clause:
Query
CREATE TABLE students (
ROLL_NO INT,
NAME VARCHAR(50),
ADDRESS VARCHAR(100),
PHONE VARCHAR(20),
AGE INT
);
Inserting some random data to perform distinct operations.
Apurva Agarwal 22SCSE1012878 Section26

INSERT INTO students (ROLL_NO, NAME, ADDRESS, PHONE, AGE)


VALUES
(1, 'Shubham Kumar', '123 Main Street, Bangalore', '9876543210',
23),
(2, 'Shreya Gupta', '456 Park Road, Mumbai', '9876543211', 23),
(3, 'Naveen Singh', '789 Market Lane, Delhi', '9876543212', 26),
(4, 'Aman Chopra', '246 Forest Avenue, Kolkata', '9876543213',
22),
(5, 'Aditya Patel', '7898 Ocean Drive, Chennai', '9876543214',
27),
(6, 'Avdeep Desai', '34 River View, Hyderabad', '9876543215',
24);
Select * from students;
Output

Query:
SELECT DISTINCT NAME FROM Student;

Output:

LIMIT Clause:

Student Table:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);

INSERT INTO student (id, name, age)


VALUES (1, 'Shubham Thakur', 18),
Apurva Agarwal 22SCSE1012878 Section26

(2, 'Aman Chopra', 19),


(3, 'Bhavika uppala', 20),
(4,'Anshi Shrivastava',22);
Output:

Queries:
SELECT *
FROM student
LIMIT 3;
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy