Vous êtes sur la page 1sur 15

MySQL

Use MySQL to perform the following:


● Create a database

● Create a table

● Load data into a table

● Retrieve data from the table in various ways

● Use multiple tables


Datatypes in MySQL
● Text
● Number
● Date
Create a database
● show databases;

● create database employee;

● use employee

● select database();
Create a table
● show tables;

● create table emp (


emp_no int not null primary key,
emp_name varchar(20),
state varchar(30),
join_date date,
join_basic decimal(8,2)
);

● desc emp;
EMP_NO EMP_NAME STATE JOIN_DATE JOIN_BASIC
-----------------------------------------------------------------------------------------------
1001 Subhash bose Karnataka 01-06-07 3000
1002 NADEEM SHAH KERALA 01-06-07 2500
1003 SAM BOOLE CHENNAI 01-06-07 3000
1004 Bhagat Singh BANGA 01-06-07 5000
Insert and delete data from a table
● insert into emp values(1001,'Subhash bose','Karnataka',str_to_date('01-06-
07','%d-%m-%y'),3000);

● insert into emp values(1002,'NADEEM SHAH','KERALA',str_to_date('01-06-


07','%d-%m-%y'),2500);

● insert into emp values(1003,'SAM BOOLE','CHENNAI',str_to_date('01-06-07','%d-


%m-%y'),3000);


insert into emp values(1004,'BHAGAT singh','BANGA',str_to_date('01-06-07','%d-
%m-%y'),5000);


delete from emp where emp_no=1004;
Retrieve data from the table in
various ways(1)
SELECT what_to_select FROM which_table WHERE conditions_to_satisfy;
● Selecting all data
select * from emp;
● Selecting particular rows
select * from emp where state='karnataka' and join_basic=3000;
select * from emp where state='karnataka' or state='kerala';
● Sorting rows
SELECT * FROM emp ORDER BY STATE;
● Add/Modify a new field to the table
alter table emp add(desig varchar(3));
alter table emp modify emp_name varchar(20) not null;
● Updating rows
update emp set desig='exe' where emp_no=1001;
update emp set desig='dir' where emp_no=1002;
update emp set desig='mgr' where emp_no=1003;
● Selecting columns from a table
select emp_name, desig from emp;
select distinct desig from emp;
Retrieve data from the table in
various ways(2)
● Date calculations
select emp_name, join_date, datediff(CURDATE(), join_date) as NO_OF_DAYS from
emp;
● Pattern Matching
SELECT * FROM emp WHERE STATE LIKE 'k%';
SELECT * FROM emp WHERE STATE LIKE '%i';
SELECT * FROM emp WHERE STATE LIKE '%e%';
SELECT * FROM emp WHERE STATE LIKE '______';
● Counting Rows
SELECT COUNT(*) FROM emp;
SELECT desig, COUNT(*) FROM emp group by desig;
● MAX and MIN
SELECT MAX(join_basic) AS HighestSalary FROM emp;
● SUM and AVG
SELECT SUM(join_basic) FROM emp;
SELECT AVG(join_basic) FROM emp;
Use multiple tables
● create table salary(
emp_no int not null,
basic decimal(8,2),
commission decimal(8,2),
deduction decimal(8,2),
salary_date date,
foreign key (emp_no) references emp(emp_no) on
delete cascade
);
INSERT THE FOLLOWING DATA

EMP_NO BASIC COMMISSION DEDUCTION SALARY_DATE


--------- ---------- -------------- ------------ --------------
1001 3000 200 250 30-06-07
1002 2500 120 200 30-06-07
1003 3000 500 290 30-06-07
1001 3000 200 250 31-07-07
1002 2500 120 200 31-07-07
1003 3000 500 290 31-07-07
Inserting values into salary table
● insert into salary values(1001,3000,200,250,str_to_date('30-06-07','%d-%m-%y'));

● insert into salary values(1002,2500,120,200,str_to_date('30-06-07','%d-%m-%y'));


insert into salary values(1003,3000,500,290,str_to_date('30-06-07','%d-%m-%y'));

● insert into salary values(1001,3000,200,250,str_to_date('31-07-07','%d-%m-%y'));


insert into salary values(1002,2500,120,200,str_to_date('31-07-07','%d-%m-%y'));

● insert into salary values(1003,3000,500,290,str_to_date('31-07-07','%d-%m-%y'));


● Between operator (selects values within a given
range)
select * from salary where deduction between 200 and
250;
● Column aliases
select emp_name as names,join_basic as basic from
emp where join_basic>=3000;
● Join operation
SELECT column_name(s) FROM table1 INNER
JOIN table2 ON table1.column_name =
table2.column_name;
select
e.emp_name,s.emp_no,s.basic,s.commission,s.deducti
on,s.salary_date from emp e inner join salary s on
s.emp_no=e.emp_no;

Vous aimerez peut-être aussi