Guru Nanak Dev Engineering Collage, Ludhiana.

Submitted To

Submitted By

Er. Gunjan Oberoi (Deptt. Of CSE )

Nitish Kamal D3 CSE (A2) 95065/90370305200



01 02 03 04 05 06 07 08 09(a) 09(b)

Name of Practical
To Execute Various Queries Using Commands Of Sql To Use A Loop In Sql To Find Out The Area Of A Circle When Radius Is Given Introduction To Views Program To Create And Drop An Index Introduction To Packages Introduction To Triggers Write A Program To Find Salary Grade Using Cursor Create Or Replace Procedure My_Proc As Create Table Mytable (Num_Col Number, Char_Col Varchar2(60));
02 19 21 23 28 31 33 35 37 38




(1) To create an employee table with the fields- employee name , department name,


You must create your tables before you can enter data into them. Use the Create Table command. Syntax: Create table tablename using filename (fieldname fieldtype(length), fieldname fieldtype(length), fieldname fieldtype(length));


the ALTER TABLE syntax is: ALTER TABLE table_name ADD column_name column-definition. modify. 5 .(2) To add the following fields to the employee table: employee number. date of birth. phone number. It can also be used to add. or drop a column from an existing table. The ALTER TABLE statement allows you to rename an existing table. To add a column to an existing table.

You can use the modify SQL command if you need to resize a column in MySQL. example: alter table people modify name VARCHAR(35) . It is written as: alter table modify [column name] VARCHAR(5) . By doing this you can allow more or less characters than before.(3) To modify the width of the field in employee table. 6 . This changes the column called "name" on the table called "people" to now allow 35 characters.

