0% found this document useful (0 votes)
3 views16 pages

MySQL

The document contains a series of MySQL commands demonstrating the creation and manipulation of databases, tables, and records. It includes operations such as creating a database, inserting, updating, and deleting records, as well as altering table structures and querying data. The document showcases the use of various SQL functions and commands to manage employee and project data effectively.

Uploaded by

amit.ac9189
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views16 pages

MySQL

The document contains a series of MySQL commands demonstrating the creation and manipulation of databases, tables, and records. It includes operations such as creating a database, inserting, updating, and deleting records, as well as altering table structures and querying data. The document showcases the use of various SQL functions and commands to manage employee and project data effectively.

Uploaded by

amit.ac9189
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

-------------------------------Assignment 2------------------------------

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.06 sec)

mysql> create database mydb;


Query OK, 1 row affected (0.03 sec)

mysql> use mydb;


Database changed
mysql> create table employee(emp_id int, ename varchar(20), address
varchar(20));
Query OK, 0 rows affected (0.06 sec)

mysql> desc employee;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| emp_id | int | YES | | NULL | |
| ename | varchar(20) | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)

mysql> insert into Employee values(4216,'Asutosh Panda','AIT Pune');


Query OK, 1 row affected (0.02 sec)

mysql> select * from employee;


+--------+---------------+----------+
| emp_id | ename | address |
+--------+---------------+----------+
| 4216 | Asutosh Panda | AIT Pune |
+--------+---------------+----------+
1 row in set (0.01 sec)

mysql> insert into Employee values(4212,'Aryan Rajawat','AIT Pune');


Query OK, 1 row affected (0.01 sec)

mysql> insert into Employee values(4209,'Anmol Singh','AIT Pune');


Query OK, 1 row affected (0.01 sec)

mysql> select * from employee;


+--------+---------------+----------+
| emp_id | ename | address |
+--------+---------------+----------+
| 4216 | Asutosh Panda | AIT Pune |
| 4212 | Aryan Rajawat | AIT Pune |
| 4209 | Anmol Singh | AIT Pune |
+--------+---------------+----------+
3 rows in set (0.00 sec)

mysql> update employee


-> set emp_id=4215 where ename = 'Anmol Singh';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee;


+--------+---------------+----------+
| emp_id | ename | address |
+--------+---------------+----------+
| 4216 | Asutosh Panda | AIT Pune |
| 4212 | Aryan Rajawat | AIT Pune |
| 4215 | Anmol Singh | AIT Pune |
+--------+---------------+----------+
3 rows in set (0.00 sec)

mysql> insert into Employee values(4203,'Aman','Hyderabad');


Query OK, 1 row affected (0.01 sec)

mysql> delete from employee where ename = 'Aryan Rajawat';


Query OK, 1 row affected (0.01 sec)

mysql> select * from employee;


+--------+---------------+-----------+
| emp_id | ename | address |
+--------+---------------+-----------+
| 4216 | Asutosh Panda | AIT Pune |
| 4215 | Anmol Singh | AIT Pune |
| 4203 | Aman | Hyderabad |
+--------+---------------+-----------+
3 rows in set (0.00 sec)
-------------------------------Assignment 3------------------------------
mysql> create table Department(did int primary key, dname varchar(20));
Query OK, 0 rows affected (0.04 sec)

mysql> drop table employee;


Query OK, 0 rows affected (0.03 sec)

mysql> create table Project(pid int primary key, Pname varchar(20),


Location
Id int);
Query OK, 0 rows affected (0.05 sec)

mysql> create table Employee(empid int primary key auto_increment, ename


varchar(20) not null, contactno int unique, salary int, did int, check
(salary>15000), foreign key(did) references Department(did));
Query OK, 0 rows affected (0.10 sec)

mysql> desc employee;


+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| empid | int | NO | PRI | NULL | auto_increment |
| ename | varchar(20) | NO | | NULL | |
| contactno | int | YES | UNI | NULL | |
| salary | int | YES | | NULL | |
| did | int | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> alter table employee add pid int;


Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc employee;


+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| empid | int | NO | PRI | NULL | auto_increment |
| ename | varchar(20) | NO | | NULL | |
| contactno | int | YES | UNI | NULL | |
| salary | int | YES | | NULL | |
| did | int | YES | MUL | NULL | |
| pid | int | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> alter table employee add foreign key(pid) references project(pid);


Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc project;


+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| pid | int | NO | PRI | NULL | |
| Pname | varchar(20) | YES | | NULL | |
| LocationId | int | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table project drop LocationId;


Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table project modify pname varchar(50);


Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc project;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pid | int | NO | PRI | NULL | |
| pname | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
-------------------------------Assignment 4------------------------------

mysql> use mydb;


Database changed
mysql>
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| department |
| employee |
| project |
+----------------+
3 rows in set (0.02 sec)

mysql> desc employee;


+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| empid | int | YES | | NULL | |
| ename | varchar(20) | YES | | NULL | |
| contactno | int | YES | | NULL | |
| DOB | date | YES | | NULL | |
| DOJ | date | YES | | NULL | |
| salary | int | YES | | NULL | |
| pid | int | YES | | NULL | |
| did | int | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

mysql> select * from employee;


+-------+---------------+-----------+------------+------------+--------
+------+------+
| empid | ename | contactno | DOB | DOJ | salary |
pid | did |
+-------+---------------+-----------+------------+------------+--------
+------+------+
| 1256 | Asutosh Panda | 77852 | 2000-10-17 | 2019-12-12 | 17025 |
2 | 5 |
| 1256 | Anmol Singh | 77852 | 2000-10-19 | 2013-12-12 | 17025 |
2 | 5 |
| 1256 | Aryan | 77852 | 2000-12-11 | 2013-12-02 | 17025 |
2 | 5 |
| 1256 | Aman | 77852 | 2004-12-11 | 2013-12-02 | 17025 |
2 | 5 |
+-------+---------------+-----------+------------+------------+--------
+------+------+
4 rows in set (0.00 sec)

mysql> update employee set DOB = '2000-09-12' where ename ='Aman';


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from employee;
+-------+---------------+-----------+------------+------------+--------
+------+------+
| empid | ename | contactno | DOB | DOJ | salary |
pid | did |
+-------+---------------+-----------+------------+------------+--------
+------+------+
| 1256 | Asutosh Panda | 77852 | 2000-10-17 | 2019-12-12 | 17025 |
2 | 5 |
| 1256 | Anmol Singh | 77852 | 2000-10-19 | 2013-12-12 | 17025 |
2 | 5 |
| 1256 | Aryan | 77852 | 2000-12-11 | 2013-12-02 | 17025 |
2 | 5 |
| 1256 | Aman | 77852 | 2000-09-12 | 2013-12-02 | 17025 |
2 | 5 |
+-------+---------------+-----------+------------+------------+--------
+------+------+
4 rows in set (0.00 sec)

mysql> update employee set salary = 25000 where ename ='Aman';


Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee;


+-------+---------------+-----------+------------+------------+--------
+------+------+
| empid | ename | contactno | DOB | DOJ | salary |
pid | did |
+-------+---------------+-----------+------------+------------+--------
+------+------+
| 1256 | Asutosh Panda | 77852 | 2000-10-17 | 2019-12-12 | 17025 |
2 | 5 |
| 1256 | Anmol Singh | 77852 | 2000-10-19 | 2013-12-12 | 17025 |
2 | 5 |
| 1256 | Aryan | 77852 | 2000-12-11 | 2013-12-02 | 17025 |
2 | 5 |
| 1256 | Aman | 77852 | 2000-09-12 | 2013-12-02 | 25000 |
2 | 5 |
+-------+---------------+-----------+------------+------------+--------
+------+------+
4 rows in set (0.00 sec)

mysql> update employee set salary = 25011 where ename ='Anmol Singh';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee where salary>25000;


