0% found this document useful (0 votes)
31 views

DBMS Record

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

DBMS Record

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex. No:1 DATA DEFINITION COMMANDS, DATA MANIPULATION


COMMANDS FOR INSERTING, DELETING, UPDATING AND
RETRIEVING TABLES AND TRANSACTION CONTROL
STATEMENTS

AIM:
To design and implement a database for manipulating & storing data items in MYSQL by using
SQL commands.

CREATE: (Syntax)
Database:
Create database <database name>;
Use <database name>;
Table:
Create table <table name> (column_name1 datatype1 constraints, column_name2
datatype2 constraint....column_nameN datatypeN constraints);

Note: Constraints is optional


DDL COMMANDS:
1) Create
2) Alter
 Add
 Modify
 Drop
3) Rename
4) 4) Drop
ALTER:
ADD:
Alter table<table name>add column_name1 datatype1 constraints;
MODIFY:
Alter table<table name>modify column_name1 datatype1;
DROP:
Alter table<table name>drop column_name;
RENAME:
Rename table <old table name>to<new table name>;
DROP:
Drop table<table name>;
DML COMMANDS:
1) Insert 3) Select
2) Update 4) Delete
INSERT:
Insert into<tablename>values(Value1,value2…….ValueN);
SELECT:
Select<column name>from<table name> where condition;
UPDATE:
Update<table name>set<column name>=values where condition;

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 1


CS8481 DBMS LAB Department of IT/CSE 2021-2022

DELETE:
Delete from<table name> where condition;
TCL COMMANDS:
1) Commit
2) Rollback
3) Savepoint
COMMIT:
Commit;
ROLLBACK:
Rollback to <savepoint>;
SAVEPOINT:
Savepoint <savepoint name>;

PROBLEM STATEMENT:

 A branch contains many accountholders.


 A branch provides more than one loan.
 A loan can be availed by more than customer.
 A customer can get more than one loan.
 A customer can have more one account.
 An account can have more than one customer.

1. TABLE FROM THE PROBLEM STATEMENT:


1) Branch_m
2) Account_m
3) Loan_m
4) Customer_m
Database Name: it
mysql>create database it;
mysql>use it;
Table Name: Branch_m
mysql> create table branch_m(branch_name varchar(20) primary
key,branch_city varchar(20),asset int);

Query OK, 0 rows affected

mysql> desc branch_m;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| branch_name | varchar(20) | NO | PRI | NULL | |
| branch_city | varchar(20) | YES | | NULL | |
| asset | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 2


CS8481 DBMS LAB Department of IT/CSE 2021-2022

3 rows in set
Table name: Customer_m
mysql> create table customer_m(customer_id varchar(20) primary
key,customer_name varchar(20),customer_street
varchar(20),customer_city varchar(20));
Query OK, 0 rows affected

mysql> desc customer_m;


+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| customer_id | varchar(20) | NO | PRI | NULL | |
| customer_name | varchar(20) | YES | | NULL | |
| customer_street | varchar(20) | YES | | NULL | |
| customer_city | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set
Table name: Account_m
mysql> create table account_m(account_no varchar(20) primary
key,branch_name varchar(20),balance int,foreign key(branch_name)
references branch(branch_name));
Query OK, 0 rows affected

mysql> desc account_m;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| account_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| balance | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
Table name: Loan_m
mysql> create table loan_m(loan_no varchar(20) primary
key,branch_name varchar(20),amount int,foreign key(branch_name)
references branch(branch_name));

Query OK, 0 rows affected


mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 3


CS8481 DBMS LAB Department of IT/CSE 2021-2022

3 rows in set

2.Alter the table branch_m by increasing the field width of branch city to 25.
mysql> desc branch_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| branch_name | varchar(20) | NO | PRI | NULL | |
| branch_city | varchar(20) | YES | | NULL | |
| asset | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
mysql> alter table branch_m modify branch_city varchar(25);

Query OK, 0 rows affected


Records: 0 Duplicates: 0 Warnings: 0
mysql> desc branch_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| branch_name | varchar(20) | NO | PRI | NULL | |
| branch_city | varchar(25) | YES | | NULL | |
| asset | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
3.Drop the primary key from loan_m

mysql> desc loan_m;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
mysql> alter table loan_m drop primary key;

Query OK, 0 rows affected


Records: 0 Duplicates: 0 Warnings: 0
mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 4


CS8481 DBMS LAB Department of IT/CSE 2021-2022

+-------------+-------------+------+-----+---------+-------+
3 rows in set
4.Alter the primary key to loan_m
mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table loan_m add primary key(loan_no);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
5.Add new column to loan_m
mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
mysql> alter table loan_m add roi int;

Query OK, 0 rows affected


Records: 0 Duplicates: 0 Warnings: 0
mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
| roi | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 5


CS8481 DBMS LAB Department of IT/CSE 2021-2022

4 rows in set
6.Drop the column from loan_m
mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
| roi | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set
mysql> alter table loan_m drop roi;

Query OK, 0 rows affected


Records: 0 Duplicates: 0 Warnings: 0
mysql> desc loan_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set

7. Rename the customer_m as customer_ma

mysql> desc customer_m;

+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| customer_id | varchar(20) | NO | PRI | NULL | |
| customer_name | varchar(20) | YES | | NULL | |
| customer_street | varchar(20) | YES | | NULL | |
| customer_city | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set

mysql> rename table customer_m to customer_ma;

Query OK, 0 rows affected

mysql> desc customer_m;


ERROR 1146 (42S02): Table 'lab.customer' doesn't exist

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 6


CS8481 DBMS LAB Department of IT/CSE 2021-2022

mysql> desc customer_ma;

