Académique Documents
Professionnel Documents
Culture Documents
● Create a table
● use employee
● select database();
Create a table
● show tables;
● 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(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
●
insert into salary values(1003,3000,500,290,str_to_date('30-06-07','%d-%m-%y'));
●
insert into salary values(1002,2500,120,200,str_to_date('31-07-07','%d-%m-%y'));