CRUD - operations
mysql -h 10.10.12.108 -u t31332 -p
create table studentTable(id int primary key, name varchar(30), roll int(5), mobile int(10), branch varchar(30));
alter table studentTable modify column mobile bigint(13);
insert into studentTable values(1, "Sangmeshwar", 31332, 8010965429, "Comp");
update studentTable set branch = "Computer" where id = 1;
alter table studentTable add foriegn key(id) references classStudent(id) on delete cascade;
delete from studentTable where id = 1;
insert into stuentTableCopy select * from studentTable;
truncate studentTable;
drop studentTable
select * from studentTable;
select * from studentTable where branch = "Computer";
JOINS - all types
select * from studentTable natural join marksTable;
select * from studentTable s inner join marks m on s.id = m.id;
select * from stduentTable s left join marks m on s.id = m.id;
select * from stduentTable s right join marks m on s.id = m.id;
VIEWS
create or replace view v1 as select * from studentTable where branch = "Computer" with check option;
//Insert, update and delete queries are same as simple table queries
create or replace view v2 as select * from studentTable s inner join marksTable m on s.id = m.id;
//Insert, update and delete queries for complex view may not be possible
INDEX
create index i1 on studetTable(id);
show indexes from studetTable;
PROCEDURE
delimiter $
create procedure calArea(IN radius float)
-> begin
-> declare radiusBound condition for sqlstate '45000';
-> if(radius<5 || radius>9)
-> then
-> signal sqlstate '45000' set message_text = "the radius is not in the range";
-> else
-> insert into areaTable values(radius, pi() * radius * radius);
-> end if;
-> end $
call calArea(6)$
FUNCTION
delimiter $
create function calArea(radius float)
-> return float(20);
-> as
-> begin
-> declare radiusBound condition for sqlstate '45000';
-> if(radius>9 || radius<5)
-> then
-> signal sqlstate '45000' set message_text = "radius is not in the range";
-> else insert into areaTable values(radius, pi() * radius * radius);
-> return pi()radiusradius;
-> end if;
-> end $
select calArea(6)$
CURSOR
delimiter $
create procedure clone1(IN rollN int)
-> begin
-> DECLARE c1 CURSOR AS SELECT * FROM studentTable WHERE roll = rollN;
-> OPEN c1;
-> FETCH c1 into rollN;
-> if not exists(select * from studentTable where roll = rollN)
-> then
-> insert into newStudentTable values select * from studentTable where roll = rollN;
-> end if;
-> end $
call clone1(31332)$
TRIGGER
delimiter $
create trigger uppdateTrigger after update on studentTable for each row
begin
insert into auditStudentTable set name = NEW.name, roll = NEW.roll, branch = NEW.branch, mobile = NEW.mobile;
end;
$
ASSIGNMENT ON MONGODB (JDBC)
-
Connect to mongo –
MongoClient mongo = new MongoClient(“host”, 27017); -
Connect to database –
MongoDatabase db = mongo.getDatabase(“t31332db”); -
Get collection –
MongoCollection collection = db.getCollection(“student”); -
Create document –
Document document = new Document(“name”, “Sangmeshwar”).append(“roll”, 31332); -
Insert document –
collection.insertOne(document); -
Display documents –
FindIterable iterDoc = collection.find();
Iterator it = iterDoc.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} -
Update document –
collection.updateOne(Filters.eq("roll", 31332));
Updates.set("fName", fName)); -
Delete documet –
collection.deleteOne(Filters.eq(“roll”, 31332)); -
Delete collection –
collection.drop();