+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| customer_id | varchar(20) | NO | PRI | NULL | |
| customer_name | varchar(20) | YES | | NULL | |
| customer_street | varchar(20) | YES | | NULL | |
| customer_city | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set

8)a) Drop customer_ma

mysql> desc customer_ma;


+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| customer_id | varchar(20) | NO | PRI | NULL | |
| customer_name | varchar(20) | YES | | NULL | |
| customer_street | varchar(20) | YES | | NULL | |
| customer_city | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set

mysql> drop table customer_ma;

Query OK, 0 rows affected

mysql> desc customer_ma;


ERROR 1146 (42S02): Table 'lab.customer1' doesn't exist

8)b) Rename the column loanamount to amount from loan table.

mysql> create table loan_m(loan_no int primary key,branch_name


varchar(20),loanamount int);
Query OK, 0 rows affected (0.20 sec)

mysql> desc loan_m;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | int(11) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | | NULL | |
| loanamount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 7


CS8481 DBMS LAB Department of IT/CSE 2021-2022

mysql> alter table loan_m change column loanamount amount int;


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc loan_m;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| loan_no | int(11) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

9. INSERTRECORDS IN ALL THE FOUR CREATED TABLES:


Insert the values given below.(Branch Table)
BRANCH_NAME BRANCH_CITY ASSETS

Perryridge Rye 50000


Downtown Stamford 100000
Brighton Paloalto 25000
Redwood Harrison 150000
Mianus Pitsfield 450000
Roundhill Princeton 150000
mysql> desc branch_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| branch_name | varchar(20) | NO | PRI | NULL | |
| branch_city | varchar(25) | YES | | NULL | |
| asset | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set

mysql> insert into branch_m values('perryridge','rye',50000);


Query OK, 1 row affected

Insert the values given below. (Loan Table)


LOAN BRANCH_NAME AMOUNT

1_11 Roundhill 900


mysql> desc loan_m;
1_14 Downtown 1500
+-------------+-------------
1_15 Perryridge 1500 +------+-----+---------+-------
1_16 Perryridge 1300 +
1_17 Downtown 1000
St. 1_23 Redwood
Joseph’s college of Engineering/St.2000
Joseph’s Institute of Technology 8
1_93 Mianus 500
l_102 Mianus Null
CS8481 DBMS LAB Department of IT/CSE 2021-2022

| Field | Type | Null | Key | Default | Extra |


+-------------+-------------+------+-----+---------+-------+
| loan_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| amount | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
mysql> insert into loan_m values('l_23','redwood',2000);
Query OK, 1 row affected
Insert the values given below. (Customer Table)
CUSTOMER_NAM CUSTOMER_STREE CUSTOMER_CIT
CUSTOMER_ID E T Y
c_01 Smith north rye
c_02 Turner putnam stamford
c_03 Johnson alma paloalto
c_04 Curry north rye
c_05 Jones main harrisdon
c_06 Adoms spring pittsfield
c_07 Lindsay park pittsfeild
c_08 Hayes main harrison
c_09 Williams nassu princeton
mysql> desc customer_m;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| customer_id | varchar(20) | NO | PRI | NULL | |
| customer_name | varchar(20) | YES | | NULL | |
| customer_street | varchar(20) | YES | | NULL | |
| customer_city | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set
mysql>insert into customer_m
values('c_01','smith','north','rye');
Query OK, 1 row affected
Insert the values given below. (Account Table)
ACCOUNT_NO BRANCH_NAME BALANCE

019_28_3746 Perryridge 1500


182_73_6091 Downtown 1800 mysql>desc account_m;
192_83_7465 Brighton 500 +-------------
321_12_3123 Redwood 2300 +-------------+------
336_96_9999 Mianus 500 +-----+---------+-------+
963_96_3963 Roundhill 500 | Field | Type
376_66_9999 Mianus 900 | Null | Key | Default |
963_96_3964 Mianus 1300 Extra |

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 9


CS8481 DBMS LAB Department of IT/CSE 2021-2022

+-------------+-------------+------+-----+---------+-------+
| account_no | varchar(20) | NO | PRI | NULL | |
| branch_name | varchar(20) | YES | MUL | NULL | |
| balance | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
mysql>insert into account_mvalues(019_28_3746,’perryridge’,1500);
Query ok,1 row affected.
10. Find the names of all branches in loan relation.
mysql> select branch_name from loan_m;
+-------------+
| branch_name |
+-------------+
| downtown |
| downtown |
| mianus |
| perryridge |
| perryridge |
| redwood |
| roundhill |
+-------------+
7 rows in set
11. Find the names of all branches in loan relation eliminateduplicate.
mysql> select distinct branch_name from loan_m;
+-------------+
| branch_name |
+-------------+
| downtown |
| mianus |
| perryridge |
| redwood |
| roundhill |
+-------------+
5 rows in set

12.Updatethe customer city stamford to rye in customerrelation.


mysql> update customer_m set customer_city='rye' where
customer_city='stamford';

Query OK, 2 rows affected


Rows matched: 2 Changed: 2 Warnings: 0

mysql>commit;
13.Showthe effect of savepoint and roll back command using delete query with example.
mysql> start transaction;
Query OK, 0 rows affected
mysql> select * from loan_m;

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 10


CS8481 DBMS LAB Department of IT/CSE 2021-2022

