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

Database Labreport

The document discusses topics covered in several SQL labs, including data types, creating and dropping databases and tables, adding primary keys, and performing queries like joins, aggregation, sorting, and updates.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Database Labreport

The document discusses topics covered in several SQL labs, including data types, creating and dropping databases and tables, adding primary keys, and performing queries like joins, aggregation, sorting, and updates.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

LAB 1(10/01/2024)

We have learned following topic in the lab:


1.Data type
2.How to create a database
3.How to drop a database
4.How to create a table
5.How to drop a table
6.How to add primary key to the table.
7.How to Alter table (ADD Primary key, ADD column, DROP column).

1.Common Datatypes in sql:

Data type Description

VARCHAR(size) A VARIABLE length string (can contain letters,


numbers, and special characters).
The size parameter specifies the maximum string
length in characters - can be from 0 to 65535

TINYBLOB For BLOBs (Binary Large Objects). Max length:


255 bytes

TINYTEXT Holds a string with a maximum length of 255


characters

TEXT(size) Holds a string with a maximum length of 65,535


bytes
BIT(size) A bit-value type. The number of bits per value is
specified in size. The size parameter can hold a
value from 1 to 64. The default value for size is
1.

BOOL Zero is considered as false, nonzero values are


considered as true.

BOOLEAN Equal to BOOL

INT(size) A medium integer. Signed range is from -


2147483648 to 2147483647. Unsigned range is
from 0 to 4294967295. The size parameter
specifies the maximum display width (which is
255)

FLOAT(size, d) A floating point number. The total number of


digits is specified in size. The number of digits
after the decimal point is specified in
the d parameter. This syntax is deprecated in
MySQL 8.0.17, and it will be removed in future
MySQL versions

FLOAT(p) A floating point number. MySQL uses the p value


to determine whether to use FLOAT or DOUBLE
for the resulting data type. If p is from 0 to 24,
the data type becomes FLOAT(). If p is from 25
to 53, the data type becomes DOUBLE()

DOUBLE(size, d) A normal-size floating point number. The total


number of digits is specified in size. The number
of digits after the decimal point is specified in
the d parameter
DATE A date. Format: YYYY-MM-DD. The supported
range is from '1000-01-01' to '9999-12-31'

char(n) Fixed 8,000 characters Defined width


width
character
string

varchar(n) Variable 8,000 characters 2 bytes +


width number of chars
character
string

varchar(max) Variable 1,073,741,824 2 bytes +


width characters number of chars
character
string

numeric(p,s) Fixed precision and scale numbers.

Allows numbers from -10^38 +1 to 10^38 –1.

The p parameter indicates the maximum total number


of digits that can be stored (both to the left and to the
right of the decimal point). p must be a value from 1 to
38. Default is 18.

The s parameter indicates the maximum number of


digits stored to the right of the decimal point. s must be
a value from 0 to p. Default value is 0
Necessar queries for these :
2.Creat Database:
Syntax: CREATE DATABASE database_name;
Example: CREATE DATABASE Sanjida;

3.Drop Database:
DROP DATABASE databasename;
Example: DROP DATABASE Sanjida;

4.Creat Table: (Previously we created another database:lab_1_50_3)


CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example: CREATE TABLE student(
ID int,
Name varchar(255),
Email varchar(255)
CGPA float,
Address varchar(255)
);
5.Drop Table:(We created another table test(item int primary key,Name
varchar(100))
DROP TABLE table_name:
Example: DROP TABLE test;

6.How to add primary key to the table:


Example:
CREATE TABLE student(
ID int,
Name varchar(255),
Email varchar(255)
CGPA float,
Address varchar(255),
PRIMERY KEY (ID)
);

7.How to alter table:


7.1:If we forget to set primary key while creating the table ,we can use
alter table to set primary key;
Example:
ALTER TABLE student
ADD PRIMARY KEY (ID);

7.2:If we want to add extra column to an existing table we can use alter table
to add column;
ALTER TABLE table_name
ADD column_name datatype;
Example:
ALTER TABLE student
ADD Department varchar(10);

7.3: If we want to drop a column from an existing table we can use alter table
to drop column;
ALTER TABLE table_name
DROP COLUMN column_name;
Example:
ALTER TABLE student
DROP COLUMN Department;

Output of the Lab:


Table:

Console:
LAB 3(24/01/2024)
We have learned following topic in the lab:
1.Firstly we repeated the process of creating database and table as we
learned in the previous lecture.
2.How to add UNIQUE key to an existing table.
3.Insert Information Into the table.
4.Lastly we performed various type of operations.

Necessary queries for these :


1.Creat Database:
CREAT DATABASE lab_3_50_3;
2.Creat table:
CREAT TABLE customer(
ID int,
Name varchar(255),
Address varchar(255),
Phone varchar(255),
Email varchar(255),
Country varchar(255),
PRIMARY KEY(ID)
)
Table(png):
3.Alter table to add unique key;
Example:
ALTER TABLE customer
ADD UNIQUE (Phone);
ALTER TABLE customer
ADD UNIQUE (Email);

4.Insert Information Into the table;


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example:
INSERT INTO customer( ID , Name ,Address ,Phone ,Email ,Country)
VALUES(1,’Sakib’,’Mirpur,Dhaka’,’01645355566’,’[email protected],’Bangladesh’);
(N.B:We should repeat this procedure till we finish completing the table by
updating the value of the following)

5.Various operation:
5.1:Print all information.
Queries: SELECT * FROM customer

5.2:Print Customer from outside Bangladesh.

Queries: SELECT * FROM customer WHERE Country NOT LIKE ‘Bangladesh’;

5.3:Print customer who use gmail.

Queries: SELECT * FROM customer WHERE Email LIKE ‘%gmail.com’;

 (N.B:’%’ uses of this sign indicates that anything can be placed before
‘gmail.com’ but string should be end with ‘gmail.com’. This called ‘LIKE’
operator. The percent sign % represents zero, one, or multiple characters)

5.4:Print customer who use gmail.

Queries: SELECT * FROM customer WHERE Phone LIKE ‘017%’;


5.5:Print customer whose ID ID 5.

Queries: SELECT * FROM customer WHERE ID LIKE ‘5’;

Console(png):
LAB 4(31/01/2024)
We have learned following topic in the lab:
1.Perform different queries operation on an existing table

Necessary queries for these :


First we create a database ‘Lab_4_50_3’,Then create a table ‘product’.

Table(png):

i)Find maximum price :


Queries: SELECT MAX(Price) FROM ‘product’
ii)Find second maximum price:
Queries: SELECT MAX(Price) WHERE Price NOT IN (SELECT MAX(Price)
FROM ‘product’);
(N.B: This is called nested queries)

iii)Find total of price column:


Queries: SELECT SUM(Price) FROM ‘product’

iv)Find minimum available product:


Queries: SELECT MIN(Available), Item FROM ‘product’;

v)Find average of available product:


Queries: SELECT AVG(Available) FROM ‘product’;

vi)Print the Items whose value is less than average value:


Queries: SELECT Item FROM ‘product’ WHERE Price <= (SELECT AVG(Price) FROM ‘product’);

vii)Print average value of each country:


Queries: SELECT AVG(Price), Made_in FROM ‘product’ GROUP BY Made_in;

viii)Print the Item Which product has maximum amount of stock:


Queries: SELECT Item, MAX(Price*Available) FROM ‘product’ WHERE (Price*Available)=(SELECT
MAX(Price8Available) FROM ‘product’)

ix)Find the total number of row:


Queries: SELECT COUNT(Item) FROM ‘product’;
x)Find price between 60-160:
Queries: SELECT Price FROM ‘product’ WHERE Price BETWEEN 60 AND 160;

xi)Increase each price by 10%:


Queries: UPDATE product
SET Price=Price*1.1

xii)Again increase Chinese product price by 20% and print:


Queries: UPDATE product
SET Price=Price*1.2
WHERE Made_in=’China’;
SELECT Price FROM ‘product’ WHRE Made_in=’China’;

xiii)Now print difference between light and soap price:


Queries: (SELECT price FROM products WHERE product_name = 'light') -
(SELECT price FROM products WHERE product_name = 'soap') AS price_difference;

