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

DBMS Assignment

The document outlines a graded assignment for a Database Management System course at Chandigarh University. It includes tasks such as creating a database and tables, adding attributes, inserting data, updating records, and writing SQL queries using GROUP BY and HAVING clauses. The assignment is structured into three main questions with specific SQL commands to be executed for each task.
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 views8 pages

DBMS Assignment

The document outlines a graded assignment for a Database Management System course at Chandigarh University. It includes tasks such as creating a database and tables, adding attributes, inserting data, updating records, and writing SQL queries using GROUP BY and HAVING clauses. The assignment is structured into three main questions with specific SQL commands to be executed for each task.
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/ 8

Chandigarh University

Graded Assignment – 1 (Database Management


System)

Graded Subjective 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

CREATE TABLE customer (


cust_id INT PRIMARY KEY,
name VARCHAR(50),
address VARCHAR(100)
);
c. Purchase Table

CREATE TABLE purchase (


cust_id INT,
product_id INT,
dop DATE,
FOREIGN KEY (cust_id) REFERENCES customer(cust_id),
FOREIGN KEY (product_id) REFERENCES product(product_id)
);

Step 3: Add the "unit" Attribute to the "product" Table

ALTER TABLE product


ADD COLUMN unit VARCHAR(20) AFTER quantity;
Question 2
1. Add the following row to the product table
(product_id int, name varchar(50), quantity int, unit
varchar(50), price float, seller varchar(50)). The values
are in thesame sequence as the attributes mentioned
a. 999, USB-C Charger, 1, pieces, 200, Electron
Traders
2. Add the following row to the customer table (cust_id
int, name varchar(50), addressvarchar(100)). The values
are in the same sequence as the attributes mentioned
a. 7777, “Deep Kasturi”, “Shivaji Nagar, Mumbai”
3. Update the cust_id of the row added in the previous
question from 7777 to 7788.

Step 1: Add a Row to the Product Table


INSERT INTO product (product_id, name, quantity, unit, price, seller)
VALUES (999, 'USB-C Charger', 1, 'pieces', 200, 'Electron Traders');
Step 2: Add a Row to the Customer Table
INSERT INTO customer (cust_id, name, address)
VALUES (7777, 'Deep Kasturi', 'Shivaji Nagar, Mumbai');

Step 3: Update the cust_id in the Customer Table


UPDATE customer
SET cust_id = 7788
WHERE cust_id = 7777;

These SQL commands perform the following actions:


1. Inserts a new row into the "product" table with the specified
values.
2. Inserts a new row into the "customer" table with the specified
values.
3. Updates the "cust_id" in the "customer" table from 7777 to 7788.
Make sure to execute these commands in the order provided to
achieve the desired results. Adjust the values and column names based
on your actual table structure and requirements.
Question 3: Write the following query using GROUP BY
and HAVING clauses in MySQL. From the purchase
table, group all the rows according to cust_ids. Choose a
cust_id if that customer has made more than 2
purchases, and also choose the total number of
purchases the customer has done

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.

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