+---------+-------------+--------+
| loan_no | branch_name | amount |
+---------+-------------+--------+
| l_102 | mianus | NULL |
| l_11 | roundhill | 900 |
| l_14 | downtown | 1500 |
| l_15 | perryridge | 1500 |
| l_16 | perryridge | 1300 |
| l_17 | downtown | 1000 |
| l_23 | redwood | 2000 |
| l_24 | mianus | 500 |
+---------+-------------+--------+
8 rows in set
mysql> savepoint s1;
savepoint created.
mysql> delete from loan_m;
Query OK, 8 rows affected
mysql> select * from loan_m;
Empty set
mysql> rollback to s1;
Query OK, 0 rows affected
mysql> select * from loan_m;
+---------+-------------+--------+
| loan_no | branch_name | amount |
+---------+-------------+--------+
| l_102 | mianus | NULL |
| l_11 | roundhill | 900 |
| l_14 | downtown | 1500 |
| l_15 | perryridge | 1500 |
| l_16 | perryridge | 1300 |
| l_17 | downtown | 1000 |
| l_23 | redwood | 2000 |
| l_24 | mianus | 500 |
+---------+-------------+--------+
8 rows in set

mysql>commit;

Result:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 11


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex. No: 2 DATABASE QUERYING – SIMPLE QUERIES, NESTED QUERIES,


SUB QUERIES AND JOINS

AIM:

To implement and execute a query for manipulating & storing data items in mysql database
using Structured Query Language commands

SYNTAX:

NESTED and SUB QURIES:


SELECT column_name(s) FROM table_name WHERE condition OPERATOR (SELECT
column_name(s) FROM table_name);

JOINS:
INNER JOIN:
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON
table_name1.column_name=table_name2.column_name

LEFT JOIN
SELECT column_name(s)FROM table_name1LEFT JOIN table_name2ON
table_name1.column_name=table_name2.column_name

RIGHT JOIN
SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON
table_name1.column_name=table_name2.column_name

FULL JOIN
SELECT column_name(s)FROM table_name1FULL JOIN table_name2ON
table_name1.column_name=table_name2.column_name
SIMPLE QUERIES

1. Display the loan relation with attributes amount multiplied by 100.


mysql> select amount*100 from loan_m;
+------------+
| amount*100 |
+------------+
| 90000 |
| 150000 |

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 12


CS8481 DBMS LAB Department of IT/CSE 2021-2022

| 150000 |
| 130000 |
| 100000 |
| 200000 |
| 50000 |
+------------+
7 rows in set
2. Findall numbers for loan made at perryridge branch with loan amount greater than
1400.

mysql> select loan_no from loan_m where branch_name='perryridge'


and amount>1400;
+---------+
| loan_no |
+---------+
| l_15 |
+---------+
1 row in set
3. Findall loan numbers for loan’s with loan amount between 900 and 1500.
mysql> select loan_no from loan_m where amount between 900 and
1500;
+---------+
| loan_no |
+---------+
| l_11 |
| l_14 |
| l_15 |
| l_16 |
| l_17 |
+---------+
5 rows in set
4. Findthe names of customer of customer whose street names includes the character r in
the third position.
mysql> select customer_name from customer_m where
customer_street like'__r%';
+---------------+
| customer_name |
+---------------+
| smith |
| curry |
| adoms |
+---------------+
3 rows in set (0.00 sec)
5. Findthe names of customer_m whose street name starts withsubstring ‘ma’.
mysql> select customer_name from customer_m where
customer_street like'ma%';

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 13


CS8481 DBMS LAB Department of IT/CSE 2021-2022

+---------------+
| customer_name |
+---------------+
| williams |
+---------------+
1 row in set

6. Displaythe entire loan relation in descending order ofamount.


mysql> select * from loan_m order by amount desc;
+---------+-------------+--------+
| loan_no | branch_name | amount |
+---------+-------------+--------+
| l_23 | redwood | 2000 |
| l_14 | downtown | 1500 |
| l_15 | perryridge | 1500 |
| l_16 | perryridge | 1300 |
| l_17 | downtown | 1000 |
| l_11 | roundhill | 900 |
| l_24 | mianus | 500 |
+---------+-------------+--------+
7 rows in set
7. Findtotal number of customer.
mysql> select count(customer_id) from customer_m;
+--------------------+
| count(customer_id) |
+--------------------+
| 6 |
+--------------------+
1 row in set
8. Findall the loan number that appears in the loan relationwith NULL values for amount.
mysql> select loan_no from loan_m where amount is null;
+---------+
| loan_no |
+---------+
| l_102 |
+---------+
1 row in set
9.mysql> select * from customer_m where customer_id=’c_02’;

+-------------+---------------+-----------------+---------------+
| customer_id | customer_name | customer_street | customer_city |
+-------------+---------------+-----------------+---------------+
| c_02 | turner | putnam | rye |
+-------------+---------------+-----------------+---------------+
1 rows in set (0.00 sec)

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 14


CS8481 DBMS LAB Department of IT/CSE 2021-2022

NESTED and SUB QUERIES:

1. Find all the name of all branches that have assets greater than atleast one bank
located in Stanford. (Nested queries)
mysql>Select branch_name from branch_ma where assets >ANY
(select assets from branch_ma where branch_city='stanford');

BRANCH_NAME
------------------------------
Redwood
Mianus
Roundhill
2. Display the entire customer name in alphabetical order that have loan in
Perryridge.(Nested queries).
mysql>Select customer_name from customer_ma where
customer_id=ANY (
select customer_id from borrow_ma where borrow_ma.loan_no=ANY(
select loan_no from loan_ma where branch_name='Perryridge'))
order by customer_ma.customer_name asc;

CUSTOMER_NAME
--------------------
Johnson
3. Find all customer having both account and loan at same branch.(Nested queries)

mysql>Select depositor_ma.customer_id from depositor_ma where


customer_id IN (select borrow_ma.customer_id from borrow_ma);

CUSTOMER_ID
----------------------
c_03
c_05

