0% found this document useful (0 votes)
150 views3 pages

JUW Summer 2021 Database Lab Task

This document provides instructions for a database systems lab task involving multiple tables. It contains 3 questions: 1. Write SQL queries to retrieve information from a sample products table, including finding maximum prices by supplier, minimum and maximum expiry dates, and average/sum/min/max unit prices. 2. Create tables for shops, customers, and shopping with the given column definitions and data types. 3. Add primary and foreign keys to the shopping table, write a JOIN query to retrieve titles and publisher data, and a subquery to find employee names working in IT. The task must be submitted individually on the university's LMS by the deadline.

Uploaded by

naimish khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views3 pages

JUW Summer 2021 Database Lab Task

This document provides instructions for a database systems lab task involving multiple tables. It contains 3 questions: 1. Write SQL queries to retrieve information from a sample products table, including finding maximum prices by supplier, minimum and maximum expiry dates, and average/sum/min/max unit prices. 2. Create tables for shops, customers, and shopping with the given column definitions and data types. 3. Add primary and foreign keys to the shopping table, write a JOIN query to retrieve titles and publisher data, and a subquery to find employee names working in IT. The task must be submitted individually on the university's LMS by the deadline.

Uploaded by

naimish khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JINNAH UNIVERSITY FOR WOMEN

DEPARTMENT OF COMPUTER SCIENCE & SOFTWARE ENGINEERING


SUMMER 2021
FINAL LAB TASK

Course Title: Database Systems Batch: BS (SE/CS)


Course Code: CSS 1021 Instructor: Sana Iqbal
Marks: 30 (Lab Task: 10, Lab Assignment: 20) Duration: 2 hrs.

Instructions

 The lab task must be submitted on JUW LMS.


 Email submission will not be accepted
 Each student has to perform the lab task individually
 Plagiarism may lead to marks deduction

Question 1 [Marks: 4]
Write down queries for the following scenario.
ProductI ProductName SupplierID Unit Expiry Price
D
1 Tea 1 10 boxes * 20 28-10-2019 18
bags
2 Jelly 1 24-12 oz bottles 29-10-2019 19
3 Syrup 1 12-50 ml bottles 25-01-2025 10
4 Ketchup 2 46 – 48 jars 11-10-2004 22
5 Jumbo Mix 2 36 boxes 01-01-2001 22.5

1. Show the maximum Price of Product related to each Supplier. [Hint: use group by clause]
SELECT MAX(unitprice) FROM products;
2. Display Maximum and Minimum Expire date of all the Products, Rename columns to Max
and Min.
3. Display Average, Max, Min and Sum of Unit Price for all the Products.
SELECT AVG(UnitPrice)AS AVG, SUM(UnitPrice)AS SUM, MIN(UnitPrice) AS MIN, MAX( .4
UnitPrice) AS MAX FROM Products
5. Show complete record of Products whose start with K and whose third letter is t.

select * from table where ProductName like 'K%t' 

6. Why alter table are used? Use an example to support your answer.

The ALTER TABLE statement is used to add, delete, or modify columns


in an existing table.

ALTER TABLE Customers

ADD Email varchar(255);

Page 1 of 3
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT OF COMPUTER SCIENCE & SOFTWARE ENGINEERING
SUMMER 2021
FINAL LAB TASK

7. What are joins? Which key is used to allow join operations on multiple tables?

Joins are used to combine the rows from multiple tables using mutual columns. 
In SQL, to fetch data from multiple tables, the join operator is used.

8. Update Price of Products which supplierID belongs to 1.


Update Price of Products
Set ProductName = ‘Tea’
Where SupplierID = 1;

9. Why table aliases are used? Use an example to support your answer.

aliases are used to give a table, or a column in a table, a temporary


[Link] are often used to make column names more readable.

Question 2 [Marks: 3]
Create the following tables in your own named database. (Note: You can make primary and
foreign keys given in Part C at the time of table creation or in alteration of table)

1. Table shop has the attributes shop_id(PK), shop_name, shop_location and shop_type (use
proper data type and size for each column)

CREATE TABLE `shop` (


`shop_id(PK)` INT(10),
`shop_name` TEXT(20),
`shop_location ` DATETIME DEFAULT '',
`shop_type ` VARCHAR(20) CHARACTER SET armscii8 COLLATE
armscii8_general_ci
);