+-------+-------------+-----------+------------+------------+--------
+------+------+
| empid | ename | contactno | DOB | DOJ | salary |
pid | did |
+-------+-------------+-----------+------------+------------+--------
+------+------+
| 1256 | Anmol Singh | 77852 | 2000-10-19 | 2013-12-12 | 25011 |
2 | 5 |
+-------+-------------+-----------+------------+------------+--------
+------+------+
1 row in set (0.00 sec)

mysql> select * from employee where ename like '____';


+-------+-------+-----------+------------+------------+--------+------
+------+
| empid | ename | contactno | DOB | DOJ | salary | pid |
did |
+-------+-------+-----------+------------+------------+--------+------
+------+
| 1256 | Aman | 77852 | 2000-09-12 | 2013-12-02 | 25000 | 2 |
5 |
+-------+-------+-----------+------------+------------+--------+------
+------+
1 row in set (0.00 sec)

mysql> select * from employee where ename like '%A';


+-------+---------------+-----------+------------+------------+--------
+------+------+
| empid | ename | contactno | DOB | DOJ | salary |
pid | did |
+-------+---------------+-----------+------------+------------+--------
+------+------+
| 1256 | Asutosh Panda | 77852 | 2000-10-17 | 2019-12-12 | 17025 |
2 | 5 |
+-------+---------------+-----------+------------+------------+--------
+------+------+
1 row in set (0.00 sec)

mysql> select ucase(ename) from employee;


+---------------+
| ucase(ename) |
+---------------+
| ASUTOSH PANDA |
| ANMOL SINGH |
| ARYAN |
| AMAN |
+---------------+
4 rows in set (0.01 sec)

mysql> insert into Employee values(1222,'Debasmita Adak',77453,'2004-04-


13',
'2007-07-19',27521,1,5);
Query OK, 1 row affected (0.01 sec)

mysql> select ucase(ename) from employee;


+----------------+
| ucase(ename) |
+----------------+
| ASUTOSH PANDA |
| ANMOL SINGH |
| ARYAN |
| AMAN |
| DEBASMITA ADAK |
+----------------+
5 rows in set (0.00 sec)

mysql> select length(ename) from employee;


+---------------+
| length(ename) |
+---------------+
| 13 |
| 11 |
| 5 |
| 4 |
| 14 |
+---------------+
5 rows in set (0.00 sec)

mysql> select now();


+---------------------+
| now() |
+---------------------+
| 2024-04-24 23:38:24 |
+---------------------+
1 row in set (0.01 sec)

mysql> select date(now());


+-------------+
| date(now()) |
+-------------+
| 2024-04-24 |
+-------------+
1 row in set (0.01 sec)

mysql> select time(now());


+-------------+
| time(now()) |
+-------------+
| 23:38:59 |
+-------------+
1 row in set (0.00 sec)

mysql> select bin(salary) from employee;


+-----------------+
| bin(salary) |
+-----------------+
| 100001010000001 |
| 110000110110011 |
| 100001010000001 |
| 110000110101000 |
| 110101110000001 |
+-----------------+
5 rows in set (0.01 sec)
mysql> select abs(contactno) from employee;
+----------------+
| abs(contactno) |
+----------------+
| 77852 |
| 77852 |
| 77852 |
| 77852 |
| 77453 |
+----------------+
5 rows in set (0.00 sec)

mysql> select substr(ename,1,4) from Employee;


+-------------------+
| substr(ename,1,4) |
+-------------------+
| Asut |
| Anmo |
| Arya |
| Aman |
| Deba |
+-------------------+
5 rows in set (0.00 sec)

mysql> select sum(salary) from Employee;


+-------------+
| sum(salary) |
+-------------+
| 111582 |
+-------------+
1 row in set (0.01 sec)

mysql> select avg(salary) from Employee;


+-------------+
| avg(salary) |
+-------------+
| 22316.4000 |
+-------------+
1 row in set (0.00 sec)

mysql> select count(salary) from Employee;


+---------------+
| count(salary) |
+---------------+
| 5 |
+---------------+
1 row in set (0.01 sec)

mysql> select min(salary) from Employee;


+-------------+
| min(salary) |
+-------------+
| 17025 |
+-------------+
1 row in set (0.01 sec)