4. Find all customers who have loan at bank but who don’t have account at same branch.
(Nested queries).

mysql>Select borrow_ma.customer_id from borrow_ma where


customer_id NOT IN (
select depositor_ma.customer_id from depositor_ma);

CUSTOMER_ID
-------------------------
c_01
c_01

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 15


CS8481 DBMS LAB Department of IT/CSE 2021-2022

5. Find the name of all branches that have assets greater than those atleast one branch
located in Harrison.(Nested queries).

mysql>Select branch_name from branch_ma where assets>any(select


assets from branch_ma
where branch_city='Harrison');

BRANCH_NAME
------------------------------
Mianus

JOINS:

1. For all customer who have loan from the bank find their ID’s , loan number and loan
amount.(Join)

mysql>Select borrow_ma.customer_id, loan_ma.loan_no,


loan_ma.amount from borrow_ma,
loan_ma where loan_ma.loan_no=borrow_ma.loan_no;

CUSTOMER_ID LOAN_NO AMOUNT


----------------------------------------------------------------
----
c_01 L_11 900
c_01 L_23 2000
c_03 L_93 500
c_05 L_17 1000
c_03 L_16 1300
c_05 L_14 1500
6 rows selected.

2. For all customers who have loan at perryridge branch find their ID’s,loan ID,loan
amount.(Join)

mysql>Select borrow_ma.customer_id, loan_ma.loan_no,


loan_ma.amount from loan_ma,borrow_ma where
borrow_ma.loan_no=loan_ma.loan_no and
loan_ma.branch_name='Perryridge';

CUSTOMER_ID LOAN_NO AMOUNT


------------------------------------------------------
c_03 L_16 1300

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 16


CS8481 DBMS LAB Department of IT/CSE 2021-2022

3. Find the number of depositor at each branch.(Join)

mysql>Select
account_ma.branch_name,count(depositor_ma.customer_id) from
account_ma,depositor_ma where
depositor_ma.account_no=account_ma.account_no group by
account_ma.branch_name;

BRANCH_NAME COUNT (DEPOSITOR_MA.CUSTOMER_ID)


-----------------------------------------------------------
Brighton 1
Downtown 1
Mianus 2
Redwood 1
Roundhill 1

Result:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 17


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex. No: 3 VIEWS,SEQUENCE AND SYNONYM

AIM:
To implement and execute view,sequence,indexes,savepoint in MYSQL using SQL
commands.

VIEWS:
Create view <viewname> as any select query; (Ex: select * from <tablename> where
<condition>)

SEQUENCE:
Create table <tablename> (column1 datatype1primary key auto_increment,column2 datatype
2……columnN datatypeN)

SYNONYM:
Create synonym<synonym name> for <table name>;
VIEWS:
1) Create a view using aggregate functions to calculate the age of the Person
 mysql> create table person_m(name varchar(20) primary
key,dob date,person_city varchar(20));
Query OK, 0 rows affected
mysql> desc person_m;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| name | varchar(20) | NO | PRI | NULL | |
| dob | date | YES | | NULL | |
| person_city | varchar(20) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set
mysql> insert into person_m values('abdulkalam','1931-08-
18','rameshwaram');
Query OK, 1 row affected
mysql> select * from person_m;
+----------------+------------+-------------+
| name | dob | person_city |
+----------------+------------+-------------+
| abdulkalam | 1931-10-15 | rameshwaram |
| maheshboobathy | 1974-07-02 | chennai |
| sachin | 1973-05-04 | mumbai |
| viswanathanand | 1970-03-06 | chennai |
+----------------+------------+-------------+
4 rows in set
mysql> create view personage_m as select
name,datediff(sysdate(),dob)/365.25 as age from person_m;
Query OK, 0 rows affected
mysql> select * from personage_m;

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 18


CS8481 DBMS LAB Department of IT/CSE 2021-2022

+----------------+---------+
| name | age |
+----------------+---------+
| abdulkalam | 83.9726 |
| maheshboobathy | 41.1006 |
| sachin | 42.2615 |
| viswanathanand | 45.4237 |
+----------------+---------+
4 rows in set

mysql> insert into person_m values('jorden','1970-07-08','usa');


Query OK, 1 row affected (0.06 sec)

mysql> select * from personage_m;


+----------------+---------+
| name | age |
+----------------+---------+
| abdulkalam | 83.9726 |
| jorden | 45.0842 |
| maheshboobathy | 41.1006 |
| sachin | 42.2615 |
| viswanathanand | 45.4237 |
+----------------+---------+
5 rows in set (0.00 sec)

SEQUENCE:
2) Create a sequence and design the department table in the given attribute.

mysql> create table department_m(department_id int primary key


auto_increment,department_name varchar(20));
Query OK, 0 rows affected (0.14 sec)

mysql> desc department_m;


+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| department_id | int(11) | NO | PRI | NULL | auto_increment |
| department_name | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> insert into department_m(department_name)values('it');
Query OK, 1 row affected (0.08 sec)
mysql> insert into department_m(department_name)values('cse');
Query OK, 1 row affected (0.08 sec)
mysql>
insert into department_m(department_name)values('mechanical');
Query OK, 1 row affected (0.06 sec)
mysql> insert into department_m(department_name)values('aero');

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 19


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Query OK, 1 row affected (0.07 sec)


mysql> select * from department_m;
+---------------+-----------------+
| department_id | department_name |
+---------------+-----------------+
| 1 | it |
| 2 | cse |
| 3 | mechanical |
| 4 | aero |
+---------------+-----------------+
4 rows in set (0.00 sec)

SYNONYM(UsingOracle)
3) Show the effect of synonym
CREATING A SYNONYM FOR A TABLE