2. Table customer has the attributes cust_id(PK), cust_name, address, phone and date of birth
(use proper data type and size for each column)

CREATE TABLE `customer` (

`cust_id(PK)` INT(20),

`cust_name` TEXT(20),

`address` VARCHAR(50),

`phone` INT(11),

`date of birth ` DOUBLE(15)

Page 2 of 3
3. Create another table shopping. Include attributes shopping_id and shopping_date (use
proper data type and size for each column)

CREATE TABLE `shopping` (


`shopping_id ` INT(20),
`shopping_date ` DATE(20)
);

Question 3 [Marks: 3]
Perform the given tasks using tables created for Question 2.

1. Include the primary key columns of first two tables as the foreign key in table Shopping.
CREATE TABLE `shopping` (
`shopping_id ` INT(20),
`shopping_date ` DATE(20),
PRIMARY KEY (`shopping_id `,`shopping_date `)
);
2. Write a query to display the titles and price from titles table and publishers name and city
from publishers table. [Hint: use Pubs , Logic: JOIN]
3. Write a query to find the name (first_name, last_name) of all employees who works in the
IT department. [Hint: use Northwind, Logic: Subquery]

Page 3 of 3

Common questions

Powered by AI

Primary keys maintain table record uniqueness by ensuring that each entry in a table is distinguished by a specific column or set of columns. In the context of 'shop' and 'customer' tables, the 'shop_id' for shops and 'cust_id' for customers serve as primary keys, preventing duplicate entries and enabling precise referencing from other related tables. This structure supports data integrity and efficient relational queries.

Joins in SQL are used to combine rows from two or more tables based on a related column between them. A common key, such as a primary key in one table and a foreign key in another, is essential for conducting join operations. This allows for related data to be fetched simultaneously.

You can determine the maximum and minimum expiry dates for a range of products using SQL aggregate functions like MAX() and MIN(). To rename these result columns, you can use the AS keyword to alias the column names. The SQL query can be formulated as: SELECT MAX(Expiry) AS Max, MIN(Expiry) AS Min FROM Products;

The ALTER TABLE statement is used to add, delete, or modify columns in an existing database table. For instance, to add an email column to a Customers table, you can use: ALTER TABLE Customers ADD Email varchar(255)

Table aliases are significant for simplifying complex SQL queries by giving tables or columns shorter, more readable temporary names. This makes the queries easier to write and understand. For example, using 'c' as an alias for the Customer table: SELECT c.cust_name FROM Customer AS c;

For querying product records that meet specific naming patterns, use the SQL LIKE clause, which allows pattern matching with wildcards. For example, to find products starting with 'K' and having 't' as the third letter, execute: SELECT * FROM Products WHERE ProductName LIKE 'K_t%';

To update the price of products from suppliers with a specific SupplierID, use the UPDATE statement with a WHERE clause to target specific rows. For example, to update products with SupplierID 1 to have a new price: UPDATE Products SET Price = new_value WHERE SupplierID = 1; Considerations include ensuring the correct rows are affected by verifying the SupplierID condition.

Data type constraints ensure that the data entered into a table conforms to expected formats and ranges, preventing anomalies and preserving data integrity. In the 'shopping' table, using an incorrect data type, such as DATE(20), could lead to storage inconsistency and retrieval errors. Proper data types ensure consistency and accuracy of stored information, enabling reliable database operations and analysis.

While designing a database table for a shopping application, it is crucial to select appropriate data types and sizes based on data characteristics and storage efficiency. For names, VARCHAR can be used with a reasonable size to accommodate varying lengths. For locations and similar text fields, VARCHAR with an adequate character limit is appropriate. For dates, DATE or DATETIME should be used depending on the precision needed. Proper consideration avoids data truncation and ensures efficient querying.

Foreign keys in relational database tables establish and enforce links between tables, maintaining data integrity. An example involving table creation with foreign keys is incorporating the primary keys of related tables as foreign keys. When creating a Shopping table, include shop_id and cust_id as foreign keys referencing Shop and Customer tables respectively: CREATE TABLE Shopping (shopping_id INT, shopping_date DATE, shop_id INT, cust_id INT, FOREIGN KEY(shop_id) REFERENCES Shop(shop_id), FOREIGN KEY(cust_id) REFERENCES Customer(cust_id))

You might also like