mysql> select count(salary) from employee where DOJ between '2009-12-03'


and '2104-05-03';
+---------------+
| count(salary) |
+---------------+
| 4 |
+---------------+
1 row in set (0.01 sec)

mysql> select did,min(salary) from employee group by did;


+------+-------------+
| did | min(salary) |
+------+-------------+
| 5 | 17025 |
+------+-------------+
1 row in set (0.01 sec)
-------------------------------Assignment 5------------------------------

mysql> desc department;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| did | int | NO | PRI | NULL | |
| dname | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into department values(4,'IT');


Query OK, 1 row affected (0.02 sec)

mysql> insert into department values(16,'ENTC');


Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(18,'COMP');


Query OK, 1 row affected (0.02 sec)

mysql> insert into department values(12,'MECH');


Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(9,'ROBOTICS');


Query OK, 1 row affected (0.01 sec)

mysql> select * from department;


+-----+----------+
| did | dname |
+-----+----------+
| 4 | IT |
| 9 | ROBOTICS |
| 12 | MECH |
| 16 | ENTC |
| 18 | COMP |
+-----+----------+
5 rows in set (0.00 sec)

mysql> select * from employee;


+-------+----------------+-----------+------------+------------+--------
+------+------+
| empid | ename | contactno | DOB | DOJ | salary |
pid | did |
+-------+----------------+-----------+------------+------------+--------
+------+------+
| 1256 | Asutosh Panda | 77852 | 2000-10-17 | 2019-12-12 | 17025 |
2 | 5 |
| 1256 | Aman | 77852 | 2000-09-12 | 2013-12-02 | 25000 |
2 | 5 |
| 1222 | Debasmita Adak | 77453 | 2004-04-13 | 2007-07-19 | 27521 |
1 | 5 |
+-------+----------------+-----------+------------+------------+--------
+------+------+
5 rows in set (0.00 sec)

mysql> create view v1 as select empid,ename,salary from employee;


Query OK, 0 rows affected (0.02 sec)

mysql> select * from v1;


+-------+----------------+--------+
| empid | ename | salary |
+-------+----------------+--------+
| 1256 | Asutosh Panda | 17025 |
| 1256 | Aman | 25000 |
| 1222 | Debasmita Adak | 27521 |
+-------+----------------+--------+
5 rows in set (0.01 sec)

mysql> insert into v1 values(1245,'Anish',34000);


Query OK, 1 row affected (0.01 sec)

mysql> update v1 set empid = 1243 where ename='Asutosh Panda';


Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> update v1 set empid = 1247 where ename='Anmol Singh';


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from v1;


+-------+----------------+--------+
| empid | ename | salary |
+-------+----------------+--------+
| 1243 | Asutosh Panda | 17025 |
| 1256 | Aman | 25000 |
| 1222 | Debasmita Adak | 27521 |
| 1245 | Anish | 34000 |
+-------+----------------+--------+
6 rows in set (0.00 sec)

mysql> create view v2 as select ename,sum(salary) from employee group by


ena
me;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from v2;


+----------------+-------------+
| ename | sum(salary) |
+----------------+-------------+
| Asutosh Panda | 17025 |
| Aman | 25000 |
| Debasmita Adak | 27521 |
| Anish | 34000 |
+----------------+-------------+
6 rows in set (0.01 sec)
mysql> create view v3 as select * from employee where salary in (select
max
(salary) from employee);
Query OK, 0 rows affected (0.02 sec)

mysql> select * from v3;


+-------+-------+-----------+------+------+--------+------+------+
| empid | ename | contactno | DOB | DOJ | salary | pid | did |
+-------+-------+-----------+------+------+--------+------+------+
| 1245 | Anish | NULL | NULL | NULL | 34000 | NULL | NULL |
+-------+-------+-----------+------+------+--------+------+------+
1 row in set (0.00 sec)

mysql> select * from department;


+-----+----------+
| did | dname |
+-----+----------+
| 4 | IT |
| 9 | ROBOTICS |
| 12 | MECH |
| 16 | ENTC |
| 18 | COMP |
+-----+----------+
5 rows in set (0.00 sec)

