No: 9


To study the various Procedures and Functions in PL/SQL. THEORY Subprograms Subprograms are named PL/SQL blocks that can accept parameters and also can be invoked whenever required. Similar to a PL/SQL block, a subprogram can also have a declarative part, an executable part and an exception handling part. Some of the important features offered by subprograms are given below:  Modularity  Reusability  Maintainability PL/SQL supports two types of subprograms. They are,  Procedures  Functions Procedures A procedure is a subprogram that performs a specific action. The syntax for creating a procedure is given below, Syntax create or replace procedure <proc_name> (argument {in,out,inout}datatype) {is,as} <local declarations>; begin (executable statements) [exception] (exception handlers) end; Syntax for execution SQL> exec <proc_name> (parameters);

Parameters The parameter list can hold any of the following modes, namely in (by default), and out. These parameter modes can be used within any subprogram. In Parameter The in parameter mode is used t pass values to the subprogram when invoked. It acts like a constant and therefore it cannot be assigned a value. Out Parameter The out parameter mode is used to return values to the caller of a subprogram. Since the initial value for an out parameter is undefined, its value can be assigned to another variable. Functions A function is a subprogram that computes a value. Syntax for creation create or replace function <function_name > (argument in datatype,…….) return datatype {is,as} <local declarations> begin (executable statements) [exception] (exception handlers) end; Examples using PL/SQL Procedures and Functions 1 ) Write a PL/SQL procedure to accept the eid as input & raise an exception if salary is null, otherwise display the employee details. Creation of employee table SQL> create table emp101(eid number(10),ename varchar2(20),designation varchar2(20),location varchar 2(20),bp number(10)); Table created. Insert values into emp101 table SQL> insert into emp101 values(201,'hari','programmer','chennai',10000);

1 row created. SQL> insert into emp101 values(202,'mani','analyst','trichy',-1); 1 row created. SQL> insert into emp101 values(203,'jas','engineer','bangalore',5000); 1 row created. Display the emp101 table SQL> select *from emp101; EID ENAME DESIGNATION LOCATION BP

--------------------------------------------------------------------------------------201 hari 202 mani 203 jas programmer analyst engineer Chennai trichy Bangalore 10000 -1 5000

Creation of Procedure (pro_emp) SQL> create or replace procedure pro_emp(e_no number) is 2 a emp101.eid%type; 3 b emp101.ename%type; 4 c emp101.designation%type; 5 d emp101.location%type; 6 e emp101.bp%type; 7 total exception; 8 begin 9 select eid,ename,designation,location,bp into a,b,c,d,e from emp101 where eid=e_no; 10 if e is null then 11 raise total; 12 else 13 dbms_output.put_line(a||''||b||''||c||''||d||''||e); 14 end if; 15 exception 16 when total then 17 dbms_output.put_line('The bp is null'); 18 end; 19 / Procedure created. Procedure Execution (pro_emp101) SQL> exec pro_emp(201); PL/SQL procedure successfully completed. SQL> set serveroutput on

SQL> / Procedure created. SQL> exec pro_emp(201); 201hariprogrammerchennai10000 PL/SQL procedure successfully completed. Insert more number of records with bp field is null SQL> insert into emp101 values(204,'kala','manager','chennai',0); 1 row created. SQL> insert into emp101 values(205,'rose','manager','delhi',null); 1 row created. Display the emp101 table SQL> select *from emp101; EID ENAME DESIGNATION LOCATION BP ---------------------------------------------------------------------------------------201 hari programmer Chennai 10000 202 mani analyst trichy -1 203 jas engineer Bangalore 5000 204 kala manager Chennai 0 205 rose manager delhi SQL> exec pro_emp(205); The bp is null PL/SQL procedure successfully completed. 2) Write a procedure to accept the eid as input & update the location to be andra. Creation of Procedure (proupdate_emp101) SQL> create or replace procedure proupdate_emp101(e_no number) is 2 a emp101.eid%type; 3 begin 4 select eid into a from emp101 where eid=e_no; 5 update emp101 set location='andra' where eid=e_no; 6 dbms_output.put_line('Table updated'); 7 end; 8 / Procedure created. SQL> exec proupdate_emp101(201);

6 if c is null then 7 raise sal_ex.put_line(to_char(bp)).put_line('No salary'). 4 begin 5 select bp into c from emp101 where eid=e_id. 15 / Function (fun_emp101) is called in PL/SQL blocks SQL>set serveroutput on. SQL> declare 2 bp number. 10000 .Table updated PL/SQL procedure successfully completed. EID ENAME DESIGNATION LOCATION BP ---------------------------------------------------------------------------------------201 hari programmer andra 10000 202 mani analyst trichy -1 203 jas engineer Bangalore 5000 204 kala manager Chennai 0 205 rose manager delhi 3) Write the function which accepts eid as input and returns the salary drawn by the employee. 10 end if. 11 exception 12 when sal_ex then 13 dbms_output. 9 / Enter value for id: 201 old 5: a:=&id. 8 else 9 return c. 3 a number. 8 end. 4 begin 5 a:=&id. new 5: a:=201. 6 bp:=fun_emp101(a). SQL> create or replace function fun_emp101(e_id in number) return number is 2 c emp101.bp%type. 3 sal_ex exception. 14 end. 7 dbms_output.

