DBMS Record
DBMS Record
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);
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:
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
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);
+-------------+-------------+------+-----+---------+-------+
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;
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;
+-----------------+-------------+------+-----+---------+-------+
| 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
+-----------------+-------------+------+-----+---------+-------+
| 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
+-------------+-------------+------+-----+---------+-------+
| 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
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;
+---------+-------------+--------+
| 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:
AIM:
To implement and execute a query for manipulating & storing data items in mysql database
using Structured Query Language commands
SYNTAX:
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
| 150000 |
| 130000 |
| 100000 |
| 200000 |
| 50000 |
+------------+
7 rows in set
2. Findall numbers for loan made at perryridge branch with loan amount greater than
1400.
+---------------+
| customer_name |
+---------------+
| williams |
+---------------+
1 row in set
+-------------+---------------+-----------------+---------------+
| customer_id | customer_name | customer_street | customer_city |
+-------------+---------------+-----------------+---------------+
| c_02 | turner | putnam | rye |
+-------------+---------------+-----------------+---------------+
1 rows in set (0.00 sec)
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)
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).
CUSTOMER_ID
-------------------------
c_01
c_01
5. Find the name of all branches that have assets greater than those atleast one branch
located in Harrison.(Nested queries).
BRANCH_NAME
------------------------------
Mianus
JOINS:
1. For all customer who have loan from the bank find their ID’s , loan number and loan
amount.(Join)
2. For all customers who have loan at perryridge branch find their ID’s,loan ID,loan
amount.(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;
Result:
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;
+----------------+---------+
| name | age |
+----------------+---------+
| abdulkalam | 83.9726 |
| maheshboobathy | 41.1006 |
| sachin | 42.2615 |
| viswanathanand | 45.4237 |
+----------------+---------+
4 rows in set
SEQUENCE:
2) Create a sequence and design the department table in the given attribute.
SYNONYM(UsingOracle)
3) Show the effect of synonym
CREATING A SYNONYM FOR A TABLE
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.
IMPLICIT CURSOR:
Sql%attribute name
Where attribute name can be FOUND, NOT FOUND, ISOPEN, %ROWCOUNT
EXPLICIT CURSOR:
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:
To implement and execute Procedures and functions in Mysql using Procedural Language
concepts.
SYNTAX :
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)
DELIMITER ;
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);
+------------------------+
| check11(101) |
+------------------------+
| Mobile Number is there |
+------------------------+
1 row in set, 1 warning (0.00 sec)
+--------+-------+
| custid | phone |
+--------+-------+
| 101 | 899 |
| 102 | NULL |
+--------+-------+
2 rows in set (0.00 sec)
+---------------------------+
| check12(102) |
+---------------------------+
| Mobile Number is not there|
+---------------------------+
1 row in set, 1 warning (0.00 sec)
RESULT
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.
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)
+---------+-------------+------+-----+---------+-------+
| 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)
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 ;
RESULT:
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.
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:
COLLEGE
PLAYERS
TEAMS
GAMES
GROUND
AWARDS
DESCRIPTION ABOUT ENTITY:
ATTRIBUTES:
COLLEGE
PLAYERS
AWARDS
GROUN
D
TEAMS
GAMES
RELATIONSHIP:
BINARY:
• Plays
• Has
• Student of
• Receives
• Takes place
In a given table there is no multivalued and composite attributes, so it is satisfying normal form1
studentid,courseid studentname
studentid,courseid studentcity
studentid,courseid coursename
studentid,courseid duration
studentid,courseid marks
STUDENT
COURSE
COURSEID COURSENAME DURATION
RESULT
STUDENTID COURSEID MARKS GRADE
studentid,courseid grade
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;
/**
* 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);
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("");
}
}
});
.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:
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);}
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;
/**
* 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;
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;
/**
* 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:");
}else{
JOptionPane.showMessageDialog(ReturnBook.this,"Sorry,
unable to return book!");
}
}
});
OUTPUT:
LIBRARY MANAGEMENT SYSTEM SCREEN SHOT
RESULT: