You are on page 1of 16

Oeag Tables('ractica)

207

Module - 11 (Practícals)

CREATING TABLES

PRACTICAL EXERCISES IN MySQL


Write MySQL comnmand to create the following tables:
a) Table STUDENT having the following columns:
Roll no. integer should automatically increase by 1, primary key.
Name character variable width 20 columns.
Age integer default value 20.
Marks integer should not be empty.
Rysql> create table student
-) (ro1lno smallint auto_increment prinary key.
-) name varchar(20),
-> age snallint default 20,
-> narks smallint not null);
Query OK, 0rows affected (0.06 sec)

b) Table EMP having the


following columns:
Employee number integer primary key
First name character variable width 20 columns
Middle name character variable width 20 columnns
Last name character variable width 20 columns
Gender character 1 column
Salary 5 integers and 2 decimals
Married True or False
Aysql1> Create table enp
) (empno
-) fname sma1lint primary
varchar(20>, key,
nname varchar(20),
> Iname varchar(20>,
--gender
salary char(1),
decimal(?,2>,
->
married
Mery 0K, 0 rowsboole an>;
affected (0.13 sec)
208 Computer Systems and Applcatilons (T Y BCom)

e) Table DErARTMENT having the following columns


Department number integer primary key
charncter variable width 20 columns
Department name
Manager ldenity No. integer
Target very lurHe integer number
Location character variable width 20 columns
Employee no. integer foreign key to tablc EMP
Rysql) creato table dopartmont
(de pt no Nnallint prlmary koy.
de ptname varchar<20>.
mÊrid Nmallint,
targot bigint,
locat ion varc har<20),
ompno &mAllint,
Foroign koy (ompno> rof oroncos onp (ompno>>;
Ruery 0K, @rows affoctod (0.13 soc)
d) Table CAR having the following columns:
Car make character variable width 10 columns should not be empty
Registration No. character variable width 15 columns primary key
Cost of the car 7integers 2 decimals
Date of purchase date
nysql> create table car
-> (car make varchar(10) not null,
-> regn no varchar(15) prinary key,
-> cost decimal(9,2),
-> dop date>;
Query 0K, 8 rows affected <8.06 sec)
e) Table EMPLOYEES having the following columns
Employee ld integer should not be empty, primary key
First name character variable width 20 columns
Last name character variable width 20 columns
Email character variable width 25 columns
Phone number Character variable width 10 columns
Hire date Date
Job Id character variable width 10 columns
Salary 6 integers, 2 decimals
Commission 4 integers 2 decimals
Manager ld integer
Department Id integer
ysql> create table enployees
-) (emp_id smallint not null prinary key,
-) fnane varchar28),
-) Iname varchar(28),
-> enail varchar(25),
-> phoneno varchar(10),
-) hire_date date,
-) jobid varchar(10),
-) salary decisal(8,2),
-> conNiss ion decial(6,,2),
->anager_id snallint,
-) deptid sallint);
Ruery 0K, 0 rovs af fected (8.11 sec)
DeatingTables(Pracico) 209

Table JOB GRADES having the columns:


Grade_level character Icolumns should not be empty
LowestSalary 6 integers and 2 decimals
HighestSalary 7 integers and 2 decimals
create table job grades
ysa -) (grade_leyel char(1) not null,
Lowest_sal decinal(8,2).
-) highest_sal decinal(9,25):
affected <8.06 sec)
Ruery OK, 0 rows
210
Computer Systems and Applicattons (T. Y. B.Com))(Sem,
000, vy

Module - 19 (Practicals)
MODIFYINGTABLES

Consider the following table PAY whose column structure is shown below:
mysql> desc pay;
+
! Fie ld 1 Type I Null Key i Default } Extra !
enpno | smallint (6) I NO I PRI
iname I varchar(20> I YES NULL
i age I sma1lint (6) ! YES INULL
I city 1 varchar(20> I YES I NULL
salary decima1(?,2> I YES I NULL
| dob I date YES I NULL
6 rowS in set (0.09 sec)

Write MySOL statements for the following:


a) Add acolumn 'gender' of character type of size lcolumn to this
table.
nys alter table pay
add gender
Query OK,
Records :
char(12; (0.17
0rows affected
0 Duplicates : 9
sec)
Warnings 0

b) Add a column 'bonus' decimal(7,2) to this table after the salary column with
default value 2000.

mysql) add
alterbonus
table pay
decimal(?,2) default 2000 af ter salary;
Query 0K, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings : 9

c) Change the size of column 'name' to varchar(25)


nysql) alter table pay
-) nodify nane varchar(25);
Query OK, 0 rows affected (0.1? sec)
Records : 8 Duplicates : 8 Warnings: 0

