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

Database Lab 2

database management system lab csit 4th sem

Uploaded by

qccqstha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Database Lab 2

database management system lab csit 4th sem

Uploaded by

qccqstha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Lab no: 3 Date: 2024/ 08/ 04

Title: Structured Query Language (SQL) commands.

Objective: To practice and implement SQL “JOIN” commands.

Theory: A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.

Here are the different types of the JOINs in SQL:

 LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table.
 RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table.
PROCEDURE:

1. Firstly, we are using MySQL database for our practical.


2. Now, we use MySQL to create a database named “student” and use the database to meet our
objective.
3. Firstly, creating the database and using it to create tables.
CREATE DATABASE student;
USE student;

4. Now, we create the tables and add the value into the table to fulfill our objective of implementing
the “JOIN” clause in the database.

CREATE TABLE course (


course_id INT PRIMARY KEY,
course_name VARCHAR(100)
);
CREATE TABLE student (
student_id INT PRIMARY KEY,
student_name VARCHAR(100),
course_id INT,
FOREIGN KEY (course_id) REFERENCES course(course_id)
);

5. Now we insert the values into the respective tables.


6. After inserting the values into the respective tables, we firstly perform the left join into the tables.

USE student;
SELECT s.std_id, s.std_name, c.course_id, c.course_name
FROM student s
JOIN course c ON s.course_id = c.course_id;

As we can see we have performed the “LEFT JOIN” which provides us with the
values of the student table dominant and course table values on the right side which
have been discarded.

7. After the “LEFT JOIN” we now implement the “RIGHT JOIN”.

USE student;
SELECT s.std_id, s.std_name, c.course_id, c.course_name
FROM student s
RIGHT JOIN course c ON s.course_id = c.course_id;

As we can see after the “RIGHT JOIN” the course table values are dominant than
the student table values.
CONCLUSION:
Hence, we have successfully implemented the
“JOIN” clause in our SQL database.
Lab no: 4 Date: 2024/ 08/ 08

Title: Structured Query Language (SQL) commands.

Theory: A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.

Here are the different types of the JOINs in SQL:

 LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table.
 RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table.

OBJECTIVE: Create a database with a Course Table, Students Table and Marks Table
and assign each students all the subjects.

KEY OBJECTIVE: Assign all subjects to all the Students in the Table.

PROCEDURE:
1. Firstly, we are using MySQL database for our practical.
2. Now, we use MySQL to create a database named “mbmc” and use the database to meet our
objective.
3. Firstly, creating the database and using it to create tables.

CREATE DATABASE IF NOT EXISTS mbmc;


USE mbmc;

4. Now, we create the tables and add the value into the table to fulfill our objective of assigning
the subjects to the students.
5. Firstly, we create COURSE Table, STUDENTS Table and MARKS Table
respectively.

CREATE TABLE IF NOT EXISTS course (


course_id INT,
course_name VARCHAR(100),
PRIMARY KEY (course_id)
);

CREATE TABLE IF NOT EXISTS students (


std_id INT,
std_name VARCHAR(100),
PRIMARY KEY (std_id)
);

CREATE TABLE marks (


std_id INT,
course_id INT,
marks INT DEFAULT NULL,
PRIMARY KEY (std_id, course_id),
FOREIGN KEY (std_id) REFERENCES students(std_id),
FOREIGN KEY (course_id) REFERENCES course(course_id)
);

NOTE: WE HAVE MADE THE FOREIGN KEY REFERENCES IN THE MARKS


TBALE FROM BOTH COURSE AND STUDENT TABLES.

6. Now, inserting the values to work with and to meet our objectives.

INSERT INTO course (course_id, course_name) VALUES


(1, 'Mathematics'),
(2, 'Science'),
(3, 'History')
ON DUPLICATE KEY UPDATE course_name=VALUES(course_name);

INSERT INTO students (std_id, std_name) VALUES