7 / -1 PL/SQL procedure successfully completed. 6 while b>0 7 loop 8 fact:=fact*b.b). 3 b number. SQL> create function fact(a number) return number is 2 fact number:=1.PL/SQL procedure successfully completed.b out number)is 2 bp number. 5) Write the function to find out factorial of a number.put_line(to_char(b)). . new 5: a:=205. 3 begin 4 inout_emp101(202. 3 begin 4 select bp into b from emp101 where eid=a. 10 end loop. No salary PL/SQL procedure successfully completed. 4) Write the procedures which accept eid as input & returns the salary drawn by the employee using out parameter. 6 / Procedure created. SQL> declare 2 b number. Creation of Procedure (inout_emp101) SQL> create or replace procedure inout_emp101(a in number. 5 end. SQL> / Enter value for id: 205 old 5: a:=&id. 9 b:=b-1. 6 end. 4 begin 5 b:=a. 5 dbms_output.

SQL> insert into emp1 values(11.designation varchar2(20). 7 end. SQL>insert into emp1 values(13. Insert values into emp1 table SQL> insert into emp1 values(10.11 return(fact).Write a procedure that will accept eid as input and return the total salary drawn by the employee using out parameter. 1 row created. 6 dbms_output.25000).‘nivas‘. SQL> declare 2 a number:=7. 13 / Function created.‘analyst‘). Table created. 4 begin 5 f:=fact(a).10000). 12 end.'ram'. Lab Exercises 1.put_line('The factorial of the given number is'||f).'jaya'.bp number(9)).ename varchar2(20). Creation of employee table SQL> create table emp1(eid number(10).'sri'.20000).'programmer'. 3 f number(10). 1 row created. SQL> set serveroutput on.'manager'. 1 row created.'clerk'. SQL> insert into emp1 values(12. 1 row created. . 8 / The factorial of the given number is5040 PL/SQL procedure successfully completed.

6 / Procedure created. 6 dbms_output. 5 dbms_output. SQL> set serveroutput on SQL> declare 2 b number. 5 end. 5 update emp1 set designation='sales person' where ename=e_name.b out number)is 2 bp number. EID ENAME DESIGNATION BP --------------------------------------------------------------------------------10 sri programmer 20000 11 ram manager 25000 12 jaya clerk 10000 13 nivas analyst Creation of procedure (inout_emp1) SQL> create or replace procedure inout_ emp1 (a in number.b). 3 begin 4 select bp into b from emp1where eid=a. 7 / 20000 PL/SQL procedure successfully completed. Creation of Procedure (proupdate_emp1) SQL> create or replace procedure proupdate_emp1(e_name varchar) is 2 a emp1. 2. 3 begin 4 inout_emp1 (10.ename%type. .put_line(to_char(b)). 3 begin 4 select ename into a from emp1where ename=e_name.put_line('table updated'). 6 end.Display the emp1 table SQL> select * from emp1. Write a procedure that accept ename as input and update the designation to be ‘salesperson’.

14 end. table updated PL/SQL procedure successfully completed. . 3 a number. 15 / Function created. 8 else 9 return c.bp%type.Rewrite the above program using function. 10 end if.put_line('no salary'). SQL> declare 2 bp number. 4 begin 5 select bp into c from emp1where eid=e_id. SQL> select * from emp1. 11 exception 12 when sal_ex then 13 dbms_output. SQL> exec proupdate_ emp1 ('jaya'). Function (fun_emp1) is called in PL/SQL blocks SQL> set serveroutput on. 8 / Procedure created. SQL> create or replace function fun_ emp1 (e_id in number)return number is 2 c emp1. 3 sal_ex exception.7 end. 6 if c is null then 7 raise sal_ex. EID ENAME DESIGNATION BP ----------------------------------------------------------------------------10 sri programmer 20000 11 ram manager 25000 12 jaya sales person 10000 13 rama analyst 3.

SQL>/ Enter value for eid:13 old 5: a:=&eid. . new 5: a:=13.Write a procedure for ‘inout’ parameters. Creation of Procedure (inout_emp1) SQL> create or replace procedure inout_emp1 (a in out number)is 2 bp number. 5 end. no salary PL/SQL procedure successfully completed. Result Thus the procedures and functions were executed and their outputs were verified. bp:fun_ emp1(a). 6 / Procedure created.4 5 6 7 8 9 begin a:=&eid. 4. / Enter value for eid: 10 old 5: a:=&eid. 3 begin 4 select bp into a fromemp1 where eid=a. 20000 PL/SQL procedure successfully completed. end.put_line(to_char(bp)). new 5: a:=10. dbms_output.

After It fires the trigger after executing the trigger statement. Types of Triggers The various types of triggers are as follows. It also specifies the table to which the trigger is associated.  Trigger restriction Restrictions on the trigger can be achieved.  Trigger statement Specifies the DML statements (insert or delete or update) and fires the trigger body (between begin and end PL/SQL block). The parts of triggers are. An enabled trigger executes the trigger body if trigger statement is issued. Syntax Drop trigger <trigger_name>. . Dropping Trigger This command is used to drop a trigger.    Before It fires the trigger before executing the trigger statement.Ex. No: 10 AIM IMPLEMENTATION OF TRIGGERS To study and implement the concept of triggers. Syntax alter trigger <trigger_name> disable. THEORY Trigger A trigger is a statement that is executed automatically by the system as a side effect of a modification to the database. Enabling and Disabling Triggers A trigger can be either enabled or disabled. Syntax create or replace trigger <trigger_name> [before/after] [insert/update/delete] on <table_name> [for each row] [when condition]. For each row It specifies that the trigger fires once per row.  Trigger body or trigger action It is a PL/SQL block that is executed when the triggering statement is used between begin and end block.

Table created.salary number(10)). . 1 row created.10000). 4 end. SQL> insert into emp01 values('yyy'. ENAME EID SALARY ---------. SQL> insert into emp01 values('xxx'.10500).eid number(5). 1 row created. 5 / Trigger created.15500). The error message should be a character string.12.11. Syntax raise_application_error(error number. SQL> select *from emp01. To create the table ‘emp01’ SQL> create table emp01(ename varchar2(10).---------xxx 11 10000 yyy 12 10500 zzz 13 15500 SQL> create trigger emp01trigger before insert or update or delete on emp01 for each row 2 begin 3 raise_application_error(-20010.User defined error message The package ‗raise_application_error‘ is used to issue the user defined error messages.'You cannot do manipulation'). The error number can lie between -20000 and -20999. SQL> insert into emp01 values('zzz'.---------.13. 1 row created. Example Exercises using Triggers 1) To create a simple trigger that does not allow insert.‘error message‘). update and delete operations on the table emp01.

EMP01TRIGGER". update emp01 set eid=15 where ename='yyy' * ERROR at line 1: ORA-20010: You cannot do manipulation ORA-06512: at "SCOTT. 1 row deleted. SQL> select *from emp01. line 2 ORA-04088: error during execution of trigger 'SCOTT.14. Check for insert or update or delete operations SQL> insert into emp01 values('aaa'.14.EMP01TRIGGER' To drop the created Trigger SQL> drop trigger emp01trigger.---------. ENAME EID SALARY ---------.SQL> insert into emp01 values('aaa'. Trigger dropped. .EMP01TRIGGER' SQL> update emp01 set eid=15 where ename='yyy'. line 2 ORA-04088: error during execution of trigger 'SCOTT.EMP01TRIGGER".EMP01TRIGGER' SQL> delete from emp01 where ename='xxx'. SQL> select *from emp01.---------xxx 11 10000 yyy 12 10500 zzz 13 15500 aaa 13 34000 SQL> delete from emp01 where ename='zzz'. delete from emp01 where ename='xxx' * ERROR at line 1: ORA-20010: You cannot do manipulation ORA-06512: at "SCOTT. line 2 ORA-04088: error during execution of trigger 'SCOTT. insert into emp01 values('aaa'.34000).13. 1 row created.EMP01TRIGGER".34000).34000) * ERROR at line 1: ORA-20010: You cannot do manipulation ORA-06512: at "SCOTT.

SQL> select *from emp01.---------. SQL> insert into dept01 values(15.'HR'. 5 begin 6 day:=to_char(sysdate.deptname varchar2(20).'Delhi'). To create the table dept01 SQL> create table dept01(deptno number(10).'Management'. . SQL> insert into dept01 values(14.'chennai').---------xxx 11 10000 yyy 12 10500 aaa 13 34000 SQL> update emp01 set eid=15 where ename='yyy'. 4 date1 date. SQL> select *from dept01. 1 row created.---------xxx 11 10000 yyy 15 10500 aaa 13 34000 2) Write the dept01trigger after update for each row giving the date and day on which the updation is performed.-------------------12 HR Delhi 14 Management chennai 15 Govt kolkata SQL> create or replace trigger dept01trigger after update on dept01 for each row 2 declare 3 day char(5). DEPTNO DEPTNAME LOCATION ---------. Table created.---------.'dy'). 1 row updated. 1 row created.'Govt'. SQL> insert into dept01 values(12.'kolkata').location varchar2(20)). 1 row created. ENAME EID SALARY ---------.ENAME EID SALARY ---------.-------------------.

3. 9 end. Drop the created trigger on before delete operation for each row on emp table. SQL> set serveroutput on.put_line('The day and date of updation is'||''||day||''||to_char(date1)). Write a database trigger before delete for each row not allowing deletion and give the appropriate message on emp table. 8 dbms_output. SQL> update dept01 set location='andra' where deptno=14. The day and date of updation is tue 01-JAN-08 1 row updated.7 date1:=sysdate. Result Thus the triggers programs were executed and their outputs were verified. . Lab Exercise using Triggers 1. 10 / Trigger created. 2. Enable and disable the created trigger for dept table with corresponding data values.

The return value is FALSE. Syntax: SQL%FOUND . A cursor contains information on a select statement and the rows of data accessed by it. The cursor attributes available are %FOUND. DELETE and UPDATE do not affect row and if SELECT…. Implicit cursors Attributes  %FOUND The return value is TRUE. implicit cursor attributes can be used to find out whether any row has been returned by the SELECT statement.. THEORY Cursor A cursor is a temporary work area created in the system memory when a SQL statement is executed. This temporary work area is used to store the data retrieved from the database. %NOTFOUND. and %ROWCOUNT. and DELETE statements are executed. INSERT. when you execute INSERT.INTO statement do not return a row. and manipulate this data. UPDATE.INTO statement return at least one row.. No: 11 AIM IMPLEMENTATION OF CURSORS To write PL/SQL blocks that implement the concepts of cursors. The set of rows the cursor holds is called the active set. INTO statement is executed in a PL/SQL Block. There are two types of cursors in PL/SQL: Implicit cursors: These are created by default when DML statements like. When a SELECT. PL/SQL returns an error when no data is selected.Ex. if the DML statements like INSERT. UPDATE. Oracle provides few attributes called as implicit cursor attributes to check the status of DML operations. or DELETE statements the cursor attributes tell us whether any rows are affected and how many have been affected. A cursor can hold more than one row. but can process only one row at a time. They are also created when a SELECT statement that returns just one row is executed. For example. if DML statements like INSERT. DELETE and UPDATE affect at least one row and if SELECT ….

Syntax: SQL%NOTFOUND  %ROWCOUNT Return the number of rows affected by the DML operations INSERT. DELETE and UPDATE at least one row and if SELECT …. Even though the cursor stores multiple records. UPDATE. %NOTFOUND The return value is FALSE. The return value is TRUE. SELECT Syntax: SQL%ROWCOUNT Explicit cursors They must be created when you are executing a SELECT statement that returns more than one row.INTO statement does not return a row. Loops The LOOP construct allows you to repeatedly execute a sequence of statements. if a DML statement like INSERT. which is called as current row. only one record can be processed at a time. Note Explicit cursors store the cursor point values into other explicit variables not by using any explicit attributes. DELETE and UPDATE do not affect even one row and if SELECT …. When you fetch a row the current row position moves to next row. . if DML statements like INSERT.INTO statement return at least one row. DELETE. The syntax for a cursor FOR loop is: FOR record_index IN [cursor_name | (SELECT statement)] LOOP executable_statement(s) END LOOP.

Example Exercises using Cursors To create the table ‘empcur’ SQL> create table empcur(eid number(10).15). SQL> declare 2 begin 3 for emy in (select eid.job varchar2(10).ename from empcur) 4 loop 5 dbms_output. SQL> insert into empcur values(1. 1 row created. 1 row created. 1 row created. SQL> insert into empcur values(3.13000. EID ENAME JOB SAL DNO ---------.'jas'. 6 end loop.16).put_line('Employee id and employee name are'||emy.ename).11).'manager'.'hari'.12000.---------1 mala lecturer 34000 11 2 kala manager 20000 12 3 mani assistant 14000 15 4 hari lecturer 12000 20 5 jas lecturer 13000 16 1) To write a PL/SQL block to display the employee id and employee name using cursor for loop SQL> set serveroutput on.---------. 1 row created. Table created.'lecturer'.dno number(10)).20000. SQL> insert into empcur values(2.'lecturer'.34000.'mani'.ename varchar2(20).20).sal number(10). 1 row created.'mala'.'kala'. SQL> insert into empcur values(5.12).14000.'lecturer'. SQL> insert into empcur values(4.---------.eid||'and'||emy. .'assistant'. SQL> select *from empcur.-------------------.

sal from empcur where dno=11. 3 begin 4 open cemp. 8 / Employee id and employee name are1 and mala Employee id and employee name are2 and kala Employee id and employee name are3 and mani Employee id and employee name are4 and hari Employee id and employee name are5 and jas PL/SQL procedure successfully completed. 11 / PL/SQL procedure successfully completed. 3 ecode empcur. EID ENAME JOB SAL DNO ---------. SQL> select *from empcur.sal%type.---------1 mala lecturer 39000 11 2 kala manager 20000 12 3 mani assistant 14000 15 4 hari lecturer 12000 20 5 jas lecturer 13000 16 3) To write a PL/SQL block to display the employee id and employee name where department number is 11 using explicit cursors SQL> declare 2 cursor cenl is select eid. 8 end loop. 10 end. 4 esal empcur. 9 close cemp.eid%type. 2) To write a PL/SQL block to update the salary of all employees where department no is 11 by 5000 using cursor for loop and to display the updated table SQL> declare 2 cursor cemp is select eid.ename.-------------------.eid. .dno from empcur where dno=11.7 end. 5 for remp in cemp 6 loop 7 update empcur set sal=remp.---------.---------.sal.sal+5000 where eid=remp.