xvi)Calculate % of comparison:
Queries: SELECT

((SELECT price FROM products WHERE product_name = 'light') -

(SELECT price FROM products WHERE product_name = 'soap')) /

(SELECT price FROM products WHERE product_name = 'light') * 100 AS price_difference_percentage;


xv)Change deem light type to LED type;

Queries: UPDATE product


SET Description=’LED Light’
WHERE Item=’LIGHT’;

Console(png):
LAB 5(05/02/2024)
Table(png):
Queries that we performed in today’s lab:
1.Print Full Name of every customers.
SELECT first_name || ‘ ‘ || last_name FROM Customers

2.Country wise average age of customers


SELECT AVG(age),country
FROM Customers group By (country)

3.Customer id 4 which item?


SELECT Items FROM Orders WHERE customer_id=4;

4.Average Cart Value of a order.


SELECT avg(amount) as Average_Cart_Value from Orders

5.Who spend maximum amount in the market in his/her lifetime?


SELECT Customers.customer_id,Customers.first_name, MAX(Orders.amount) AS
max_amount_spent
FROM Orders
JOIN Customers ON Orders.customer_id = Customers.customer_id
GROUP BY Customers.first_name,Customers.customer_id
ORDER BY max_amount_spent DESC
LIMIT 1;

6.Print the pending item product list.


SELECT item FROM Orders Join Shippings ON Shippings.shipping_id=Orders.order_id
where Shippings.status='Pending'
7.Which items are delivered to the customer?
SELECT item FROM Orders Join Shippings ON Shippings.shipping_id=Orders.order_id
where Shippings.status='Delivered'

Lab 6(07/02/2024)

Our tasks for today’s lab:


 Created a Database :Lab_6_50_3
 Created Table: CREATE TABLE customer(
c_id int AUTO_INCREMENT,
c_name varchar(255),
c_address varchar(300),
c_email varchar(50)
);

CREATE TABLE orders(


o_id int AUTO_INCREMENT,
c_id varchar(255),
o_date varchar(300),
o_cart varchar(50)
);

N.B: AUTO_INCREMENT is used for maintaining a ascending order by default.


Tables:

customer table:

orders table:

Queries that we performed in today’s lab:


 Natural Join two tables:
Query: SELECT *
FROM customer
NATURAL JOIN orders;

 Left Join:
Query: SELECT *
FROM customer
LEFT JOIN orders
ON customer.c_id = order.c_id;

 Right Join:
Query: SELECT *
FROM customer
RIGHT JOIN orders
ON customer.c_id = order.c_id;
 Full Join
Query: SELECT *
FROM customer
FULL OUTER JOIN orders
ON customer.c_id = order.c_id
WHERE condition;

 Print order details of Dhaka’s customer


Query: Select c_name, o_id,o_date,o_cart
FROM customer LEFT JOIN orders
on customer.c_id=orders.c_id WHERE customer.c_address=’%Dhaka’;

 Total sell of each district


Query: Select SUM(o_cart)
FROM customer LEFT JOIN orders
on customer.c_id=orders.c_id GROUP BY customer.c_address

 Who buy products on 2nd February 2024?


Query: Select c_name, o_id,o_date,o_cart
FROM customer LEFT JOIN orders
on customer.c_id=orders.c_id WHERE customer.c_address=’%-02’;

 Average sell of each date


Query: Select AVG(o_cart)
FROM customer LEFT JOIN orders
on customer.c_id=orders.c_id GROUP BY order.o_date
Console:
Lab 7()
In today’s lab, task was to design a database according to the given demo:
Provided Demo:

Completed Task:
Console:

Some key points that we should keep in mind to create relational database:
 Draw a demo database schema at first
 Determine the Primary key and Foreign key which is a must
 Start creating the table from Left-to-Right
 Sometimes queries might not work as expected, so we should try in different
ways
Lab 9()
In todays lab, we learned how to draw an ER diagram using online based tool draw.io:
Lab work:

Process to draw this ER diagram:


 At first we have to go to draw.io website
 Then we have to select a create new project and select the directory path to save
the program
 Then select ER diagram format and select necessary objects for drawing an er
diagram

You might also like