(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie')
ON DUPLICATE KEY UPDATE std_name=VALUES(std_name);

SELECT * FROM marks;


DELETE FROM marks WHERE std_id IN (1, 2, 3) AND course_id IN (1, 2, 3);

7. Now, we perform the join opeation to join the table.

INSERT INTO marks (std_id, course_id, marks)


SELECT students.std_id, course.course_id, NULL
FROM students
CROSS JOIN course;

8. Now we update the marks to get our objectives.


UPDATE marks SET marks = 20 WHERE std_id = 1 AND course_id = 1;
UPDATE marks SET marks = 30 WHERE std_id = 2 AND course_id = 2;
UPDATE marks SET marks = 70 WHERE std_id = 3 AND course_id = 3;

CONCLUSION:
Hence, we met our objective and assigned all the course to all the students.
Lab no: 5 Date: 2024/ 08/ 09

Title: Structured Query Language (SQL) commands.

Objective: Construct a database with Tables Customer (Cno, Cname, Caddress, Ccontact),
Purchase (Cno, Pid) & Product (Pid, Pname, price, quantity).
a. Find the names of all products having price 1000.
b. Find the name of those customers who purchased Dell Laptop
c. Find the total number of products purchased by customer ‘Ram’
d. Increase price of all products by 5%
e. Find total price of Apple Mobiles.

PROCEDURE:

1. Firstly, we are using MySQL database for our practical.


2. Now, we use MySQL to create a database named “record” and use the database to meet our
objective.
3. Firstly, creating the database and using it to create tables.

CREATE DATABASE RECORD;


USE RECORD;
4. Now, we create the tables and add the value into the table to fulfill our objectives.

CREATE TABLE IF NOT EXISTS CUSTOMER(


CNO INT PRIMARY KEY,
CNAME VARCHAR(100),
CADDRESS VARCHAR(100),
CCONTACT VARCHAR(20)
);

CREATE TABLE IF NOT EXISTS PRODUCT(


PID INT PRIMARY KEY,
PNAME VARCHAR(100),
PRICE INT,
QUANTITY INT
);

CREATE TABLE IF NOT EXISTS PURCHASE(


PURCHASE_ID INT PRIMARY KEY,
CNO INT,
PID INT,
FOREIGN KEY (CNO) REFERENCES CUSTOMER(CNO),
FOREIGN KEY (PID) REFERENCES PRODUCT(PID)
);
NOTE: THE TABLE PURCHASE HAS FOREIGN KEY REFERENCES FROM
OTHER 2 TABLES CUSTOMER AND PRODUCT.

5. Now, after the tables have been formed, we enter the value respectively in the tables.
 TABLE CUSTOMER:

 TABLE PRODUCT:

 TABLE PURCHASE:

6. Now for the first objective “a” we solve it by running the following query:

SELECT * FROM PRODUCT WHERE PRICE =1000;


7. Now, for the objective no “b” we run the following query:

SELECT C.CNAME FROM PURCHASE P


JOIN PRODUCT PR ON P.PID =PR.PID
JOIN CUSTOMER C ON P.CNO = C.CNO
WHERE PR.PNAME = 'DELL LAPTOP'

8. Now for the objective no “c” we run the following query:

USE RECORD;
SELECT COUNT(PH.PID) AS TOTALPRODUCTSPURCHASED
FROM PURCHASE PH
JOIN PRODUCT P ON PH.PID = P.PID
JOIN CUSTOMER C ON C.CNO = PH.CNO
WHERE C.CNAME = 'RAM';
9. Now for the objective no “d” we run the following query:

USE RECORD;
UPDATE PRODUCT
SET PRICE = PRICE * 1.05
WHERE PID IN (2001,2002,2003)

10. Now for the objective no “e” we run the following query:

USE RECORD;
SELECT SUM (PRICE * QUANTITY) AS TOTALPRICE
FROM PRODUCT
WHERE PNAME = 'APPLE MOBILE';

CONCLUSION:
Hence, we met our objective & displayed all the values and tasks asked by the
question.
Lab no: 6 Date: 2024/ 08/ 09

Title: Structured Query Language (SQL) commands.

Objective: Retrieve Tname, Sname, Sphone for "ABC" school using SQL.

PROCEDURE:

1. Firstly, we are using MySQL database for our practical.


2. Now, we use MySQL to create a database named “record” and use the database to meet our
objective.
3. Firstly, creating the database and using it to create tables.

CREATE DATABASE RECORD;


USE RECORD;
4. After, creating the database we create the tables according to the objective respectively.

CREATE TABLE TEACHER (


TID INT,
TNAME VARCHAR(20),
TADDRESS VARCHAR(20),
TQUALIFICATION VARCHAR(20),
PRIMARY KEY (TID)
);

CREATE TABLE SCHOOL (


SID INT,
SNAME VARCHAR(20),
SADDRESS VARCHAR(20),
SPHONE BIGINT,
PRIMARY KEY (SID)
);

CREATE TABLE SCHOOL_TEACHER (


NO_OF_PERIOD INT,
SID INT,
TID INT,
FOREIGN KEY (SID) REFERENCES SCHOOL(SID),
FOREIGN KEY (TID) REFERENCES TEACHER(TID)
);
NOTE: HERE THE TABLE SCHOOL_TEACHER CONTAINS THE FOREIGN KEY
REFRENCE TO THE OTHER TABLES SCHOOL AND TEACHER RESPECTIVELY.
5. Now, after the tables have been formed, we now enter the values into the table according to
the need of the question and our need to meet our objectives.

 TABLE SCHOOL:

 TABLE TEACHER:

 TABLE SCHOOL_TEACHER:

USE RECORD;
SELECT * FROM RECORD.SCHOOL_TEACHER;
INSERT INTO SCHOOL_TEACHER (NO_OF_PERIOD, SID, TID)
VALUES (5, 1001, 2001);
6. Now after inserting the respective values into the Tables, we join the Tables SCHOOL &
TEACHER with SCHOOL_TEACHER to fulfill our objective.

SELECT
T.TNAME,
S.SNAME,
S.SPHONE
FROM
SCHOOL_TEACHER ST
JOIN SCHOOL S ON S.SID=ST.SID
JOIN TEACHER T ON T.TID=ST.TID
WHERE
S.SNAME = 'ABC'

CONCLUSION:
Hence, we met our objective & retrieved Tname, Sname, Sphone for "ABC" school
using SQL.
Lab no: 7 Date: 2024/ 08/ 09

Title: Structured Query Language (SQL) commands.

Objective: Consider a banking database with three labels and primary key underlined as given
below:

Customer (CustomerID , CustomerName, Address, Phone, Email)

Borrows (CustomerID, LoanNumber )

Loan ( LoanNumber , LoanType, Amount )

Write both relational algebra and SQL queries:

a) To display name of all customers who live in “Lalitpur” in ascending order of name.
b) To count total number of customers having loan at the bank.
c) To find name of those customers who have loan amount greater than or equal to 500000.
d) To find average loan amount of each accoun’t type.