9 if sql%found then 10 dbms_output.---------1 mala lecturer 49000 11 2 kala manager 30000 12 .put_line('Employee record is not found'). 4) To write a PL/SQL block to update the salary by 10000 where the job is manager.---------. to check if updates are made using implicit cursors and to display the updated table SQL> declare 2 county number. 3 begin 4 update empcur set sal=sal+10000 where job='manager'. 5 county:=sql%rowcount. 8 end if.put_line('Employee code and employee salary are'||ecode||'and'||esal). EID ENAME JOB SAL DNO ---------. 7 loop 8 fetch cenl into ecode.esal. 6 if county>0 then 7 dbms_output. 16 / The number of rows are1 Employee record modification done successfully PL/SQL procedure successfully completed. 14 end if. 13 end if. 13 end.---------. 9 exit when cenl%notfound. 11 end loop. 15 end.-------------------. 11 else if sql%notfound then 12 dbms_output.put_line('The number of rows are'||county).put_line('Employee record modification done successfully'). SQL> select *from empcur.5 begin 6 open cenl. 14 / Employee code and employee salary are 1 and 49000 PL/SQL procedure successfully completed. 10 dbms_output. 12 close cenl.

5. Write a PL/SQL block to display the dept no and dept name using cursor for loop. 7. Write a PL/SQL block to display the department name where deptno is 20 using explicit cursors. to check the updates are made using implicit cursors and to display the updated table. Result Thus the various operations were performed on the table using cursors and the output was verified. 6. Create the department table with deptno and deptname. Write a PL/SQL block to insert the new row with deptno and deptname=‘jas‘.3 mani 4 hari 5 jas assistant lecturer lecturer 14000 12000 13000 15 20 16 Lab Exercise using Cursors 4. .