d) Change the name of the column 'bonus' to 'mbonus'


nysql> alter table pay
-) change bonus mbonus decinal(?.2> default 2900:
Query OK, 0 rows affected (0.82 sec)
Becords:9 Duplicates : 8 Warnings: 0
Akli Tables(Practicat) 211

the name of the column'dob' to 'd birth'.


e) Change
Rysql) alter table pay
chango dob d birth dato:
Quory O
OK, Ø rows affoctod (0.16 00c)
Records: Duplicates : 0 Warnings : 0

0 Delete the column mbonus.


nysq1>altor table pay
drop mbonus;
Query Ok, 0 rows af fected (0.17 sec)

o) Rename the table PAY to EPAY


hysgl>.altor table pay renano_opay;
Ruory OK, 0 rows affocted <0.05 s0c)

h) Delete the table EPAY.


nysql> drop table epay;
Duery OK, rows affected (0.03 sec)
212
Computer Systems and Applications TY, B.Com.) (Sem..-V)

Module - 11 (Practícals)
INSERTING /ALTERING DATA

a) Create a table called MY EMPLOYEE containing the


columns employee jd
smallint which will be the primary key, last name varchar(25), first name
varchar(25), userid varchar(8), and salary decimal(9,2).
mysql> create table my_omployee
-> (empid smallint
name varchaacrAnary key.
-> fname varchar(25)
-> userid varchar(8>)
-> salary decimal{9,2>>;
Query 0K, 0 rows affected (0.42 sec>
b) Display the structure of MY EMPLOYEE table listing the names of the columns.
mysql> desc my_emplo ye e;
! Pie ld : Type Null ! Key ! Default ! Extra !
empid sma1lint(6> ! NO PRI ! NULL
lname ! varchar(25> ! YES NULL
fname Ivarchar(25) ! YES NULL
userid varchar8) YES : NULL
salary ! dec imal(9,2) YES ! NULL
5 roWs in set (O.03 soc>

c) Add a row to the table MY EMPLOYEE without


own data, but give employee id as 1.
naming the columns. Use your
nysql) insert into ny_emplOyee yalues
-) (1,'shah, Bobby','
bob1',35O00);
Query OK, 1 row affected (0.05 sec)
d) Add another row to the table MY
EMPLOYEE using the column names. Use youl
own data but give employee id as 2.
mysql-) insert
(2.' neht into
a' my onployee values
jigneshg
Query OK, 1 row affected (a.a 49008>;
e) Add another 3 rowS of data together to the table MY EMPLOYEE. Use your ow
data but give employee id as 3,.4,5.
(3,'kapuranee
nysql> insert into my values
kir3',33000)
-> (4,' joshi'.'vaibhav'.'vai4',43000)
-) (5,'shroff'.' bhikaji','bhiS' ,49000):
Query 0K, 3 rows affected (0.82 sec)
Records: 3 Duplicates : 0 Warnings: 0
wYAlering.Data(Practical) 213

Changethe last name of employee id 3to Mehta in the table MY EMPLOYEE.


n update my enployee
-) set Iname nehta'
-)where enpid=3;
Query OK, 1 rOW affected (0.08 sec)
natched: 1 Changed: 1 Warn ings: 0
Rows
Change the salary of all the employees to 40000 if the salary is below 40000 in the
g) table MY EMPLOYEE,
aysql)update_ny
-> set enployee
salary=4'006
-) where salary(40000:
affected <0.03 sec)
Query OK, 2 rows 2
Rows
natched: 2 Changed: Warn ings : 0
aDelete the row of employee id Sin the table MY EMPLOYEE.
husal) delete fron ny_employee
-) where empid=5;
Auery Ok, 1row affected (0.05 sec)
214
Computer Systems and Applications (T.Y. B.Com.) (Sem. -V)

Module - 111(Practícals)
QUERIES

Table EMPLOYEES contains the fields emp id smallint not null, fname varchar(20)
Iname varchar(20), email varchar(25), phoneno varchar(10), hire date date, jobid
varchar(10), salary decimal(8,2), commission decimal(6,2), manager id smallint, dept id
smallint, where emp id is the primary key.
Write MySQL statements for the following:
a) Display the structure of this table.
Rysql> desc emplo ye es ;
| Pie ld : Type 1 Nul1 Key ! Default Extra !
| emp_id i smallint (6) | NO PRI I NULIL
! fnae ! yarchar(20) YES ! NULL
lnane varc har(20> YES NULL
enail i varchar(25 ) YES ! NULL
phoneno varchar(10) YES NULL,
hire date ! date YES NULL
jobid varchar(10) YES 1 NULL
salary ! decinal(8,2) YES NULL
comAíss ion decinal(6,2) YES I NULL
| manager id smallint <6) YES I NULL
| dept_id Snallint (6> I YES I NULL
11 rows in set (0.05 sec)

b) Add 1row of data to this table. Then add another 6 rows.


Pysql) ínnert into emplo yees values
Query 0K, G11,'riyaz'.'sadíualla','riyutyahoo
1 roy affected (8.&2 sec) .con'.'43551123','1999-83-82'sal gr',54000,5488.S15.14):
Sirmilarly add another 6 rows of data.
c) Display all the rows in this table.
y) elect * fron enp loyees;
enp_i fsane lbane Ieail Iphoneno hire_date jobid isalary cOmmission anager id deptid i
1dinkar jeshi ioshighotmil.con 23994512 2031-18-C5 ad vp 121C00.00 2100.C0 515 10M
2 kiran iganhi kiranlyahoo.coa 23889932 1998-2-21 st clerk 14008.C0 1400.00 S12 1R
3hiral Jain Ihirallhotmil.con 26230985 2001-10-12 sa_rep I19000.00 1900.00 511 1M
4fidosh ehta !firdesheotail.con 24229%59 I 1998-11-24 ac gP 25003.C0 511 1O
adialla riulyabo0. coR 43551123 ! 1999-83-2 sal r S4000.00 I 5400.C0 S15 1MI
S11 ri2 515 1
S12 binita shah binitalgMil.con 42209115 1999-12-10 pur gT 35C00.00 35C0.C0
515 pinz eta pinlyaail.con 23556687 ! 1995-10-28 gen gr 55000.00 S500.00 MIL, 10

7ov in set (0.0 sec)


e s(Practical) 215

display last name and salary of all employees earning more than
d) To14,000
se lect Iname,salary
Lusq From employees
-s where salary>14000:
! salary
lname +
21000-00
joshi ! 19000.0Ø
Jain 25Ø00.Qg
nehta
gadiwalla 54000.00 !
shah
35000.00 !
55009.00
nehta
set (9.03 sec)
6
rOWS 1n

e) To display last name and department id for employee id 3.


ysq1) select Iname, dept_id
-> from employees
-) where emp_id = 3:
Inane dept_id !

lain 104 H

hrow in set (Ø.03 sec)

) To display last name and salary for all employees whose salary is in the range
15000 to 24000.
nysql> se lect lname,salary
-> from employees
-) where salary between 15000 and 24000;
Iname salary
joshi i 21900.90
Jain

2rows in set <0.01 sec)

g) To display last name, job id and hire date of employees hired between
Feb 20, 1998 and May 1, 1998 in the ascending order of hire date.
aysql>
->
se lect lname . jobid, hire_date
from emplo yees
-> where hire date>'1998-02-20 and hire date<'1998-05-01
-> order by hire_date;

InameI jobid
+
hire_date
4Sandhi st clerk 1998-02-211
row in set <0.00 sec>

n) To display the last name and department of the employees in department 101 or
103 in the alphabetic order of last name.
ysql> select lnane.dept_id
-) from employees
Rwhere dept_id in (101,103>
) order by lname ;
lname dept id
joshi !
nehta 101
103
B
rows in set (0.01 sec)
216
Computer Systems and Applications (T.Y. B.Com.) (Sem. - V)
i) To display the last
name and salary of employees who earn from 15000 to 24000
and are in department 101 or 103. Label the columns as Employee
Salary respectively. and monthly
nysql> select lname Enplo yee, salary 'Monthly Salary'
-> from employees
7where salary between 15000 and 24000 and dept_id in (181,103);
Enployee Monthly Salary
joshi 21000.00
1 row in set (0.90 sec)

DTo display the last name and hire date of every employee who was hired in 2001
nysql> select Iname,hire_date
from employees
- where yearhire _date) =2901 or hire_date like "2001z';
! lname ! hire date
joshi } 2001-10-05 !
Jain 2001-10-12 !
2 rows in set, 1 warning (0.06 sec
k) To display the last name and job type of all employees who do not have
manager.
nysgl> select lname,jobid
3 from employees
where manager_id is null;
! lname ! jobid
| mehta gen mgr i
4+

1 row in set (0.00 sec)

) Display the last name salary and commission of all employees who eam
Commissions in the descending order of salary and commission.
mysql> se lect Inane , salary,comniss ion
employees
-> where comniss ion is not null or comniss ion ?
-> order by salary desc,comnission desc ;
H lname salary I commiss ion

