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

my sql file

The document outlines three experiments related to SQL database operations. Experiment 1 covers creating, modifying, truncating, and dropping tables, while Experiment 2 focuses on data manipulation through insertion, updating, and deletion of records. Experiment 3 demonstrates the use of aggregate and mathematical functions such as AVG, COUNT, MAX, MIN, and GROUP BY with HAVING clauses.

Uploaded by

taniyaitani66
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)
3 views

my sql file

The document outlines three experiments related to SQL database operations. Experiment 1 covers creating, modifying, truncating, and dropping tables, while Experiment 2 focuses on data manipulation through insertion, updating, and deletion of records. Experiment 3 demonstrates the use of aggregate and mathematical functions such as AVG, COUNT, MAX, MIN, and GROUP BY with HAVING clauses.

Uploaded by

taniyaitani66
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/ 9

Experiment-1

1) Create database tables and using datatypes.


• Create Table
• Modify Table
• Drop Table
• Truncate Table

Sql Query:-
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xyz_company |
+--------------------+
5 rows in set (0.00 sec)
# Create Database
mysql> create database taniya;
Query OK, 1 row affected (0.16 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| taniya |
| xyz_company |
+--------------------+
6 rows in set (0.00 sec)

mysql> use taniya;


Database changed
#Create Table
mysql> create table product(product_code int, product_name varchar(30),
product_price int);
Query OK, 0 rows affected (0.18 sec)

mysql> desc product;


+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| product_code | int | YES | | NULL | |
| product_name | varchar(30) | YES | | NULL | |
| product_price | int | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
#Modify Table
mysql> alter table product
-> add product_age int;
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc product;


+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| product_code | int | YES | | NULL | |
| product_name | varchar(30) | YES | | NULL | |
| product_price | int | YES | | NULL | |
| product_age | int | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
#Truncate Table
mysql> truncate table product;
Query OK, 0 rows affected (0.07 sec)

mysql> desc product;


+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| product_code | int | YES | | NULL | |
| product_name | varchar(30) | YES | | NULL | |
| product_price | int | YES | | NULL | |
| product_age | int | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
# Drop Table
mysql> drop table product;
Query OK, 0 rows affected (0.06 sec)

mysql> desc product;


ERROR 1146 (42S02): Table 'taniya.product' doesn't exist
mysql>
Experiment-2
2) Practical based on Data Manipulation
• Adding with insert
• Modifying data with update
• Deleting records with delete

Sql Query:-
# Adding data with insert
mysql> create table product1(product_code int, product_name varchar(30),
product_price int);
Query OK, 0 rows affected (0.08 sec)

mysql> desc product1;


+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| product_code | int | YES | | NULL | |
| product_name | varchar(30) | YES | | NULL | |
| product_price | int | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into product1(product_code,product_name,product_price)
-> values
-> (101,'study table',500),
-> (102,'pen',20),
-> (103,'RD Sharma',400),
-> (104,'school bag',900);
Query OK, 4 rows affected (0.08 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> select * from product1;


+--------------+--------------+---------------+
| product_code | product_name | product_price |
+--------------+--------------+---------------+
| 101 | study table | 500 |
| 102 | pen | 20 |
| 103 | RD Sharma | 400 |
| 104 | school bag | 900 |
+--------------+--------------+---------------+
4 rows in set (0.00 sec)
# Modifying data with update
mysql> update product1
-> set product_price=950
-> where product_code=104;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from product1;


+--------------+--------------+---------------+
| product_code | product_name | product_price |
+--------------+--------------+---------------+
| 101 | study table | 500 |
| 102 | pen | 20 |
| 103 | RD Sharma | 400 |
| 104 | school bag | 950 |
+--------------+--------------+---------------+
4 rows in set (0.00 sec)
# Deleting record with delete
mysql> delete from product1
-> where product_code=102;
Query OK, 1 row affected (0.05 sec)
mysql> select * from product1;
+--------------+--------------+---------------+
| product_code | product_name | product_price |
+--------------+--------------+---------------+
| 101 | study table | 500 |
| 103 | RD Sharma | 400 |
| 104 | school bag | 950 |
+--------------+--------------+---------------+
3 rows in set (0.00 sec)
Experiment-3
3) Practical based on Aggregate and Mathematical Functions:
• AVG
• COUNT
• MAX
• MIN
• GROUP BY, HAVING etc.

Sql Query:-
#AVG
mysql> select avg(product_price) as product_price from product1;
+---------------+
| product_price |
+---------------+
| 616.6667 |
+---------------+
1 row in set (0.09 sec)
#COUNT
mysql> select count(*) as total_product from product1;
+---------------+
| total_product |
+---------------+
| 3|
+---------------+
1 row in set (0.07 sec)
#MAX
mysql> select max(product_price) as maximum from product1;
+---------+
| maximum |
+---------+
| 950 |
+---------+
1 row in set (0.06 sec)
#MIN
mysql> select min(product_price) as minimum from product1;
+---------+
| minimum |
+---------+
| 400 |
+---------+
1 row in set (0.04 sec)
# GROUP BY
mysql> select product_code, sum(product_price) as product_total from
product1
-> group by product_code;
+--------------+---------------+
| product_code | product_total |
+--------------+---------------+
| 101 | 500 |
| 103 | 400 |
| 104 | 950 |
+--------------+---------------+
3 rows in set (0.05 sec)
#HAVING
mysql> select product_code, sum(product_price) as product_total from
product1
-> group by product_code
-> having sum(product_price)>=450
-> order by product_total desc;
+--------------+---------------+
| product_code | product_total |
+--------------+---------------+
| 104 | 950 |
| 101 | 500 |
+--------------+---------------+
2 rows in set (0.05 sec)

You might also like