'10/10/2005') When using SQL INSERT INTO you might not want to enter values for all columns and in this case you have to specify the list of columns you are entering values for. 20. Here is how you can insert a new row into the Weather table. 20. AverageTemperature. with a slightly modified SQL INSERT INTO syntax: INSERT INTO Weather VALUES ('Los Angeles'. The SQL INSERT INTO clause facilitates the process of inserting data into a SQL table.(4) To insert the values in employee table. '10/10/2005') You can produce the same result. If you do not enter values 7 . using SQL INSERT INTO: INSERT INTO Weather (City. Date) VALUES ('Los Angeles'.

The following SQL INSERT example enters only 2 of the 3 columns in the Weather table: INSERT INTO Weather (City.for all columns. we can also insert a bulk of data into d tables by specifying the columns of the table and creating an input interface like the following: 8 . Instead of using insert into command again and again. '10/10/2005') (5) To insert various records into employee table without repeating insert command. Date) VALUES ('Boston'. then the columns you have omitted must allow NULL values or at least have a default value defined.

And the data values inserted there will be inserted into the table as below: (6) To update the records into employees table where employee name is “neha” 9 .

… (7) To increase the salary of all the employees by Rs. 10 . Column2 = Value2.12000. The SQL UPDATE clause basic syntax looks like this: UPDATE Table1 SET Column1 = Value1.The SQL UPDATE clause serves to update data in database table.

Hence the querry that will use will execute will raise the salary of all the employees in equal amount. the amount is raised by Rs.The salary of the employees in tha table can be increased by adding the equal amount to the salary column of each employee. 11 . 12000 and the corresponding changes are shown by again showing all the contents of the table. (8) To delete a particular record from employees table. Here in this particular querry.

12 .The SQL DELETE clause is used to delete data from a database table. The simplest SQL DELETE syntax looks like this: DELETE FROM Table1 The SQL DELETE statement above will delete all data from the Table1 table. Most of the time we will want to delete only table rows satisfying certain search criteria defined in the SQL WHERE clause.

13 .(9) To delete all the records from employee table. that means the query will delete all the data from the table. Hence this statement will delete all the data contained in the table. we can do that. it will not be deleted. But the structure f the table will remain stored . In the case we want to again enter the data values in the same table. but the structure will still remain preserved in the database. The delete command will delete the columns in the table. Here we use delete employees.

as we can use the DROP TABLE command. Fortunately. it would be problematic if we cannot do so because this could create a maintenance nightmare for the DBA's.(10) To drop the employee table. The syntax for DROP TABLE is DROP TABLE "table_name" So. In fact. if we wanted to drop the table called customer that we created in the CREATE TABLE section. we simply type DROP TABLE customer. 14 . Sometimes we may decide that we need to get rid of a table in the database for some reason. SQL allows us to do it.

After we have dropped the table. the query will give an error saying that the table doesn’t exist. so if we will perform any transaction on deleted table. the data as well as the structure of the table is deleted.(11) To view all the records of employee table.. 15 .

After the command returns. 16 . Changes that have not been moved to the table are not committed. whether they were made through Oracle OLAP or through another form of access (such as SQL) to the database. The COMMIT command only affects changes in workspaces that you have attached in read/write access mode. The COMMIT command executes a SQL COMMIT command. UPDATE moves changes from a temporary work area to the database table in which the workspace is stored. then you must first update the workspace using the UPDATE command.(12) To make changes permanent to the database. all committed changes are visible to other users who subsequently attach the workspace. When you want changes that you have made in a workspace to be committed when you execute the COMMIT command. All changes made in your database session are committed.

17 .(13) To undo the uncommitted change in the database. The ROLLBACK aborts the current transaction. (14) To give access rights to a user on a particular database. Syntax ROLLBACK [WORK] Description ROLLBACK rolls back the current transaction and causes all the updates made by the transaction to be discarded. WORK has no special sense and is supported for compatibility with SQL standards only.

18 . Here's the syntax of the statement: GRANT <permissions> [ON <table>] TO <user/role> [WITH GRANT OPTION] (15) To take rights from a user on a table. it's time to begin strengthening security by adding permissions. We'll accomplish this through the use of the SQL GRANT statement. Our first step will be to grant appropriate database permissions to our users.Once we've added users to our database.

it often proves necessary to revoke them at a later date. Here's the syntax: REVOKE [GRANT OPTION FOR] <permissions> ON <table> FROM <user/role> (16) To add not null constraint on the salary column of the table. Fortunately. SQL provides us with the REVOKE command to remove previously granted permissions. 19 .Once we've granted permissions.

The NOT NULL constraint enforces a column to NOT accept NULL values. a table column can hold NULL values. or update a record without adding a value to this field. 20 .By default. This means that you cannot insert a new record. The NOT NULL constraint enforces a field to always contain a value.

2 21 . PRACTICAL NO.Now whenever we try to leave the coloumn where not null constraint is applied.

put_line(‘loop exited as the value of I has reached ‘ || to_char(i)). end.TO USE A LOOP IN SQL Declare i number :=0 . Begin Loop i:=i+2. exit when i>10. Output:- 22 . end loop. dbms_output.


14. begin radius :=3. radius number(5).2) := 3. end.2). end loop.TO FIND OUT THE AREA OF A CIRCLE WHEN RADIUS IS GIVEN create table areas(radius number(5). area number(14. Output: 24 .2)).2). declare pi constant number(4. area number(14.area). while radius<=7 loop area:=pi*power(radius. radius:= radius+1. insert into areas values(radius.


INTRODUCTION TO VIEWS VIEW:.acct_fd_no.’sb432’.A view is a virtual table which provides access to a subset of columns from one or more tables. Create a view: create view v_nominees as select nominee_no. The dynamic result of one or more relational operations operating on base relations to produce new relation. It is a query stored as an object in the database which does not have its own data.’ram’). Inserting values in a view: • insert into v_nominees values (‘n100’. 26 . A view is a list of columns or a series of records retrieved from one or more existing tables or as a combination of one or more views and one or more from from nominee_mstr.

27 .Displaying view: Updating values in view: • update v_nominees set name=’vaishali’ where name=’sharan’. deleting values from view:• delete from v_nominees where name=’vaishali’.

28 .Dropping view:drop view v_nominees.

29 .

<column name2>). example: create index idxtransAcctno ON trans_mstr (trans_no. 30 . composite index: create index <index name> ON <table name> (<column name1>. example: create index idxveri Empno ON acct_mstr (veri_emp_no).PRACTICAL NO 5 PROGRAM TO CREATE AND DROP AN INDEX theory: there are two types of indexes: simple index and composite index simple index: create index <index name> ON <table name> (<column name>). acctNo).

select <index name> from <cursor indexes> unique index: create unique index <index name> ON <table name> (<column name >. Function based index: Create index <index name> ON <table name> (<function> (<column name>)). <column name>). Example: create index idx_name ON cust_mstr (upper(fname)). Alter index <index name> rebuild no reverse. Dropping an index: Drop index<index name>. alter index: example: if reverse index re built into normal index. reverse key index: create index <index name> ON <table name> (<column name>) reverse. 31 .


sal number. job varchar. sal. Name out varchar. Dno number) is Begin Insert into emp (empno. If SQL%Found Then Return (‘y’). amount number) Return number is N number. End retrieve. sal into name. deptno) values (Eno. Dno). End if. Procedure retrieve (Eno in number. 33 . End insert_oper.Create or replace package body operation as procedure insert_oper (Eno number. name varchar. sal out number) is Begin Select ename. ename. Begin Update emp set sal=sal + amount where deptno = Dno. name. Function update_oper( dno number. Else Return(‘n’). N:= SQL%RowCount. End update_oper. Return(N). Function Delete_oper (Eno number) return char is Begin Delete emp where empno=Eno. job. job. sal from emp where empno=Eno. sal.

Output:- PRACTICAL NO 7 INTRODUCTION TO TRIGGERS 34 . End operation.End delete_oper.

losal number(10).’salary’||to_char(:new.Ename). job_classification number(10)). End. Create table salgrade ( grade number(5). hisal number(10).job. If (:new.Program to explain the working of a trigger.job||’for employee’||:new. Maxsal number(10). job ON emp99 for each row Declare Minsal number(10).sal > maxsal) then Raise salary_out_of_range. Salary_out_of_range exception. Exception When salary_out_of_range then Raise_application_error(-20300. When no_data_found then Raise _application_error(-20322. Begin Select losal. maxsal from salgrade where job_classification =:new. 35 . hisal into minsal. Create or replace trigger salary_check Before insert or update of sal. Endif.’invalid job classification’||:new.sal)||’out of range for’||’job classification’||:new.sal < minsal or :new.job_classification).

OUTPUT: PRACTICAL NO 8 Write a program to find salary grade using cursor Declare 36 .

Esal emp. If Esal > 2500 and esal < 3500 then grade=”C”. Loop exit when csal%not found. esal.sal%type. Begin Epen csal. If Esal > 3500 and esal < 4500 then grade=”B”. sal from emp.Cursor csal is Select empno. 37 . Grade varchar(2). endif dbms_output. endif If Esal > 4500 and esal < 5500 then grade=”A”.empno% type. endif If Esal > 5500 then grade=”A+”. Fetch csal into eno.put_line(eno + ’has’ + grade + ’grade’). endif. Eno emp.

OUTPUT:- PRACTICAL NO 9(a) CREATE OR REPLACE PROCEDURE MY_PROC AS begin dbms_output.put_line(‘hello world’). end. 38 .endloop.

OUTPUT PRACTICAL NO 9 (b) CREATE TABLE MYTABLE (NUM_COL NUMBER. begin my_proc. create or replace procedure insertIntoTemp as v_num 1 number:=1. CHAR_COL VARCHAR2(60)). v_string1 varchar2(50):= ‘hello world!’. begin 39 .end my_proc. v_outputstr varchar2(50). end.

OUTPUT:- 40 .put_line(v_output str). select char_col into v_output str from mytable where num_col=v_num1. end insertIntoTemp.insert into mytable(num_col. char_col) values (v_num1. v_string1). end. begin insertIntoTemp. dbms_output.

41 .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.