mysql> create view v4 as select ename, dname from employee, Department


where
employee.did=Department.DID;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into department values(5,'COMP_A');


Query OK, 1 row affected (0.02 sec)

mysql> create view v5 as select ename, dname from employee, Department


where employee.did=Department.did;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from v5;


+----------------+--------+
| ename | dname |
+----------------+--------+
| Asutosh Panda | COMP_A |
| Aman | COMP_A |
| Debasmita Adak | COMP_A |
+----------------+--------+
5 rows in set (0.00 sec)
-------------------------------Assignment 6------------------------------
mysql> delimiter $$
mysql> select * from employee;
-> $$
+-------+--------------+-----------+------------+------------+--------
+------+------+
| empid | ename | contactno | DOB | DOJ | salary |
Pid | did |
+-------+--------------+-----------+------------+------------+--------
+------+------+
| 1256 | Asutosh Panda| 77852 | 2000-10-17 | 2017-10-14 | 17025 |
2 | 5 |
| 1257 | Aryan | 96890 | 0000-00-00 | 2014-03-27 | 21005 |
4 | 1 |
| 1271 | Anmol | 78523 | 1995-08-10 | 2018-07-11 | 25418 |
3 | 4 |
| 1222 | Debasmita | 77453 | 1998-04-01 | 2019-12-12 | 27521 |
1 | 5 |
+-------+--------------+-----------+------------+------------+--------
+------+------+
4 rows in set (0.00 sec)

mysql> drop procedure if exists helloworld$$


Query OK, 0 rows affected, 1 warning (0.49 sec)

mysql> create procedure helloworld()


-> begin
-> select "Hello World";
-> end$$
Query OK, 0 rows affected (0.36 sec)

mysql> call helloworld()$$


+-------------+
| Hello World |
+-------------+
| Hello World |
+-------------+
1 row in set (0.13 sec)

Query OK, 0 rows affected (0.14 sec)

mysql> create procedure funsqrt(num1 int)


-> begin
-> declare result int;
-> set result = sqrt(num1);
-> select result;
-> end$$
Query OK, 0 rows affected (0.51 sec)

mysql> call funsqrt(4)$$


+--------+
| result |
+--------+
| 2 |
+--------+
1 row in set (0.06 sec)

Query OK, 0 rows affected (0.06 sec)

mysql> create procedure oddeven(num1 int)


-> begin
-> if mod(num1,2)=0 then
-> select "Even number";
-> else
-> select "Odd number";
-> end if;
-> end$$
Query OK, 0 rows affected (0.21 sec)

mysql> call oddeven(4)$$


+-------------+
| Even number |
+-------------+
| Even number |
+-------------+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> call oddeven(15)$$


+------------+
| Odd number |
+------------+
| Odd number |
+------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> create function funsqrt(num1 int)


-> returns int
-> deterministic
-> begin
-> declare result int;
-> set result = sqrt(num1);
-> return result;
-> end$$
Query OK, 0 rows affected (0.25 sec)

mysql> select funsqrt(49)$$


+-------------+
| funsqrt(49) |
+-------------+
| 7 |
+-------------+
1 row in set (0.09 sec)
mysql> create procedure classemp(eid int)
-> begin
-> declare sal int;
-> select salary into sal from employee where empid=eid;
-> if sal<25000 then
-> select "class X";
-> end if;
-> end$$
Query OK, 0 rows affected (0.27 sec)

mysql> call classemp(1)$$


Query OK, 0 rows affected (0.21 sec)

mysql> call classemp(4)$$


Query OK, 0 rows affected (0.00 sec)

mysql> create function classemp(eid int)


-> returns varchar(20)
-> deterministic
-> begin
-> declare sal int;
-> declare class varchar(20);
-> select salary into sal from employee where empid=eid;
-> if sal<25000 then
-> set class = "class X";
-> end if;
-> return class;
-> end$$
Query OK, 0 rows affected (0.15 sec)

mysql> call classemp(2)$$


Query OK, 0 rows affected (0.00 sec)

You might also like