java) Code import java. No: 12 AIM IMPLEMENTATION OF EMBEDDED SQL To execute the embedded SQL program in Java.mdb‖)  And select database name as (db5. . Open Microsoft Office Access 2003. Save all the changes and specify the Table name (In my case table name is Table1).mdb)  Specify Data Source Name as db5 click select button Goto the specified directory (In my case path is ―D:\java\db5.*. It will display the Table format to insert the fields. 4. Procedure to create the database connectivity 1. 5. It will display db5 database and select Create table in Design View Right click on Create table in Design View select open. Type the following java code and save the java file in particular location. Open the Notepad.mdb). Goto File  New Select Blank Database choose location  and specify the File Name (In my case db5) 3. String s="insert into Table1 values("+args[0]+")".odbc.*. Now database is created (db5. (In my case D:\java\emb5.lang. import java. ID and it‘s data type will be Number.io.mdb)  click Ok button  Finally close the window.*. Goto Control Panel select Administrative Tools select Data Sources (ODBC) In User DSN  Click Add Microsoft Access Driver (*.sql.JdbcOdbcDriver"). 6. class emb5 { public static void main(String args[])throws Exception { Class. 7. Insert one field.Ex. 8. 2. import java.jdbc.forName("sun. And close the window.

Open Command Prompt Window Goto the specified location D:\java>) 10. Set the following path files. Compile the java file (emb5.executeUpdate(s).getConnection("jdbc:odbc:db5").6. 11.println("Data Inserted" +i).java) javac emb5.createStatement().6. if(i>0){ System.out. } } 9. D:\java>java emb5 1001 Data Inserted1 D:\java>java emb5 1002 Data Inserted1 D:\java>java emb5 1003 . int i=st.0\bin". D:\java>set path="C:\Program Files\Java\jdk1.Connection con=DriverManager. } else{} con. Execute the code.java Output Table before Insertion 12. D:\java>set JAVA_HOME="C:\Program Files\Java\jdk1. D:\java>set classpath="C:\Program Files\Java\jdk1.0".close(). Statement st=con.0\bin".6.

