ACTCET-CSE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EX. NO:1 DDL COMMANDS Date: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Aim : To know the uses of following DDL Commands Create Table Alter Table Drop Table Syntax & Description: The SQL command for creating an empty table has the following form: create table <table> ( <column 1> <data type> [not null] [unique] [<column constraint>],… <column n> <data type> [not null] [unique] [<column constraint>], [<table constraint(s)>] ); For each column, a name and a data type must be specified and the column name must be unique within the table definition. Column definitions are separated by comma. There is no difference between names in lower case letters and names in upper case letters. In fact, the only place where upper and lower case letters matter are strings comparisons. A not null constraint is directly specified after the data type of the column and the constraint requires defined attribute values for that column, different from null. The keyword unique specifies that no two tuples can have the same attribute value for this column. Unless the condition not null is also specified for this column, the attribute value null is allowed and two tuples having the attribute value null for this column do not violate the constraint. Modifying Table- and Column Definitions It is possible to modify the structure of a table (the relation schema) even if rows have already been inserted into this table. A column can be added using the alter table command alter table <table> add(<column> <data type> [default <value>] [<column constraint>] ); If more than only one column should be added at one time, respective add clauses need to be separated by colons. A table constraint can be added to a table using alter table <table> add (<table constraint>); Note that a column constraint is a table constraint, too. not null and primary key constraints can only be added to a table if none of the 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
To know the uses of following DDL Commands Create Table Alter Table Drop Table
Syntax & Description:The SQL command for creating an empty table has the following form:create table <table> (<column 1> <data type> [not null] [unique] [<column constraint>],…<column n> <data type> [not null] [unique] [<column constraint>],[<table constraint(s)>] );For each column, a name and a data type must be specified and the column name must be
unique within the table definition. Column definitions are separated by comma. There is no difference between names in lower case letters and names in upper case letters. In fact, the only place where upper and lower case letters matter are strings comparisons. A not null constraint is directly specified after the data type of the column and the constraint requires defined attribute values for that column, different from null. The keyword unique specifies that no two tuples can have the same attribute value for this column. Unless the condition not null is also specified for this column, the attribute value null is allowed and two tuples having the attribute value null for this column do not violate the constraint.
Modifying Table- and Column DefinitionsIt is possible to modify the structure of a table (the relation schema) even if rows have already been
inserted into this table. A column can be added using the alter table commandalter table <table>add(<column> <data type> [default <value>] [<column constraint>] );If more than only one column should be added at one time, respective add clauses need to beseparated by colons. A table constraint can be added to a table usingalter table <table> add (<table constraint>);Note that a column constraint is a table constraint, too. not null and primary key constraints can only be added to a table if none of the specified columns contains a null value. Table definitions can be modified in an analogous way. This is useful, e.g., when the size of strings that can be stored needs to be increased. The syntax of the command for modifying a column isalter table <table>modify(<column> [<data type>] [default <value>] [<column constraint>]);
Deleting a TableA table and its rows can be deleted by issuing the command drop table <table> [cascade constraints];
TO CREATE TABLE. SQL> create table depart(dno number(10),dname varchar2(10),primary key(dno));
Table created.1
ACTCET-CSE
SQL> desc depart;
Name Null? Type ----------------------------------------- -------- ---------------------------- DNO NOT NULL NUMBER(10) DNAME VARCHAR2(10)
Name Null? Type ----------------------------------------- -------- ---------------------------- ENO NUMBER(10) ENAME VARCHAR2(10) DNO NUMBER(10) SAL NUMBER(10) JOBID VARCHAR2(10) MGRID VARCHAR2(10)
Name Null? Type ----------------------------------------- -------- ---------------------------- CNAME VARCHAR2(15) CID NOT NULL NUMBER(5) CADDR CHAR(10) CACCNO NUMBER(5) CACCTYPE VARCHAR2(10) CBALANCE FLOAT(126)
TO ALTER TABLE ADD SQL> alter table emp add(primary key(eno),addr varchar2(10));
Table altered.
2
ACTCET-CSESQL> desc emp;
Name Null? Type ----------------------------------------- -------- ---------------------------- ENO NOT NULL NUMBER(10) ENAME VARCHAR2(10) DNO NUMBER(10) SAL NUMBER(10) JOBID VARCHAR2(10) MGRID VARCHAR2(10) ADDR VARCHAR2(10)
SQL> alter table emp add(phno number(5));
Table altered.
SQL> desc emp;
Name Null? Type ----------------------------------------- -------- ---------------------------- ENO NOT NULL NUMBER(10) ENAME VARCHAR2(10) DNO NUMBER(10) SAL NUMBER(10) JOBID CHAR(20) MGRID VARCHAR2(10) ADDR VARCHAR2(10) PHNO NUMBER(5)
MODIFY SQL> alter table emp modify(jobid char);
Table altered.
SQL> desc emp;
Name Null? Type ----------------------------------------- -------- ---------------------------- ENO NOT NULL NUMBER(10) ENAME VARCHAR2(10) DNO NUMBER(10) SAL NUMBER(10) JOBID CHAR(20) MGRID VARCHAR2(10) ADDR VARCHAR2(10) PHNO VARCHAR2(10)
3
ACTCET-CSESQL> alter table emp modify(jobid char(20));
Table altered.
SQL> desc emp;
Name Null? Type----------------------------------------- -------- ---------------------------- ENO NOT NULL NUMBER(10) ENAME VARCHAR2(10) DNO NUMBER(10) SAL NUMBER(10) JOBID CHAR(20) MGRID VARCHAR2(10) ADDR VARCHAR2(10) PHNO VARCHAR2(10)
SQL> alter table emp modify(jobid char(5));
alter table emp modify(jobid char(5)) *ERROR at line 1:
ORA-01441: cannot decrease column length because some value is too big
DROP
SQL> alter table emp drop(phno);
Table altered.
SQL> desc emp;
Name Null? Type ----------------------------------------- -------- ---------------------------- ENO NOT NULL NUMBER(10) ENAME VARCHAR2(10) DNO NUMBER(10) SAL NUMBER(10) JOBID CHAR(20) MGRID VARCHAR2(10) ADDR VARCHAR2(10)
SQL> alter table emp drop(addr);
Table altered.
4
ACTCET-CSESQL> desc emp;
Name Null? Type----------------------------------------- -------- ----------------------------ENO NOT NULL NUMBER(10)ENAME VARCHAR2(10)DNO NUMBER(10)SAL NUMBER(10)JOBID CHAR(20)MGRID VARCHAR2(10)
Syntax & Description:The most simple way to insert a tuple into a table is to use the insert statement
insert into <table> [(<column i, . . . , column j>)]values (<value i, . . . , value j>);
For each of the listed columns, a corresponding (matching) value must be specified. Thus an insertion does not necessarily have to follow the order of the attributes as specified in the create table statement. If a column is omitted, the value null is inserted instead. If no column list is given, however, for each column as defined in the create table statement a value must be given.
For modifying attribute values of (some) tuples in a table, we use the update statement:update <table> set<column i> = <expression i>, . . . , <column j> = <expression j>[where <condition>];An expression consists of either a constant (new value), an arithmetic or string operation, oran SQL query. Note that the new value to assign to <column i> must a the matching data type.An update statement without a where clause results in changing respective attributes of alltuples in the specified table. Typically, however, only a (small) portion of the table requires an update. All or selected tuples can be deleted from a table using the delete command:delete from <table> [where <condition>];If the where clause is omitted, all tuples are deleted from the table. An alternative commandfor deleting all tuples from a table is the truncate table <table> command. However, in thiscase, the deletions cannot be undone
Name Null? Type ----------------------------------------- -------- ---------------------------- CNAME VARCHAR2(15) CID NOT NULL NUMBER(5) CADDR CHAR(10) CACCNO NUMBER(5) CACCTYPE VARCHAR2(10) CBALANCE FLOAT(126)
6
ACTCET-CSE
SQL> insert into cust values('Anitha',01,'Chennai',1001,'savings',15000);
1 row created.
SQL> insert into cust values('Shriram',02,'Pondy',1002,'savings',25000);
1 row created.
SQL> insert into cust values('Chamundi',03,'Salem',1003,'fd',36200);
1 row created.
SQL> insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance);Enter value for cname: SubhaEnter value for cid: 04Enter value for caddr: SalemEnter value for caccno: 1009Enter value for cacctype: 5000Enter value for cbalance: 5000Old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)New 1: insert into cust values('Subha',04,'Salem',1009,'RD',5000)
1 row created.SQL> insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance);Enter value for cname: MadhanEnter value for cid: 4Enter value for caddr: SalemEnter value for caccno: 1004Enter value for cacctype: checkingsEnter value for cbalance: 5000old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)new 1: insert into cust values('Madhan',4,'Salem',1004,'checkings',5000)
1 row created.
SQL> insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance);Enter value for cname: SubhaEnter value for cid: 5Enter value for caddr: TrichyEnter value for caccno: 1005Enter value for cacctype: checkingsEnter value for cbalance: 10000old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)new 1: insert into cust values('Subha',5,'Trichy',1005,'checkings',10000)
1 row created.
SQL> insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance);7
ACTCET-CSEEnter value for cname: JayashreeEnter value for cid: 6Enter value for caddr: PondyEnter value for caccno: 1006Enter value for cacctype: fdEnter value for cbalance: 15000old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)new 1: insert into cust values('Jayashree',6,'Pondy',1006,'fd',15000)
1 row created.
SQL> insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance);Enter value for cname: SridharanEnter value for cid: 7Enter value for caddr: KanchiEnter value for caccno: 1007Enter value for cacctype: fdEnter value for cbalance: 22000old 1: insert into cust values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)new 1: insert into cust values('Sridharan',7,'Kanchi',1007,'fd',22000)
To know the uses of following commands in Oracle Between…and In Not in Like Relational Operators Logical Operators
Syntax & Description:select [distinct] [<alias ak>.]<column i>, . . . , [<alias al>.]<column j>from <table 1> [<alias a1>], . . . , <table n> [<alias an>][where <condition>]The specification of table aliases in the from clause is necessary to refer to columns that have the same name in different tables. For example, the column DEPTNO occurs in both EMP and DEPT. If we want to refer to either of these columns in the where or select clause, a table alias has to be specified and put in the front of the column name. Instead of a table alias also the complete relation name can be put in front of the column such as DEPT.DEPTNO, but this sometimes can lead to rather lengthy query formulations.SQL> select * from cust;
To know the joins, its types and to use joins in tables
Description :Comparisons in the where clause are used to combine rows from the tables listed in the from clause.Example: In the table EMP only the numbers of the departments are stored, not their name. For each salesman, we now want to retrieve the name as well as the number and the name of the department where he is working:select ENAME, E.DEPTNO, DNAMEfrom EMP E, DEPT Dwhere E.DEPTNO = D.DEPTNOand JOB = ’SALESMAN’;Explanation: E and D are table aliases for EMP and DEPT, respectively. The computation of thequery result occurs in the following manner (without optimization):1. Each row from the table EMP is combined with each row from the table DEPT (this operationis called Cartesian product). If EMP contains m rows and DEPT contains n rows, wethus get n _ m rows.2. From these rows those that have the same department number are selected (whereE.DEPTNO = D.DEPTNO).3. From this result finally all rows are selected for which the condition JOB = ’SALESMAN’holds.In this example the joining condition for the two tables is based on the equality operator “=”.The columns compared by this operator are called join columns and the join operation is calledan equijoin. Any number of tables can be combined in a select statement.Example: For each project, retrieve its name, the name of its manager, and the name ofthe department where the manager is working:select ENAME, DNAME, PNAMEfrom EMP E, DEPT D, PROJECT Pwhere E.EMPNO = P.MGRand D.DEPTNO = E.DEPTNO;It is even possible to join a table with itself:Example: List the names of all employees together with the name of their manager:select E1.ENAME, E2.ENAMEfrom EMP E1, EMP E2where E1.MGR = E2.EMPNO;Explanation: The join columns are MGR for the table E1 and EMPNO for the table E2.The equijoin comparison is E1.MGR = E2.EMPNO.
ACTCET-CSE Name Null? Type ----------------------------------------- -------- --------------------------- LID NOT NULL NUMBER(5) CITY VARCHAR2(10) AREA VARCHAR2(5)
SQL>desc dep; Name Null? Type ----------------------------------------- -------- -------------------- DNO NOT NULL NUMBER(5) DNAME VARCHAR2(10) LID NUMBER(5)
SQL>desc grade; Name Null? Type ----------------------------------------- -------- ------------------ GNO NOT NULL NUMBER(5) LS NUMBER(8) HS NUMBER(8)SQL>insert into locn values(&lid,'&city','&area');
13
ACTCET-CSEenter lid:1enter city:chennaienter area:aaaold 1: insert into locn values(&lid,'&city','&area')new 1: insert into locn values(1,'chennai','aaa')1 row createdSQL>insert into dep values(&dno,'&dname',&lid);enter dno:1enter dname:adminenter lid:2old 1: insert into dep values(&dno,'&dname',&lid)new 1: insert into dep values(1,'admin',2)1 row createdSQL>insert into emp values(&eid,'&ename',&dno,&esal, &jobid,&mgr id);enter eid:3enter ename:zzzenter dno:3enter esal :3500enter jobid:2enter mgr id:2old 1: insert into emp values(&eid,'&ename',&dno,&esal,&jobid,&mgrid)new 1: insert into emp values(1,'zzz',3500,2,2)1 row created
SQL>insert into grade values(&gno,&ls,&hs);enter gno:1enter ls:1000enter hs:2000old 1: insert into grade values(&gno,&ls,&hs)new 1: insert into grade values(1,1000,2000)
SELFJOIN----TO DISPLAY ENAME & THEIR MANAGER NAMES~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SQL> select e.ename,m.ename from emp e,emp m where e.mgrid=m.eid;
ENAME ENAME---------- ----------bbc yyy
16
ACTCET-CSEyyy xxxzzz yyy
SELFJOIN----TO DISPLAY MANAGER'S SALARY FOR EVERY EMPLOYEE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SQL> select e.ename,m.esal from emp e,emp m where e.mgrid=m.eid;
Syntax To create viewCreate view viewname as select columnname 1…columnname n from tablename where condition;
ExampleCreate view myview as select regno, name from stud;
Syntax To display viewselect columnname 1…columnname n from viewname where condition;
ExampleSelect regno, name from myview;
Syntax To delete viewdrop viewname;
Exampledrop myview;.
Result:
Thus the view is successfully created and displayed.
18
ACTCET-CSE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:5a PL/SQL-CONTROLSTRUCTUREDate:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Aim :To implement control structure of PL/SQL.
SYNTAX's of CONTROL STATEMENTS in PL/SQL
1. BRANCHING2. SELECTION3. LOOPING
BRANCHING STATEMENTS
1.Simple IF2.ELSIF3.ELSE IF
SIMPLE IF
IF condition THEN statement1; statement2;END IF;
IF-THEN-ELSE STATEMENT
IF condition THEN statement1;ELSE statement2;END IF;
ELSIF STATEMENTS
IF condition1 THEN statement1;ELSIF condition2 THEN statement2;ELSIF condition3 THEN statement3;ELSE
19
ACTCET-CSE statementn;END IF;
ITERATIONS IN PL/SQL
SIMPLE LOOP
LOOP statement1;EXIT [ WHEN Condition];END LOOP;
WHILE LOOP
WHILE condition LOOPstatement1;statement2;
END LOOP;
FOR LOOP
FOR counter IN [REVERSE] LowerBound..UpperBoundLOOP
statement1;statement2;
END LOOP;
WRITE A PL/SQL PROGRAM TO FIND THE LARGEST OF TWO NUMBERS
declarea number;b number;begina:=&a;b:=&b;if a=b thendbms_output.put_line('BOTH ARE EQUAL');elsif a>b thendbms_output.put_line('A IS GREATER');elsedbms_output.put_line('B IS GREATER');end if;end;
20
ACTCET-CSEOUTPUT:SQL> @ GREATESTOF2.sql 13 /Enter value for a: 5old 5: a:=&a;new 5: a:=5;Enter value for b: 2old 6: b:=&b;new 6: b:=2;A IS GREATER
PL/SQL procedure successfully completed.
WRITE A PL/SQL PROGRAM TO FIND THE FACTORIAL OF A GIVEN NUMBER
declaren number;f number:=1;beginn:=&n;for i in 1..nloopf:=f*i;end loop;dbms_output.put_line('the factorial is'|| f);end;
OUTPUT:
SQL> @ FACTORIAL.sql 12 /Enter value for n: 5old 5: n:=&n;new 5: n:=5;the factorial is120
PL/SQL procedure successfully completed.
Result:
Thus the control structures using PL/SQL have been run.
Syntax & Description :PL/SQL provides sophisticated language constructs to program procedures and functions as stand-alone PL/SQL blocks. They can be called from other PL/SQL blocks, other procedures and functions. The syntax for a procedure definition iscreate [or replace] procedure <procedure name> [(<list of parameters>)] is<declarations>begin<sequence of statements>[exception<exception handling routines>]end [<procedure name>];The optional clause or replace re-creates the procedure/function. Valid parameters include all data types. However, for char, varchar2, and number no length and scale, respectively, can be specified. For example, the parameter number(6) results in a compile error and must be replaced by number.Constants, variables, cursors, and exceptions used in a PL/SQL block must be declared in the declare section of that block. Variables and constants can be declared as follows:<variable name> [constant] <data type> [not null] [:= <expression>];A PL/SQL block may contain statements that specify exception handling routines. Each error or warning during the execution of a PL/SQL block raises an exception. One can distinguish between two types of exceptions:• system defined exceptions• user defined exceptions (which must be declared by the user in the declaration part of a block where the exception is used/implemented)System defined exceptions are always automatically raised whenever corresponding errors or warnings occur. User defined exceptions, in contrast, must be raised explicitly in a sequence of statements using raise <exception name>. After the keyword exception at the end of a block, user defined exception handling routines are implemented. An implementation has the patternwhen <exception name> then <sequence of statements>;The most common errors that can occur during the execution of PL/SQL programs are handled by system defined exceptions. The table below lists some of these exceptions with their names and a short description.SQL> create table stud(rno number(2),mark1 number(3),mark2 number(3),total number(3),primary key(rno));
Table created.
SQL> desc stud; Name Null? Type ----------------------------------------- -------- ---------------------------- RNO NOT NULL NUMBER(2) MARK1 NUMBER(3) MARK2 NUMBER(3) TOTAL NUMBER(3)
SQL> create or replace procedure studd(rnum number) is 2 m1 number; 3 m2 number; 4 total number; 5 begin 6 select mark1,mark2 into m1,m2 from stud where rno=rnum; 7 if m1<m2 then 8 update stud set total=m1+m2 where rno=rnum; 9 end if; 10 end; 11 /
ACTCET-CSE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:5c PL/SQL FUNCTIONSDate:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Aim :To write function in PL/SQL.
Procedure:
Write the following pl/sql program in oracle editorSave it as cube..sqlRun it in sql prompt as SQL> @cube.sql
PL/SQL PROGRAMS- FUNCTIONS
CUBE
create or replace function cube(n number) return numberasc number;beginc:=n*n*n;return c;end;
output:-SQL> @cub /
Function created.
SQL> select cube(6)from dual;
CUBE(6) ------ 216
Result:
Thus the Function using PL/SQL have been run.
25
ACTCET-CSE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:5d.i SINGLE ROW FUNCTIONSDate:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Aim :
To know the following functions Character Functions Number Functions Date Functions
Syntax & Description :In order to compare an attribute with a string, it is required to surround the string by apostrophes,
e.g., where LOCATION = ’DALLAS’. A powerful operator for pattern matching is the like operator. Together with this operator, two special characters are used: the percent sign % (also called wild card), and the underline , also called position marker. For example, if one is interested in all tuples of the table DEPT that contain two C in the name of the department, the condition would be where DNAME like ’%C%C%’. The percent sign means that any (sub)string is allowed there, even the empty string. In contrast, the underline stands for exactly one character. Thus the condition where DNAME like ’%C C%’ would require that exactly one character appears between the two Cs. To test for inequality, the not clause is used.Further string operations are:• upper(<string>) takes a string and converts any letters in it to uppercase, • lower(<string>) converts any letter to lowercase,• length(<string>) returns the length of the string.• substr(<string>, n [, m]) clips out a m character piece of <string>, starting at positionn. If m is not specified, the end of the string is assumed.substr(’DATABASE SYSTEMS’, 10, 7) returns the string ’SYSTEMS’.The concatenation operator “||” can be used to concatenate single strings to one string.SQL> DESC EMPLOYEE22; Name Null? Type ----------------------------------------- -------- ----------------------- ENO NOT NULL NUMBER(5) ENAME VARCHAR2(50) DEPTID NUMBER(3) SALARY NUMBER(7) JOB VARCHAR2(5) MGRID NUMBER(3) COM VARCHAR2(6)
SQL> select * from employee22;
ENO ENAME DEPTID---------- -------------------------------------------------- ---------- SALARY JOB MGRID COM---------- ----- ---------- ----------------------------------- 1 x 10 100000 MD 0 15
2 y 20 50000 Mgr 1 10
26
ACTCET-CSE
3 z 20 25000 Mgr 2 2
ENO ENAME DEPTID---------- -------------------------------------------------- ---------- SALARY JOB MGRID COM---------- ----- ---------- -------------------------------------------- 4 a 30 15000 Mgr 2 1
5 b 40 10000 Emp 3 5
SQL> select * from employee22 where LOWER(ename)='x';
ENO ENAME DEPTID---------- -------------------------------------------------- -------------------------- SALARY JOB MGRID COM---------- ----- ---------- ---------------------------------------------------- 1 X 10 100000 MD 0 15
SQL> select * from employee22 where UPPER(ename)='y';
no rows selected
SQL> select * from employee22 where UPPER(ename)='Y';
ENO ENAME DEPTID---------- -------------------------------------------------- ---------- SALARY JOB MGRID COM---------- ----- ---------- ---------------------------------------------- 2 y 20 50000 Mgr 1 10
SQL> select 'Salary of ' || ename || ' is ' || salary from employee22;
'SALARYOF'||ENAME||'IS'||SALARY--------------------------------------------------------------------------Salary of X is 100000Salary of y is 50000Salary of z is 25000Salary of a is 15000Salary of b is 10000
27
ACTCET-CSE
SQL> select * from employee22 where LOWER(ename)='x';
ENO ENAME DEPTID---------- -------------------------------------------------- ---------- SALARY JOB MGRID COM---------- ----- ---------- ------------------------------------------------- 1 X 10 100000 MD 0 15
SQL> select * from employee22 where UPPER(ename)='y';no rows selected
SQL> select * from employee22 where UPPER(ename)='Y';
ENO ENAME DEPTID---------- -------------------------------------------------- ------------------------- SALARY JOB MGRID COM---------- ----- ---------- ----------------------------------------------------------- 2 y 20 50000 Mgr 1 10
SQL> select 'Salary of ' || ename || ' is ' || salary from employee22;
'SALARYOF'||ENAME||'IS'||SALARY--------------------------------------------------------------------------------Salary of X is 100000Salary of y is 50000Salary of z is 25000Salary of a is 15000Salary of b is 10000
SQL> desc sample Name Null? Type ----------------------------------------- -------- ---------------------------- NAME VARCHAR2(30) FNAME VARCHAR2(10) LNAME VARCHAR2(10)
SQL> alter table sample add(fno float(10));
Table altered.
SQL> desc sample28
ACTCET-CSE Name Null? Type ----------------------------------------- -------- ---------------------------- NAME VARCHAR2(30) FNAME VARCHAR2(10) LNAME VARCHAR2(10) FNO FLOAT(10)
SQL> insert into sample values('Hello world','Lovelyn','Divya',120.856);
1 row created.
SQL> insert into sample values('Welcome everyone','Dhivyaa','Meens',340.15);
1 row created.
SQL> insert into sample values('Hi everybody','Harsha','Innanji',280.59);
1 row created.
SQL> select * from sample;
NAME FNAME LNAME FNO------------------------------ ---------- ---------- -------------------------------Hello world Lovelyn Divya 120.9Welcome everyone Dhivyaa Meens 340.2Hi everybody Harsha Innanji 280.6 SQL> select concat(fname,lname),fno from sample;
SQL> desc sample Name Null? Type ----------------------------------------- -------- ---------------------------- NAME VARCHAR2(30) FNAME VARCHAR2(10) LNAME VARCHAR2(10) FNO FLOAT(10) HIREDATE DATE
SQL> insert into sample values('REC CSE','Anitha','Anu',320.7,'21-mar-88');
1 row created.
SQL> insert into sample values('Hi chennai','aishwarya','sukumar',170,'30-oct-87');
1 row created.
SQL> select * from sample;
NAME FNAME LNAME FNO HIREDATE------------------------------ ---------- ---------- ---------- -------------------------------------Hello world Lovelyn Divya 120.9Welcome everyone Dhivyaa Meens 340.2Hi everybody Harsha Innanji 280.6REC CSE Anitha Anu 320.7 21-MAR-88Hi chennai aishwarya sukumar 170 30-OCT-87
SQL> select lname,hiredate from sample where lname like 'a%';
no rows selected
SQL> select lname,hiredate from sample where lname like 'A%';
To know about the following multi row functions Order By Group By Aggregate Functions
Syntax & DescriptionIt is also possible to specify a sorting order in which the result tuples of a query are displayed. For this the order by clause is used and which has one or more attributes listed in the select clause as parameter. desc specifies a descending order and asc specifies an ascending orderselect [distinct] <column(s)>from <table>[ where <condition> ][ order by <column(s) [asc|desc]> ]Often applications require grouping rows that have certain properties and then applying an aggregate function on one column for each group separately. For this, SQL provides the clause group by <group column(s)>. This clause appears after the where clause and must refer to columns of tables listed in the from clause.select <column(s)>from <table(s)>where <condition>group by <group column(s)>[having <group condition(s)>];Those rows retrieved by the selected clause that have the same value(s) for <group column(s)> are grouped. Aggregations specified in the select clause are then applied to each group separately.It is important that only those columns that appear in the <group column(s)> clause can be listed without an aggregate function in the select clause !Aggregate functions are statistical functions such as count, min, max etc. They are used to compute a single value from a set of attribute values of a column: count Counting RowsExample: How many tuples are stored in the relation EMP?select count(_) from EMP;Example: How many different job titles are stored in the relation EMP?select count(distinct JOB) from EMP;max Maximum value for a columnmin Minimum value for a columnExample: List the minimum and maximum salary.select min(SAL), max(SAL) from EMP;Example: Compute the difference between the minimum and maximum salary.select max(SAL) - min(SAL) from EMP;sum Computes the sum of values (only applicable to the data type number)Example: Sum of all salaries of employees working in the department 30.select sum(SAL) from EMPwhere DEPTNO = 30;avg Computes average value for a column (only applicable to the data type number)
ORDER BY========SQL> select cname from cust order by cname desc;
CNAME---------------SubhaSridharanShriramMadhanJayashreeChamundiAnusha7 rows selected.SQL> select cname,caccno,cbalance from cust order by cbalance desc,cname asc;
COUNT(CNAME)------------ 7SQL>select sum(cbalance) as totbal,avg(sum(cbalance)) as average from cust where cacctype='savings';
TOTBAL AVERAGE-----------------------------
40000 20000
SQL> select max(cbalance),min(cbalance) from cust; MAX(CBALANCE) MIN(CBALANCE)----------------------------------------------------- 36200 5000
Result:
Thus the built in multi row functions using SQL have been done.
36
ACTCET-CSE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:6 FRONT END TOOLSDate:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Aim:
To know the uses of Front end tools.
List of Front End Tools:-
PICTURE: This control is used to display images are set with the picture property. The picture box control supports of number of methods for generating drawings
LABEL: This control display text on a form that user can’t edit labels commonly identify other controls and can be transparent so that text appears to be placed directly on the form your can set the label is text with the cap from property.
TEXT BOX: This control display text that the user can edit we can set the text during the program and password property allows to hide the entering text by using ‘*’delimiter.
FRAM: This control used to draw boxes on the form and to group other elements.
COMMAND BUTTON: This control looks like a button with gray color if in used to trigger any event while clicking it.
CHECK BOX: If present one or more choice that the user selects we can set is the value to the check box as/or 0’1’ represent checked status ‘0’ represent cleared status.
OPTION BUTTON (OR) RADIO BUTTON: Appeared in group user can chose only one of them. If the value is set by ‘tree’ then it is toggled and false otherwise.
COMBO BOX: If is similar like box but it contains a text edit field.
LIST BOX: Thus control used to list to more items user can scroll the list box to view it.
HOREGENTAL,VERTICAL SCROLL BOXES: By scrolling this control we car view lengthier item in the screen.
TIMER: To set the interval of an action.
FILE SYSTEM CONTROL:37
ACTCET-CSE This control used to add files, folders ,directions to your screen.
DRIVE LIST BOX: This control used to display drop- down list.
SHAPE CONTROL: Used to draw graphical elements MSG boxes used to display the message .
DATA: Used to point-click access to the DB.
OLE: Used to link other object.
OUTPUT:
RESULT: Thus we study the basic front end tools successfully.
38
ACTCET-CSE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:7 FORMS CREATIONDate:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Aim:
To create forms using VB.
Procedure:STEP 1: Open a new standard project.STEP 2: Create new form STEP 3: Select Add_Ins - > Visual Data ManagerSTEP 4: Create new table using MS Access 7.0STEP 5: Build the table and add minimum 5 new recordsSTEP 6: Design form using labels, text boxes and frame.STEP 7: Insert data grid to display recordsSTEP 8: Assign the data base name, record source to data gird in its properties table.STEP 9: Assign data field values to the respective text boxes.STEP 10: Run the form.
Sample output:
Result: Thus the Form design done by using MS access and VB.
39
ACTCET-CSE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:8 TRIGGERSDate:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Aim :To use the triggersSyntax & DescriptionA trigger definition consists of the following (optional) components:• trigger namecreate [or replace] trigger <trigger name>• trigger time pointbefore | after• triggering event(s)insert or update [of <column(s)>] or delete on <table>• trigger type (optional)for each row• trigger restriction (only for for each row triggers !)when (<condition>)• trigger body<PL/SQL block>The clause replace re-creates a previous trigger definition having the same <trigger name>. The name of a trigger can be chosen arbitrarily, but it is a good programming style to use a trigger name that reflects the table and the event(s), e.g., upd ins EMP. A trigger can be invoked before or after the triggering event. The triggering event specifies before (after) which operations on the table <table> the trigger is executed. A single event is an insert, an update, or a delete; events can be combined using the logical connective or. If for an update trigger no columns are specified, the trigger is executed after (before) <table> is updated. If the trigger should only be executed when certain columns are updated, these columns must be specified after the event update. If a trigger is used to maintain an integrity constraint, the triggering events typically correspond to the operations that can violate the integrity constraint. In order to program triggers efficiently (and correctly) it is essential to understand the difference between a row level trigger and a statement level trigger. A row level trigger is defined using the clause for each row. If this clause is not given, the trigger is assumed to be a statement trigger. A row trigger executes once for each row after (before) the event. In contrast, a statement trigger is executed once after (before) the event, independent of how many rows are affected by the event. For example, a row trigger with the event specification after update is executed once for each row affected by the update. Thus, if the update affects 20 tuples, the trigger is executed 20 times, for each row at a time. In contrast, a statement trigger is only executed once. When combining the different types of triggers, there are twelve possible trigger that can be defined
BEFORE TRIGGERcreate or replace trigger tday before insert or delete or update on empdeclarewe varchar2(10);beginwe:=to_char(sysdate,'dy');if we='sat' or we='sun' thenraise_application_error(-20015,'its a weekend');end if;end;
40
ACTCET-CSEoutput:-
SQL> @trig1 /
Trigger created.
SQL> delete from emp where empno=7902;
ERROR at line 1:ORA-20015: its a weekendORA-06512: at "SCOTT.TDAY", line 6ORA-04088: error during execution of trigger 'SCOTT.TDAY'AFTER TRIGGER
create or replace trigger ttime after insert or delete or update on emp1 for each rowdeclarett varchar2(5);begintt:=to_char(sysdate,'hh24');if tt not between 10 and 17 thenraise_application_error(-20010,'not working hours');end if;end;
output:-SQL> @trig2; 10 /
Trigger created.
SQL> update emp1 set empno=7777 where empno=7902;
*ERROR at line 1:ORA-20010: not working hours
RESULT: Thus the sample Trigger experiment successfully executed.
41
ACTCET-CSE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:9 MENU DESIGNDate:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Aim:
Design the menu for the text editor using visual basicProcedure:
Step1: Open STD Dxe VB projectStep2: Select tools menu->go to menu editor (ctrl+E)Step3: Create main menu for EditStep4: Create sub menu under edit menu that are Cut, Copy, PasteStep5: Create Exit main menu
Main Program:Copy:
Private Sub Form_copy ()Clipboard. ClearClipboard.SetText Text1.SelTextEnd Sub
Cut: Private Sub Cut_Click ()Clipboard.SetText Text1.SelTextText1.SelText = " "End Sub
Paste:Private Sub Paste_Click ()Text1.SelText = Clipboard.GetTextEnd Sub
SAMPLE OUTPUT:
RESULT: Thus the Menu Design experiment successfully executed.
Procedure:STEP 1: open a new standard project.STEP 2: set up the data source.STEP 2a: open the project menu and select add data environment.STEP 2b: click on connection 1 and changes its name property.STEP 2c: right click on connection1 and select the data link property.STEP 2d: on the provider tab select Microsoft jet 3.51 ole database provider and click next.STEP 2c: on the connection tab, click on the builder button for dbname,
then click text connection button.Step 2f: right click on connection 1,select command and changes the properties.STEP 2g: for source of data ,drop down the list for db object and select table.STEP 3: design the report.STEP 3.1: open the project menu and select all data report.STEP 3.2: set the report properties.STEP 3.2a: drop down the list for the data source properties then change the data environment1.STEP 3.2b: set the data member property to command 1.STEP 3.3: close the report header selection.STEP 3.4: place a title in the page header section.STEP 3.5: place found controls in the details click on the field in the data environment window and drag a control on to the detail section of the report layout.STEP 4: save the report design.STEP 5: Design new form using command button with caption Generate ReportSTEP 6: Write the code.
Private Sub Command1_Click()DataReport1.ShowEnd Sub
STEP 7: Run the form
43
ACTCET-CSE
Output:
RESULT: Thus the Report Generation experiment successfully executed.
44
ACTCET-CSE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EX. NO:11 SAMPLE MINI PROJECTDate: Student Data Base System~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Aim: To create “Student Data Base System” for an organization using VB, MS Access.
Procedure: Step 1: Open Standard exe VB form.Step 2: Goto Menu bar -> AddIns ->Visual Data Manager -> create MS Access 7.0 table named
Newtable with following fields regno, sname, m1…m5, total, avg, grade.Step 3: Create Index using regno.Step 4: Design form using Labels, Textboxes, command buttons, DAO controls as in the scree shot.Step 5: Type coding as in the code part.Step 6: Run the form
Abstract:In this system we propose student data base management system to generate student mark sheet
with grade for given marks.
System Requirements: PIII..V processor computer CPU, 17” Monitor, Mouse, 103 keyboardFront end: Visual Basic 6.0Back end: Ms Access 7.0
Modular diagram / List of modules:1. Admin login2. Student view3. Update table 4. Generate Report
Gantt Chart:
Date
Phase
13/0
3/20
12
20/0
3/20
12
27/0
3/20
12 29/0
3/20
12 03/0
4/20
12 04/0
4/20
12 10/0
4/20
12 16/0
4/20
12Phase 1 Select
mini
project
name,
write
abstract
Phase 2 Select
45
ACTCET-CSEfront
end and
back
end
tools
Phase 3 Collect
dams
question
s
Phase 4 Add the
question
s into
database
Phase 5 Design
forms
Phase 6 Write
coding
Phase 7 Testing
the
project
Phase 8 Make
that as
docume
nt
46
ACTCET-CSEDATA BASE DESIGN
Coding47
ACTCET-CSE
Dim db As DatabaseDim rs As RecordsetDim rs1 As RecordsetDim ws As WorkspaceDim rno, i As IntegerDim stname As StringDim m1 As IntegerDim m2 As IntegerDim m3 As IntegerDim m4 As IntegerDim m5 As IntegerDim tot As IntegerDim ave As DoubleDim gra As StringDim op As String Dim L As Label Option Explicit
Private Sub Form_Load()Set ws = DBEngine.Workspaces(0)Set db = ws.OpenDatabase("D:\dbms minipjt\kavidb.mdb")Set rs = db.OpenRecordset("newtable", dbOpenDynaset)End Sub
Screen shots Sample output
To Add new record
53
ACTCET-CSE
Go first.
54
ACTCET-CSE
Go last.
To delete record
55
ACTCET-CSE
Result:Thus the “Student Data Base System” has been demonstrated using VB, MS Access.