PROCEDURE:

1. Firstly, we are using MySQL database for our practical.


2. Now, we use MySQL to create a database named “banking” and use the database to meet our
objective.
3. Firstly, creating the database and using it to create tables.

CREATE DATABASE BANKING;


USE BANKING;

4. After creating the database, we are creating tables necessary for meeting our
objectives.

CREATE TABLE CUSTOMER(


CID INT,
CNAME VARCHAR(40),
CADDRESS VARCHAR(40),
CPHONE BIGINT,
CEMAIL VARCHAR(30),
PRIMARY KEY(CID)
);

CREATE TABLE LOAN(


LOAN_NUMBER BIGINT,
LOAN_TYPE VARCHAR(30),
LAMOUNT BIGINT,
PRIMARY KEY(LOAN_NUMBER)
);
CREATE TABLE BORROW(
CID INT,
LOAN_NUMBER BIGINT,
FOREIGN KEY (CID) REFERENCES CUSTOMER(CID),
FOREIGN KEY (LOAN_NUMBER) REFERENCES LOAN(LOAN_NUMBER)
);

NOTE: THE TABLE BORROW HAS THE FOREIGN KEY REFERENCES


FROM TABLE CUSTOMER AND LOAN.

5. After creating the tables, we are going to insert the values into the table necessary for
meeting our objectives.

 TABLE CUSTOMER:

 TABLE LOAN:
 TABLE BORROW:

USE BANKING;
SELECT * FROM BANKING.BORROW;
INSERT INTO BORROW (CID, LOAN_NUMBER) VALUES
(1, 1000), (2,1001), (3,1002), (4,1003);

6. Now, after the values have been inserted, we are going to insert the queries required to meet
our objectives.

a) For meeting the objective no ‘a’ we are going to enter the following query:

SELECT CNAME FROM


CUSTOMER WHERE CADDRESS ='LALITPUR'
ORDER BY CNAME ASC
b) For meeting the objective no ‘b’ we are going to enter the following query:

SELECT COUNT(C.CID)
FROM BORROW B
JOIN CUSTOMER C ON B.CID=C.CID
JOIN LOAN L ON B.LOAN_NUMBER=L.LOAN_NUMBER

c) For meeting the objective no ‘c’ we are going to enter the following query:

SELECT C.CNAME
FROM BORROW B
JOIN CUSTOMER C ON C.CID=B.CID
JOIN LOAN L ON L.LOAN_NUMBER=B.LOAN_NUMBER
WHERE L.LAMOUNT>=500000
d) For meeting the objective no ‘d’ we are going to enter the following query:

SELECT DISTINCT LOAN_TYPE,AVG(LAMOUNT)


FROM LOAN
GROUP BY LOAN_TYPE

CONCLUSION:
Hence, all the objectives that was provided by the question have been
met.

You might also like