Data Inserted1 D:\java>java emb5 1004 Data Inserted1 Table after Insertion Result Thus the embedded SQL application is implemented successfully. .

NORMAL FORMS Condition using keys and FDs of a relation to certify whether a relation schema is in a particular normal form First Normal Form First normal form. it does not require additional information such as functional dependencies. DESCRIPTION OF NORMALIZATION DEFINITION Process of decomposing unsatisfactory "bad" relations by breaking up their attributes into smaller relations until the functional dependencies are satisfied. if the schema of a relation employee included an attribute children whose domain elements are sets of names. No: 13 AIM STUDY EXPERIMENT. FDs and keys are used to define normal forms for relations in relational schemas. For example.NORMALIZATION To study the process function of Normalization. first list the functional dependencies (FDs) that must always hold between the domain values. imposes a very basic requirement on relations. . unlike the other normal forms. A domain values are atomic if elements of the domain are considered to be indivisible units. the schema would not be in first normal form. Normalization is the process of efficiently organizing data in a database with two goals in mind  First goal: eliminate redundant data  Second Goal: ensure data dependencies make sense FUNCTIONAL DEPENDENCIES To design a relational database. Say that a relation schema R is in first normal form (1NF) if the domains of all attributes of R are atomic.Ex. Example 1  A set of names is an example of a nonatomic value. Functional dependencies play a key role in differentiating good database designs from bad database designs.