CREATE TABLE product_m (product_nameVARCHAR2(25) PRIMARY KEY,


product_price NUMBER(4,2), quantity_on_hand NUMBER(5,0), last_stock_date DATE);
Table created.

AFTER INSERTING THE RECORDS TO PRODUCT TABLE

SQL> SELECT * FROM product_m;


PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC
------------------------- -------------------------- ------------------------------ ------------------
Product 1 99 1 15-JAN-03
Product 2 75 1000 15-JAN-02
Product 3 50 100 15-JAN-03
Product 4 25 10000 14-JAN-03
Product 5 9.95 1234 15-JAN-04
Product 6 45 1 31-DEC-08
6 rows selected.

SQL> SELECT * FROM prod_m;


SELECT * FROM prod_m
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> CREATE SYNONYM prod_m FOR product_m;
Synonym created.

SQL> SELECT * FROM prod_m;


PRODUCT_NAME PRODUCT_PRICE QUANTITY_ON_HAND LAST_STOC
------------------------- -------------------------- ------------------------------ ------------------
Product 1 99 1 15-JAN-03
Product 2 75 1000 15-JAN-02

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 20


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Product 3 50 100 15-JAN-03


Product 4 25 10000 14-JAN-03
Product 5 9.95 1234 15-JAN-04
Product 6 45 1 31-DEC-08
SQL> drop SYNONYM prod_m;
Synonym dropped.

SQL> drop table product_m;


Table dropped.
Result:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 21


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex.No.4 DATABASE PROGRAMMING: IMPLICIT AND EXPLICIT CURSORS


AIM:

To implement and execute cursor in Mysql using Procedural Language concepts.

Cursor:A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A
cursor holds the rows (one or more) returned by a SQL statement. The set of rows the cursor holds is
referred to as the active set.

There are two types of cursors −


 Implicit cursors
 Explicit cursors
SYNTAX:

IMPLICIT CURSOR:

Sql%attribute name
Where attribute name can be FOUND, NOT FOUND, ISOPEN, %ROWCOUNT

EXPLICIT CURSOR:

Creating an explicit cursor


CURSOR <cursor_name> IS select_statement;

Opening the Cursor


OPEN <cursor_name>;

Fetching the Cursor


FETCH <cursor_name>INTO attributes;

Closing the Cursor


CLOSE <cursor_name>;

mysql> select * from student;


+--------+----------+-----------+
| stu_id | stu_name | stu_class |
+--------+----------+-----------+
| 1 | david | 10 |
| 2 | shah | 20 |
| 3 | mike | 30 |
| 4 | maze | 40 |
+--------+----------+-----------+
4 rows in set (0.00 sec)

IMPLICIT CURSOR (in Oracle)


DECLARE
rows number(2);
BEGIN

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 22


CS8481 DBMS LAB Department of IT/CSE 2021-2022

UPDATE student SET stu_class = stu_class + 2;


IF sql%notfound THEN
dbms_output.put_line('no student selected');
ELSIF sql%found THEN
rows := sql%rowcount;
dbms_output.put_line(rows || ' student selected ');
END IF;
END;

4 student selected

EXPLICIT CURSOR:
mysql> delimiter $$
mysql> create procedure curdemo(id int)
-> begin
-> declare name varchar(255);
-> declare cur1 cursor for select stu_name from student
where stu_id=id;
->open cur1;
-> fetch cur1 into name;
->select name;
->close cur1;
->end $$
Query OK, 0 rows affected (0.13 sec)

mysql> delimiter ;
mysql> call curdemo(2);
+------+
| name |
+------+
| shah |
+------+
1 row in set (0.39 sec)

Result:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 23


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex.No.5 PROCEDURES AND FUNCTIONS


AIM:

To implement and execute Procedures and functions in Mysql using Procedural Language
concepts.

PROCEDURES: Procedure is a sub program used to perform an action. Replace-recreates the


procedure if it already exists.
3 MODES:
1) IN – Means you must specify a value for the argument at the time execution of the procedure.
2) OUT-passes back a value to its calling program.
3) INOUT – When calling the procedure, yu must specify the value and that procedures passes
value back to the calling procedure.
SYNTAX:

Create procedure <procedure_name> (argument {in, out, in out} data type) is


Variable declaration
Begin
Pl/SQL Subprogram body.
End;
FUNCTION: A function is a sub program that accepts argument and returns a unique value to the caller.

SYNTAX :

Create function <function_name> (parameter) return <data type> is


Variable declaration
Begin
Pl/SQL Subprogram body.
Return statement
End;

1. Procedures:
Create a procedure to update the phone number to customer table
Mysql> create table customer_m (cust_id int primary key,cust_name
varchar(20),cust_phone int);
Query OK, 0 rows affected (0.04 sec)

Mysql> desc customer_m;


+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| cust_id | int(11) | NO | PRI | NULL | |
| cust_name | varchar(20) | YES | | NULL | |
| cust_phone | int(11) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 24


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Mysql> insert into customer_m (cust_id,cust_name)values(201,'raja');


Query OK, 1 row affected (0.30 sec)
Mysql> insert into customer_m (cust_id,cust_name)values(202,'ravi');
Query OK, 1 row affected (0.00 sec)

Mysql> select * from customer_m;


+---------+-----------+------------+
| cust_id | cust_name | cust_phone |
+---------+-----------+------------+
| 201 | raja | NULL |
| 202 | ravi | NULL |
+---------+-----------+------------+
2 rows in set (0.00 sec)
Procedure
---------
DELIMITER //
CREATE PROCEDURE c
(IN ph int,IN cid int)
BEGIN
declare name varchar(20);
select cust_name into name from customer_m where cust_id=cid;
if name is null then
select 'Name is not there';
ELSE
update customer_m set cust_phone=ph where cust_id=cid;
END IF;
END //

DELIMITER ;

Mysql> call c(66,88);


+-------------------+
| Name is not there |
+-------------------+
| Name is not there |
+-------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Mysql> call c(98567,201);


Query OK, 1 row affected (0.16 sec)
Mysql> select * from customer_m;
+---------+-----------+------------+
| cust_id | cust_name | cust_phone |
+---------+-----------+------------+
| 201 | raja | 98567 |
| 202 | ravi | NULL |
+---------+-----------+------------+
2 rows in set (0.00 sec)

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 25


CS8481 DBMS LAB Department of IT/CSE 2021-2022

2. Functions
Create a function to check whether particular customer is having phone number or Not
create table customer_m (custid int,phone int);
insert into customer_m values(101,899);

mysql> create function check12(cust_id int)


->returns varchar(20)
-> begin
-> declare ph int;
->select phone into ph from customer_m where custid=cust_id;
->if ph is null then
-> return 'Mobile Number is not there';
-> ELSE
-> return 'Mobile Number is there';
-> END IF;
->end //
Query OK, 0 rows affected (0.00 sec)

mysql> select * from customer_m;


+--------+-------+
| custid | phone |
+--------+-------+
| 101 | 899 |
+--------+-------+
1 row in set (0.00 sec)

mysql> select check12(101);

+------------------------+
| check11(101) |
+------------------------+
| Mobile Number is there |
+------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> insert into customer_m (custid)values(102);

Query OK, 1 row affected (0.17 sec)

mysql> select * from customer_m;

+--------+-------+
| custid | phone |
+--------+-------+
| 101 | 899 |
| 102 | NULL |
+--------+-------+
2 rows in set (0.00 sec)

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 26


CS8481 DBMS LAB Department of IT/CSE 2021-2022

mysql> select check12(102);

+---------------------------+
| check12(102) |
+---------------------------+
| Mobile Number is not there|
+---------------------------+
1 row in set, 1 warning (0.00 sec)

RESULT

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 27


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex.No.6 TRIGGERS
AIM:

To implement and execute triggers and functions in Mysql using Procedural Language concepts.
TRIGGERS:
1) Trigger is a special type of procedure that the oracle executes when an insert, modify or
delete operation is performed against a given table.
2) It is a stored sub program associated with a table.
3) It is used to keep an audit trial of a table, to prevent invalid transaction, enforce complex
security authorization, to generate data automatically.

SYNTAX FOR TRIGGER

CREATE TRIGGER <TRIGGER NAME>


{BEFORE/AFTER}
{INSERT/UPDATE/DELETE}
ON <TABLENAME>
REFRENCECING {OLD AS OLD /NEW AS NEW}
[FOR EACH STATEMENT /FOR EACH ROW [WHEN <CONDITION>]]
DECLARE
Variable declaration
Constant declaration
BEGIN
PL/SQL Sub program body.
END;

1) Create a trigger to calculate the total and average of a student in the student table.
mysql> create table student_m (regno int primary key,name
varchar(20),tamil int,english int,maths int,science int,social
int,total int,average int);
Query OK, 0 rows affected (0.01 sec)

Mysql> desc student_m;

+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| regno | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| tamil | int(11) | YES | | NULL | |
| english | int(11) | YES | | NULL | |
| maths | int(11) | YES | | NULL | |
| science | int(11) | YES | | NULL | |
| social | int(11) | YES | | NULL | |
| total | int(11) | YES | | NULL | |
| average | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 28


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Mysql> CREATE TRIGGER avg1 BEFORE INSERT ON student_m FOR EACH ROW
-> BEGIN
-> SET
new.total=new.tamil+new.english+new.maths+new.science+new.social;
-> SET new.average=new.total/5;
-> END;//
Query OK, 0 rows affected (0.15 sec)
DELIMITER ;

mysql> insert into student_m


(regno,name,tamil,english,maths,science,social)values(101,'raja',90,95
,100,100,99);
Query OK, 1 row affected (0.16 sec)

Mysql> select * from student;


+-------+-------+-------+---------+-------+---------+--------+-------+---------+
| regno | name | tamil | english | maths | science | social | total | average |
+-------+-------+-------+---------+-------+---------+--------+-------+---------+
| 101 | raja | 90 | 95 | 100 | 100 | 99 | 484 | 97 |
+-------+-------+-------+---------+-------+---------+--------+-------+---------+
1 row in set (0.01 sec)

RESULT:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 29


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex.No.7 EXCEPTION HANDLING

AIM:
To implement and execute PL/SQL Block that handles all types of exceptions in Oracle
Database using Procedural Language concepts.

SYNTAX
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;

ZERO_DIVIDE EXCEPTION
SQL> BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 END;
4 /
BEGIN
*
ERROR at line 1:
ORA-01476: divisor is equal to zero
ORA-06512: at line 2
------------------------------------------------------
BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 EXCEPTION
4 WHEN ZERO_DIVIDE THEN
5 DBMS_OUTPUT.PUT_LINE('Division by zero');
6 END;
7 /
Division by zero
PL/SQL procedure successfully completed.

INVALID_NUMBER EXCEPTION
1 BEGIN
2 INSERT INTO employees(DEPARTMENT_ID)VALUES('101x');
3 EXCEPTION
4 WHEN INVALID_NUMBER THEN
5 DBMS_OUTPUT.PUT_LINE('Conversion of string to number failed');
6* end;
SQL> /
Conversion of string to number failed
PL/SQL procedure successfully completed.

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 30


CS8481 DBMS LAB Department of IT/CSE 2021-2022

OTHERS EXCEPTION

