You are on page 1of 120

Sri Venkateswara College of Engineering & Technology Department of Information Technology

141452-DBMS LAB MANUAL

PREPARED BY S.Thavamaniyan

DATA DEFINITION LANGUAGE COMMANDS


AIM:
To implement all Data Definition Language Commands. DDL commands are: (1) Create (2) Alter (i)Add (ii)Modify (3)Drop (4)Truncate (5)Rename (1)Create: This DDL command is used to create a table. Syntax: create table <table name>(fieldname 1 datatype( size ),fieldname 2 datatype(size).fieldname n datatype(size));
(2) Alter:

This DDL command is used to modify the already existing table, but we cannot change or rename the table. (i)Add: This DDL command is used to add the column to an existing table. Syntax: alter table <table name> add (fieldname 1 datatype(size),fieldname2 datatype(size)); (ii)Modify: It is used to modify column in already existing table.

Syntax: alter table <table name> modify ( column datatype(size)); (3)Drop: This DDL command is used to drop a table. Syntax: drop table <tablename>; (4)Truncate: This DDL command is used to delete data in the table. Syntax: truncate table <tablename>; (5) Rename: This DDL command is used to rename a table. Syntax: rename <oldname> to <newname>;

OUTPUT:
SQL> create table asz(regno number(5), name varchar(20)); Table created. SQL> desc asz; Name Null? Type ----------------------------------------- -------- ---------------------------REGNO NUMBER(5) NAME VARCHAR2(20) SQL> alter table asz add(dept varchar(10)); Table altered. SQL> desc asz; Name Null? Type ----------------------------------------- -------- ---------------------------REGNO NUMBER(5) NAME VARCHAR2(20) DEPT VARCHAR2(10) SQL> alter table asz modify(dept varchar(20)); Table altered. SQL> desc asz; Name Null? Type ----------------------------------------- -------- ---------------------------REGNO NUMBER(5) NAME VARCHAR2(20) DEPT VARCHAR2(20) SQL> rename asz to yoki; Table renamed. SQL> desc asz; ERROR: ORA-04043: object asz does not exist SQL> desc yoki; Name Null? Type ----------------------------------------- -------- ---------------------------REGNO NUMBER(5) NAME VARCHAR2(20) DEPT VARCHAR2(20)

SQL> truncate table yoki; Table truncated. SQL> drop table yoki; Table dropped.

RESULT:

DATA MANIPULATION LANGUAGE COMMANDS


AIM: To implement all DML commands (1)Insert (2) Select (3)Update (4)Delete
(1)Insert:

This DML command is used to insert the details into the table. Syntax: insert into tablename values (&field1, &field2, );
(2)Select:

This command is used to show the details present in a table. Syntax: select * from <table name >;
(3) Update:

This command is used to change a value of field in a row. Syntax: Update <tablename> set < field=new value> where <field=oldvalue>;
(4)Delete:

This command is used to delete a row in table. Syntax: Delete from <tablename> where< fieldname=value>;

OUTPUT:
SQL> insert into yoki values('&regno','&name','&grade'); Enter value for regno: 1000 Enter value for name: sri Enter value for grade: c old 1: insert into yoki values('&regno','&name','&grade') new 1: insert into yoki values('1000 ','sri','c') 1 row created. SQL> / Enter value for regno: 1001 Enter value for name: saj Enter value for grade: b old 1: insert into yoki values('&regno','&name','&grade') new 1: insert into yoki values('1001','saj','b') 1 row created. SQL> insert into yoki(regno,name,grade)values(1002,'yok','b'); 1 row created. SQL> insert into yoki values(1003,'dum','a'); 1 row created. SQL> select * from yoki; REGNO NAME GRADE ---------- ---------- ----1000 sri c 1001 saj b 1002 yok b 1003 dum a SQL> select * from yoki; REGNO NAME GRADE ---------- ---------- ----1000 sri c 1001 saj b 1002 yok b 1003 dum a SQL> select * from yoki where name = 'dum';

REGNO NAME GRADE ---------- ---------- ----1003 dum a SQL> update yoki set grade='a' where name='yok'; 1 row updated. SQL> select * from yoki; REGNO NAME GRADE ---------- ---------- ----1000 sri c 1001 saj b 1002 yok a 1003 dum a SQL> delete from yoki where grade='c'; 1 row deleted. SQL> select * from yoki; REGNO NAME GRADE ---------- ---------- ----1001 saj b 1002 yok a 1003 dum a

RESULT:

DATA CONTROL LANGUAGE


AIM: To implement all DCL commands :(i)Grant (ii)Revoke
(1)Grant:

This DCL command is used to grant privilege to other user. Syntax: Grant <privilege> on <tablename> to <user name>;
(2)Revoke:

It is used to revoke privileges from one to other user or new user. Syntax: revoke <privilege> on <tablename> from <username>;

Log on to ramya SQL> grant select on yoki to ramya; Grant succeeded. SQL> grant insert on yoki to ramya; Grant succeeded. SQL> grant all on yoki to ramya; Grant succeeded. SQL> select * from yoki; REGNO NAME GRADE ---------- ---------- ----1000 jjj a 1001 kkk b 1001 kkk b 1001 kkk b SQL> revoke select on yoki from ramya; Revoke succeeded. SQL> revoke insert on yoki from ramya; Revoke succeeded. SQL> revoke all on yoki from ramya; Revoke succeeded. Log on to scott SQL> select * from scott.yoki; select * from scott.yoki * ERROR at line 1: ORA-00942: table or view does not exist SQL> select * from scott.yoki;

REGNO NAME

GRADE

---------- ---------- ----1000 jjj a 1001 kkk b 1001 kkk b 1001 kkk b SQL> insert into scott.yoki values(1111,'fsef','a'); insert into scott.yoki values(1111,'fsef','a') * ERROR at line 1: ORA-01031: insufficient privileges SQL> insert into scott.yoki values(1111,'fsef','a'); 1 row created. SQL> select * from scott.yoki; REGNO NAME GRADE ---------- ---------- ----1000 jjj a 1001 kkk b 1001 kkk b 1001 kkk b 1111 fsef a SQL> select * from scott.yoki; REGNO NAME GRADE ---------- ---------- ----1000 jjj a 1001 kkk b 1001 kkk b 1001 kkk b 1111 fsef a SQL> update scott.yoki set grade='c' where grade='a'; 2 rows updated. SQL> select * from scott.yoki;

REGNO NAME GRADE ---------- ---------- ----1000 jjj c 1001 kkk b 1001 kkk b 1001 kkk b 1111 fsef c SQL> delete from scott.yoki where grade='c'; 2 rows deleted. SQL> select * from scott.yoki; REGNO NAME GRADE ---------- ---------- ----1001 kkk b 1001 kkk b 1001 kkk b SQL> select * from scott.yoki; select * from scott.yoki * ERROR at line 1: ORA-01031: insufficient privileges SQL> insert into scott.yoki values(1111,'sfs','a'); insert into scott.yoki values(1111,'sfs','a') * ERROR at line 1: ORA-01031: insufficient privileges SQL> insert into scott.yoki values(1111,'sfs','a'); insert into scott.yoki values(1111,'sfs','a') * ERROR at line 1: ORA-01031: insufficient privileges SQL> delete from scott.yoki where grade='b'; delete from scott.yoki where grade='b' * ERROR at line 1: ORA-00942: table or view does not exist

RESULT:

TRANSACTION CONTROL LANGUAGE


AIM: To implement all the TCL commands .They are : (i)Save point (ii)Commit (iii)Rollback.
(1)Savepoint:

It is used to set a mark on the table. Syntax: Savepoint <savepoint name>;


(2) Commit:

This TCL command is used to make changes permanent. Syntax: Commit:


(3)Rollback:

This TCL command is used to perform an undo operation. Syntax: Rollback to <savepoint name>;

SQL> select * from anandh; ENO -----1001 1002 1003 NAME AGE SALARY ---------------------- ---------- -----------arun 26 20000 thiyagu 25 18000 vinoth 29 21000

SQL> savepoint sp; Savepoint created. SQL>delete anandh where eno=1003; 1 row deleted. SQL> select * from anandh; ENO -----1001 1002 NAME AGE SALARY ---------------------- ---------- -----------arun 26 20000 thiyagu 25 18000

