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.
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0 ratings0% 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.
+---------+-------------+------+-----+---------+-------+ | 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)
+--------+---------------+----------+ | 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)
+-------------------+ | 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)
+-------------+ | 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)