1 BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 EXCEPTION
4 WHEN OTHERS THEN
5 DBMS_OUTPUT.PUT_LINE('An exception occurred');
6* END;
7 /
An exception occurred
PL/SQL procedure successfully completed.

RESULT:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 31


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex.No.8 Database Design using ER modeling, normalization and


Implementation for any application
Aim:
To design a database using ER modeling and Normalization for student portal and sports meet
Application

Problem Statement: ER Diagram


 A College is conducting a sports meet.
 Teams from recognized colleges are allowed.
 A team should have the players of same college.
 A player can play for more than one team.
 Events occurs in various grounds in the college.
 Winning teams receive awards.
 A captain is a player of a team.
 A player is a student of a college.
 Many teams can play a game.
 A game takes place in a ground.
 A college can have many teams.
 Only first two teams are awarded.
IDENTIFICATION OF ENTITY:

 COLLEGE
 PLAYERS
 TEAMS
 GAMES
 GROUND
 AWARDS
DESCRIPTION ABOUT ENTITY:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 32


CS8481 DBMS LAB Department of IT/CSE 2021-2022

ATTRIBUTES:

 COLLEGE – CID, Name, Address line1, Address line2


 PLAYERS – FN, LN, POS, DOB, GENDER, PID
 TEAM - TID, Name, NOP, Rank, Team
 GAMES - GID, Name
 GROUND - ID, Name, Area
 AWARDS – Name, Position, Prize, Team

DESCRIPTION ABOUT ATTRIBUTES:

COLLEGE

PLAYERS

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 33


CS8481 DBMS LAB Department of IT/CSE 2021-2022

AWARDS

GROUN
D

TEAMS

GAMES

RELATIONSHIP:
BINARY:
• Plays
• Has
• Student of
• Receives
• Takes place

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 34


CS8481 DBMS LAB Department of IT/CSE 2021-2022

ATTRIBUTES IN THE RELATIONSHIP:

 Student of – CID, PID


 Has – TID, CID
 Plays – TID, GID
 Takes place – GID, ID
 Receives – TID, Position
CARDINALITY AND RELATIONSHIP:
 ONE TO ONE:Plays, Takes place
 MANY TO ONE:Student of
 ONE TO MANY:has
CARDINALITY ABOUT RELATIONSHIP:
 PLAYERS STUDENT OF COLLEGE.
 MANY TEAMS PLAY A GAME.
 A GAME TAKES PLACE IN A GROUND.
 A COLLEGE HAS MANY TEAMS.
ER DIAGRAM:
CON
TACT
NOS NO.O
NA F
CI ME PLAY TI GI G
TI GEND ERS NA
D D D ME
I
D ER RANKI
TI D
NG
D
H
AS N TEAMS 1 PLA
YS 1 GAMES
CON
TACT TI
1 G
NOS I
D TAKES
RE D
NAM CEI PLACE I
E 1
COLLEG VE
S
D
CI POSITI
D E ID
ADD 1
ON
NA
1
GROUN NA
RESS
AWAR ME
POSITI D ME
LIN ON ARE
E1
LIN
E2
DS PRI A
ZE
CI STUD CAP
D ENT N PLAYERS N TAI
N
OF
PI
D
NA PI 1 CA
PID
ME D
FN AG GEND
LN E ER

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 35


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Problem Statement: Normalization


Createacollegedatabasethatcontainsstudentid,studentname,studentcity,dateofbirth,course id,
course name, duration of the course, marks and grade and their relationships. The
requirements are listedbelow:
 A college can offer one or more courses.
 A student can enroll in one or more courses.
 Courses can be taken by one or more students.
 A student can have student_id, student_name, date _of _birth andstudent_city.
 A student belongs to one city.
 A city can have one or more students.
 A course can have course_id, course_name and duration.
 When a student finishes the course, a grade and marks are awarded.
 Grades are calculated based on themarks
Below 45 – U, 45-50 – E, 50-60– D, 60-70 – C, 70-80 – B, 80-90 – A, 90-100 –S

FIRST NORMAL FORM

A relation is said to be in first normal form if and only if


*All the attributes in the relation must be atomic in nature.
*No multivalued and composite attributes in the table

In a given table there is no multivalued and composite attributes, so it is satisfying normal form1

STUDENT STUDENT STUDENT DOB COURSE COURSE DURATION MARKS GRADE


ID NAME CITY ID NAME

SECOND NORMAL FORM

A relation is said to be in second normal form if and only if


*It is in the first normal form and
*No partial dependencies exist between non-key attributes and key attributes.

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 36


CS8481 DBMS LAB Department of IT/CSE 2021-2022

From Requirements: (studentid, courseid is Composite Primarykey)

studentid,courseid studentname

studentid,courseid studentcity

studentid,courseid dob Partial Functional dependencies.

studentid,courseid coursename

studentid,courseid duration

studentid,courseid marks

studentid,coursed grade Full Functional dependencies

After removing partial functional dependencies from above table

STUDENT

STUDENTID STUDENTNAME STUDENTCITY DOB

COURSE
COURSEID COURSENAME DURATION

RESULT
STUDENTID COURSEID MARKS GRADE

THIRD NORMAL FORM

A relation is said to be in the third normal form if and only if


*it is in Second Normal Form
*No transitive dependency exists between non-key attributes and key attribute
studentid,coursed marks

marks grade Transitive dependency

studentid,courseid grade

After removing transitive dependency from above table


STUDENT
STUDENTID STUDENTNAME STUDENTCITY DOB
COURSE
COURSEID COURSENAME DURATION
MARKS
MARKID RANGE1 RANGE2
RESULT
STUDENTID COURSEID MARKID
Result:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 37


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Ex.No.9 Database Connectivity with Front End Tools