a FD Y  Z where removal of any attribute from Y (set of primary key attributes) means the FD does not hold any more with Z attributes (Non-prime attribute). If all attribute values are atomic: no repeating group.attribute that is belongs to primary key K. Examples  {SSN. PNUMBER}  ENAME is not a full FD (it is called a partial dependency ) since SSN  ENAME also holds Note A relation schema R is in second normal form (2NF) if every non-prime attribute (not a primary key) A in R is fully functionally dependent on the primary key. PNUMBER}  HOURS is a full FD since neither SSN  HOURS nor PNUMBER  HOURS hold  {SSN. such as an attribute address with component attributes street and city. and are therefore nonatomic.  Such identification numbers can be divided into smaller units. Second Normal Form Second Normal form contains non-Prime attribute . so the set of integers is an atomic domain. Full functional dependency . no composite attributes Example 2 Consider an organization that assigns employees identification numbers of the following form:  The first two letters specify the department and the remaining four digits are a unique number within the department for the employee.  Integers are assumed to be atomic (unbreakable). . also have nonatomic domains. Examples of such numbers would be CS0012 and EE1127. Composite attributes.

. either: – Y is a subset of X or. • Boyce-Codd Normal Form (BCNF) A relation is in BCNF if and only if every determinant is a candidate key.Third Normal Form Third normal form meet all the requirements of the 1NF (attribute domain values allow only atomic values) and meet all the requirements of the 2NF (non-prime attributes fully functionally dependent on the primary key). (all determinants are candidate key). Transitive functional dependency – if there a set of attribute Z that are neither a primary or candidate key (it‘s a non-prime attributes) and both X  Z Examples  SSN  DMGRSSN is a transitive FD since SSN  DNUMBER and DNUMBER  DMGRSSN hold  SSN  ENAME is non-transitive since there is no set of attributes X where SSN  X and X  ENAME Note A relation schema R is in third normal form (3NF) if it is in 2NF and non-prime attribute A in R is transitively dependent on the primary key (XY & YZ) holds. For every functional dependency X->Y in a set F of functional dependencies over relation R. – X is a superkey of R Result Thus the process function of normalization was studied in detail. It remove columns that are not dependent upon the primary key.

Basic concepts Entity sets An entity is a ―thing‖ or ―object‖ in the real world that is differ from all other objects.For example. for example. Attributes An entity is represented by a set of attributes. It was developed to facilitate database design by allowing specification of an enterprise schema.y.Relationship Model The Entity-Relationship (E-R) data model perceives the real world as consisting of basic objects. An entity set is a set of entities of the same type that share the same properties. account entity.z) who are customers at a given bank. which represents the overall logical structure of a database. and relationships among these objects. called entities. person entity. and customer-city. o Composite attributes can be divided into subparts (other attributes) . depositor entity. Similarly. customer-name. Attribute types  Simple and composite attributes  Single-valued and multi-valued attribute  Derived attribute  Simple and composite attributes o Simple attributes are not divided into subparts. No: 14 AIM DATABASE DESIGN USING E-R MODEL To analyze and study the database design using Entity-Relationship Model. For example. Attributes are descriptive properties possessed by each member of an entity set. Possible attributes of the customer entity set are customer-id.Ex. Possible attributes of the loan entity set are loan-number and amount. or attributes. the entity set loan might represent the set of all loans awarded by a particular bank. can be defined as the entity set customer. THEORY Entity. customer-street. Examples The set of all persons (x.

an attribute name could be structured as a composite attribute consisting of first-name.   For example. middle-initial. and last-name.  A relationship may also have attributes called ―descriptive attributes‖. and different employees may have different numbers of phones.  Example 2 Consider the two entity sets loan and branch.  We could associate the attribute access-date to that relationship (depositor) to specify the most recent date on which a customer accessed an account.  Single-valued and multi-valued attribute o Single-valued attribute The attributes all have a single value for a particular entity. Multi-valued attribute Attribute has a set of values for a specific entity. Relationship sets  A relationship is an association among several entities. or several phone numbers.o For example. . the loan-number attribute for a specific loan entity refers to only one loan number.  Example 1 Consider the two entity sets customer and loan  We define the relationship set borrower to denote the association between customers and the bank loans that the customers have.  Derived attribute o The value for this type of attribute can be derived from the values of other related attributes or entities.  Example  Consider a relationship set depositor with entity sets customer and account. one. We can define the relationship set loan-branch to denote the association between a bank loan and the branch in which that loan is maintained. consider an employee entity set with the attribute phone-number. o An employee may have zero. o For example.

An entity in A is associated with at most one entity in B. An entities in A is associated with at most one entity in B.  The relationship set from customer to loan is ―one to many‖.  Many to many. Constraints  An E-R schema may define certain constraints to which the contents of a database must conform. can be associated with any number (zero or more) of entities in A. and an entity in B is associated with any number (zero or more) of entities in A. a loans can belong to only one customer. or cardinality ratios. The depositor relationship among the entities corresponding to customer Jones and account A-217 has the value ―23 May 2001‖ for attribute access-date.  Many to one.  Two types of constraints  Mapping Cardinalities  Participation Constraints  Mapping Cardinalities  Mapping cardinalities. and a customer can have several loans.  If.  Example  Consider the borrower relationship set. and an entity in B is associated with at most one entity in A. An entity in A is associated with any number (zero or more) of entities in B. in a particular bank. An entity in B. express the number of entities to which another entity can be associated via a relationship set. however.  One to many. . An entity in A is associated with any number (zero or more) of entities in B.  Types  One to one. however. can be associated with at most one entity in A. which means that the most recent date that Jones accessed account A-217 was 23 May 2001. An entity in B.

the participation of entity set E in relationship R is said to be partial.  For example. (uniquely identifies the entity)  Keys also help uniquely identify relationships. we expect every loan entity to be related to at least one customer through the borrower relationship.  Participation Constraints  The participation of an entity set E in a relationship set R is said to be total if every entity in E participates in at least one relationship in R. (uniquely identifies the relationship)  Types of keys related to Entity sets  Superkey  Candidates keys  Primary key  Superkey  A superkey is a set of one or more attributes that. Therefore the participation of loan in the relationship set borrower is total.  In other words. taken collectively. allow us to identify uniquely an entity in the entity set.  It is possible that only some of the customer entities are related to the loan entity set through the borrower relationship. and the participation of customer in the borrower relationship set is therefore partial. . no two entities in an entity set are allowed to have exactly the same value for all attributes. the relationship set is many to many. and thus distinguish relationships from each other. Keys  The values of the attribute values of an entity must be such that they can uniquely identify the entity.  If only some entities in E participate in relationships in R.  A key allows us to identify a set of attributes to distinguish entities from each other. If a loans can belong to several customers (as can loans taken jointly by several business partners).

 Similarly. which represent entity sets  • Ellipses. since the attribute customer-id alone is a candidate key. customer-id is a superkey. their combination does not form a candidate key.  Thus.  The customer-name attribute of customer is not a superkey. customer-street} are candidate keys. which represent relationship sets  • Lines. changed. since it is likely to change.  Then. the combination of customer-name and customer-id is a superkey for the entity set customer. Such a diagram consists of the following major components:  • Rectangles.  For Example.  Social-security numbers. on the other hand. both {customer-id} and {customer-name.  Candidates key  It is possible that several distinct sets of attributes could serve as a candidate key. are guaranteed to never change. which represent multi-valued attributes .  Although the attributes customerid and customer-name together can distinguish customer entities. which link attributes to entity sets and entity sets to relationship sets  • Double ellipses. the customer-id attribute of the entity set customer is sufficient to distinguish one customer entity from another. Entity-Relationship Diagram  E-R diagram can express the overall logical structure of a database graphically. the address field of a person should not be part of the primary key. or very rarely. because several people might have the same name. For example.  Primary key  The primary key should be chosen such that its attributes are never. which represent attributes  • Diamonds.  Suppose that a combination of customer-name and customer-street is sufficient to distinguish among members of the customer entity set.

one-to-many. which represent weak entity sets Example  The diagram consists of two entities sets.   The attributes of an entity set that are members of the primary key are underlined. • Dashed ellipses. .  borrower cannot be a many-to-many or a one-to-many relationship set from customer to loan.  To distinguish among these types. customer and loan. from customer to loan. customer-name. which indicate total participation of an entity in a relationship set  • Double rectangles.  The attributes associated with customer are customer-id.  The relationship set borrower may be many-to-many. and customer-city. we draw either a directed line (→) or an undirected line (—) between the relationship set and the entity set. customer-street.  Undirected line  An undirected line from the relationship set borrower to the entity set loan specifies that borrower is either a many-to-many or one-to-many relationship set from customer to loan. or one-to-one.  Directed line  A directed line from the relationship set borrower to the entity set loan specifies that borrower is either a one-to-one or many-to-one relationship set. related through a binary relationship set borrower. many-to-one. which denote derived attributes  • Double lines.  The attributes associated with loan are loan-number and amount.

then we link these attributes to that relationship set. .  Here. city. and last-name replaces the simple attribute name of customer. and zip-code replaces the attributes customer-street and customer-city of customer.  The accessdate descriptive attribute attached to the relationship set depositor to specify the most recent date on which a customer accessed that account. a composite attribute name. middle-initial. state. a composite attribute address.  Also. If a relationship set has also some attributes (descriptive attributes) associated with it. with component attributes first-name. whose component attributes are street.

consider the relationship borrower between customers and loans.  That is.  Role Indicators  Indicate roles in E-R diagrams by labeling the lines that connect diamonds to rectangles.  A double line from loan to borrower. depicted by a dashed ellipse. indicates that each loan must have at least one associated customer. as in Figure 2.  For instance. . The attribute street is itself a composite attribute whose component attributes are streetnumber.12 shows the role indicators manager and worker between the employee entity set and the works-for relationship set. depicted by a double ellipse.14.  Participation Constraints  Double lines are used in an E-R diagram to indicate that the participation of an entity set in a relationship set is total. street-name. each entity in the entity set occurs in at least one relationship in that relationship set.  A multivalued attribute phone-number. and a derived attribute age. and apartment number.  Figure 2.

 For example.  Example:  For example. payment-date..  A maximum value of 1 indicates that the entity participates in at most one relationship.  An entity set that has a primary key is termed a strong entity set.1.. and payment-amount. Cardinality Limits  E-R diagrams also provide away to indicate more complex constraints on ―the number of times each entity participates in relationships in a relationship set”.  That is. which has the three attributes: payment-number. where l is the minimum and h the maximum cardinality.  Weak Entity sets  An entity set may not have sufficient attributes to form a primary key.15. Such an entity set is termed a weak entity set.h.  The limit 0. The edge between loan and borrower has a cardinality constraint of 1. each loan must have exactly one associated customer.  A Minimum value of 1 indicates total participation of the entity set in the relationship set. consider Figure 2.. shown in the form l. . consider the entity set payment.∗ on the edge from customer to borrower indicates that a customer can have zero or more loans. meaning the minimum and the maximum cardinality are both 1. while a maximum value ∗ indicates no limit.  An edge between an entity set and a binary relationship set can have an associated minimum and maximum cardinality.

16.  Thus. Payment numbers are typically sequential numbers. but payments for different loans may share the same payment number. generated separately for each loan.  In Figure 2.  Thus. and a doubly outlined diamond indicates the corresponding identifying relationship. although each payment entity is distinct. this entity set does not have a primary key. it is a weak entity set. the weak entity set payment depends on the strong entity set loan via the relationship set loan-payment.  In E-R diagrams. starting from 1. a doubly outlined box indicates a weak entity set. E-R Notations .

with the attributes loan-number. the base attribute start-date. and payment-amount.Banking Applications  Entity Sets  The branch entity set. with attributes branch-name.  The customer entity set. Additional descriptive features are the multi valued attribute dependent-name.  Two account entity sets—savings-account and checking-account —with the common attributes of account-number and balance.  The weak entity set loan-payment. . employee-name. and the derived attribute employment-length. with attributes employee-id. paymentdate. amount. in addition. customer-name.Example.  The employee entity set. and assets.and lower-level entity sets  Attribute inheritance  Aggregation. branch-city. customerstreet and customer-city. Extended E-R Features  Extended E-R features are  Specialization  Generalization  Higher. with attributes payment-number. with attributes customer-id. salary.  The loan entity set. telephonenumber. savings-account has the attribute interest-rate and checking-account has the attribute overdraftamount.

in which multiple entity sets are synthesized into a higher-level entity set on the basis of common features. (but some entities will share the attributes)  Generalization  The refinement from an initial entity set into successive levels of entity sub groupings (higher and lower level entities set) represents a top-down design process.  The design process may also proceed in a bottom-up manner.  The attributes of the higher-level entity sets are said to be inherited by the lowerlevel entity sets.  Attribute Inheritance  A crucial attributes of the higher. .and lower-level entities created by specialization and generalization is attribute inheritance. Specialization  An entity set may include sub groupings of entities that are distinct in some way from other entities in the set. a subset of entities within an entity set may have attributes that are not shared by all the entities in the entity set.  For instance.

we regard the relationship set works-on (relating the entity sets employee.  Thus.  Aggregation is an abstraction through which relationships are treated as higher level entities (those entities are connected with the relationship set).  We can then create a binary relationship manages between works-on and manager to represent who manages what tasks. branch. . represent the ―relationships among relationships”. and job) as a higher-level entity set called works-on. for our example. Aggregation  In E-R model.

. a many-to-one relationship set that indicates in which branch a loan originated.E-R diagram for Banking Applications  Relationship Sets  borrower. a many-to-many relationship set between customer and loan.  loan-branch. Note that this relationship set replaces the attribute originating branch of the entity set loan.

 depositor. with relationship attribute access-date. a many-to-many relationship set between customer and account. loan-payment. a many-to-one relationship set expressing that a customer can be advised by a bank employee. a one-to-many relationship from loan to payment. . Result Thus the analysis and study of database design using Entity-Relationship Model was completed successfully. indicating that a customer owns an account.  works-for. with relationship attribute type.  cust-banker. which documents that a payment is made on a loan. the mapping cardinalities express that an employee works for only one manager and that a manager supervises one or more employees. and that a bank employee can advise one or more customers. a relationship set between employee entities with role indicators managerm and worker.

These are the main divergence from the old BASIC. Therefore. In BASIC. THEORY Visual Basic -Front End Tool VISUAL BASIC is a high level programming language which evolved from the earlier DOS version called BASIC. Microsoft Access to develop application software. MS-Access Database. programming is done in a graphical environment. because the user may click on certain object randomly. you just need to drag and drop any graphical object anywhere on the form. In VB. and "power users" can use it to build software applications. On the other hand. a VB Program is made up of many subprograms. It is a very easy programming language to learn. The code looks a lot like English Language. In VB. so each object has to be programmed independently to be able to response to those actions (events). No: 15 DESIGN AND IMPLEMENTATION OF VISUAL BASIC APPLICATIONS USING MS-ACCESS AND ORACLE DATABASES AIM To design and implement the Visual Basic Applications using MS-Access and Oracle Databases. and you can change its color any time using the properties windows. BASIC means Beginners' All-purposeSymbolic Instruction Code. is a database management system from Microsoft that combines the relational Microsoft Jet Database Engine with a graphical user interface and software-development tools. However. and each can be executed independently and at the same time each can be linked together in one way or another. an object- . programming is done in a text-only environment and the program is executed sequentially. each has its own program code. Access is supported by Visual Basic for Applications.Back End Tool Microsoft Office Access.Ex. VISUAL BASIC is a VISUAL and events driven Programming Language. Like other Office applications. previously known as Microsoft Access.

. modular storage and servers. There is no need for peak workloads. the physical storage of data can be managed without affecting the access to logical storage structures.oriented programming language that can reference a variety of objects including DAO (Data Access Objects). The database has logical structures and physical structures. Enterprise grid computing creates large pools of industry-standard. a server reliably manages a large amount of data in a multiuser environment so that many users can concurrently access the same data. because capacity can be easily added or reallocated from the resource pools as needed. The purpose of a database is to store and retrieve related information. Oracle Database is the first database designed for enterprise grid computing. In general. A database server is the key to solving the problems of information management. With this architecture. the most flexible and cost effective way to manage information and applications. A database server also prevents unauthorized access and provides efficient solutions for failure recovery. All this is accomplished while delivering high performance. Oracle Database.Back End Tool An Oracle database is a collection of data treated as a unit. Because the physical and logical structures are separate. each new system can be rapidly provisioned from the pool of components.

Open Microsoft office access 2003.Click components.Add New Fields.Then Apply.Microsoft DataGrid Control 6.and select Microsoft ADO Data Control 6.Name.It will open the Project1 – Form1 2.Click New from File Menu. PROCEDURE In MS. . Right Click on Tool box (Righthand side).Office Access 2003-Back End Tool 1. Goto Microsoft Visual Basic – Click Standard EXE.Click Save Changes.Specify the Table Name (Table1) 2. Click Table1 and add field values (as Name-aaa & Phone.0.Click Ok.Front End Tool 1.Ex.Click Blank Database – Click Create table in Design View (Specify the database name as PhoneBook1).2275) In Visual Basic. Phone and Address.0. No: 15a DESIGN AND IMPLEMENTATION OF EMPLOYEE INFORMATION SYSTEM IN VISUAL BASIC USING MS-ACCESS DATABASE AIM To design and implement the Employee Information System in Visual Basic using MSAccess Database.

Right click the component – Click Properties.Click Test Connection.select 2-adCmdTable-select Table or Stored Procedure Name (In my case Table1).DataGrid Component. Place the two components in Form1 (Adodc and DataGrid components) 4. AllowDelete. Goto Properties of (click datasource name) DataGrid1 (Righthand side):DataSource Adodc1 .select Microsoft Jet 4. 7.0 OLE Provider.3. Click RecordSource from Menu.Select or enter a database name.Then select all checkboxes (AllowAddNew.It will show Test Connection Succeeded.Click Next. 6. 5. ColumnHeaders. Right click Adodc component.Then Apply.Click Ok. Goto Form1. AllowEnabled).in my case PhoneBook1.Goto Adodc Properties.Click Build.click ok. AllowUpdate.click ok.

Add the new data (Double click on the final stored field value) 10. Finally execute the Form1.It shows database field values 9.8. If you want to delete select the field and press Delete Button on Keyboard Check it in MS-Office Access database from Table1 .

Finish Result Thus the design and implementation of Employee Information System in Visual Basic using MS-Access Database was executed successfully. .11.