mehta 1 55000.00 5500.00


gadiwalla 54000.00 5400.00
Shah
mehta
35000.00
25000.00
3500.00 i
2500.00 !
joshi 21000.00 2100.00
Jain 19000.00 1900.00 !
gandhi 14000.00 1400.00

rows in set <0.05 sec)

m) Display the last names of all employees where the third letter of the name is an h.
nysql) select Inane
-> fron employees
-> where Iname like h';

mehta !
| nehta !
2 rows in set (0.00 sec)
217
es
(Pracçcal)
last
To display the last name of the employees who have an 'a' and an 'e' in their
n) name.
select lname
hsqFrom omployees
-) where
Iname 1ike '%az' and lnano 11ke %0%';
Iname !

nehta
nehta
in set (a.00 sec)
rOws
2

employees whose jobid is sales


o) To display the last name, jobid and salary of the 21000, 25000or 37000.
rep or store clerk and whose salary is not equal to
se lect Iname,jobid, salary
hysq From emplo yees 'sa_rep' >
-s where jobid in (st_clerk'
(21000,25000.356e8):
-) and salary not in
Inane I jobid ! salary

stclerk 14000.00 !
gandhi I 19000.00
Jain ! sarep
(0.01 sec)
2 rows in set

of all the employees whose


p) To display the last name, salary and commission
commission amount is below 2000.
Rysql> se lect lname,salary,comnission
from employees
3 where commission<2000:
Iname Hsalary ! commiss ion
Igandhi ! 14Ø00.00 ! 1408.00 1
I Jain 1900.00

2 rOws in set (0.00 sec>

\STY.BCom --Com. Sys. & Appl. (Sem. - V)


Computer Systems and Applications(T.Y. B.Com.) (Sem.
218

Module - 111 (Practícals)


MULTI-TABLE QUERIES

Consider the following tables employees, departments and locations :


nysql) select *fron enployees;
emp_id fnane Inane enail phoneno hire_ date jobid Isalary connission anager_id I deptid
IdjoshiPhotail.con 23994512 12001-10-5 ad_yp 21000.80 2100.00 S15 131 !
1 dinkar Ijoshi 23889932 1998-%2-21 Ist clerk i 14008.0 1400.00 512 12
2 kiran igandhi kiranlyahoo .com 26238905 2001-10-12 sa rep 19000.08! 1908.00 S1 14
3 hiral IJain Ihiralehotnail.com 24229059 1998-11-24 I ac MT I 25000.00 ! 2500.00 511 13
4 firdosh nehta Ifidosh@hotnail.con
43551123 1999-83-2 salwgr 54000. 00 S400,00 515 14
511 riyaz gadivalla riyulyahoo .con
512 binita shah Ibinitalgnail.con 42200115 1999-12-18 pur Mr 35000.00 3500.00 515 12
515 pinaz mehta Ipinlgaail.coR 2355668? 1995-10-20I gen ngr S5000.00 5500.00 NULL 1&
?POMS in set (8.08 sec)

pysql) select *
-> from departments;
dept id ! deptname ngrid locat ionid !
101 ! admin 515 !
102 ! purchase 512 1800
103 accounts 511 ! 1500
104 sales 515 ! 1400

rows in set (0.Ø0 sec)

nysql) select * from locat ions ;


I postalcodeIcity I state countryid I
I! locat ion id streetadr
1400 19,n.g. road I 415510 ngabad maharashtra I in
21 marina road 462001 I bhop I nadhya pradesh ! in
1500 ! 400050 I mumbai
1700 92. hill road I maharashtra I in
1800 I 11 m.g. road !411013 pune I maharashtra
4 rows in set (0.00 sec)
Tabe Queries (Practical) 219
Managerld and department ld are common columns in the tables employees and
dezartments Location Id is the column common to the tables departments and locations.
queriessfor the
following:
Wnte
a) Write a query to display the last name, department number and department name
for allthe employees.
RySql> select a.lname , a. deptcid, b.deptnane
) fron enployees a, departents b
-) vhere a.dept id=b. dept id;
lnane I dept id deptnane
joshi 101 ! adain
gandhi 182 purchase
shah 162 purchase
ehta 182 purchase
! ehta 183 accounts
Jain 104 ! sales
gadiva lla ! 104 sales

n ros in set (0.82 sec)

b) Create a unique listing of all jobs that are in department 101. Include the location
of the department in the output.
nysql> select distinct a-jobid, b. locat ion id
-> from employees a,departments b
-> where a.dept id=b.dept id and a.dept id=101;
jobid locat ion id
+

|! ad vp i 1708

H row in set <0.02 sec)

