Wednesday 13 March 2013

DBMS Spool file Creation Steps

how to create a spool file?

steps:


spool off;
spool e:/insert3.sql;
REM WHICH FILE U WANT TO WRITE
set echo on;
@e:/insert2.sql;
REM WHICH FILE U WANT TO READ

DBMS Employee payroll


SQL> REM Emp_Details ( eid, ename, dob, sex, designation, address,
phone, email, REM REM department
)
SQL> REM Payroll ( eid, basic, da, hra, pf, mc, gross, tot_deduc,
net_pay ) eid REM REM references E
mp_Details(eid)
SQL>
SQL>
SQL> drop table Payroll;
Table dropped.
SQL> drop table Emp_Details;
Table dropped.
SQL>
SQL> create table Emp_Details(
2 eid number(5) CONSTRAINT pk_eid PRIMARY KEY,
3 ename varchar2(25),
4 dob date,
5 sex varchar2(7),
6 designation varchar2(25),
7 address varchar2(25),
8 phone number(10),
9 email varchar2(20),
10 department varchar2(10)
11 );
Table created.
SQL>
SQL> create table payroll(
2 Eid number(5) CONSTRAINT pk_Eid PRIMARY KEY,
3 basic number(7) CONSTRAINT not_basic NOT NULL,
4 da number(5),
5 hra number(5),
6 pf number(5),
7 mc number(5),
8 gross number(5),
9 tot_deduc number(7),
10 net_pay number(7),
11 CONSTRAINT fk_eid FOREIGN KEY(Eid) REFERENCES
Emp_Details(eid)
12 );
Table created.
SQL>
SQL> create or replace procedure calc(peid in payroll.Eid%type,
2 pbasic in Payroll.basic%type)
3 is
4 p payroll%rowtype;
5 temp number;
6 begin
7 temp:=0;
8 p.da:=(60/100)*pbasic;
9 p.hra:=(11/100)*pbasic;
10 p.pf:=(4/100)*pbasic;
11 p.mc:=(3/100)*pbasic;
12 p.gross:=(pbasic+p.da+p.hra);
13 p.tot_deduc:=(p.pf+p.mc);
14 p.net_pay:=(p.gross - p.tot_deduc);
15 select 1 into temp
16 from payroll
17 where Eid=peid;
18 EXCEPTION when no_data_found then
19 insert into Payroll values
20
(peid,pbasic,p.da,p.hra,p.pf,p.mc,p.gross,p.tot_deduc,p.net_pay);
21 end;
22 /
Procedure created.
SQL>
package Employee;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
/**
*
* @author Ananthi
*/
public class Employee extends javax.swing.JFrame {
Connection con;
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
public Employee()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
JOptionPane.showMessageDialog(this,"Driver Loaded");
try
{
con=DriverManager.getConnection("jdbc:odbc:ananthi",
"system","ssn");
JOptionPane.showMessageDialog(this,"Connection
Established");
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(this,ex.getMessage());
}
}
catch(ClassNotFoundException ex){
JOptionPane.showMessageDialog(this,"Not loaded");
}
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated
Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
employee = new javax.swing.JTabbedPane();
EXIT = new javax.swing.JPanel();
ADDRESS = new javax.swing.JLabel();
SEX = new javax.swing.JLabel();
design = new javax.swing.JTextField();
DOB = new javax.swing.JLabel();
EMP_ID = new javax.swing.JLabel();
EMP_NAME = new javax.swing.JLabel();
department = new javax.swing.JTextField();
phone = new javax.swing.JTextField();
DEPT = new javax.swing.JLabel();
ename = new javax.swing.JTextField();
eid = new javax.swing.JTextField();
DESIGN = new javax.swing.JLabel();
EMAIL = new javax.swing.JLabel();
F = new javax.swing.JRadioButton();
M = new javax.swing.JRadioButton();
PHONE = new javax.swing.JLabel();
email = new javax.swing.JTextField();
dob = new javax.swing.JTextField();
INSERT = new javax.swing.JButton();
UPDATE = new javax.swing.JButton();
DELETE = new javax.swing.JButton();
SEARCH = new javax.swing.JButton();
FIRST = new javax.swing.JButton();
LAST = new javax.swing.JButton();
sex = new javax.swing.JTextField();
PREVIOUS = new javax.swing.JButton();
NEXT = new javax.swing.JButton();
CLEAR = new javax.swing.JButton();
exit = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
address = new javax.swing.JTextArea();
payroll = new javax.swing.JPanel();
employee_id = new javax.swing.JLabel();
Basic = new javax.swing.JLabel();
Da = new javax.swing.JLabel();
Hra = new javax.swing.JLabel();
Pf = new javax.swing.JLabel();
Mc = new javax.swing.JLabel();
Gross = new javax.swing.JLabel();
Total_ded = new javax.swing.JLabel();
Net_pay = new javax.swing.JLabel();
eid1 = new javax.swing.JTextField();
basic = new javax.swing.JTextField();
da = new javax.swing.JTextField();
hra = new javax.swing.JTextField();
pf = new javax.swing.JTextField();
mc = new javax.swing.JTextField();
gross = new javax.swing.JTextField();
total_ded = new javax.swing.JTextField();
net_pay = new javax.swing.JTextField();
calc = new javax.swing.JButton();
DISPLAY = new javax.swing.JButton();
Clear = new javax.swing.JButton();
EXIT1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_
ON_CLOSE);
ADDRESS.setText("Address");
SEX.setText("Sex");
DOB.setText("Date of Birth");
EMP_ID.setText("Employee Id");
EMP_NAME.setText("Employee Name");
DEPT.setText("Department");
eid.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
eidActionPerformed(evt);
}
});
DESIGN.setText("Designation");
EMAIL.setText("E-Mail");
buttonGroup1.add(F);
F.setText("Female");
F.setActionCommand("Male");
F.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
FActionPerformed(evt);
}
});
buttonGroup1.add(M);
M.setText("Male");
M.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
MActionPerformed(evt);
}
});
PHONE.setText("Phone");
email.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
emailActionPerformed(evt);
}
});
INSERT.setText("INSERT");
INSERT.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
INSERTActionPerformed(evt);
}
});
UPDATE.setText("UPDATE");
UPDATE.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
UPDATEActionPerformed(evt);
}
});
DELETE.setText("DELETE");
DELETE.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
DELETEActionPerformed(evt);
}
});
SEARCH.setText("SEARCH");
SEARCH.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
SEARCHActionPerformed(evt);
}
});
FIRST.setText("FIRST");
FIRST.setActionCommand("First");
FIRST.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
FIRSTActionPerformed(evt);
}
});
LAST.setText("LAST");
LAST.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
LASTActionPerformed(evt);
}
});
sex.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
sexActionPerformed(evt);
}
});
PREVIOUS.setText("PREVIOUS");
PREVIOUS.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
PREVIOUSActionPerformed(evt);
}
});
NEXT.setText("NEXT");
NEXT.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
NEXTActionPerformed(evt);
}
});
CLEAR.setText("CLEAR");
CLEAR.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
CLEARActionPerformed(evt);
}
});
exit.setText("EXIT");
exit.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
exitActionPerformed(evt);
}
});
address.setColumns(20);
address.setRows(5);
jScrollPane1.setViewportView(address);
javax.swing.GroupLayout EXITLayout = new
javax.swing.GroupLayout(EXIT);
EXIT.setLayout(EXITLayout);
EXITLayout.setHorizontalGroup(
EXITLayout.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addGroup(EXITLayout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.LEADING)
.addGroup(EXITLayout.createSequentialGroup()
.addGroup(EXITLayout.createParallelGroup(j
avax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(FIRST,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(INSERT,
javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
.addGap(90, 174, Short.MAX_VALUE)
.addGroup(EXITLayout.createParallelGroup(j
avax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(PREVIOUS,
javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
.addComponent(DELETE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(37, 37, 37)
.addGroup(EXITLayout.createParallelGroup(j
avax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(LAST,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(SEARCH,
javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
.addGap(43, 43, 43)
.addGroup(EXITLayout.createParallelGroup(j
avax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(CLEAR,
javax.swing.GroupLayout.PREFERRED_SIZE, 78,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(exit,
javax.swing.GroupLayout.PREFERRED_SIZE, 78,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(63, 63, 63))
.addGroup(EXITLayout.createSequentialGroup()
.addGroup(EXITLayout.createParallelGroup(j
avax.swing.GroupLayout.Alignment.LEADING)
.addGroup(EXITLayout.createSequentialG
roup()
.addGroup(EXITLayout.createParalle
lGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLay
out.Alignment.TRAILING, EXITLayout.createSequentialGroup()
.addComponent(EMP_ID,
javax.swing.GroupLayout.PREFERRED_SIZE, 82,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(52, 52, 52))
.addGroup(EXITLayout.createSeq
uentialGroup()
.addGroup(EXITLayout.creat
eParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(DOB,
javax.swing.GroupLayout.PREFERRED_SIZE, 72,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SEX,
javax.swing.GroupLayout.PREFERRED_SIZE, 27,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(EMAIL)
.addComponent(PHONE)
.addComponent(DESIGN)
.addComponent(ADDRESS)
.addComponent(EMP_NAME
, javax.swing.GroupLayout.PREFERRED_SIZE, 120,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)))
.addGroup(EXITLayout.createParalle
lGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(design,
javax.swing.GroupLayout.PREFERRED_SIZE, 225,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(EXITLayout.createPar
allelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(eid)
.addComponent(ename)
.addComponent(dob)
.addComponent(email)
.addGroup(EXITLayout.creat
eSequentialGroup()
.addComponent(M)
.addGap(18, 18, 18)
.addComponent(F)
.addPreferredGap(javax
.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(sex))
.addComponent(department)
.addComponent(phone,
javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1
, javax.swing.GroupLayout.PREFERRED_SIZE, 225,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(EXITLayout.createPar
allelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(UPDATE,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(NEXT,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))))
.addComponent(DEPT,
javax.swing.GroupLayout.PREFERRED_SIZE, 96,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())))
);
EXITLayout.setVerticalGroup(
EXITLayout.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addGroup(EXITLayout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.TRAILING, false)
.addGroup(EXITLayout.createSequentialGroup()
.addComponent(eid,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.C
omponentPlacement.UNRELATED)
.addComponent(ename,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.C
omponentPlacement.UNRELATED)
.addComponent(dob,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(EXITLayout.createSequentialGroup()
.addComponent(EMP_ID,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(EMP_NAME,
javax.swing.GroupLayout.PREFERRED_SIZE, 14,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(DOB,
javax.swing.GroupLayout.PREFERRED_SIZE, 18,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.TRAILING)
.addGroup(EXITLayout.createParallelGroup(javax
.swing.GroupLayout.Alignment.BASELINE)
.addComponent(M)
.addComponent(F)
.addComponent(SEX))
.addComponent(sex,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.LEADING, false)
.addComponent(design)
.addComponent(DESIGN,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(19, 19, 19)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.LEADING)
.addGroup(EXITLayout.createSequentialGroup()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))
.addGroup(javax.swing.GroupLayout.Alignment.TR
AILING, EXITLayout.createSequentialGroup()
.addComponent(ADDRESS)
.addGap(54, 54, 54)))
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.LEADING, false)
.addComponent(phone)
.addComponent(PHONE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.Component
Placement.RELATED, 18, Short.MAX_VALUE)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.LEADING, false)
.addComponent(email)
.addComponent(EMAIL,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.LEADING, false)
.addComponent(department)
.addComponent(DEPT,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(44, 44, 44)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.BASELINE)
.addComponent(INSERT)
.addComponent(UPDATE)
.addComponent(DELETE)
.addComponent(SEARCH)
.addComponent(CLEAR))
.addGap(18, 18, 18)
.addGroup(EXITLayout.createParallelGroup(javax.swi
ng.GroupLayout.Alignment.BASELINE)
.addComponent(FIRST)
.addComponent(LAST)
.addComponent(PREVIOUS)
.addComponent(NEXT)
.addComponent(exit))
.addGap(21, 21, 21))
);
employee.addTab("EMPLOYEE", EXIT);
employee_id.setText("Employee ID");
Basic.setText("Basic");
Da.setText("DA");
Hra.setText("HRA");
Pf.setText("PF");
Mc.setText("MC");
Gross.setText("GROSS");
Total_ded.setText("Total Deduction");
Net_pay.setText("Net Pay");
eid1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
eid1ActionPerformed(evt);
}
});
da.setBackground(new java.awt.Color(240, 240, 240));
da.setDisabledTextColor(new java.awt.Color(172, 168,
153));
hra.setBackground(new java.awt.Color(240, 240, 240));
pf.setBackground(new java.awt.Color(240, 240, 240));
pf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
pfActionPerformed(evt);
}
});
mc.setBackground(new java.awt.Color(240, 240, 240));
gross.setBackground(new java.awt.Color(240, 240, 240));
total_ded.setBackground(new java.awt.Color(240, 240,
240));
net_pay.setBackground(new java.awt.Color(240, 240, 240));
calc.setText("CALCULATE");
calc.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
calcActionPerformed(evt);
}
});
DISPLAY.setText("DISPLAY");
DISPLAY.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
DISPLAYActionPerformed(evt);
}
});
Clear.setText("CLEAR");
Clear.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
ClearActionPerformed(evt);
}
});
EXIT1.setText("EXIT");
EXIT1.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
EXIT1ActionPerformed(evt);
}
});
javax.swing.GroupLayout payrollLayout = new
javax.swing.GroupLayout(payroll);
payroll.setLayout(payrollLayout);
payrollLayout.setHorizontalGroup(
payrollLayout.createParallelGroup(javax.swing.GroupLay
out.Alignment.LEADING)
.addGroup(payrollLayout.createSequentialGroup()
.addGap(34, 34, 34)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.LEADING)
.addComponent(employee_id)
.addComponent(Basic)
.addComponent(Da)
.addComponent(Hra)
.addComponent(Pf)
.addComponent(Mc)
.addComponent(Gross)
.addComponent(Total_ded)
.addComponent(Net_pay)
.addComponent(calc))
.addGap(59, 59, 59)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.LEADING)
.addGroup(payrollLayout.createParallelGroup(ja
vax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(basic,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addComponent(da,
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(hra,
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(eid1)
.addComponent(pf)
.addComponent(mc)
.addComponent(gross)
.addComponent(total_ded)
.addComponent(net_pay))
.addGroup(payrollLayout.createSequentialGroup(
)
.addComponent(DISPLAY)
.addGap(61, 61, 61)
.addComponent(Clear)
.addGap(54, 54, 54)
.addComponent(EXIT1)))
.addContainerGap(209, Short.MAX_VALUE))
);
payrollLayout.setVerticalGroup(
payrollLayout.createParallelGroup(javax.swing.GroupLay
out.Alignment.LEADING)
.addGroup(payrollLayout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.BASELINE)
.addComponent(employee_id)
.addComponent(eid1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.BASELINE)
.addComponent(Basic)
.addComponent(basic,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.BASELINE)
.addComponent(Da)
.addComponent(da,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.LEADING)
.addComponent(hra,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(payrollLayout.createSequentialGroup(
)
.addGap(3, 3, 3)
.addComponent(Hra,
javax.swing.GroupLayout.PREFERRED_SIZE, 20,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.LEADING, false)
.addComponent(pf,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(payrollLayout.createSequentialGroup(
)
.addGap(3, 3, 3)
.addComponent(Pf,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(18, 18, 18)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.LEADING, false)
.addGroup(payrollLayout.createSequentialGroup(
)
.addComponent(mc,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(gross,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(total_ded,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(payrollLayout.createSequentialGroup(
)
.addComponent(Mc,
javax.swing.GroupLayout.PREFERRED_SIZE, 22,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(Gross,
javax.swing.GroupLayout.PREFERRED_SIZE, 21,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.C
omponentPlacement.UNRELATED)
.addComponent(Total_ded,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(18, 18, 18)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Net_pay,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(net_pay))
.addPreferredGap(javax.swing.LayoutStyle.Component
Placement.RELATED, 64, Short.MAX_VALUE)
.addGroup(payrollLayout.createParallelGroup(javax.
swing.GroupLayout.Alignment.BASELINE)
.addComponent(calc)
.addComponent(DISPLAY)
.addComponent(Clear)
.addComponent(EXIT1))
.addGap(82, 82, 82))
);
employee.addTab("PAYROLL", payroll);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addComponent(employee)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addComponent(employee)
);
pack();
}// </editor-fold>
private void INSERTActionPerformed(java.awt.event.ActionEvent
evt) {
try {
String sql = ("insert into emp_details
values(?,?,?,?,?,?,?,?,?)");
pstmt = con.prepareStatement(sql);
pstmt.setString(1, eid.getText());
pstmt.setString(2, ename.getText());
pstmt.setString(3, dob.getText());
if (M.isSelected()) {
pstmt.setString(4, "M");
}
if (F.isSelected()) {
pstmt.setString(4, "F");
}
pstmt.setString(5, design.getText());
pstmt.setString(6, address.getText());
pstmt.setString(7, phone.getText());
pstmt.setString(8, email.getText());
pstmt.setString(9, department.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Inserted
Successfully!!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
private void UPDATEActionPerformed(java.awt.event.ActionEvent
evt) {
String id, name, dep, des, em, ph, add, s, dat;
int e_id;
try
{
stmt = con.createStatement();
} catch (SQLException e)
{
JOptionPane.showMessageDialog(null, "statement not
created");
}
try {
id = new String();
name = new String();
s = new String();
add = new String();
ph = new String();
em = new String();
dat = new String();
id = eid.getText();
name = ename.getText();
dep = department.getText();
des = design.getText();
ph = phone.getText();
s = sex.getText();
em=email.getText();
add = address.getText();
dat=dob.getText();
e_id = Integer.parseInt(id);
if (name.isEmpty() == false) {
stmt.executeUpdate("update emp_details set
ename='" + name + "' where eid=" + e_id + "" + "");
}
if (dep.isEmpty() == false) {
stmt.executeUpdate("update emp_details set
department='" + dep + "' where eid=" + e_id + "" + "");
}
if (des.isEmpty() == false) {
stmt.executeUpdate("update emp_details set
designation='" + des + "' where eid=" + e_id + "" + "");
}
if (ph.isEmpty() == false) {
stmt.executeUpdate("update emp_details set
phone='" + ph + "' where eid=" + e_id + "" + "");
}
if (add.isEmpty() == false) {
stmt.executeUpdate("update emp_details set
address='" + add + "' where eid=" + e_id + "" + "");
}
if (em.isEmpty() == false) {
stmt.executeUpdate("update emp_details set
email='" + em + "' where eid=" + e_id + "" + "");
}
if (s.isEmpty() == false) {
stmt.executeUpdate("update emp_details set sex='"
+ s + "' where eid=" + e_id + "" + "");
}
if (dat.isEmpty() == false) {
stmt.executeUpdate("update emp_details set dob='"
+ dat + "' where eid=" + e_id + "" + "");
}
JOptionPane.showMessageDialog(this, "Upadated
Succesfully");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
private void DELETEActionPerformed(java.awt.event.ActionEvent
evt) {
try {
String sql1 = ("delete from payroll where eid=?");
PreparedStatement pstmt1 = con.prepareStatement(sql1);
pstmt1.setString(1, eid.getText());
pstmt1.executeUpdate();
String sql = ("delete from emp_details where eid=?");
pstmt = con.prepareStatement(sql);
pstmt.setString(1, eid.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(this," DELETED
Successfully!!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
private void SEARCHActionPerformed(java.awt.event.ActionEvent
evt) {
try {
String sql = " select *from emp_details where eid=?";
PreparedStatement pstmt1 = con.prepareStatement(sql);
pstmt1.setString(1, eid.getText());
rs = pstmt1.executeQuery();
rs.next();
ename.setText(rs.getString(2));
dob.setText(rs.getString(3));
sex.setText(rs.getString(4));
design.setText(rs.getString(5));
address.setText(rs.getString(6));
phone.setText(rs.getString(7));
email.setText(rs.getString(8));
department.setText(rs.getString(9));
JOptionPane.showMessageDialog(this, "Successfully
searched!!!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "No Record
Found!");
}
}
private void FIRSTActionPerformed(java.awt.event.ActionEvent
evt) {
try {
stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("select * from emp_details");
rs.first();
eid.setText(rs.getString(1));
ename.setText(rs.getString(2));
dob.setText(rs.getString(3));
sex.setText(rs.getString(4));
design.setText(rs.getString(5));
address.setText(rs.getString(6));
phone.setText(rs.getString(7));
email.setText(rs.getString(8));
department.setText(rs.getString(9));
JOptionPane.showMessageDialog(this, "First Record
Displayed Successfully!!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
private void LASTActionPerformed(java.awt.event.ActionEvent
evt) {
try {
stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("select * from emp_details");
rs.last();
eid.setText(rs.getString(1));
ename.setText(rs.getString(2));
dob.setText(rs.getString(3));
sex.setText(rs.getString(4));
design.setText(rs.getString(5));
address.setText(rs.getString(6));
phone.setText(rs.getString(7));
email.setText(rs.getString(8));
department.setText(rs.getString(9));
JOptionPane.showMessageDialog(null, "Last Record
Displayed Successfully!!!");
} catch (SQLException e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, "EOF is
Encountered..");
}
}
private void
PREVIOUSActionPerformed(java.awt.event.ActionEvent evt) {
/*try {
//
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet
.CONCUR_READ_ONLY);
// rs=stmt.executeQuery("select * from emp_details");
rs.previous();
eid.setText(rs.getString(1));
ename.setText(rs.getString(2));
dob.setText(rs.getString(3));
sex.setText(rs.getString(4));
design.setText(rs.getString(5));
address.setText(rs.getString(6));
phone.setText(rs.getString(7));
email.setText(rs.getString(8));
department.setText(rs.getString(9));
} catch (SQLException e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, "eof is
encountered..");
}*/
String sql = "SELECT * from emp_details";
try {
con.createStatement(rs.TYPE_SCROLL_INSENSITIVE,
rs.CONCUR_READ_ONLY);
PreparedStatement psmt = con.prepareStatement(sql,
rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_READ_ONLY);
rs = psmt.executeQuery();
boolean nxt = rs.next();
while (nxt) {
if (rs.getString(1).equals(eid.getText())) {
boolean nx1 = rs.previous();
if (nx1) {
eid.setText(rs.getString(1));
ename.setText(rs.getString(2));
dob.setText(rs.getString(3));
sex.setText(rs.getString(4));
design.setText(rs.getString(5));
address.setText(rs.getString(9));
phone.setText(rs.getString(7));
email.setText(rs.getString(6));
department.setText(rs.getString(8));
} else {
JOptionPane.showMessageDialog(this,
"PREVIOUS RECORD NOT AVAILABLE!");
}
}
boolean next1 = rs.next();
nxt = next1;
if (rs.isFirst()) {
break;
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
private void NEXTActionPerformed(java.awt.event.ActionEvent
evt) {
String sql = "SELECT * from emp_details";
try {
PreparedStatement psmt = con.prepareStatement(sql,
rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_READ_ONLY);
rs = psmt.executeQuery();
con.createStatement(rs.TYPE_SCROLL_INSENSITIVE,
rs.CONCUR_READ_ONLY);
boolean nxt = rs.next();
while (nxt) {
if (rs.getString(1).equals(eid.getText())) {
boolean nx1 = rs.next();
if (nx1) {
eid.setText(rs.getString(1));
ename.setText(rs.getString(2));
dob.setText(rs.getString(3));
sex.setText(rs.getString(4));
design.setText(rs.getString(5));
address.setText(rs.getString(9));
phone.setText(rs.getString(7));
email.setText(rs.getString(6));
department.setText(rs.getString(8));
}
}
if (rs.isLast()) {
break;
}
boolean next1 = rs.next();
nxt = next1;
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "NEXT RECORD NOT
AVAILABLE!");
}
}
private void CLEARActionPerformed(java.awt.event.ActionEvent
evt) {
eid.setText("");
ename.setText("");
dob.setText("");
sex.setText("");
design.setText("");
address.setText("");
phone.setText("");
email.setText("");
department.setText("");
}
private void calcActionPerformed(java.awt.event.ActionEvent
evt) {
try
{
String sql1 = ("select * from payroll where eid=" +
eid1.getText() + "");
PreparedStatement psmt1 = con.prepareStatement(sql1);
rs = psmt1.executeQuery();
eid1.setText(rs.getString(1));
basic.setText(rs.getString(2));
da.setText(rs.getString(3));
hra.setText(rs.getString(4));
pf.setText(rs.getString(5));
mc.setText(rs.getString(6));
gross.setText(rs.getString(7));
total_ded.setText(rs.getString(8));
net_pay.setText(rs.getString(9));
JOptionPane.showMessageDialog(this, "PAYROLL DETAILS
ALREADY CALCULATED!");
}
catch (SQLException ex)
{
try {
String str = "{call calc(?,?)}";
CallableStatement pstmt = con.prepareCall(str);
pstmt.setString(1, eid1.getText());
pstmt.setString(2, basic.getText());
pstmt.execute();
String sql1 = ("select * from payroll where eid="
+ eid1.getText() + "");
PreparedStatement stmt1 =
con.prepareStatement(sql1);
rs = stmt1.executeQuery();
rs.next();
eid.setText(rs.getString(1));
basic.setText(rs.getString(2));
da.setText(rs.getString(3));
hra.setText(rs.getString(4));
pf.setText(rs.getString(5));
mc.setText(rs.getString(6));
gross.setText(rs.getString(7));
total_ded.setText(rs.getString(8));
net_pay.setText(rs.getString(9));
JOptionPane.showMessageDialog(this, "PAYROLL
DETAILS CALCULATED SUCCESSFULLY!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Enter a Valid
Input!!!!");
}
}
}
private void DISPLAYActionPerformed(java.awt.event.ActionEvent
evt) {
try {
String sql = " select *from payroll where eid=?";
PreparedStatement pstmt1 = con.prepareStatement(sql);
pstmt1.setString(1, eid1.getText());
rs = pstmt1.executeQuery();
rs.next();
basic.setText(rs.getString(2));
da.setText(rs.getString(3));
hra.setText(rs.getString(4));
pf.setText(rs.getString(5));
mc.setText(rs.getString(6));
gross.setText(rs.getString(7));
total_ded.setText(rs.getString(8));
net_pay.setText(rs.getString(9));
JOptionPane.showMessageDialog(this, "Record
Found!!!");
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(this, "No Record
Found!!!");
}
}
private void ClearActionPerformed(java.awt.event.ActionEvent
evt) {
eid1.setText("");
basic.setText("");
da.setText("");
hra.setText("");
pf.setText("");
mc.setText("");
total_ded.setText("");
gross.setText("");
net_pay.setText("");
}
private void EXIT1ActionPerformed(java.awt.event.ActionEvent
evt) {
System.exit(1);
}
private void eidActionPerformed(java.awt.event.ActionEvent
evt) {
}
private void emailActionPerformed(java.awt.event.ActionEvent
evt) {
}
private void MActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void FActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void sexActionPerformed(java.awt.event.ActionEvent
evt) {
}
private void eid1ActionPerformed(java.awt.event.ActionEvent
evt) {
}
private void pfActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void exitActionPerformed(java.awt.event.ActionEvent
evt) {
System.exit(1);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Employee().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel ADDRESS;
private javax.swing.JLabel Basic;
private javax.swing.JButton CLEAR;
private javax.swing.JButton Clear;
private javax.swing.JButton DELETE;
private javax.swing.JLabel DEPT;
private javax.swing.JLabel DESIGN;
private javax.swing.JButton DISPLAY;
private javax.swing.JLabel DOB;
private javax.swing.JLabel Da;
private javax.swing.JLabel EMAIL;
private javax.swing.JLabel EMP_ID;
private javax.swing.JLabel EMP_NAME;
private javax.swing.JPanel EXIT;
private javax.swing.JButton EXIT1;
private javax.swing.JRadioButton F;
private javax.swing.JButton FIRST;
private javax.swing.JLabel Gross;
private javax.swing.JLabel Hra;
private javax.swing.JButton INSERT;
private javax.swing.JButton LAST;
private javax.swing.JRadioButton M;
private javax.swing.JLabel Mc;
private javax.swing.JButton NEXT;
private javax.swing.JLabel Net_pay;
private javax.swing.JLabel PHONE;
private javax.swing.JButton PREVIOUS;
private javax.swing.JLabel Pf;
private javax.swing.JButton SEARCH;
private javax.swing.JLabel SEX;
private javax.swing.JLabel Total_ded;
private javax.swing.JButton UPDATE;
private javax.swing.JTextArea address;
private javax.swing.JTextField basic;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton calc;
private javax.swing.JTextField da;
private javax.swing.JTextField department;
private javax.swing.JTextField design;
private javax.swing.JTextField dob;
private javax.swing.JTextField eid;
private javax.swing.JTextField eid1;
private javax.swing.JTextField email;
private javax.swing.JTabbedPane employee;
private javax.swing.JLabel employee_id;
private javax.swing.JTextField ename;
private javax.swing.JButton exit;
private javax.swing.JTextField gross;
private javax.swing.JTextField hra;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField mc;
private javax.swing.JTextField net_pay;
private javax.swing.JPanel payroll;
private javax.swing.JTextField pf;
private javax.swing.JTextField phone;
private javax.swing.JTextField sex;
private javax.swing.JTextField total_ded;
// End of variables declaration
}
INSERTION:
UPDATION:
DELETE:
SEARCH:
FIRST:
NEXT:
PREVIOUS:
LAST:
CALCULATE:
DISPLAY:

DBMS triggers


SQL> REM 1. If a tuple is inserted into Outcomes, check that the
ship and battle are listed in Ships and
SQL> REM Battles, respectively, and if not, insert tuples into one
or both of these relations, with NULL
SQL> REM components where necessary.
SQL>
SQL>
SQL> create or replace trigger trig_check
2 before insert on outcomes
3 for each row
4 declare
5 sh outcomes.ship%type;
6 bat outcomes.battle%type;
7 cursor c1 is select s.name into sh from ships s where
s.name=:new.ship;
8 cursor c2 is select b.name into bat from battles b where
b.name=:new.battle;
9 begin
10 open c1;
11 fetch c1 into sh;
12 if c1%notfound then
13 insert into ships values(:new.ship,NULL,NULL);
14 end if;
15 close c1;
16 open c2;
17 fetch c2 into bat;
18 if c2%notfound then
19 insert into battles values(:new.battle,NULL);
20 end if;
21 close c2;
22 end;
23 /
Trigger created.
SQL>
SQL> insert into outcomes values('vishak','Guadalcanal','ok');
1 row created.
SQL> insert into outcomes values('Hiei','second War','ok');
1 row created.
SQL>
SQL> select * from ships where name='vishak';
NAME CLASS LAUNCHED
--------------- --------------- ----------
vishak
SQL> select * from battles where name='second War';
NAME BATTLE_DA
--------------- ---------
second War
SQL>
SQL> insert into outcomes values('wing','new war','ok');
1 row created.
SQL> select * from ships where name='wing';
NAME CLASS LAUNCHED
--------------- --------------- ----------
wing
SQL> select * from battles where name='new war';
NAME BATTLE_DA
--------------- ---------
new war
SQL>
SQL> drop trigger trig_check;
Trigger dropped.
SQL> REM 4.Check under all circumstances that could cause a
violation, that no ship fought in a battle
SQL> REM that was at a latter date than another battle in which
that ship was sunk.
SQL>
SQL> create or replace trigger sunk_check
2 before insert on outcomes
3 for each row
4 declare
5 cursor cs is select battle_date from battles join
outcomes on (battle=name)
6 where result='sunk' and ship=:new.ship;
7 bd battles.battle_date%type;
8 ent battles.battle_date%type;
9 begin
10 if inserting then
11 select battle_date into bd from battles where
name=:new.battle;
12 open cs;
13 fetch cs into ent;
14 if (bd>ent) then
15 raise_application_error(-20003,'Ship already sunk');
16 end if;
17 end if;
18 end;
19 /
Trigger created.
SQL>
SQL> insert into outcomes values('North Carolina','Surigao
Strait','ok');
insert into outcomes values('North Carolina','Surigao
Strait','ok')
*
ERROR at line 1:
ORA-20003: Ship already sunk
ORA-06512: at "SYSTEM.SUNK_CHECK", line 12
ORA-04088: error during execution of trigger 'SYSTEM.SUNK_CHECK'
SQL> drop trigger sunk_check;
Trigger dropped.
SQL>
SQL> REM 5.When making any modification to the view – 2 that is
created in assignment – 4, ensure th
at
SQL> REM the modification is reflected in the corresponding base
table(s) also.
SQL>
SQL> alter table classes disable all triggers;
Table altered.
SQL> alter table ships disable all triggers;
Table altered.
SQL> alter table battles disable all triggers;
Table altered.
SQL> alter table outcomes disable all triggers;
Table altered.
SQL>
SQL> CREATE OR REPLACE VIEW Kongo_Yamato
2 AS SELECT s.class,o.ship,s.launched,o.battle,o.result
3 FROM ships s join outcomes o on(s.name=o.ship)
4 WHERE s.class in ('Kongo','Yamato');
View created.
SQL>
SQL>
SQL> CREATE or REPLACE trigger trig_kongo
2 INSTEAD OF
3 INSERT OR UPDATE OR DELETE ON Kongo_Yamato
4 for each row
5 begin
6 if inserting then
7 dbms_output.put_line(' Inserting.....');
8 insert into ships
values(:new.ship,:new.class,:new.launched);
9 insert into outcomes
values(:new.ship,:new.battle,:new.result);
10 elsif updating then
11 dbms_output.put_line(' Updating......');
12 if updating('class')
13 then
14 update ships
15 set class=:new.class
16 where name=:old.ship;
17 end if;
18 if updating('launched')
19 then
20 update ships
21 set launched=:new.launched
22 where name=:old.ship;
23 end if;
24 if updating('battle')
25 then
26 update outcomes
27 set battle=:new.battle
28 where ship=:old.ship;
29 end if;
30 if updating('result')
31 then
32 update outcomes
33 set result=:new.result
34 where ship=:old.ship;
35 end if;
36 elsif deleting then
37 dbms_output.put_line(' Deleting......');
38 delete from outcomes where ship=:old.ship;
39 delete from ships where name=:old.ship;
40 end if;
41 end;
42 /
Trigger created.
SQL>
SQL> REM INSERTION
SQL>
SQL> insert into Kongo_Yamato values('Yamato','new
ships',1943,'Guadalcanal','ok');
1 row created.
SQL> select * from ships where name='new ships';
NAME CLASS LAUNCHED
--------------- --------------- ----------
new ships Yamato 1943
SQL> select * from outcomes where ship='new ships' and
battle='Guadalcanal';
SHIP BATTLE RESULT
--------------- --------------- ----------
new ships Guadalcanal ok
SQL>
SQL>
SQL> REM UPDATION
SQL>
SQL> update Kongo_Yamato
2 set class='Kongo'
3 where ship='new ships' and battle='Guadalcanal';
1 row updated.
SQL>
SQL> update Kongo_Yamato
2 set launched=1918
3 where ship='new ships' and battle='Guadalcanal';
1 row updated.
SQL>
SQL> update Kongo_Yamato
2 set battle='North Atlantic'
3 where ship='new ships' and battle='Guadalcanal';
1 row updated.
SQL>
SQL>
SQL> update Kongo_Yamato
2 set result='damaged'
3 where ship='new ships' and battle='North Atlantic';
1 row updated.
SQL>
SQL> select * from ships where name='new ships';
NAME CLASS LAUNCHED
--------------- --------------- ----------
new ships Kongo 1918
SQL> select * from outcomes where ship='new ships' and
battle='North Atlantic';
SHIP BATTLE RESULT
--------------- --------------- ----------
new ships North Atlantic damaged
SQL>
SQL> delete from Kongo_Yamato where ship='new ships';
1 row deleted.
SQL> select * from ships where name='new ships';
no rows selected
SQL> select * from outcomes where ship='new ships';
no rows selected
SQL>
SQL> drop trigger trig_kongo;
Trigger dropped.
SQL> SPOOL OFF;

DBMS Procedure


SQL> REM 1: Ask the user for the name of a class and
other information required SQL> REM for a tuple of table
Classes.
Add this information to Classes and also add the ship
SQL> REM with the class name to Ships.Display appropriate
message if the Class REM already exists.
SQL>
SQL> create or replace procedure find_class(
2 c classes%rowtype,
3 temp out number)is
4 begin
5 temp:=0;
6 select 1 into temp
7 from classes
8 where class=c.class;
9 EXCEPTION when no_data_found then
10 insert into classes
values(c.class,c.type,c.country,c.numguns,c.bore,c.displa
ceme
nt);
11 insert into ships
values(c.class,c.class,NULL);
12 temp:=0;
13 end;
14 /
Procedure created.
SQL> declare
2 c classes%rowtype;
3 temp number;
4 begin
5 c.class:='&class';
6 c.type:='&type';
7 c.country:='&country';
8 c.numguns:=&numguns;
9 c.bore:=&bore;
10 c.displacement:=&displacement;
11 find_class(c,temp);
12 if temp=0 then
13 dbms_output.put_line('Class '||c.class||' is a new
class ....Inserted successfully in class and
ships..!!!!');
14 else
15 dbms_output.put_line('Class '||c.class||' already
exists!!!');
16 end if;
17 end;
18 /
Enter value for class: Iowa
old 5: c.class:='&class';
new 5: c.class:='Iowa';
Enter value for type: bb
old 6: c.type:='&type';
new 6: c.type:='bb';
Enter value for country: india
old 7: c.country:='&country';
new 7: c.country:='india';
Enter value for numguns: 3
old 8: c.numguns:=&numguns;
new 8: c.numguns:=3;
Enter value for bore: 12
old 9: c.bore:=&bore;
new 9: c.bore:=12;
Enter value for displacement: 12000
old 10: c.displacement:=&displacement;
new 10: c.displacement:=12000;
Class Iowa already exists!!!
PL/SQL procedure successfully completed.
SQL> /
Enter value for class: Renold
old 5: c.class:='&class';
new 5: c.class:='Renold';
Enter value for type: bb
old 6: c.type:='&type';
new 6: c.type:='bb';
Enter value for country: India
old 7: c.country:='&country';
new 7: c.country:='India';
Enter value for numguns: 9
old 8: c.numguns:=&numguns;
new 8: c.numguns:=9;
Enter value for bore: 19
old 9: c.bore:=&bore;
new 9: c.bore:=19;
Enter value for displacement: 16000
old 10: c.displacement:=&displacement;
new 10: c.displacement:=16000;
Class Renold is a new class ....Inserted successfully in
class and ships..!!!!
PL/SQL procedure successfully completed.
SQL> select * from classes where class='Renold';
CLASS TYPE COUNTRY NUMGUNS
BORE DISPLACEMENT
--------------- ----- --------------- ----------
---------- ------------
Renold bb India 9
19 16000
SQL> select * from ships where name='Renold';
NAME CLASS LAUNCHED
--------------- --------------- ----------
Renold Renold
SQL>
SQL> REM 2: Ask the user for the name of a battle. Find
the countries of the ships SQL> REM involved in the
battle.Print
the country with most ships sunk and the country
SQL> REM with most ships damaged. Print appropriate
result.
SQL>
SQL> create or replace procedure
2 countries_involved(bname outcomes.battle%type,
3 cur2 IN OUT sys_refcursor) is
4 begin
5 open cur2 for
6 select distinct country
7 from classes c
8 join ships s on(c.class=s.class)
9 join outcomes o on (o.ship=s.name)
10 where battle=bname;
11 end;
12 /
Procedure created.
SQL> create or replace procedure
2 max_coun(res outcomes.result%type,
3 bname outcomes.battle%type,
4 cur in out sys_refcursor) is
5 begin
6 open cur for
7 select distinct country
8 from classes
9 where class IN (select class from
ships
10 where name IN(select
ship from outcomes
11 where
result=res and
battle=bname)
12 group by class
13 having
count(name)>=ALL(select count(name)
from ships
14
where name IN(select ship from outcomes
15
where result=res and battle=bname)
16 group
by class));
17 end;
18 /
Procedure created.
SQL>
SQL> declare
2 bname outcomes.battle%type;
3 temp classes.country%type;
4 cur sys_refcursor;
5 cur2 sys_refcursor;
6 ctry classes.country%type;
7 begin
8 bname:='&battle_name';
9 dbms_output.put_line('For battle ::'||bname);
10 dbms_output.put_line('------------------------');
11 dbms_output.put_line('Countries involved');
12 dbms_output.put_line('************************');
13 countries_involved(bname,cur2);
14 loop
15 fetch cur2 into temp;
16 exit when cur2%notfound;
17 dbms_output.put_line(temp);
18 end loop;
19 close cur2;
20
dbms_output.put_line('----------------------------------'
);
21 dbms_output.put_line('COUNTRY WITH MAXIMUM SHIP
SUNK');
22
dbms_output.put_line('----------------------------------'
);
23 max_coun('sunk',bname,cur);
24 loop
25 fetch cur into ctry;
26 exit when cur%notfound;
27 dbms_output.put_line(ctry);
28 end loop;
29 close cur;
30 max_coun('damaged',bname,cur);
31
dbms_output.put_line('---------------------------------')
;
32 dbms_output.put_line('COUNTRY WITH MAXIMUM SHIP
DAMAGED');
33
dbms_output.put_line('---------------------------------')
;
34 loop
35 fetch cur into ctry;
36 exit when cur%notfound;
37 dbms_output.put_line(ctry);
38 end loop;
39 close cur;
40 end;
41 /
Enter value for battle_name: Guadalcanal
old 8: bname:='&battle_name';
new 8: bname:='Guadalcanal';
For battle ::Guadalcanal
------------------------
Countries involved
************************
USA
Gt. Britain
Japan
-------------------------------
COUNTRY WITH MAXIMUM SHIP SUNK
-------------------------------
Japan
---------------------------------
COUNTRY WITH MAXIMUM SHIP DAMAGED
---------------------------------
Gt. Britain
PL/SQL procedure successfully completed.
SQL> REM 3:Given the name of a battle, produce the two
countries whose ships were involved in the
SQL> REM battle. If there are more or fewer than two
countries involved, produce NULL for both countries.
SQL>
SQL> create or replace procedure find_coun(
2 tbattle IN battles.name%type,
3 country1 OUT classes.country%type,
4 country2 OUT classes.country%type,
5 tp out number,
6 tcount out number) is
7 cursor c is
8 select distinct country
9 from classes c
10 join ships s on(c.class=s.class)
11 join outcomes o ON(o.ship=s.name)
12 where o.battle=tbattle;
13 temp c%rowtype;
14 begin
15 tcount:=0;
16 open c;
17 LOOP
18 fetch c into temp;
19 exit when c%NOTFOUND;
20 END LOOP;
21 tcount:=c%rowcount;
22 close c;
23 open c;
24 if tcount=2
25 then
26 fetch c into country1;
27 fetch c into country2;
28 tp:=1;
29 else
30 country1:=NULL;
31 country2:=NULL;
32 tp:=0;
33 end if;
34 close c;
35 end;
36 /
Procedure created.
SQL>
SQL> declare
2 tbattle battles.name%type;
3 country1 classes.country%type;
4 country2 classes.country%type;
5 tp number;
6 tcount number;
7 begin
8 tbattle:='&Battle_Name';
9 find_coun(tbattle,country1,country2,tp,tcount);
10 if tp = 0
11 then
12 dbms_output.put_line('No of country is ');
13 dbms_output.put_line(tcount);
14 dbms_output.put_line('Country1 : NULL');
15 dbms_output.put_line('Country2 : NULL');
16 else
17 dbms_output.put_line('Country1');
18 dbms_output.put_line(country1);
19 dbms_output.put_line('Country2');
20 dbms_output.put_line(country2);
21 end if;
22 end;
23 /
Enter value for battle_name: North Cape
old 8: tbattle:='&Battle_Name';
new 8: tbattle:='North Cape';
Country1
USA
Country2
Japan
PL/SQL procedure successfully completed.
SQL> /
Enter value for battle_name: Guadalcanal
old 8: tbattle:='&Battle_Name';
new 8: tbattle:='Guadalcanal';
No of country is
3
Country1 : NULL
Country2 : NULL
PL/SQL procedure successfully completed.
SQL> REM 4:Given a ship name, determine if the ship was
in a battle with a date before the ship was launched.
REM If so, display the battle date and prompt the user
with option to change the date oF launch.
SQL>
SQL>
SQL> insert into classes
values('newclass','bb','india',12,25,34000);
1 row created.
SQL> insert into ships
values('newship','newclass','1990');
1 row created.
SQL> insert into battles values('newbat','25-may-1970');
1 row created.
SQL> insert into outcomes
values('newship','newbat','ok');
1 row created.
SQL>
SQL> create or replace function
2 func4( tship ships.name%type,
3 battle_Date battles.battle_date%type,
4 tlaunched ships.launched%type)
5 return number is
6 flag number;
7 begin
8 if substr(extract (year from (battle_Date)),3,2) <
substr(tlaunched,3,2)
9 then
10 flag:=0;
11 return flag;
12 else
13 flag:=1;
14 return flag;
15 end if;
16 end;
17 /
Function created.
SQL>
SQL> create or replace procedure
2 proc ( tlaunched ships.launched%type,
3 tship ships.name%type ) is
4 begin
5 update ships
6 set launched=tlaunched
7 where name=tship;
8 end;
9 /
Procedure created.
SQL>
SQL>
SQL> declare
2 tship ships.name%type;
3 battle_Date battles.battle_date%type;
4 tlaunched ships.launched%type;
5 flag number;
6 begin
7 tship:='&shipname';
8 select launched into tlaunched
9 from ships
10 where name=tship;
11 select battle_date into battle_Date
12 from battles b
13 join outcomes o on(b.name=o.battle)
14 where o.ship=tship;
15 dbms_output.put_line(' Battle date is
'||battle_Date);
16 dbms_output.put_line('Launched date is
'||tlaunched);
17
flag:=func4(tship,battle_Date,tlaunched);
18 if flag=0
19 then
20 tlaunched:='&NewLaunchDate';
21 proc(tlaunched,tship);
22 dbms_output.put_line( 'Launched date
updated as ' || tlaunched);
23 else
24 dbms_output.put_line( battle_Date || '
is less than '|| tlaunched);
25 end if;
26 end;
27 /
Enter value for shipname: newship
old 7: tship:='&shipname';
new 7: tship:='newship';
Enter value for newlaunchdate: 1960
old 20: tlaunched:='&NewLaunchDate';
new 20: tlaunched:='1960';
Battle date is 25-MAY-1970
Launched date is 1990
Launched date updated as 1960
PL/SQL procedure successfully completed.
SQL> /
Enter value for shipname: Revenge
old 7: tship:='&shipname';
new 7: tship:='Revenge';
Enter value for newlaunchdate: 1989
old 20: tlaunched:='&NewLaunchDate';
new 20: tlaunched:='1989';
Battle date is 11-NOV-1942
Launched date is 1916
11-NOV-42 is less than 1916
PL/SQL procedure successfully completed.
SQL> spool off;

DBMS VIEW


SQL> @ e:/assignment4.sql;
SQL> REM 1:
SQL>REM***********************************************************
SQL> REM VIEW 1:Create a view named British_Class that gives REM for each class of Great Britain its class, type,number of REM guns bore and displacement.
SQL>
SQL> CREATE OR REPLACE view British_Class as
2 SELECT class,type,numGuns,bore,country
3 FROM classes
4 WHERE country='Gt. Britain'
5 WITH CHECK OPTION CONSTRAINT ck_country;

View created.

SQL>
SQL> SELECT * FROM British_Class;

CLASS TYPE NUMGUNS BORE COUNTRY
--------------- ----- ---------- ---------- ---------------
Renown bc 6 15 Gt. Britain
Revenge bb 8 15 Gt. Britain

SQL>
SQL> REM British_Class(class,type,numGuns,bore,country)
SQL> REM OPERATIONS ON VIEW:
SQL>
SQL> SELECT COLUMN_NAME,UPDATABLE
2 FROM USER_UPDATABLE_COLUMNS
3 WHERE TABLE_NAME='BRITISH_CLASS';

COLUMN_NAME UPD
------------------------------ ---
CLASS YES
TYPE YES
NUMGUNS YES
BORE YES
COUNTRY YES

SQL>
SQL> SAVEPOINT A1;

Savepoint created.

SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO British_Class VALUES('TITAN','bb',7,20,'Gt. Britain');

1 row created.

SQL> SELECT * FROM Classes WHERE class='TITAN';

CLASS TYPE COUNTRY NUMGUNS BORE DISPLACEMENT
-------- ----- --------------- ---------- ---------- ------------
TITAN bb Gt. Britain 7 20

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE British_Class
2 SET type='bc',numGuns=12,bore=20,class='TITANIC'
3 WHERE class='TITAN';

1 row updated.

SQL> SELECT * FROM Classes WHERE class='TITANIC';

CLASS TYPE COUNTRY NUMGUNS BORE DISPLACEMENT
--------- ----- --------------- ---------- ---------- ------------
TITANIC bc Gt. Britain 12 20

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM British_Class WHERE class='TITANIC';

1 row deleted.

SQL> SELECT * FROM Classes WHERE class='TITANIC';

no rows selected

SQL>
SQL> REM ------------------------------------------
> REM CLASSES(class,type,country,NumGuns,bore,displacement);
SQL> REM OPERATIONS ON BASE TABLE:
SQL>
SQL> SELECT COLUMN_NAME,UPDATABLE
2 FROM USER_UPDATABLE_COLUMNS
3 WHERE TABLE_NAME='BRITISH_CLASS';

COLUMN_NAME UPD
------------------------------ ---
CLASS YES
TYPE YES
NUMGUNS YES
BORE YES
COUNTRY YES

SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO Classes VALUES
2 ('TITANIC','bb','Gt. Britain',7,20,26000);

1 row created.

SQL> SELECT * FROM British_Class WHERE class='TITANIC';

CLASS TYPE NUMGUNS BORE COUNTRY
--------------- ----- ---------- ---------- ---------------
TITANIC bb 7 20 Gt. Britain

SQL> SELECT * FROM Classes WHERE class='TITANIC';

CLASS TYPE COUNTRY NUMGUNS BORE DISPLACEMENT
-------- ----- --------------- ---------- ---------- ------------
TITANIC bb Gt. Britain 7 20 26000

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE Classes
2 SET type='bc',class='TITANIC1',NumGuns=10,bore=11,displacement=20000
3 WHERE class='TITANIC';

1 row updated.

SQL> SELECT * FROM British_Class WHERE class='TITANIC1';

CLASS TYPE NUMGUNS BORE COUNTRY
--------------- ----- ---------- ---------- ---------------
TITANIC1 bc 10 11 Gt. Britain

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM Classes WHERE class='TITANIC1';

1 row deleted.

SQL> SELECT * FROM British_Class WHERE class='TITANIC1';

no rows selected

SQL>
SQL> ROLL BACK TO A1;
Rollback complete.
SQL>
SQL> REM 2:
SQL>REM***********************************************************
SQL> REM VIEW 2:Define a view Kongo_Yamato that gives for each ship of Kongo and Yamato class its ship
SQL> REM name (from outcomes), year of launch, battle involved and the result.
SQL>
SQL> CREATE OR REPLACE view Kongo_Yamato as
2 SELECT o.ship,s.launched,o.battle,o.result,s.class
3 FROM ships s JOIN outcomes o ON(s.name=o.ship)
4 WHERE s.class in('Kongo','Yamato');

View created.

SQL>
SQL> SELECT * FROM Kongo_Yamato;

SHIP LAUNCHED BATTLE RESULT CLASS
--------------- --------- --------------- ---------- --------
Kongo 1913 North Atlantic sunk Kongo
Musashi 1942 North Cape ok Yamato
Haruna 1915 North Atlantic sunk Kongo
Kirishima 1915 Guadalcanal sunk Kongo
Yamato 1941 Surigao Strait ok Yamato

SQL>
SQL> REM Kongo_Yamato(SHIP,LAUNCHED,BATTLE,RESULT,CLASS);
SQL> REM OPERATIONS ON VIEW:
SQL>
SQL> SELECT COLUMN_NAME,UPDATABLE
2 FROM USER_UPDATABLE_COLUMNS
3 WHERE TABLE_NAME='KONGO_YAMATO';

COLUMN_NAME UPD
------------------------------ ---
SHIP YES
LAUNCHED NO
BATTLE YES
RESULT YES
CLASS NO

SQL>
SQL> SAVEPOINT A2;

Savepoint created.

SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO Kongo_Yamato(SHIP,BATTLE,RESULT)VALUES('Hiei','North Cape','ok');

1 row created.

SQL> SELECT * FROM OUTCOMES WHERE ship='Hiei';

SHIP BATTLE RESULT
--------------- --------------- ----------
Hiei North Cape ok

SQL> SELECT * FROM SHIPS WHERE name='Hiei';

NAME CLASS LAUNCHED
--------------- --------------- ----------
Hiei Kongo 1914

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE Kongo_Yamato
2 SET ship='Kongo',battle='Surigao Strait',result='damaged'
3 WHERE ship='Hiei';

1 row updated.

SQL>
SQL> SELECT * FROM OUTCOMES WHERE ship='Kongo' and battle='Surigao Strait';

SHIP BATTLE RESULT
--------------- --------------- ----------
Kongo Surigao Strait damaged

SQL> SELECT * FROM SHIPS WHERE name='Kongo';

NAME CLASS LAUNCHED
--------------- --------------- ----------
Kongo Kongo 1913

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM Kongo_Yamato WHERE ship='Kongo'and battle='Surigao Strait';

1 row deleted.

SQL> SELECT * FROM OUTCOMES WHERE ship='Kongo' and battle='Surigao Strait';

no rows selected

SQL> SELECT * FROM SHIPS WHERE name='Kongo';

NAME CLASS LAUNCHED
--------------- --------------- ----------
Kongo Kongo 1913

SQL>
SQL> REM ------------------------------------------
> REM OPERATIONS ON BASE TABLE:
SQL>
SQL> REM OUTCOMES('SHIP','BATTLE','RESULT');
SQL>
SQL> REM SHIPS(NAME,CLASS,LAUNCHED);
SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO SHIPS VALUES('vishak','Kongo',1966);

1 row created.

SQL> INSERT INTO OUTCOMES VALUES('vishak','Surigao Strait','sunk');

1 row created.

SQL> SELECT * FROM Kongo_Yamato WHERE ship='vishak' and battle='Surigao Strait';

SHIP LAUNCHED BATTLE RESULT CLASS
--------------- ---------- --------------- ---------- ------
vishak 1966 Surigao Strait sunk Kongo

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE SHIPS
2 SET name='shea',class='Yamato',launched=1999
3 WHERE name='Bismark';

1 row updated.

SQL>
SQL> UPDATE OUTCOMES
2 SET ship='shea',battle='North Cape',result='ok'
3 WHERE ship='vishak' and battle='Surigao Strait';

1 row updated.

SQL>
SQL> SELECT * FROM Kongo_Yamato WHERE ship='shea' AND battle='North Cape';

SHIP LAUNCHED BATTLE RESULT CLASS
--------------- ---------- --------------- ---------- -------
shea 1999 North Cape ok Yamato

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM OUTCOMES WHERE ship='shea' AND battle='North Cape';

1 row deleted.

SQL> SELECT * FROM Kongo_Yamato WHERE ship='shea' AND battle='North Cape';

no rows selected

SQL>
SQL> ROLL BACK TO A2;
Rollback complete.
SQL>
SQL> REM *********************************************************
SQL> REM 3:
SQL> REM VIEW 3:Create a view named Revenge_Ships that displays the ships (ship name, class, year of
SQL> REM launch) from the Revenge Class. Label the view columns. Do not allow a ship to be
SQL> REM reassigned to another class through the view
SQL>
SQL> CREATE OR REPLACE view Revenge_Ships (view_shipname,view_class,view_launched) as
2 SELECT name,class ,launched
3 FROM ships
4 WHERE class='Revenge'
5 WITH CHECK OPTION CONSTRAINT ck_class;

View created.

SQL>
SQL> SELECT * FROM Revenge_Ships;

VIEW_SHIPNAME VIEW_CLASS VIEW_LAUNCHED
--------------- --------------- -------------
Ramillies Revenge 1917
Resolution Revenge 1916
Revenge Revenge 1916
Royal Oak Revenge 1916
Royal Sovereign Revenge 1916

SQL> REM Revenge_Ships(VIEW_SHIPNAME,VIEW_CLASS, VIEW_LAUNCHED)
SQL> REM OPERATIONS ON VIEW:
SQL>
SQL> SELECT COLUMN_NAME,UPDATABLE
2 FROM USER_UPDATABLE_COLUMNS
3 WHERE TABLE_NAME='REVENGE_SHIPS';

COLUMN_NAME UPD
------------------------------ ---
VIEW_SHIPNAME YES
VIEW_CLASS YES
VIEW_LAUNCHED YES

SQL>
SQL> SAVEPOINT A3;

Savepoint created.

SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO Revenge_Ships VALUES('Apollo','Revenge',1990);

1 row created.

SQL> SELECT * FROM ships WHERE name='Apollo';

NAME CLASS LAUNCHED
--------------- --------------- ----------
Apollo Revenge 1990

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE Revenge_Ships
2 SET VIEW_SHIPNAME='NEW Apollo',VIEW_CLASS='Revenge',VIEW_LAUNCHED=1978
3 WHERE VIEW_SHIPNAME='Apollo';

1 row updated.

SQL>
SQL> SELECT * FROM ships WHERE name='NEW Apollo';

NAME CLASS LAUNCHED
--------------- --------------- ----------
NEW Apollo Revenge 1978

SQL>
SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM Revenge_Ships WHERE VIEW_SHIPNAME='NEW Apollo';

1 row deleted.

SQL> SELECT * FROM ships WHERE name='NEW Apollo';

no rows selected

SQL>
SQL> REM ------------------------------------------
> REM OPERATIONS ON BASE TABLE:
SQL> REM SHIPS(NAME,CLASS,LAUNCHED);
SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO Ships VALUES('Akira','Revenge',1990);

1 row created.

SQL> SELECT * FROM Revenge_Ships WHERE VIEW_SHIPNAME='Akira';

VIEW_SHIPNAME VIEW_CLASS VIEW_LAUNCHED
--------------- --------------- -------------
Akira Revenge 1990

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE Ships
2 SET name='NEW Akira'
3 WHERE name='Akira';

1 row updated.

SQL>
SQL> SELECT * FROM Revenge_Ships WHERE VIEW_SHIPNAME='NEW Akira';

VIEW_SHIPNAME VIEW_CLASS VIEW_LAUNCHED
--------------- --------------- -------------
NEW Akira Revenge 1990

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM ships WHERE name='NEW Akira';

1 row deleted.

SQL> SELECT * FROM Revenge_Ships WHERE VIEW_SHIPNAME='NEW Akira';

no rows selected

SQL>
SQL> ROLL BACK TO A3;
Rollback complete.
SQL>
SQL> REM *********************************************************
SQL> REM 5:
SQL> REM VIEW 5:Create a view named Class_Ships that displays the number of ships in each class. Label the view columns.
SQL>
SQL> CREATE OR REPLACE view Class_Ships(class,No_of_ships) as
2 SELECT class,count(name)
3 FROM ships
4 GROUP BY class;

View created.

SQL>
SQL> SELECT * FROM Class_Ships;

CLASS NO_OF_SHIPS
--------------- -----------
Iowa 4
Yamato 2
North Carolina 2
Bismark 1
Revenge 5
Renown 2
Kongo 4
Tennessee 2

8 rows selected.

SQL>
SQL> REM Class_Ships(CLASS,NO_OF_SHIPS)
SQL> REM OPERATIONS ON VIEW:
SQL>
SQL> SELECT COLUMN_NAME,UPDATABLE
2 FROM USER_UPDATABLE_COLUMNS
3 WHERE TABLE_NAME='CLASS_SHIPS';

COLUMN_NAME UPD
------------------------------ ---
CLASS NO
NO_OF_SHIPS NO

SQL>
SQL> SAVEPOINT A5;

Savepoint created.

SQL> REM INSERT:
SQL>
SQL> INSERT INTO Class_Ships VALUES('Revolver',25);
INSERT INTO Class_Ships VALUES('Revolver',25)
*
ERROR at line 1:
ORA-01733: virtual column not allowed here


SQL> SELECT * FROM Ships WHERE class='Revolver';

no rows selected

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE Class_Ships
2 SET class='Haruna'
3 WHERE class='Revolver';
UPDATE Class_Ships
*
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view


SQL>
SQL> SELECT * FROM ships WHERE class='Haruna';

no rows selected

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM ships WHERE class='Haruna';

0 rows deleted.

SQL> SELECT * FROM Class_Ships WHERE class='Haruna';

no rows selected

SQL>
SQL> REM ------------------------------------------
> REM OPERATIONS ON BASE TABLE:
SQL> REM SHIPS(NAME,CLASS,LAUNCHED);
SQL>
SQL> INSERT INTO Ships VALUES('Mogador1','Iowa',1990);

1 row created.

SQL> SELECT * FROM Class_Ships WHERE class='Iowa';

CLASS NO_OF_SHIPS
--------------- -----------
Iowa 5

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE Ships
2 SET class='Bismark',name='new'
3 WHERE class='Iowa' AND name='Mogador1';

1 row updated.

SQL>
SQL> SELECT * FROM Class_Ships WHERE class='Bismark';

CLASS NO_OF_SHIPS
--------------- -----------
Bismark 2

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM ships WHERE class='Bismark' and name='new';

1 row deleted.

SQL> SELECT * FROM Class_Ships WHERE class='Bismark';

CLASS NO_OF_SHIPS
--------------- -----------
Bismark 1

SQL>
SQL> ROLL BACK TO A5;
Rollback complete.
SQL>
SQL> REM *********************************************************
SQL> REM 6:
SQL>
SQL> REM VIEW 6:Create a view called Kongo_Battle on Kongo_Yamato (as defined in 2) that gives for each
SQL> REM ship of Kongo class its ship name, battle involved and the result.
SQL>
SQL> CREATE OR REPLACE view Kongo_Battle as
2 SELECT ship,battle,result,class FROM Kongo_Yamato
3 WHERE class ='Kongo';

View created.

SQL>
SQL> SELECT * FROM Kongo_Battle;

SHIP BATTLE RESULT CLASS
--------------- --------------- ---------- ---------------
Kongo North Atlantic sunk Kongo
Haruna North Atlantic sunk Kongo
Kirishima Guadalcanal sunk Kongo

SQL>
SQL>
SQL> REM Kongo_Battle(SHIP,BATTLE,RESULT,CLASS)
SQL> REM OPERATIONS ON VIEW:
SQL>
SQL> SELECT COLUMN_NAME,UPDATABLE
2 FROM USER_UPDATABLE_COLUMNS
3 WHERE TABLE_NAME='KONGO_BATTLE';

COLUMN_NAME UPD
------------------------------ ---
SHIP YES
BATTLE YES
RESULT YES
CLASS NO

SQL>
SQL> SAVEPOINT A6;

Savepoint created.

SQL>
SQL> REM INSERT FOR VALID CASES:
SQL>
SQL> INSERT INTO Kongo_Battle(SHIP,BATTLE,RESULT) VALUES('Kongo','Guadalcanal','ok');

1 row created.

SQL> SELECT * FROM OUTCOMES WHERE ship='Kongo' AND battle='Guadalcanal';

SHIP BATTLE RESULT
--------------- --------------- ----------
Kongo Guadalcanal ok

SQL> SELECT * FROM SHIPS WHERE name='Kongo';

NAME CLASS LAUNCHED
--------------- --------------- ----------
Kongo Kongo 1913

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE Kongo_Battle
2 SET ship='Hiei',battle='North Atlantic',result='damaged'
3 WHERE ship='Kongo' AND battle='Guadalcanal';

1 row updated.

SQL>
SQL> SELECT * FROM OUTCOMES WHERE ship='Hiei' AND battle='North Atlantic';

SHIP BATTLE RESULT
--------------- --------------- ----------
Hiei North Atlantic damaged

SQL> SELECT * FROM SHIPS WHERE name='Hiei';

NAME CLASS LAUNCHED
--------------- --------------- ----------
Hiei Kongo 1914

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM Kongo_Battle WHERE ship='Hiei';

1 row deleted.

SQL> SELECT * FROM OUTCOMES WHERE ship='Hiei' AND battle='North Atlantic';

no rows selected

SQL> SELECT * FROM SHIPS WHERE name='Hiei';

NAME CLASS LAUNCHED
--------------- --------------- ----------
Hiei Kongo 1914

SQL>
SQL>
SQL> REM ------------------------------------------
> REM OPERATIONS ON BASE TABLE 1:
SQL> REM SHIPS(NAME,CLASS,LAUNCHED);
SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO SHIPS VALUES('Admiral','Kongo',1987);

1 row created.

SQL> SELECT * FROM Kongo_Battle WHERE ship='Admiral';

no rows selected

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE SHIPS
2 SET name='new Admiral'
3 WHERE name='Admiral';

1 row updated.

SQL> SELECT * FROM Kongo_Battle WHERE ship='new Admiral';

no rows selected

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM ships WHERE name='new Admiral';

1 row deleted.

SQL> SELECT * FROM Kongo_Battle WHERE ship='new Admiral';

no rows selected

SQL>
SQL> REM ------------------------------------------
> REM OPERATIONS ON BASE TABLE 2:
SQL> REM OUTCOMES('SHIP','BATTLE','RESULT');
SQL>
SQL> REM INSERT:
SQL>
SQL> INSERT INTO OUTCOMES VALUES('Kirishima','North Cape','ok');

1 row created.

SQL> SELECT * FROM Kongo_Battle WHERE ship='Kirishima' and battle='North Cape';

SHIP BATTLE RESULT CLASS
--------------- --------------- ---------- ---------------
Kirishima North Cape ok Kongo

SQL>
SQL> REM UPDATE:
SQL>
SQL> UPDATE OUTCOMES
2 SET ship='Kongo'
3 WHERE ship='Kirishima' and battle='North Cape';

1 row updated.

SQL> SELECT * FROM Kongo_Battle WHERE ship='Kongo' and battle='North Cape';

SHIP BATTLE RESULT CLASS
--------------- --------------- ---------- ---------------
Kongo North Cape ok Kongo

SQL>
SQL> REM DELETE:
SQL>
SQL> DELETE FROM OUTCOMES WHERE ship='Kongo' and battle='North Cape';

1 row deleted.

SQL> SELECT * FROM Kongo_Battle WHERE ship='Kongo'and battle='North Cape';

no rows selected
SQL> ROLLBACK TO A6;
Rollback complete.

SQL> spool off;