0% found this document useful (0 votes)
57 views15 pages

Dbms Lab

The document describes creating a database called "hamropasal" with multiple tables and attributes. It provides the SQL code to: 1. Create the database and tables like Customers, Categories, Employees, etc. with the relevant attributes. 2. Insert data into the tables using INSERT statements. 3. Perform CRUD operations like UPDATE, DELETE, SELECT on the tables in two different labs. The tasks covered are creating tables, inserting data, updating records, deleting records, and selecting records from the tables. SQL code is given for each task.

Uploaded by

For space
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)
57 views15 pages

Dbms Lab

The document describes creating a database called "hamropasal" with multiple tables and attributes. It provides the SQL code to: 1. Create the database and tables like Customers, Categories, Employees, etc. with the relevant attributes. 2. Insert data into the tables using INSERT statements. 3. Perform CRUD operations like UPDATE, DELETE, SELECT on the tables in two different labs. The tasks covered are creating tables, inserting data, updating records, deleting records, and selecting records from the tables. SQL code is given for each task.

Uploaded by

For space
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/ 15

DBMS LAB WORK:

LAB 1: TO CREATE A DATABASE hamropasal WITH MULTIPLE


TABLES/ENTITIES AND ATTRIBUTES AS SHOWN BELOW:

Database Name: hamropasal


Table: Customers

Table: Categories

Table: Employees
Table: OrderDetails

Table: Orders

Table: Products
Table: Shippers

Table: Suppliers
SQL CODE TO MAKE THIS DATABASE IS AS BELOW:

 TO CREATE A DATABASE:
CREATE DATABASE hamropasal;

 TO CREATE TABLE ‘Customers’:


CREATE TABLE Customers
(
CustomerID int not null,
CustomerNamevarchar(50),
ContactNamevarchar(50),
Address varchar(250),
City varchar(50),
PostalCode varchar(50),
Country varchar(50),
PRIMARY KEY(CustomerID)
);

 TO INSERT DATA INTO ‘Customers’ TABLE:


INSERT INTO customers VALUES(1,'Alfreds Futterkiste','Alfreds Schmidt','Obere Str.
57','Frankfurt','12209','Germany');
INSERT INTO customers VALUES(2,'Ana Trujillo Emparedados y helados','Ana
Trujillo','Avda. de la Constitución 2222','México D.F.','05021','Mexico');
INSERT INTO customers VALUES(3,'Antonio Moreno Taqueria','Antonio
Moreno','Mataderos 2312','México D.F.','05023','Mexico');
INSERT INTO customers VALUES(4,'Around the Horn','Thomas Hardy','120 Hanover
Sq.','London','WA1 1DP','UK');
INSERT INTO customers VALUES(5,'Berglunds snabbkop','Christina
Berglund','Brguvsvagen 8','Lulea','S-958 22','Sweden');
INSERT INTO customers VALUES(6,'Blauer See Delikatessen','Hannna
Moos','Forsterstr. 57','Mannheim','68306','Germany');

 TO CREATE A TABLE ‘Categories’:


CREATE TABLE Categories(
CategoryID int not null,
CategoryNamevarchar(50),
Description varchar(50),
PRIMARY KEY(CategoryID)
);
 TO INSERT DATA INTO ‘Categories’ TABLE:
INSERT INTO categories VALUES(1,'Beverages','Soft drinks,coffees,teas,beers, and
ales');
INSERT INTO categories VALUES(2,'Condiments','Sweet and savory sauces, relishes,
spreads, and seasonings');
INSERT INTO categories VALUES(3,'Confections ','Desserts, candies, and sweet
breads');
INSERT INTO categories VALUES(4,'Dairy Products','Cheeses');

 TO CREATE A TABLE ‘Employees’:


CREATE TABLE Employees
(
EmployeeID int not null,
LastNamevarchar(50),
FirstName varchar(50),
BirthDate DATE,
Photo BLOB,
Notes varchar(500),
PRIMARY KEY(EmployeeID)
);

 TO INSERT DATA INTO ‘Employees’ TABLE:


INSERT INTO employees VALUES(1,'Davolio ','Nancy','1968-12-
08','EmpID1.pic','Education includes a BA in psychology from Colorrado State
University. She also completed(The Art of the Cold Call).Nancy is a member of
"Toastmasters International"');

INSERT INTO employees VALUES(2,'Fuller ','Andrew','1952-02-


19','EmpID2.pic','Andrew received his BTS commercial and a Ph.D. in international
marketing from the University of Dallas. He is fluent in French and Italian and reads
German. He joined the company as a sales representative, was promoted to sales
manager and was then named vice president of sales. Andrew is a member of the
Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific
Rim Importers Association');

INSERT INTO employees VALUES(3,'Leverling ','Janet','1963-08-


30','EmpID3.pic','Janet has a BS degree in chemistry from Boston College. She has
also completed a certificate program in food retailing management. Janet was
hired as a sales associate and was promoted to sales representative');
 TO CREATE A TABLE ‘Shippers’:
CREATE TABLE Shippers
(
ShipperID int NOT null,
ShipperNamevarchar(50),
Phone varchar(50),
PRIMARY KEY(ShipperID)
);

 TO INSERT DATA INTO TABLE ’Shippers’:


INSERT INTO shippers VALUES(1,'Speedy Express','(503)555-9831');
INSERT INTO shippers VALUES(2,'United Package','(503)555-3199');
INSERT INTO shippers VALUES(3,'Federal Shipping','(503)555-9931');

 TO CREATE A TABLE ’Suppliers’:


CREATE TABLE Suppliers (
SupplierID int NOT null,
SupplierNamevarchar(50),
ContactNamevarchar(50),
Address varchar(50),
City varchar(50),
Postalcodevarchar(50),
Country varchar(50),
Phone varchar(50),
PRIMARY KEY(SupplierID)
);

 TO INSERT DATA INTO TABLE ‘Suppliers’:


INSERT INTO suppliers VALUES(1, 'Exotic Liquid', 'Charlotte Copper', 'Gilgert St.',
'London', 'EC1 4SD', 'UK', '(171) 555-2222');
INSERT INTO suppliers VALUES(2, 'New Orleans Cajun Delights', 'Shelley Burke',
'P.O. Box 78934', 'New Orleans', '70117', 'USA', '(100) 555-4822');
INSERT INTO suppliers VALUES(3, "Grandma Kelley's Homestead", 'Regina Murphy',
'707 Oxford Rd.', 'Ann Arbor', '48104', 'USA', '(313) 555-5735');
INSERT INTO suppliers VALUES(4, 'Tokyo Traders', 'Yoshi Nagase', '9-8
SekimaiMusashino-shi', 'Tokyo', '100', 'Japan', '(03) 3555-5011');
INSERT INTO suppliers VALUES(5, "Cooperative de Queso 'Las Cabras'", 'Antonio del
Valle Saavedra', 'Calle del Rosal 4', 'Oviedo', '33007', 'Spain', '(98) 598 7654');
 TO CREATE A TABLE ‘Orders’:
CREATE TABLE Orders
(OrderID int NOT null,
CustomerID int,
EmployeeID int,
OrderDate date,
ShipperID int,
PRIMARY KEY(OrderID),
FOREIGN KEY(CustomerID) REFERENCES customers(CustomerID) ON DELETE
CASCADE,
FOREIGN KEY(EmployeeID) REFERENCES employees(EmployeeID) ON DELETE
CASCADE,
FOREIGN KEY(ShipperID) REFERENCES shippers(ShipperID) ON DELETE CASCADE);

 TO INSERT DATA INTO TABLE ‘Orders’:


INSERT INTO orders VALUES(10248,1,1,'1996-07-04',1);
INSERT INTO orders VALUES(10249,2,2,'1996-07-04',3);
INSERT INTO orders VALUES(10250,4,1,'1996-07-04',3);
INSERT INTO orders VALUES(10251,1,3,'1996-07-04',1);
INSERT INTO orders VALUES(10252,5,3,'1996-07-04',2);
INSERT INTO orders VALUES(10253,6,2,'1996-07-04',2);

 TO CREATE A TABLE ‘Products’:


CREATE TABLE Products
(
ProductID int NOT null,
ProductName varchar(50),
SupplierID int,
CategoryID int,
Unit varchar(50),
Price float,
PRIMARY KEY (ProductID),
FOREIGN KEY (SupplierID) REFERENCES suppliers(SupplierID) ON DELETE
CASCADE,
FOREIGN KEY (CategoryID) REFERENCES categories(CategoryID) ON DELETE
CASCADE
);
 TO INSERT DATA INTO TABLE ‘Products’:
INSERT INTO products VALUES(1,'Chais',1,1,'10 boxes x 20 bags',18);
INSERT INTO products VALUES(2,'Chang',1,1,'24-12 oz Boxes',19);
INSERT INTO products VALUES(3,'Aniseed Syrup',1,2,'12-550 ml Bottles',10);
INSERT INTO products VALUES(4,"Chef Anton's Cajun Seasoning",2,2,'48 - 6 oz
jars',22);
INSERT INTO products VALUES(5,"Chef Anton's Gumbo Mix",2,2,'36 Boxes',21.35);
INSERT INTO products VALUES(6,"Gradma's Boysenberry Spread",3,2,'12-8 oz
jars',25);
INSERT INTO products VALUES(7,"Uncle Bob's Organic Dried Pears",3,4,'12-1 lb
pkgs.',30);

 TO CREATE A TABLE ‘OrderDetails’:


CREATE TABLE OrderDetails
(
OrderDetailID int NOT null,
OrderID int,
ProductID int,
Quantity int NOT null,
PRIMARY KEY(OrderDetailID),
FOREIGN KEY (OrderID) REFERENCES orders(OrderID) ON DELETE CASCADE,
FOREIGN KEY (ProductID) REFERENCES products(ProductID) ON DELETE CASCADE
);

 TO INSERT DATA INTO TABLE ‘OrderDetails’:


INSERT INTO orderdetailsVALUES(1,10248,1,12);
INSERT INTO orderdetailsVALUES(2,10248,3,10);
INSERT INTO orderdetailsVALUES(3,10248,5,5);
INSERT INTO orderdetailsVALUES(4,10249,2,9);
INSERT INTO orderdetailsVALUES(5,10249,7,40);
INSERT INTO orderdetailsVALUES(6,10250,1,10);
LAB2: TO USE OF UPDATE AND SELECT IN DATABASE ’hamropasal’

Q.1 Update customername to Hari Bahadur of CustomerID number 5 in customer


table.
UPDATE customers SET CustomerName='Hari Bahadur' WHERE CustomerID=5;

Q.2 Add email column in customers table with varchar(255) datatype.


ALTER TABLE Customers
ADD Email varchar(255);

Q.3 Update categories to meat products and description to chicken,mutton,pork of


categoryID 3 in categories table.
UPDATE categories
SET CategoryName='Meat Products',Description='Chicken,Mutton,Pork'
WHERE CategoryID=3;

Q.4 Delete records of customerID 6 from customer table.


DELETE FROM customers WHERE CustomerID=6;

Q.5 Increment the quantity by 10% on orderdetails table.


UPDATE orderdetails
SET Quantity=Quantity*1.1;

Q.6 Display all records if every price is given a 20% raise in products table.
SELECT ProductID,ProductName,SupplierID,CategoryID,Unit,Price*1.2
FROM products;
Q.7 Display all records of products whose price is greater than 20.
SELECT * FROM products
WHERE Price>20;
LAB 3 : TO CREATE A DATABASE test WITH MULTIPLE TABLES AND
ATTRIBUTES AND USE OF CRUD OPERATION :

CREATE TABLE Employee {


Person_namevarchar(50),
Street varchar(50),
City varchar(50)
};

INSERT INTO `Employee`(`person_name`, `street`, `city`) VALUES ('Ram','Lindenwood','Pokhara');


INSERT INTO `Employee`(`person_name`, `street`, `city`) VALUES ('Rita','Sidney','Pokhara');
INSERT INTO `Employee`(`person_name`, `street`, `city`) VALUES ('Hari','Walker','Butwal');
INSERT INTO `Employee`(`person_name`, `street`, `city`) VALUES
('Brian','Ridgewood','Kathmandu');

CREATE TABLE Works{


Person_namevarchar(50),
Bank_namevarchar(50),
Salary int
};

INSERT INTO Works VALUES


(‘Ram,’Nepal Bank’,9000),
(‘Rita,’Small Bank’,11000),
(‘Hari,’Nepal Bank’,15000),
(‘Brian,’Nabil Bank’,13000),
CREATE TABLE Bank{
Bank_namevarchar(50),
City varchar(50) };

INSERT INTO `Bank`(`bank_name`, `city`) VALUES ('Nepal Bank','Pokhara');


INSERT INTO `Bank`(`bank_name`, `city`) VALUES ('Small Bank','Kathmandu');
INSERT INTO `Bank`(`bank_name`, `city`) VALUES ('Nabil Bank','Pokhara');
INSERT INTO `Bank`(`bank_name`, `city`) VALUES ('NMB Bank','Butwal');

Q.1 Find the sum of salary for each bank


 SELECT bank_name,SUM(salary)
FROM Works GROUP BY bank_name;

Q.2 Display the details of the employee that works for Nepal Bank and has salary
>10000.
 SELECT E.person_name,E.street,E.city
FROM Employee E, Works W
WHERE E.person_name=W.person_name AND W.bank_name='Nepal Bank' AND
W.salary>10000;

Q.3 Delete ‘Small Bank’ from works table.


 DELETE FROM Works
WHERE bank_name='Small Bank';
Q.2022
Q.1 Consider the relation Actress-Details and write the SQL statement for the
following queries.
i. Create the table Actress_details.

ii. Delete the data of actress whose recent release is Prem.


iii. Modify the database so that Renu's new release is "win the race" film.
iv. Insert a new record on the table above table.

Answer:
i. CREATE TABLE Actress_Details
(
Players_id int not null,
Actress_namevarchar(50),
Debut_year int,
Recent_releasevarchar(50),
Actress_fee int,
PRIMARY KEY (Players_id)
);
INSERT INTO actress_detailsVALUES(1, 'Renu', 2010, 'Samay', 400000);
INSERT INTO actress_detailsVALUES(2, 'Sita', 2022, 'Radha', 300000);
INSERT INTO actress_detailsVALUES(3, 'Geeta', 2001, 'Mato', 600000);
INSERT INTO actress_detailsVALUES(4, 'Amita', 1990, 'Man', 700000);
INSERT INTO actress_detailsVALUES(5, 'Karishma', 1989, 'Prem', 100000);

ii. DELETE FROM actress_details


WHERE Recent_release='Prem';
iii. UPDATE actress_details SET Recent_release='Win the race'
WHERE Actress_name='Renu';

iv. INSERT INTO actress_detailsVALUES(6,'Priya', 2004, 'Drishya', 500000);


Q.2019
Q.1 Consider a simple relational database of Hospital Management System.
Doctors (DoctorID, Doctor Name, Department, Address, Salary)
Patients (PatientID, Patent Name, Address, Age, Gender)
Hospitals (PatientID, Doctor ID, HostpitalName, Location)

i. Display ID of Patient admitted to hospital at Pokhara and whose name ends with
's'.
SELECT PatientID
FROM Hospitals H
INNER JOIN Patients P ON H.PatientID = P.PatientID
WHERE HospitalName = 'Pokhara' AND PatientName LIKE '%s';

ii. Delete the record of Doctors whose salary is greater than the average salary of
doctors.
- DELETE FROM Doctors
WHERE Salary > (SELECT AVG(Salary) FROM Doctors);

iii. Increase the salary of doctors by 18.5% who works in OPD department.
-UPDATE Doctors
SET Salary = Salary * 1.185
WHERE Department = 'OPD';

iv. Find the average salary of Doctors for each address who have average salary
more than 55K.
SELECT Address, AVG(Salary) AS AvgSalary
FROM Doctors
GROUP BY Address
HAVING AVG(Salary) > 55000;

You might also like