College Student
database
management
by
B.Balapriya B.E
Database and table creation
Creating database Creating Table
• create database • create table department (dep_id int primary
student_db; key ,dep_name varchar(15),hod_name varchar(20));
• use student_db; • create table if not exists student_details (stud_id int
auto_increment ,s_name varchar(20),s_email
varchar(30) unique,s_phone varchar(15)
unique,D_O_B date,gpa float,year_of_study
intcheck(year_of_study>0 and
year_of_study<=4),constraintprimarykey(stud_id));
• create table study_details (year int primary
key,sem_no int );
Inserting value
• Insert into student_details(s_name,s_email,s_phone,D_o_B,gpa,dep_id)values('nila','
[email protected]',
'967137890','2003-03-31',8.9,2);
Alter table With constraints
• Alter table student_details add column dep_id int;
• alter table student_details add foreign key (dep_id ) references department(dep_id);
• alter table student_details auto_increment=101;
Delete Query with where clause
• delete from student_details where s_name='akila';
Where and order by
• select s_name ,gpa from student_details
where year_of_study=4
order by gpa;
And Operator
• select s_name ,gpa from student_details
where year_of_study=4 And dep_id=3
order by gpa;
And Operator
• select s_name ,gpa ,year_of_study from student_details
where year_of_study=4 or year_of_study=2
order by gpa;
Not Operator
• select s_name ,gpa,year_of_study from student_details
where not year_of_study=4
order by gpa desc;
Limit Operator
-- limit --
-- selecting top 2 students from dep_id 3 --
• select s_name ,gpa from student_details
where year_of_study=4 And dep_id=3
order by gpa desc limit 2;
Aggregate function
• select max(gpa) from student_details
where year_of_study=4 And dep_id=5;
• select min(gpa) from student_details
where year_of_study=4 And dep_id=5;
• select count(s_name) from student_details
where year_of_study=3 And dep_id=5;
Wild card Operations
• select s_name from student_details
where s_name like 'a%';
select s_name from student_details
where s_name like 'a__a';
IN Operator
• select s_name ,dep_id from student_details
where dep_id in(1,2);
Joins
• select s.s_name ,s.year_of_study ,d.dep_name
from student_details as s
inner join department as d
on s.dep_id=d.dep_id;
• select s.s_name ,s.year_of_study ,d.dep_name
from student_details as s
left join department as d
on s.dep_id=d.dep_id;
• select s.s_name ,s.year_of_study ,d.dep_name
from student_details as s
right join department as d
on s.dep_id=d.dep_id;
• select s1.s_name ,s1.year_of_study , s2.s_name ,s2.year_of_study
from student_details as s1, student_details as s2
where s1.dep_id <> s2.dep_id and s1.year_of_study =s2.year_of_study
order by s1.s_name;
Date functions
• update student_details
set d_o_b= date_add(d_o_b,interval 5 day)
where s_name='aadhan';
• update student_details
set d_o_b= date_sub(d_o_b,interval 5 day)
where s_name='aadhan';
• update student_details
set d_o_b= d_o_b+2
where s_name='aadhan';
Union and union All
• select dep_id from student_details -- does not allow duplicates --
union
select dep_name from department;
select dep_id from student_details -- allow duplicates --
union all
select dep_name from department;
Having Clause
• select dep_id,count(dep_id) as count
from student_details
where year_of_study=4
group by dep_id
having count >2;
Union and union All
• select dep_id,dep_name
from department as d
where exists (select gpa
from student_details as s
where gpa > 9.0 and d.dep_id=s.dep_id );
Exists ,Any and All
• select dep_id,dep_name
from department as d
where exists (select gpa
from student_details as s
where gpa > 9.0 and d.dep_id=s.dep_id );
• select * from department
where dep_id=any(select dep_id
from student_details
where gpa>8.5
);
• select * from department
where dep_id<>all(select dep_id
from student_details
where dep_name in('cse','eee')
);
Creating View
• create view cse
as select *
from student_details
where dep_id=2;
• select * from cse;
• drop view view1;
• create user 'user1' @'localhost' identified by'bala11125';
Grant all & Revoke all
• grant all privileges on student_db.* to user @'localhost';
• revoke all privileges on student_db.* from user
@'localhost';
Grant & Revoke
• grant select on student_db.* to user3 @'localhost';
• revoke select on student_db.* from user3 @'localhost';
Rollback and Commit
• start transaction;
• delete from student_details
where stud_id=185;
• rollback;
• commit;
Current date and Timestamp
• select * from student_details
• where regexp_like(s_name,'^s[a-z]+') ;
Regular Expression
• select * from student_details
• where regexp_like(s_name,'^s[a-z]+') ;
Stored Procedure
• delimiter //
create procedure selectAllStudents()
begin
select *from student_details;
end //
delimiter //
• call selectAllStudents()
THANK YOU