DBMS Assignment
DBMS Assignment
ID : O22BCA11088
Semester : Semester III
Name : Zeba Fatima
Database Management System
Question 1
1. Create a new database with name upgraddbms. If a
database of this name already exists in your MySQL
server, choose some other name for this database.
2. Now create 3 tables with the following attributes and
their data-types. Remember thatattributes with
underline are primary key attributes.
a. product (product_id int, name varchar(50),
quantity int, price float, sellervarchar(50))
b. customer (cust_id int, name varchar(50), address
varchar(100))
c. purchase (cust_id int, product_id int, dop date).
In the purchase table, cust_id and product_id are
foreign key dependencies in customer and
producttables respectively.
3. After creation of the tables, we realize that the
product table must have another attribute“unit” after
quantity. Write the “alter table” command to add this
attribute after “quantity”.
Step 1: Create a New Database
CREATE DATABASE IF NOT EXISTS upgraddbms;
USE upgraddbms;
Step 2: Create Tables
a. Product Table
CREATE TABLE product (
product_id INT PRIMARY KEY,
name VARCHAR(50),
quantity INT,
price FLOAT,
seller VARCHAR(50)
);
b. Customer Table
To achieve this using the GROUP BY and HAVING clauses in MySQL, you
can use the following query:
SELECT cust_id, COUNT(*) AS total_purchases FROM purchase GROUP
BY cust_id HAVING total_purchases > 2;
This query does the following:
1. Groups the rows in the "purchase" table by the "cust_id" column
using the GROUP BY clause.
2. Counts the number of purchases for each customer using the
COUNT(*) function.
3. Filters the results using the HAVING clause to include only those
customers who have made more than 2 purchases.
Adjust the table and column names based on your actual database
schema.