AIM:
To design and implement a database application for library management system using Netbeans
and mysql.(Login Module)

PROBLEM STATEMENT:
The case study of library management system gives the complete information about the library.
We can enter the record of new book and retrieve the details of books available in the library. We can
issue the books to the students and maintain their records and also check how many books are issued and
stock available in the library. We can also search the books available in the library.

Sample Code

Database:

import java.sql.Connection;
import java.sql.DriverManager;

public class DB {
public static Connection getConnection(){
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");
}catch(Exception e){System.out.println(e);}
return con;
}

Login:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 38


CS8481 DBMS LAB Department of IT/CSE 2021-2022

public class AdminLogin extends JFrame {


static AdminLogin frame;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new AdminLogin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public AdminLogin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

JLabel lblAdminLoginForm = new JLabel("Admin Login Form");


lblAdminLoginForm.setForeground(Color.GRAY);
lblAdminLoginForm.setFont(new Font("Tahoma", Font.PLAIN, 18));

JLabel lblEnterName = new JLabel("Enter Name:");

JLabel lblEnterPassword = new JLabel("Enter Password:");

textField = new JTextField();


textField.setColumns(10);

JButton btnLogin = new JButton("Login");


btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name=textField.getText();
String password=String.valueOf(passwordField.getPassword());
if(name.equals("admin")&&password.equals("admin123")){

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 39


CS8481 DBMS LAB Department of IT/CSE 2021-2022

AdminSuccess.main(new String[]{});
frame.dispose();
}else{
JOptionPane.showMessageDialog(AdminLogin.this, "Sorry, Username
or Password Error","Login Error!", JOptionPane.ERROR_MESSAGE);
textField.setText("");
passwordField.setText("");
}
}
});

passwordField = new JPasswordField();


GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LE
ADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(124)
.addComponent(lblAdminLoginForm))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(19)
.addGroup(gl_contentPane.createParallelGroup
(Alignment.LEADING)
.addComponent(lblEnterName)
.addComponent(lblEnterPassword))
.addGap(47)
.addGroup(gl_contentPane.createParallelGroup
(Alignment.LEADING, false)
.addComponent(passwordField)
.addComponent(textField,
GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))))
.addContainerGap(107, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap(187, Short.MAX_VALUE)
.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE,
86, GroupLayout.PREFERRED_SIZE)
.addGap(151))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblAdminLoginForm)
.addGap(26)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BA
SELINE)
.addComponent(lblEnterName)
.addComponent(textField,
GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE))

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 40


CS8481 DBMS LAB Department of IT/CSE 2021-2022

.addGap(28)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BA
SELINE)
.addComponent(lblEnterPassword)
.addComponent(passwordField,
GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE,
37, GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
}

Output:

Database:

Login:

Result:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 41


CS8481 DBMS LAB Department of IT/CSE 2021-2022

Exp.No:10 Case Study using real life database applications

AIM:
To design and implement a database application for library management system using Netbeans
and mysql.

PROBLEM STATEMENT:
The case study of library management system gives the complete information about the library.
We can enter the record of new book and retrieve the details of books available in the library. We can
issue the books to the students and maintain their records and also check how many books are issued and
stock available in the library. We can also search the books available in the library.
ER DIAGRAM:

SAMPLE CODE:
import java.sql.Connection;
import java.sql.DriverManager;

public class DB {
public static Connection getConnection(){
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","","");
}catch(Exception e){System.out.println(e);}

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 42


CS8481 DBMS LAB Department of IT/CSE 2021-2022

return con;
}

LIBRARYIAN FORM
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LibrarianForm extends JFrame {


static LibrarianForm frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JPasswordField passwordField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibrarianForm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
RETURN BOOK

import java.awt.BorderLayout;

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 43


CS8481 DBMS LAB Department of IT/CSE 2021-2022

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ReturnBook extends JFrame {


static ReturnBook frame;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new ReturnBook();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public ReturnBook() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 516, 413);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblReturnBook = new JLabel("Return Book");
lblReturnBook.setForeground(Color.GRAY);
lblReturnBook.setFont(new Font("Tahoma", Font.PLAIN, 18));
JLabel lblBookCallno = new JLabel("Book Callno:");

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 44


CS8481 DBMS LAB Department of IT/CSE 2021-2022

JLabel lblStudentId = new JLabel("Student Id:");


textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
JButton btnReturnBook = new JButton("Return Book");
btnReturnBook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bookcallno=textField.getText();
int studentid=Integer.parseInt(textField_1.getText());
int i=ReturnBookDao.delete(bookcallno, studentid);
if(i>0){
JOptionPane.showMessageDialog(ReturnBook.this,"Book
returned successfully!");
LibrarianSuccess.main(new String[]{});
frame.dispose();

}else{
JOptionPane.showMessageDialog(ReturnBook.this,"Sorry,
unable to return book!");
}
}
});

JLabel lblNewLabel = new JLabel("Note: Check the book properly!");


lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));

JButton btnBack = new JButton("Back");


btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LibrarianSuccess.main(new String[]{});
frame.dispose();
}
});

OUTPUT:
LIBRARY MANAGEMENT SYSTEM SCREEN SHOT

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 45


CS8481 DBMS LAB Department of IT/CSE 2021-2022

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 46


CS8481 DBMS LAB Department of IT/CSE 2021-2022

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 47


CS8481 DBMS LAB Department of IT/CSE 2021-2022

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 48


CS8481 DBMS LAB Department of IT/CSE 2021-2022

RESULT:

St. Joseph’s college of Engineering/St. Joseph’s Institute of Technology 49

You might also like