c) who
Todisplay the last name, departmentname, location ld and city of allemployees
earn a comnmission.
ysql> select a.Inane,b.deptnane ,b.locat
from enplo yees a,departments b, ion 1dC.C1ty
C
-> where a.deptid-b. dept id
and b. location id=c.locat ion id
-> and a.comniss ion >0;
Iname deptname locat ion id city
joshi admin 1799 unbai
gandhi purchase 1800 pune
shah 1purchase 1800
mehta ipurchase 1800 pune
mehta accounts 1509 bhopal
Ja in ! sales 1400 aurangabad
gadiwalla sales 1400 aurangabad
? rows in set (0.09 sec)

d) Display the last name and department name of allemployees who have 'a' in their
last name.
pysql> se lect a.lname,b.deptname
-> from emplo yees a, departments b
where a.dept id=b.deptu
and a.lname like

lname deptname !
gandhi 1 purchase
shah purchase
| mehta purchase
nehta 8 accounts
Jain sales
4. gadiwalla ! sales
þ rows in set (0.00 sec>
(T.Y. B.Com.) (Sem1
222
Computer Systerns and Applications
same job_id.
d) Display the number of people with the
aysql) select jobid,count (*)
-> fron omployees
group by jobid;
jobid I count (*) I
acngr
ad_vp 1
gen gr
purgr 1
sal gr 1
1
sarep
st_c lerk
rows in set (0.0 sec>

without listing them. Label the column


e) Determine the number of managers
Number of Managers.
of Managers'
select count<distinct manager id) 'Number
nysq rom emplo yees i
Number of Managers
3
row in set (0.01 sec>

Range
) Display the difference between the highest and lowest salaries and label it
of Salary.
se lect nax(salary)-min (salary) 'Bange of Salary'
from employees ;
| Range of Salary
41000.00
1 roW in set (0.02 sec)

) Display the manager number and the salary of the lowest paid employee for that
Manager. Exclude anyone whose manager is not known. Exclude any groups
where the minimum salary is 16,000 or more. Sort the output in the descending
order of salary.
select manager_id, min (salary)

nysq?enployees
-> where manager_id is not null
-> group by manager_id
-> hauing min (salary) >16900
-> order by min (salary) desc;
manager_id min(salaPy) !

515 21000.00
511 ! 19000.00 !

2 rows in set 0.00 sec)


Sb-Queries(Practical) 223

Module - 111 (Practicals)


SUB - QUERIES

Using the table EMPLOYEES listed above write MySQL statements for the following :
a) Todisplay the employee Id and last names of all employees who earn more than
the average salary. Sort the results in ascending order of salary.
nysq select emp_id, lname
Fpom employees
-> where salary>(select avg{salary> from enplo yees>
-> order by salary;
emp_id lname
512 shah
511 gadiwalla
515 ! mehta

3 rows in set (0.00 sec)

b) To display the last name and hire date of any employee in the same department as
'Gandhi'.Exclude 'Gandhi'.
nysql> se lect lnane , hire date
from employees
-) where deptid= (select deptid fron employees where Inane =Gandhi'>
-> and lname () 'Gandhi';
1 Iname hire_date
shah 1999-12-10
i nehta 1995-10-20
2 rOWs in set (9.00 sec)

c) To display the employee Id and last names of all employees who work in a
department with any employee whose last name contains a 'w'.
pysql> se lect enp_id, Inane
-> from emplo ye es lname like z ) :
-> where deptid = (se lect dept id from enployees where
+
i enp_id I Iname
3 Ja in
S11 gadiwalla
rows in set (0.00 sec)
224
Computer Systems and Applications (T.Y. B.Com.) (Sem.
d) To display the last name, department ld and job Id of all
-V)
department location ID is 1700. employees whose
ysql> se lect Inane , dept id.jobid
fron omployees
3 where dept id-(se lect deptid fron de part nents where locat ion id -1708);
| lname dept id i jobid 1
| joshi ! 101 ad_wp
row in set (0.00 sec)

e) To display the last name and salary of every employee who reports to 'gadiwalla'
aysql> se lect InaNe ,salary
-> fron employees
3where anager id-(select emp_id fron enployees where Inane='gadivalla' >;
Inane ! salary
Jain 190O0.00 I
ehta i 25000.00
2 Os in set (0.00 sec)

f) To display the department number, last name and Job Id of every employee in the
accounts department.
ysql> select deptid, Inane.jobid
-> from enployees
-> where deptid=(se lect deptid froR departments where deptnane =' accounts');
deptid i Inae jobid !
103 ! nehta i ac_ngr H
1 ov in set (8.03 sec)

You might also like