SQL> rollback to ss; Rollback complete. SQL> select * from anandh; ENO -----1001 1002 1003 NAME AGE SALARY ---------------------- ---------- -----------arun 26 20000 thiyagu 25 18000 vinoth 29 21000

SQL> commit; Commit complete.

RESULT:

SQL FUNCTIONS
AIM: To implement and execute the SQl functions. LIST OF COMMANDS: Built in command functions or predefined command functions. The following are built in functions: NUMERIC FUNCTIONS: Sqrt(): It is used to find the square root of n values. Syntax: select sqrt(n) from dual; Cos (): It is used to find the cosine value of n. Syntax: select cos(n) from dual; Sin(): It is used to find the sine value of n. Syntax: Select sin(n) from dual; power(): It is used to find power of n values. Syntax: Select power (n,n) from dual; CHARACTER FUNCTION: Initcap(): It is used to write string with first letter caps. Syntax: Select initcap(string) from dual; Lower(char(): It is used to write string in fully lower case letters.

Syntax: Select lower (string) from dual Upper(char): It is used to write fully in capitals. Syntax: select upper(string) from dual; ltrim(char,set): It trims the left hand side of the string. Syntax: select ltrim (character,string) from dual; Rtrim(char,set):
It is used to trim the right hand side set of the string.

Syntax: select rtrim(character,string) from dual; translate(char,from,to): It is used to translate given character to another character. Syntax: Select translate(char,from,to) from dual; Replace(char,searchstring,repstring): It is used to replace a string by changing the character in it. Syntax: select replace(char,searchstring,restring) from dual; substring(char,m,n): It is used to write a string in specified starting and encoding value. Syntax: Select substr(char,m,n) from dual;

DATE FUNCTION: sysdate(): It gives systems current date. Syntax:

select sysdate from dual; addmonths(): It is used to add months to the given date. Syntax: select addmonths(ddmmyy,n) from dual; monthsbetween(): It gives difference between the specified months. Syntax: Select months between(ddmmyy,ddmmyy) from dual; next_day(): It gives the next week date of present day. Syntax: Select next_day(ddmmyy,day) from dual; last_day(): It gives last date from given month. Syntax: select last_day(ddmmyy) from dual; CHARACTER FUNCTIONS: greatest: It is used to find the greatest of two strings. Syntax: select greatest(string1,string2) from dual; least: It finds least of the two strings. Syntax: Select least(string1,string2) from dual; CONVERSION FUNCTIONS: to_date(): It is used to find days date. Syntax: select to_date(ddmmyy,ddmmyy) from dual; AGGREGATE FUNCTIONS: count(): It produces the number of rows or non-null field values that the query selected. Syntax: select count <fieldname> from <tablename>;

average(): It produces the average of all the selected values of a given field. Syntax: select avg <fieldname> from <tablename>; maximum(): It produces largest of all selected values. Syntax: select max<fieldname> from <tablename>; minimum(): It produces the minimum of all the selected values of a field. Syntax: Select min<fieldname> from <tablename>;

SQL> select abs(55) from dual; ABS(55) ---------55 55 55 SQL> desc dual; Name Null? Type ----------------------------------------- -------- ---------------------------NAME VARCHAR2(10) DUMMY VARCHAR2(10) SQL> select * from dual; NAME DUMMY ---------- ---------priya 12 narmadha 23 nithya 24 SQL> select cos(4) from dual; COS(4) ----------.65364362 -.65364362 -.65364362 SQL> select sqrt(9) from dual; SQRT(9) ---------3 3 3 SQL> select cosh(4) from dual; COSH(4) ---------27.3082328 27.3082328 27.3082328 SQL> select exp(3) from dual;

EXP(3) ---------20.0855369 20.0855369 20.0855369 SQL> select power(3,3) from dual; POWER(3,3) ---------27 27 27 SQL> select sign(5) from dual; SIGN(5) ---------1 1 1 SQL> select ceil(5) from dual; CEIL(5) ---------5 5 5 SQL> select floor(5) from dual; FLOOR(5) ---------5 5 5 SQL> select ceil(5.7) from dual; CEIL(5.7) ---------6 6 6 SQL> select floor(5.7) from dual; FLOOR(5.7) ---------5

5 5 SQL> select ln(5) from dual; LN(5) ---------1.60943791 1.60943791 1.60943791 SQL> select sin(5) from dual; SIN(5) ----------.95892427 -.95892427 -.95892427 SQL> SQL> SQL> SQL> select concat('hello','how r u ?') from dual; CONCAT('HELLO' -------------hellohow r u ? hellohow r u ? hellohow r u ? SQL> select initcap('hellllllo') from dual; INITCAP(' --------Hellllllo Hellllllo Hellllllo SQL> select lower('HELLO') from dual; LOWER ----hello hello hello SQL> select upper('heyyyy') from dual; UPPER( -----HEYYYY

HEYYYY HEYYYY SQL> select lpad('hello',6,'&&&') from dual; LPAD(' -----&hello &hello &hello SQL> select lpad('hello',16,'*') from dual; LPAD('HELLO',16, ---------------***********hello ***********hello ***********hello SQL> select rpad('hello',16,'*') from dual; RPAD('HELLO',16, ---------------hello*********** hello*********** hello*********** SQL> select replace('hello','h','l') from dual; REPLA ----lello lello lello SQL> select substr('hello',1,3) from dual; SUB --hel hel hel SQL> select length('hello') from dual; LENGTH('HELLO') --------------5 5 5

SQL> select greatest('hello','hellooo') from dual; GREATES ------hellooo hellooo hellooo SQL> select least('hello','hellooo') from dual; LEAST ----hello hello hello SQL> select sysdate from dual; SYSDATE --------10-JUL-08 10-JUL-08 10-JUL-08 SQL> SQL> select add_months('07-JAN-89',3) from dual; ADD_MONTH --------07-APR-89 07-APR-89 07-APR-89 SQL> select months_between('07-JAN-89','07-DEC-89') from dual; MONTHS_BETWEEN('07-JAN-89','07-DEC-89') ---------------------------------------11 -11 -11

SQL> select last_day('07-FEB-89') from dual; LAST_DAY( --------28-FEB-89

28-FEB-89 28-FEB-89 SQL> select next_day('10-JUL-08','THU') from dual; NEXT_DAY( --------17-JUL-08 17-JUL-08 17-JUL-08 SQL> select to_date('29-DEC-50') from dual; TO_DATE(' --------29-DEC-50 29-DEC-50 29-DEC-50

SQL> select * from yoki; REGNO NAME GRADE ---------- ---------- ----1111 fdfd a 1111 fdfd a 1111 fdfd a 1111 fdfd a SQL> insert into yoki values(1111,'ojyy','c'); 1 row created. SQL> / 1 row created. SQL> / 1 row created. SQL> / 1 row created. SQL> insert into yoki values(1222,'fsss','b'); 1 row created. SQL> /

1 row created. SQL> / 1 row created. SQL> / 1 row created. SQL> / 1 row created. SQL> select * from yoki; REGNO NAME GRADE ---------- ---------- ----1111 fdfd a 1111 fdfd a 1111 fdfd a 1111 fdfd a 1111 ojyy c 1111 ojyy c 1111 ojyy c 1111 ojyy c 1222 fsss b 1222 fsss b 1222 fsss b REGNO NAME GRADE ---------- ---------- ----1222 fsss b 1222 fsss b 13 rows selected. SQL> select min(GRADE) from yoki; MIN(G ----a SQL> select max(GRADE) from yoki; MAX(G -----

c SQL> select avg(REGNO) from yoki; AVG(REGNO) ---------1153.69231 SQL> select stddev(REGNO) from yoki; STDDEV(REGNO) ------------56.2070349 SQL> select count(2) from yoki; COUNT(2) ---------13 SQL> select count(1) from yoki; COUNT(1) ---------13 SQL> select count(GRADE) from yoki; COUNT(GRADE) -----------13

SQL> select sum(REGNO) from yoki; SUM(REGNO) ---------14998 SQL> select max(NAME) from yoki; MAX(NAME) ---------ojyy SQL> insert into yoki values(0,'z','z'); 1 row created.

SQL> select max(GRADE) from yoki; MAX(G ----z SQL> select min(REGNO) from yoki; MIN(REGNO) ---------0

RESULT:

CONSTRAINTS
AIM: To implement all the constraints on table created. CONSTRAINTS: It restricts the data value that can be inserted or deleted into the database or restricted, enforced on the values of an attribute. TYPES: *Domain Integrity Constraints *Entity Integrity Constraints *Referential Integrity Constraints DOMAIN INTEGRITY CONSTRAINTS: (1)notnull: If this constraint is enforced to a particular field, the field should contain a value compulsorily. Syntax: Create table <tablename>(fieldname datatype(size) <notnull>); (2)check: It sets the condition that the column must be satisfied to exist in the table. Syntax: Create table <tablename>(fieldname datatype(size) check <condition>); (3)unique: It is used to set a condition that the column must be unique in the table. Syntax: Create table <tablename>(fieldname datatype(size) constraint <constraintname> unique); (4)default: It is used to set a default value to the table. Syntax: Create table <tablename>(fieldname1 datatype(size) default=value);

ENTITY INTEGRITY CONSTRAINT: Primary key: It is one or more column in a table used to identify each row in the table uniquely.once it is defined, the value must be both not null and unique. Syntax: Create table <tablename>(<fieldname> datatype(size) primary key); REFERENTIAL INTEGRITY CONSTRAINT: Foreign key: It represents relationship between two tables. A foreign key column whose value has derived from the primary key of the other table. Every legal value in the foreign key column must match the same value that appears in primary key. Syntax: Create table <tablename1>(fieldname1 datatype(size) primary key,fieldname2 datatype(size)); Create table <tablename2> (fieldname1 datatype(size) primary key, fieldname2 datatype(size) references <tablename1> (fieldname));

SQL> create table emp7(ename varchar(10),eid number(10)not null); Table created. SQL> desc emp7; Name Null? Type ----------------------------------------- -------- ---------------------------ENAME VARCHAR2(10) EID NOT NULL NUMBER(10) SQL> insert into emp7 values('&ename','&eid'); Enter value for ename: feifh Enter value for eid: old 1: insert into emp7 values('&ename','&eid') new 1: insert into emp7 values('feifh','') insert into emp7 values('feifh','') * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."EMP7"."EID") SQL> / Enter value for ename: fdfd Enter value for eid: 22 old 1: insert into emp7 values('&ename','&eid') new 1: insert into emp7 values('fdfd','22') 1 row created. SQL> select * from emp7; ENAME EID ---------- ---------fdfd 22 SQL> drop table emp7; Table dropped. SQL> create table emp7(name varchar(10), id number(10), check(id<1000)); Table created. SQL> insert into emp7 values('&name','&id'); Enter value for name: hfdjhf Enter value for id: 222 old 1: insert into emp7 values('&name','&id') new 1: insert into emp7 values('hfdjhf','222') 1 row created.

SQL> / Enter value for name: fsdfd Enter value for id: 2222 old 1: insert into emp7 values('&name','&id') new 1: insert into emp7 values('fsdfd','2222') insert into emp7 values('fsdfd','2222') * ERROR at line 1: ORA-02290: check constraint (SCOTT.SYS_C007748) violated SQL> drop table emp7; Table dropped. SQL> SQL> create table emp7(name varchar(10), id number(10) primary key); Table created. SQL> insert into emp7 values('&name','&id'); Enter value for name: fefe Enter value for id: 111 old 1: insert into emp7 values('&name','&id') new 1: insert into emp7 values('fefe','111') 1 row created. SQL> / Enter value for name: fefe Enter value for id: 33 old 1: insert into emp7 values('&name','&id') new 1: insert into emp7 values('fefe','33') 1 row created. SQL> / Enter value for name: gdfgdf Enter value for id: 33 old 1: insert into emp7 values('&name','&id') new 1: insert into emp7 values('gdfgdf','33') insert into emp7 values('gdfgdf','33') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.SYS_C007753) violated SQL> drop table emp7;

Table dropped. SQL> create table emp7(name varchar(10), id number(10) primary key); Table created. SQL> insert into emp7 values('&name','&id'); Enter value for name: hell Enter value for id: 1234 old 1: insert into emp7 values('&name','&id') new 1: insert into emp7 values('hell','1234') 1 row created. SQL> / Enter value for name: treegb Enter value for id: 4321 old 1: insert into emp7 values('&name','&id') new 1: insert into emp7 values('tree','4321') 1 row created. SQL> create table emp777(name varchar(10), salary number(7), id references emp7(id)); Table created. SQL> insert into emp777 values('&name','&salary','&id'); Enter value for name: gfdf Enter value for salary: 22222 Enter value for id: 22 old 1: insert into emp777 values('&name','&salary','&id') new 1: insert into emp777 values('gfdf','22222','22') insert into emp777 values('gfdf','22222','22') * ERROR at line 1: ORA-02291: integrity constraint (SCOTT.SYS_C007756) violated - parent key not found SQL> / Enter value for name: fdfd Enter value for salary: 222222 Enter value for id: 1234 old 1: insert into emp777 values('&name','&salary','&id') new 1: insert into emp777 values('fdfd','222222','1234') 1 row created. SQL> / Enter value for name: fefef

Enter value for salary: 6543232 Enter value for id: 4321 old 1: insert into emp777 values('&name','&salary','&id') new 1: insert into emp777 values('fefef','6543232','4321') 1 row created. SQL> desc emp777; Name Null? Type ----------------------------------------- -------- ---------------------------NAME VARCHAR2(10) SALARY NUMBER(7) ID NUMBER(10) SQL> select * from emp777; NAME SALARY ID ---------- ---------- ---------fdfd 222222 1234 fefef 6543232 4321 SQL> insert into emp7 values('dsd','111'); 1 row created. SQL> insert into emp777 values('&name','&salary','&id'); Enter value for name: fdfdd Enter value for salary: 422222 Enter value for id: 111 old 1: insert into emp777 values('&name','&salary','&id') new 1: insert into emp777 values('fdfdd','422222','111') 1 row created. SQL> SQL> create table emp07(name varchar(10),roll_no number(5) unique); Table created. SQL> desc emp07; Name Null? Type ----------------------------------------- -------- ---------------------------NAME VARCHAR2(10) ROLL_NO NUMBER(5) SQL> insert into emp07 values('&name','&roll_no'); Enter value for name: a Enter value for roll_no: 1 old 1: insert into emp07 values('&name','&roll_no') new 1: insert into emp07 values('a','1')

1 row created. SQL> / Enter value for name: a Enter value for roll_no: 1 old 1: insert into emp07 values('&name','&roll_no') new 1: insert into emp07 values('a','1') insert into emp07 values('a','1') * ERROR at line 1: ORA-00001: unique constraint (GROUP1.SYS_C002783) violated SQL> create table emp077(rollno number(10), dept varchar(10), college varchar(10) default('SREC')); Table created. SQL> insert into emp077 values(1,'cse','srr'); 1 row created. SQL> / 1 row created. SQL> / 1 row created. SQL> select * from emp077;

ROLLNO DEPT COLLEGE ---------- ---------- ---------1 cse srr 1 cse srr 1 cse srr SQL> insert into emp077 (rollno,dept)values(2,'eee'); 1 row created. SQL> select * from emp077; ROLLNO DEPT COLLEGE ---------- ---------- ---------1 cse srr 1 cse srr

1 cse 2 eee

srr SREC

RESULT:

JOINS

SQL> create table sj(name varchar2(20), sno number(5) ,eno number(10)); Table created. SQL>insert into sj values('dinesh','5','5'); 1 row created. SQL>insert into sj values('deepu','2',2'); 1 row created. SQL>insert into sj values('avi','5','3'); 1 row created. SQL>select * from sj; NAME dinesh deepu avi SNO 5 2 5 ENO 5 2 3

SQL> create table s00(name varchar2(20), dept varchar2(20)); Table created. SQL>insert into s00 values('dinesh,it'); 1 row created. SQL>insert into s00 values('avi','it'); 1 row created.

SQL>insert into s00 values('ash','cse'); 1 row created. SQL>select * from s00; NAME dinesh avi ash DEPT it it cse

SQL> create table s01(name varchar2(20), dept varchar2(20)); Table created. SQL>insert into s01 values('dilli,it'); 1 row created. SQL>insert into s01 values('karti','cse'); 1 row created. SQL>select * from s01; NAME dilli karti DEPT it cse

SQL> create table s02(name varchar2(20), regno number(5)); Table created. SQL>insert into s02 values('ash',12); 1 row created. SQL>insert into s02 values('dinesh','23'); 1 row created.

SQL>select * from s02; NAME ash dinesh REGNO 12 23

SQL> create table s03( regno number(5),dept varchar2(20),cno number(15)); Table created. SQL>insert into s03 values(23,it,9884702485); 1 row created. SQL>insert into s03 values(25,eie,9790781311); 1 row created. SQL>select * from s03; REGNO 23 25 DEPT it eie CNO

9884702485 9790781311

1.SELF JOIN: SQL>select sj.name , sj.sno , sj.eno from sj where sj.sno=sj.eno; NAME dinesh deepu SNO 5 2 ENO 5 2

2.EQUI JOIN: SQL>select s00.name , s01.name ,s00.dept from s00 , s01 where s00.dept=s01.dept; NAME NAME DEPT dinesh dilli avi dilli it it

ash

karti

cse

3.NON-EQUI JOIN: SQL>select s00.name,s01.name,s00.dept from s00,s01 where s00.dept<>s01.dept; NAME dinesh avi ash NAME karti karti dilli DEPT it it cse

4.a) LEFT OUTER JOIN: SQL>select s02.name , s02.regno , s03.dept , s03.cno from s02,s03 where s02.regno(+)=s03.regno; NAME dinesh REGNO 23 DEPT it eie CNO 9884702485 9790781311

4.b) RIGHT OUTER JOIN: SQL>select s02.name , s02.regno , s03.dept , s03.cno from s02,s03 where s02.regno=s03.regno(+); NAME ash dinesh REGNO 12 23 it DEPT CNO

9884702485

RESULT:

SET OPERATORS
AIM: To combine queries into one result using set operators. SET OPERATORS: These combine the result of two or more queries into one result. The following are set operators: (1)Union (2)Union all (3)Intersect (4)Minus or set difference UNION: It merges the output of two or more queries into single set of rows and columns.The output could have only distinct rows even if the table columns N entries of same value that query return.It is also in the case if it is return by query and so. Syntax: select*from <tablename 1> union select*from <tablename 2>; UNION ALL: It returns all rows selected by query including duplicate values.If a table contains n entries of same value and another table contains m entries, then query union all results in (n+m) entries of same value. Syntax: select*from <table1>union all select*from<table2>; INTERSECT: Only the common names from same field name will be displayed.Eliminates duplicate values. Syntax: select*from<table1> intersect select*from<table2>; MINUS: It returns all distinct rows only by the first query and by second query. Syntax: select*from <table1>minus select*from<table2>;

SQL> create table s00(name varchar2(20), dept varchar2(20)); Table created. SQL>insert into s00 values('dinesh,it'); 1 row created. SQL>insert into s00 values('avi','it'); 1 row created. SQL>insert into s00(name,dept) values('ash','cse'); 1 row created. SQL>select * from s00; NAME dinesh avi ash DEPT it it cse

SQL> create table s01(name varchar2(20), dept varchar2(20)); Table created. SQL>insert into s01 values('dilli,it'); 1 row created. SQL>insert into s01 values('karti','cse'); 1 row created. SQL>insert into s01 values('dinesh','it'); 1 row created. SQL>select * from s01;

NAME

DEPT

dilli it karti cse dinesh it 1.UNION: SQL>select * from s00 union select * from s01; NAME ash avi dilli dinesh karti DEPT cse it it it cse

2.UNION ALL: SQL>select * from s00 union all select * from s01; NAME dinesh dinesh avi ash dilli karti DEPT it it it cse it cse

3.INTERSECT: SQL>select * from s00 intersect select * from s01; NAME dinesh it DEPT

4.MINUS:

SQL>select * from s00 minus select * from s01; NAME ash avi DEPT cse it

RESULT:

VIEWS
AIM: To implement all types of views in tables. VIEW: It is similar to a table because it contains rows and columns of data. Types: (1)Grouped view (2)Joined view (3)Read-only view (4)Force view (5)With check option Syntax: Create view <viewname> as select * from <tablename> where fieldname=value; (1)Grouped View: This is used to create a view grouped by some field. Syntax: Create view <viewname> as fieldname from <tablename> groupby <fieldname>; (2)Joined View: This is used to create a joined view. Syntax: Create view <viewname> as select <fieldname1,fieldname2> from <table1,table2> where table1.fieldname1=table2.fieldname2; (3)Read-only view: This is used to create a view with read-only option. Syntax: Create view <viewname> select * from table where fieldname1=value with readonly; (4)Force view: This allows us to create a view from a non-existing table. Syntax: Create force view <viewname> as select * from <non-existing table>; (5)With check option: This is used to create a view that specifies a constraint check. Syntax: Create view <viewname> as select <fieldname> from <tablename> where fieldname=value with check option;

SQL> insert into yo1 values('&regno','&name','&dept'); Enter value for regno: 1 Enter value for name: a Enter value for dept: cse old 1: insert into yo1 values('&regno','&name','&dept') new 1: insert into yo1 values('1','a','cse') 1 row created. SQL> / Enter value for regno: 2 Enter value for name: b Enter value for dept: it old 1: insert into yo1 values('&regno','&name','&dept') new 1: insert into yo1 values('2','b','it') 1 row created. SQL> / Enter value for regno: 3 Enter value for name: c Enter value for dept: ece old 1: insert into yo1 values('&regno','&name','&dept') new 1: insert into yo1 values('3','c','ece') 1 row created. SQL> / Enter value for regno: 4 Enter value for name: d Enter value for dept: eee old 1: insert into yo1 values('&regno','&name','&dept') new 1: insert into yo1 values('4','d','eee') 1 row created. SQL> select * from yo1; REGNO NAME DEPT ---------- -------------------- ---------1a cse 2b it 3c ece 4d eee SQL> create view yoview as select * from yo1 where 2 SQL> insert into yo1 values('&regno','&name','&dept'); Enter value for regno: 1 Enter value for name: e

Enter value for dept: at old 1: insert into yo1 values('&regno','&name','&dept') new 1: insert into yo1 values('1','e','at') 1 row created. SQL> / Enter value for regno: 2 Enter value for name: f Enter value for dept: mech old 1: insert into yo1 values('&regno','&name','&dept') new 1: insert into yo1 values('2','f','mech') 1 row created. SQL> / Enter value for regno: 3 Enter value for name: g Enter value for dept: bio old 1: insert into yo1 values('&regno','&name','&dept') new 1: insert into yo1 values('3','g','bio') 1 row created. SQL> select * from yo1; REGNO NAME DEPT ---------- -------------------- ---------1a cse 2b it 3c ece 4d eee 1e at 2f mech 3g bio 7 rows selected. SQL> create view yoview as select * from yo1 where regno='1'; View created. SQL> select * from yoview;

REGNO NAME DEPT ---------- -------------------- ---------1a cse 1e at

SQL> create view yoview1 as select name, dept from yo1 group by name; create view yoview1 as select name, dept from yo1 group by name * ERROR at line 1: ORA-00979: not a GROUP BY expression SQL> create view yoview1 as select name from yo1 group by name; View created. SQL> select * from yoview1; NAME -------------------a b c d e f g 7 rows selected. SQL> select *from yoview2; select *from yoview2 * ERROR at line 1: ORA-01722: invalid number SQL> select *from yoview; REGNO NAME DEPT ---------- -------------------- ---------1a cse 1e at SQL> create view yoview3 as select name, sum(dept) as dept from yo1 group by name; View created. SQL> select * from yoview3; select * from yoview3 * ERROR at line 1: ORA-01722: invalid number SQL> create view testview as select name, sum(regno) as regno from yo1 group by name;

View created. SQL> select * from testview; NAME REGNO -------------------- ---------a 1 b 2 c 3 d 4 e 1 f 2 g 3 7 rows selected. SQL> create view testview2 as select name, avg(regno) as regno from yo1 group by name; View created. SQL> select * from testview2; NAME REGNO -------------------- ---------a 1 b 2 c 3 d 4 e 1 f 2 g 3 7 rows selected. SQL> select * from yo2;

REGNO NAME DEPT ---------- -------------------- ---------4z ato 5 zx chem 6 zxc bio 1y ert 3 dd ddd SQL> drop table yo2;

Table dropped. SQL> create table yo2 (name varchar(10), year number(1)); Table created. SQL> insert into yo2 values('&name','&year'); Enter value for name: a Enter value for year: 1 old 1: insert into yo2 values('&name','&year') new 1: insert into yo2 values('a','1') 1 row created. SQL> / Enter value for name: b Enter value for year: 1 old 1: insert into yo2 values('&name','&year') new 1: insert into yo2 values('b','1') 1 row created. SQL> / Enter value for name: c Enter value for year: 2 old 1: insert into yo2 values('&name','&year') new 1: insert into yo2 values('c','2') 1 row created. SQL> / Enter value for name: d Enter value for year: 3 old 1: insert into yo2 values('&name','&year') new 1: insert into yo2 values('d','3') 1 row created. SQL> / Enter value for name: e Enter value for year: 4 old 1: insert into yo2 values('&name','&year') new 1: insert into yo2 values('e','4') 1 row created. SQL> / Enter value for name: f Enter value for year: 4 old 1: insert into yo2 values('&name','&year') new 1: insert into yo2 values('f','4')

1 row created. SQL> / Enter value for name: g Enter value for year: 3 old 1: insert into yo2 values('&name','&year') new 1: insert into yo2 values('g','3') 1 row created. SQL> select * from yo2; NAME YEAR ---------- ---------a 1 b 1 c 2 d 3 e 4 f 4 g 3 7 rows selected. SQL> create view jview as select name,dept,year from yo1,yo2 where yo1.name=yo2.name; create view jview as select name,dept,year from yo1,yo2 where yo1.name=yo2.name * ERROR at line 1: ORA-00918: column ambiguously defined SQL> create view jview as select yo1.name,yo1.dept,yo2.year from yo1,yo2 where yo1.name=yo2.name; View created. SQL> select * from jview; NAME DEPT YEAR -------------------- ---------- ---------a cse 1 b it 1 c ece 2 d eee 3 e at 4 f mech 4 g bio 3 7 rows selected. SQL> create view roview as select * from yo1 where regno='1' with read only;

View created. SQL> select * from roview; REGNO NAME DEPT ---------- -------------------- ---------1a cse 1e at SQL> update roview set name='b' where name='a'; update roview set name='b' where name='a' * ERROR at line 1: ORA-01733: virtual column not allowed here SQL> create force view forceview as select * from math; Warning: View created with compilation errors. SQL> select * from forceview; select * from forceview * ERROR at line 1: ORA-04063: view "GROUP1.FORCEVIEW" has errors SQL> create view checkview as select * from yo2 where year>1 with check option; View created. ` SQL> select * from checkview; NAME YEAR ---------- ---------c 2 d 3 e 4 f 4 g 3

RESULT:

PL/SQL PROGRAM
AIM: To study and implement PL/SQL and to write simple programs. STRUCTURE OF PL/SQL: declare <<declar active statements>>; begin <<executable statements>>; exception <<exception handling>>; end;

FIBONACCI SERIES
AIM: To study and implement PL/SQL to write a simple program of Fibonacci series.

ALGORITHM: *Initialise a,b,c,n where a=-1,b=1,c=0. *Usen for forloop *Add a,b and store it in c. *Exchange b to a and c to b. *Get output from c.

FIBONACCI SERIES PROGRAM : declare a number(3); b number(3); c number(3); n number(3); begin n:=&n; c:=0; b:=1; a:=-1; for i in 1..n loop c:=a+b; a:=b; b:=c; dbms_output.put_line (c); end loop; end;

OUTPUT:

enter value for n : 5 0 1 1 2 3

RESULT:

ARITHMETIC OPERATIONS
AIM: To study and implement the PL/SQL to write a program on arithmetic operations. ALGORITHM: *Declare a,b,c,d,e,f,g. *Get input of a and b. *Do arithmetic operations between a and b. *And store it in c,d,e,f,g. *Display output.

PROGRAM: declare a number(10); b number(10); c number(10); d number(10); e number(10); f number(10); g number(10); begin a:=&a; b:=&b; c:=a+b; d:=a-b; e:=a*b; f:=a/b; g:=a mod b; dbms_output.put_line(addition of a and b is||c); dbms_output.put_line(subtraction of a and b is||d); dbms_output.put_line(multiplication of a and b is||e); dbms_output.put_line(quotient of a and b is||f); dbms_output.put_line(modulus of a and b is||g); end;

OUTPUT: enter value for a : 2 enter value for b : 5 addition of a and b is 7 subtraction of a and b is -3 multiplication of a and b is 10 quotient of a and b is 0 modulus of a and b is 2

RESULT:

EMPLOYEE DETAILS
AIM: To study and implementPL/SQL to write a program of employee details. ALGORITHM: * Declare no,name,salary,bonus in employee table. *Select employee no. *Bonus is calculated as salary*0.003 *Update bonus in employee table. *Display output.

SQL>set serveroutput on; SQL>declare no emp00.empno%type; name emp00.ename%type; salary emp00.esalary%type; bon emp00.bonus%type; begin select empno,ename,esalary from emp00 where empno=27; bon:=salary * (0.03); update emp00 set bonus = bon where empno=27; dbms_output.put_line(empno=||no); dbms_output.put_line(ename=||name); dbms_output.put_line(esalary=||salary); dbms_output.put_line(bonus=||bon); end;

SQL>create table ad(no number(10),name varchar2(20),salary number(10),bonus number(10)); Table created. SQL>insert into ad values(&no,&name,&salary,&bonus); Enter value for no:9 Enter value for name:dinesh Enter value for salary:20000 Enter value for bonus:10000 1 row created. SQL>/ Enter value for no:27 Enter value for name:amitab Enter value for salary:40000 Enter value for bonus:20000 1 row created. SQL>select * from ad; NO 9 27 NAME dinesh amitab SALARY BONUS 20000 10000 40000 20000

no=27

name=amitab salary=40000 bonus=1200 SQL>select * from ad; NO 9 27 NAME dinesh amitab SALARY BONUS 20000 10000 40000 1200

RESULT:

TRIGGERS
AIM: To write a trigger for database model. TRIGGER: It is a statement that system executes automatically as a slide effect of modification to the database,a database trigger is a stored procedure that is associated with a table. Syntax: Create or replace trigger[triggername] [before/after][insert/update/delete]on <table name> [for each statement or row] [when <condition>] Types: Before After For each row For each statement

TRIGGER
AIM: To create a trigger to convert lowercase into uppercase in a table. ALGORITHM: *Start the program. *Create a trigger. *Using formula:newname:=Upper(:new name) *Convert inserted lowercase value into uppercase value in table. *Stop the program.

SQL>create or replace trigger s before delete or insert on qaz begin if to_char (sysdate,dy)=thu then raise_application_error(-20021,no insert or delete on thu); end if; end;

SQL> create table din (regno number(5),name varchar(10),dept varchar(6),address varchar(10)); Table created. SQL> insert into din values('102','kiran','it','paris'); 1 row created. SQL> insert into din values('103','hari','ece','trichy'); 1 row created. SQL> select * from din; REGNO NAME 102 103 kiran hari DEPT ADDRESS it paris ece trichy

Trigger created. SQL> insert into din values('104','ashok','ece','lucknow'); Error during execution of trigger 3IT09.SH

RESULT:

TRIGGER
AIM: To create a trigger to generate an error message. ALGORITHM: * Start a program. * Create a trigger. *If today is thursday then raise error message on insert/delete. *End if statement. *Stop the program.

SQL>create trigger sh before insert or update of name on qaz for each row begin :new.name:=UPPER(:new.name); end; /

SQL> create table din (regno number(5),name varchar(10),dept varchar(6),address varchar(10)); Table created. SQL> insert into din values('102','kiran','it','paris'); 1 row created. SQL> insert into din values('103','hari','ece','trichy'); 1 row created. SQL> select * from din; REGNO NAME 102 103 kiran hari DEPT ADDRESS it paris ece trichy

SQL> insert into din values('104','ashok','ece','lucknow'); 1 row created. SQL> select * from din; REGNO NAME 102 103 104 kiran hari ashok DEPT ADDRESS it paris ece trichy ece lucknow

RESULT:

CURSOR
AIM: To implement high level language extensions using cursors in PL/SQL using oracle 9: CURSOR: It is one area where we can bring the records from a database, manipulate the records and update back to the database. TYPES: IMPLICIT CURSOR: SQL Y. found,SQL % not found SQLY is open,SQL%row count EXPLICIT CURSOR: %found, %not found % is open,SQL%row count. OPENING CURSOR: Opening a cursor executes the selective statements and identifies the active set. s open <cursor name>-curs; FLETCHING A CURSOR: It retrieves each row in the active set. Fletch <cursor name>-cursor into <variable1,variable2..variable n> CLOSING A CURSOR: Close statement disables the cursors. Close <cursor-name>-cursors;

CURSORS
AIM: To write a PL/SQL program to open a cursor and get student details. ALGORITHM: * Start the program. * Declare the variable,regno,name,dept,address. * Using cursor get the value of attributes of student details table and store in variables . * Open the cursor. * Using for loop check the condition c1% not found. If the condition is true exit from the loop and close cursor.

SQL>set serveroutput on; SQL> declare no cp1.regno%type; sname cp1.name%type; dep cp1.dept%type; adr cp1.address%type; cursor cl is select regno,name,dept,address into no,sname,dep,adr from cp1; begin open cl; dbms_output.put_line('regno name dept address'); loop fetch cl into no,sname,dep,adr; exit when cl% notfound; dbms_output.put_line(no||' '||sname||' '||dep||' '||adr); end loop; close cl; end;

SQL> create table cp1 (regno number(5),name varchar(10),dept varchar(6),address varchar(10)); Table created. SQL> insert into cp1 values('102','lala','it','pandy'); 1 row created. SQL> insert into cp1 values('103','kaka','ece','trichy'); 1 row created. SQL> select * from cp1; REGNO NAME 102 103 lala kaka DEPT ADDRESS it paris ece trichy

regno name dept address 102 lala it paris 103 kaka ece trichy PL/SQL procedure successfully completed

RESULT:

CURSORS
AIM: To write a PL/SQL program to find the total and percentage of student using cursor. ALGORITHM: * Start the program. * Declare the variables no,sub1,sub2,name. * Using cursor get the value of attributes of student table and store them in variables * Using for loop check the condition c1% not found. If true exit the loop. * Find total and percentage and point values. * Close the cursor. * Stop the program.

SQL>set serveroutput on; SQL> declare no cp2.no%type; name cp2.name%type; sub1 cp2.sub1%type; sub2 cp2.sub2%type; total number(10); per number(10); cursor cl is select no,name,sub1,sub2 into no,name,sub1,sub2 from cp2; begin open cl; dbms_output.put_line('no name sub1 sub2 total per'); loop fetch cl into no,name,sub1,sub2; exit when cl% notfound; total:=sub1+sub2; per:=(total/2); dbms_output.put_line(no||' '||name||' '||sub1||' '||sub2||' '||total||' ' ||per); end loop; close cl; end;

SQL> create table cp2 (no number(4),name varchar(10),sub1 number(4),sub2 number(5)); Table created. SQL> insert into cp2 values('115','lavanya','98','89'); 1 row created. SQL> insert into cp2 values('116','sankari','90','80'); 1 row created. SQL> select * from cp2; NO NAME 115 lavanya 116 sankari SUB1 98 90 SUB2 89 80

dbms_output.put_line(no||' '||name||' '||sub1||' '||sub2||' '||total||' ' ||per); end loop; close cl; end; no name sub1 sub2 total per 115 lavanya 98 89 187 94 116 sankari 90 80 170 85 PL/SQL procedure successfully completed.

RESULT:

PROCEDURES
AIM: To implement procedures using PL/SQL. PROCEDURE: A procedure is a stored sub-program that performs a specific function. Syntax: Create or replace procedure <procedure name> (parameter-1,parameter-2) [local declarations] Begin <<executable statements>> Exception <<exception handling>> End;

SQL>set serveroutput on; SQL> create or replace procedure proc(no number) is num c.sno%type; m1 c.mark1%type; m2 c.mark2%type; p number(10); begin select sno,mark1,mark2 into num,m1,m2 from c where sno=no; p:=(m1+m2)/2; update c set percent=p where sno=no; dbms_output.put_line(stud no:||num); dbms_output.put_line(percentage:||p); dbms_output.put_line(Table updated); end proc;

SQL> create table c(sno number(5),mark1 number(10),mark2 number(10),percent number(10)); Table created. SQL> insert into c values('9','52','52',''); 1 row created. SQL> insert into c values('27','95','95','); 1 row created. SQL> insert into c values('8',65',21,); 1 row created. SQL> select * from c; SNO MARK1 9 27 8 52 95 65 MARK2 PERCENT 52 95 21

/ Procedure created. SQL>exec proc(27); stud no:27 percentage:95 Table updated. PL/SQL procedure successfully completed. SQL>select * from c; SNO MARK1 9 27 8 RESULT: 52 95 65 MARK2 PERCENT 52 95 21 95

FUNCTIONS
AIM: To implement functions using PL/SQL. FUNCTION: It is stored sub-program that computes a value. Syntax: Create or replace function <function-name>(parameter1,parameter2) is returntype is [local declarations] Begin <<executable statements>> Exception <<exception handling>> End;

SQL>set serveroutput on; SQL> create or replace function fun1(no number) return number is num c.sno%type; m1 c.mark1%type; m2 c.mark2%type; p number; begin select sno,mark1,mark2 into num,m1,m2 from c where sno=no; p:=(m1+m2)/2; return p; end fun1;

SQL> create table c(sno number(5),mark1 number(10),mark2 number(10)); Table created. SQL> insert into c values('9','52','52'); 1 row created. SQL> insert into c values('27','95','95'); 1 row created. SQL> insert into c values('8',65',21); 1 row created. SQL> select * from c; SNO MARK1 9 27 8 52 95 65 MARK2 52 95 21

/ Function created. SQL>select fun1(8) from c; FUN1(8) 65 21

RESULT:

NORMALIZATION
Normalization is the process of efficiently organizing data in a database. There are two goals of the normalization process: eliminating redundant data (for example, storing the same data in more than one table) and ensuring data dependencies make sense (only storing related data in a table). Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored.

The Normal Forms


The database community has developed a series of guidelines for ensuring that databases are normalized. These are referred to as normal forms and are numbered from one (the lowest form of normalization, referred to as first normal form or 1NF) through five (fifth normal form or 5NF). In practical applications, you'll often see 1NF, 2NF, and 3NF along with the occasional 4NF. Fifth normal form is very rarely seen and won't be discussed in this article. Before we begin our discussion of the normal forms, it's important to point out that they are guidelines and guidelines only. Occasionally, it becomes necessary to stray from them to meet practical business requirements. However, when variations take place, it's extremely important to evaluate any possible ramifications they could have on your system and account for possible inconsistencies. That said, let's explore the normal forms.

First Normal Form (1NF)


First normal form (1NF) sets the very basic rules for an organized database:

Eliminate duplicative columns from the same table. Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).

Second Normal Form (2NF)


Second normal form (2NF) further addresses the concept of removing duplicative data:

Meet all the requirements of the first normal form. Remove subsets of data that apply to multiple rows of a table and place them in separate tables. Create relationships between these new tables and their predecessors through the use of foreign keys.

Third Normal Form (3NF)


Third normal form (3NF) goes one large step further:

Meet all the requirements of the second normal form.

Remove columns that are not dependent upon the primary key.

Fourth Normal Form (4NF)


Finally, fourth normal form (4NF) has one additional requirement:

Meet all the requirements of the third normal form. A relation is in 4NF if it has no multi-valued dependencies.

Remember, these normalization guidelines are cumulative. For a database to be in 2NF, it must first fulfill all the criteria of a 1NF database.

Rule 1: Eliminate Repeating Groups. Make a separate table for each set of related attributes, and give each table a primary key. Unnormalized Data Items for Puppies

puppy number puppy name kennel code kennel name kennel location trick ID trick name trick where learned skill level

In the original list of data, each puppy description is followed by a list of tricks the puppy has learned. Some might know 10 tricks, some might not know any. To answer the question Can Fifi roll over? we need first to find Fifis puppy record, then scan the list of tricks associated with the record.This is awkward, inefficient, and extremely untidy. Moving the tricks into a seperate tablehelps considerably. Seperating the repeating groupsof tricks from the puppy information results in first normal form. The puppy number in the trick table matches the primarykey in the puppy table, providing a foreign key for relating the two tables with a join operation. Now we can answer our question with a direct retrieval look to see if Fifis puppy number and the trick ID for roll over appear together in the trick table. First Normal Form: Puppy Table puppy number puppy name kennel name kennel location primary key

Trick Table puppy number trick ID trick name trick where learned skill level Rules of Normalization II Rule 2: Eliminate Redundant Data, if an attribute depends on only part of a multi-valued key, remove it to a separate table. The trick name (e.g. roll over) appears redundantly for every puppy that knows it. Just trick ID whould do. TRICK TABLE Puppy Number Trick ID Trick Name Where Learned Skill Level 52 27 roll over 16 9 53 16 Nose Stand 9 9 54 27 roll over 9 5 *Note that trick name depends on only a part (the trick ID) of the multi-valued, i.e. composite key. In the trick table, the primary key is made up of the puppy number and trick ID. This makes sense for the where learned and skill level attributes, since they will be different for every puppy-trick combination. But the trick name depends only on the trick ID. The same name will appear redundantly every time its associated ID appears in the trick table. Second Normal Form puppy table puppy number puppy name kennel code kennel name kennel location tricks table tricks ID tricks name Puppy-Tricks puppy number trick ID trick where learned skill level

Suppose you want to reclassify a trick, i.e. to give it a different trick ID. The change has to be made for every puppy that knows the trick. If you miss some of the changes, you will have several puppies with the same trick under different IDs, this is an update anomaly. Rules of Normalization III Rule 3: Eliminate columns not dependent on key. If attributes do not contribute to a description of the key, remove them to a separate table. Puppy Table puppy number puppy name kennel code kennel name The puppy table satisfies the first normal form, since in contains no repeating groups. It satisfies the second normal form, since it does not have a multivalued key. But the key is puppy number , and the kennel name and the kennel location describe only a kennel, not a puppy. To achieve the third normal form, they must be moved into a separate table. Since they describe a kennel, kennel code becomes the key of the new kennels table. Third Normal Form Puppies puppy number puppy name kennel code Kennel kennel code kennel name kennel location Tricks trick ID trick name Puppy Tricks puppy number trick ID trick where learned skill level The motivation for this is the same as for the second normal form. We want to avoid update and delete anomalies. For example suppose no puppies from the Puppy Farm were currently stored in the database. With the previous design, there would be no record of its existence.

Rules of Normalization IV Rule 4: Isolate independent multiple relationships. No table may contain two or more 1:n (one-to-many) or n:m (many-to-many) relationships that are not directly related. This applies only to designs that include one-to-many and many-to-many relationships. An example of a one-to-many relationship is that one kennel can hold many puppies. An example of a many-to-many relationship is that a puppy can know many tricks and several puppies can know the same tricks. Puppy Tricks and Costumes puppy number trick ID trick where learned skill level costume suppose we want to add a new attribute to the puppy-trick table, Costume, this way we can look for puppies that can both set-up-and-beg and wear a Groucho Marx mask, for example. The forth normal form dictates against this (i.e. against using the puppy-tricks table not against begging while wearing a Groucho mask). The two attributes do not share a meaningful relationship. A puppy may be able to wear a wet suit. This does not mean it can simultaneously sit up and beg. How will you represent this if you store both attributes in the same table? Forth Normal Form Puppies puppy number puppy name kennel code Kennels kennel code kennel name kennel location Tricks trick ID trick name Puppy-Tricks puppy number trick ID trick where learned skill level

Costumes costume number costume name Puppy-Custumes puppy number costume number

Example E-R diagaram:

RESULT:

EMBEDDED SQL

ODBC:
ODBC (Open Database Connectivity), a C-based interface to SQL-based database engines, provides a consistent interface for communicating with a database and for accessing database metadata.

JDBC:
A JDBC driver is a class that implements the JDBC Driver interface and understands how to convert program (and typically SQL) requests for a particular database. Clearly, the driver is what makes it all work. Syntax: Load the JDBC driver.
Class.forName( DriverClassName);

Connect to a data source.


Connection con = DriverManager.getConnection(URL,Username,Password );

DATABASE DESIGN: SQL>create table author(name varchar2(20),id number(25)); Table created. SQL>insert into author (name,id)values(senthil,27); 1 row created. SQL>insert into author values(koundamani,35); 1 row created. SQL>insert into author (name,id)values(vivek,20); 1 row created. SQL>select * from author; NAME senthil koundamani vivek ID 27 35 20

PROGRAM: import java.sql.*; public class author { public static void main(String x[]) { try { String str="SELECT * FROM hari where name='senthil'"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:hari","3it55","3it55"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(str); System.out.println("The result is "); rs.next(); String name=rs.getString("name"); int j=Integer.parseInt(rs.getString("no")); System.out.println(name); System.out.println(j); con.close(); } catch(Exception ex) { System.out.println(ex); } } }

OUTPUT: C:\Documents and Settings\DINESH>cd\ C:\>d: D:\>cd jdk1.5\bin D:\>javac author.java D:\>java author The result is senthil 27

RESULT:

LIBRARY MANAGEMENT SYSTEM DATABASE DESIGN: SQL>create table dglib(bookno number(10),bookname varchar2(25),author varchar2(25),price number(10),publisher varchar2(25),bookavail number(10)); Table created. SQL> insert into dglib values(103,real,dilli,780,DN publisher,100); 1 row created. SQL> insert into dglib values(104,treasure,dilli,430,DN publishers,30); 1 row created. SQL> insert into dglib values(105,dochy,manoj,450,DN publishers,320); 1 row created. SQL> insert into dglib values( 106, dochy, manoj ,320, DN publishers,450); 1 row created. SQL> insert into dglib values(104,HARRYPOTTER,ROWLING,250, TATAMACGRAW HILL,2500); 1 row created. SQL> select * from dglib; BOOKNO BOOKNAME 103 104 105 106 104 AUTHOR PRICE PUBLISHER BOOKAVAIL

real. dilli 780 DN publisher 100 treasure dilli 430 DN publishers 30 dochy manoj 450 DN publishers 320 dochy manoj 320 DN publishers 450 HARRY POTTER ROWLING 250 TATA MCGRAWHILL 2500

PROGRAM CODING: FORM1: Dim flag As Integer Private Sub Command1_Click() flag = 1 Text1.SetFocus Text1.Text = " " Text2.Text = " " Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = "" MsgBox "INSERT A NEW RECORD" Adodc1.Refresh Adodc1.Recordset.AddNew Command2.Enabled = False Command3.Enabled = False End Sub Private Sub Command2_Click() flag = 2 MsgBox "select the record to be deleted" Command1.Enabled = False Command3.Enabled = False End Sub Private Sub Command3_Click() flag = 3 Text1.Enabled = False MsgBox "select the record to be modified" Text2.SetFocus Command1.Enabled = False Command2.Enabled = False End Sub Private Sub Command4_Click() If flag = 1 Then Adodc1.Recordset.Update Adodc1.Refresh MsgBox "saved"

flag = 0 End If If flag = 2 Then Adodc1.Recordset.Delete Adodc1.Recordset.MoveNext Adodc1.Refresh MsgBox "record deleted" flag = 0 End If If flag = 3 Then Adodc1.Recordset.Update MsgBox "record modified" flag = 0 End If Command1.Enabled = True Command2.Enabled = True End Sub Private Sub Command5_Click() Form2.Show End Sub Private Sub Command6_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = "" End Sub

FORM2: Private Sub Command1_Click() End End Sub Private Sub Command2_Click() Form1.Show End Sub

FORM1

FORM2

AFTER EXECUTE: FORM1

FORM2:

PAYROLL PROCESSING SYSTEM DATABASE VIEW: SQL>create table pyl(employeeid number(10),employeename varchar2(25),designation varchar2(25),salary number(6),hra number(5),esi number(4),pf number(7),netsalary number(6),address varchar2(20),phoneno number(20)); Table created. SQL> insert into pyl values('01','s','se','30000','1500','1200','3000','40000','1 ram st','256478123'); 1 row created. SQL>insert into pyl values('02','a','se','30000','1500','1200','3000','40000','1 ram st','256478123'); 1 row created. SQL>insert into pyl values('03','r','se','30000','1500','1200','3000','40000','1 ram st','256478123'); 1 row created. SQL>select * from pyl; EMPLOYEEID EMPLOYEENAME DESIGNATION SALARY HRA ESI PF NETSALARY ADDRESS PHONENO 01 40000 02 40000 03 40000 s 1 ram st a 1 ram st r 1 ram st se 256478123 se 256478123 se 256478123 30000 30000 30000 1500 1200 3000 1500 1200 3000 1500 1200 3000

PROGRAM CODING: FORM 1: Dim flag As Integer Private Sub command1_Click() flag = 1 Text1.SetFocus MsgBox "insert a new record" Adodc1.Refresh Adodc1.Recordset.AddNew Command2.Enabled = False Command4.Enabled = False End Sub Private Sub command2_Click() flag = 2 MsgBox "select the record to be deleted" Command1.Enabled = False Command4.Enabled = False End Sub Private Sub command3_Click() Form3.Show End Sub Private Sub command4_Click() flag = 3 Text1.Enabled = False MsgBox "selet the record to be modify" Text2.SetFocus Command1.Enabled = False Command2.Enabled = False End Sub Private Sub command5_Click() If flag = 1 Then Adodc1.Recordset.Update Adodc1.Refresh MsgBox "saved"

flag = 0 End If If flag = 2 Then Adodc1.Recordset.Delete Adodc1.Recordset.MoveNext Adodc1.Refresh MsgBox "record deleted" flag = 0 End If If flag = 3 Then Adodc1.Recordset.Update MsgBox "record modify" Adodc1.Refresh flag = 0 End If Command1.Enabled = True Command4.Enabled = True Command2.Enabled = True Text1.Enabled = True End Sub Private Sub command6_Click() Text1.Text = " " Text2.Text = " " Text3.Text = " " Text4.Text = " " Text5.Text = " " Text6.Text = " " Text7.Text = " " Text8.Text = " " End Sub Private Sub command9_Click() End End Sub Private Sub command7_Click() Form2.Show End Sub Private Sub command8_Click() Text5.Text = Val(Text4.Text) * 0.05 Text6.Text = Val(Text3.Text) * 0.03

Text7.Text = Val(Text2.Text) * 0.02 Text8.Text = (Val(Text5.Text) + Val(Text6.Text) + Val(Text7.Text)) End Sub FORM 2: Dim flag As Integer Private Sub command1_Click() flag = 1 Text1.SetFocus MsgBox "insert a new record" Adodc1.Refresh Adodc.Recordset.AddNew Command2.Enabled = False Command4.Enabled = False End Sub Private Sub command2_Click() flag = 2 MsgBox "select the record to be deleted" Command1.Enabled = False Command4.Enabled = False End Sub Private Sub command3_Click() Form3.Show End Sub Private Sub command4_Click() flag = 3 Text1.Enabled = False MsgBox "selet the record to be modify" Text2.SetFocus Command1.Enabled = False Command2.Enabled = False End Sub Private Sub command5_Click() If flag = 1 Then Adodc1.Recordset.Update Adodc1.Refresh MsgBox "saved"

flag = 0 End If If flag = 2 Then Adodc1.Recordset.Delete Adodc1.Recordset.MoveNext Adodc1.Refresh MsgBox "record deleted" flag = 0 End If If flag = 3 Then Adodc1.Recordset.Update MsgBox "record modify" Adodc1.Refresh flag = 0 End If Command1.Enabled = True Command4.Enabled = True Command2.Enabled = True Text1.Enabled = True End Sub Private Sub command6_Click() Text1.Text = " " Text2.Text = " " Text3.Text = " " Text4.Text = " " Text5.Text = " " Text6.Text = " " Text7.Text = " " Text8.Text = " " End Sub Private Sub command7_Click() End End Sub FORM 3:

FORM1:

FORM3:

AFTER EXECUTION:

BANKING SYSTEM DATABASE DESIGN: SQL> create table bank (cname varchar2(20),cno number(10),accno number(10),bal number(10),branch varchar2(20)); Table created. SQL>insert into bank values('karthi','20','5020','36000520','panagalpark') 1 row created. SQL>insert into bank values('aswin','04','5004','36521450','ambur') 1 row created. SQL> select * from bank; CNAME karthi aswin SQL> commit; Commit complete. CNO ACCTNO BAL 20 4 5020 36000520 5004 36521450 BRANCH panagalpark ambur

FORM1

FORM2

PROGRAM CODING: FORM 1: Dim FLAG As Integer Private Sub command1_click() FLAG = 1 Text1.SetFocus MsgBox "Insert a new record'" Adodc1.Refresh Adodc1.Recordset.AddNew Command2.Enabled = False Command3.Enabled = False End Sub Private Sub Command11_Click() If rs.EOF = True Then MsgBox " last record" Else Call retr rs.MoveNext End If End Sub Private Sub Command14_Click() End End Sub Private Sub command5_click() Text1.Text = " " Text2.Text = " " Text3.Text = " " Text4.Text = " " Text5.Text = " " End Sub

Private Sub command12_click() Dim a As Integer a = InputBox("Enter the amount for deposit") Text4.Text = Val(Text4.Text) + a Adodc1.Recordset.Update End Sub Private Sub command13_click() Dim b As Integer b = InputBox("enter the amount to withdraw") If (b < Val(Text4.Text)) Then If (Val(Text4.Text) > 500) Then Text4.Text = Val(Text4.Text) - b Adodc1.Recordset.Update Else MsgBox "amount is low to withdraw" Adodc1.Recordset.Update End If Else MsgBox "box is not sufficient" Adodc1.Recordset.Update End If End Sub Private Sub Command6_Click() Form2.Show End Sub Private Sub command10_click() Dim c As Integer FLAG = 4 c = Val(Text4.Text) MsgBox (c) End Sub Private Sub command2_click() FLAG = 2 MsgBox "select the record to be deleted"

Command1.Enabled = False Command3.Enabled = False End Sub Private Sub command3_click() FLAG = 3 Text1.Enabled = False MsgBox "select the record to be modified" Text2.SetFocus Command1.enables = False Command2.Enabled = False Adodc1.Recordset.Update End Sub Private Sub command4_click() If FLAG = 1 Then Adodc1.Recordset.Update Adodc1.Refresh MsgBox "saved" FLAG = 0 End If If FLAG = 2 Then Adodc1.Recordset.Delete Adodc1.Recordset.MoveNext Adodc1.Refresh MsgBox "record deleted" FLAG = 0 End If If FLAG = 3 Then Adodc1.Recordset.Update Adodc1.Recordset.MoveNext MsgBox "record modified" Adodc1.Refresh FLAG = 0 End If If FLAG = 4 Then Adodc1.Recordset.Update Adodc1.Recordset.MoveNext

MsgBox "record modified" Adodc1.Refresh Text1.Enabled = True End If End Sub Private Sub command7_Click() If Adodc1.Recordset.BOF = True Then MsgBox " first record" Else Call bnk Adodc1.Recordset.MovePrevious End If End Sub FORM 2: Private Sub command1_click() Form1.Show End Sub

AFTER EXECUTE:

ADD

WITHDRAW

You might also like