Branch
Branchid Branchname HOD
Student
USN Name Address Branchid Sem
Author
Authorid Authorname Country age
Book
Bookid Bookname Authorid Publisher Branchid
Borrow
USN Bookid Borrowed_Date
ER Model
Reg
Branch Student
has have
set
book Borrow
has
Author
Branch -name
Branch_id HOD
Branch
sem
usn branch
Name Student address
Author_id
Book name Publisher_id
Branch_id
Book_id
Book
Authorname
Author_id Country
Author
create table branch
Age
(
branch_id int(20),
USN
branchname varchar(20),
hod varchar(20),
primary key(branch_id)
);
create table student
usn int(10),
name varchar(20),
addresss varchar(20),
branch_id int(20),
sem int(10),
primary key(usn),
foreign key(branch_id) references branch(branch_id)
);
create table authors
(
author_id int(10),
authorname varchar(20),
country varchar(20),
age int(20),
primary key(author_id)
);
create table book
book_id int(10),
bookname varchar(20),
author_id int(20),
publisher varchar(20),
branch_id int(20),
sem int(10),
primary key(book_id),
foreign key(author_id) references authors(author_id),
foreign key(branch_id) references branch(branch_id)
);
create table borrow
usn int(10),
book_id int(20),
borrowed_date date,
foreign key(usn) references student(usn),
foreign key(book_id) references book(book_id)
);
insert into branch values(101,'MCA','Hunagunda');
insert into student values(1,'Karan','Banglore','101',1 );
insert into authors values(1590,'Timothy A','Algeria',67);
insert into book values(10001,'Computer Ntework',1009,'john',101,1);
insert into borrow values(2017003,10001,18-3-1997);
1.select * from student where branch_id=101 and sem=2;
+-----+------+----------+-----------+------+
| usn | name | addresss | branch_id | sem |
+-----+------+----------+-----------+------+
| 6 | guru | Bagalkot | 101 | 2 |
| 7 | Manu | Bagalkot | 101 | 2 |
+-----+------+----------+-----------+------+
2 rows in set (0.03 sec)
2.select * from student where USN not in (select USN from borrow);
+-----+---------+----------+-----------+------+
| usn | name | addresss | branch_id | sem |
+-----+---------+----------+-----------+------+
| 5 | Goutham | Bagalkot | 104 | 2 |
+-----+---------+----------+-----------+------+
1 row in set (0.02 sec)
3.select student.usn ,student.name,branch.bname, book.bname, aname,
borrowdate from student ,branch, book, author, borrow where
student.usn=borrow.usn and borrow.bookid=book.boid and
book.authorid =author.authorid and student.branchid=branch.branchid
and student.sem=2 and branch='mca';
4.select authorname,COUNT(*) AS 'number of books' FROM
authors,book WHERE authors.author_id=book.author_id GROUP BY
authorname;
+------------+-----------------+
| authorname | number of books |
+------------+-----------------+
| Raja | 1|
| Ram | 3|
| Ramu | 1|
| sham | 1|
+------------+-----------------+
4 rows in set (0.01 sec)
5.select usn,book_id,count(*) from borrow GROUP BY book_id
HAVING COUNT(book_id)>2;
+------+---------+----------+
| usn | book_id | count(*) |
+------+---------+----------+
| 1| 11 | 4|
+------+---------+----------+
1 row in set (0.00 sec)
6.select s.* from student s where exists (select br.usn from borrow br
,book b where br.book_id=b.book_id and br.usn=s.usn group by usn
having count( distinct author_id)>1);
+-----+--------+----------+-----------+------+
| usn | name | addresss | branch_id | sem |
+-----+--------+----------+-----------+------+
| 1 | Karan | Banglore | 101 | 1 |
| 2 | seema | Badami | 102 | 1 |
| 3 | saanvi | hubli | 103 | 2 |
| 7 | Manu | Bagalkot | 101 | 2 |
+-----+--------+----------+-----------+------+
4 rows in set (0.00 sec)
7.select bookname from book order by bookname desc;
+----------+
| bookname |
+----------+
| python |
| os |
| DBMS |
| DBMS |
| CN |
| CN |