49
Guru Nanak Dev Engineering Collage, Ludhiana. Submitted To Submitted By 1

Reference Practicle file for RDBMS-II

Embed Size (px)

DESCRIPTION

Reference Practicle file for RDBMS-II for 6th Semester CSE PTU Students

Citation preview

Page 1: Reference Practicle file for RDBMS-II

Guru Nanak Dev Engineering Collage, Ludhiana.

Submitted To Submitted By

Er. Gunjan Oberoi Nitish Kamal

(Deptt. Of CSE ) D3 CSE (A2)

95065/90370305200

1

Page 2: Reference Practicle file for RDBMS-II

2

Page 3: Reference Practicle file for RDBMS-II

INDEX

S.no Name of Practical P.no

01 To Execute Various Queries Using Commands Of Sql 02

02 To Use A Loop In Sql 19

03 To Find Out The Area Of A Circle When Radius Is Given 21

04 Introduction To Views 23

05 Program To Create And Drop An Index 28

06 Introduction To Packages 31

07 Introduction To Triggers 33

08 Write A Program To Find Salary Grade Using Cursor 35

09(a) Create Or Replace Procedure My_Proc As 37

09(b) Create Table Mytable (Num_Col Number, Char_Col Varchar2(60)); 38

3

Page 4: Reference Practicle file for RDBMS-II

PRACTICAL NO. 1

TO EXECUTE VARIOUS QUERIES USING COMMANDS OF SQL

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

salary.

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));

4

Page 5: Reference Practicle file for RDBMS-II

(2) To add the following fields to the employee table: employee number, date of birth, phone number.

The ALTER TABLE statement allows you to rename an existing table. It can also be used to add, modify, or drop a column from an existing table.

To add a column to an existing table, the ALTER TABLE syntax is:

ALTER TABLE table_name ADD column_name column-definition;

5

Page 6: Reference Practicle file for RDBMS-II

(3) To modify the width of the field in employee table.

You can use the modify SQL command if you need to resize a column in MySQL. By doing this you can allow more or less characters than before. It is written as:

alter table modify [column name] VARCHAR(5) ;

example:

alter table people modify name VARCHAR(35) ; This changes the column called "name" on the table called "people" to now allow 35 characters.

6

Page 7: Reference Practicle file for RDBMS-II

(4) To insert the values in employee table.

The SQL INSERT INTO clause facilitates the process of inserting data into a SQL table. Here is how you can insert a new row into the Weather table, using SQL INSERT INTO:

INSERT INTO Weather (City, AverageTemperature, Date)VALUES ('Los Angeles', 20, '10/10/2005')You can produce the same result, with a slightly modified SQL INSERT INTO syntax:

INSERT INTO WeatherVALUES ('Los Angeles', 20, '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. If you do not enter values for all columns, then the columns you have omitted must allow NULL values or at least have a default value defined. The following SQL INSERT example enters only 2 of the 3 columns in the Weather table:

INSERT INTO Weather (City, Date)VALUES ('Boston', '10/10/2005')

7

Page 8: Reference Practicle file for RDBMS-II

(5) To insert various records into employee table without repeating insert command.

Instead of using insert into command again and again, 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

Page 9: Reference Practicle file for RDBMS-II

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”

The SQL UPDATE clause serves to update data in database table. The SQL UPDATE clause basic syntax looks like this:

UPDATE Table1SET Column1 = Value1, Column2 = Value2, …

(7)To increase the salary of all the employees by Rs.12000.9

Page 10: Reference Practicle file for RDBMS-II

The salary of the employees in tha table can be increased by adding the equal amount to the salary column of each employee.

Hence the querry that will use will execute will raise the salary of all the employees in equal amount. Here in this particular querry, the amount is raised by Rs. 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.

10

Page 11: Reference Practicle file for RDBMS-II

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.

11

Page 12: Reference Practicle file for RDBMS-II

(9) To delete all the records from employee table.

The delete command will delete the columns in the table. Here we use delete employees; that means the query will delete all the data from the table. But the structure f the table will remain stored . it will not be deleted. In the case we want to again enter the data values in the same table, we can do that.

Hence this statement will delete all the data contained in the table, but the structure will still remain preserved in the database.

12

Page 13: Reference Practicle file for RDBMS-II

(10) To drop the employee table.

Sometimes we may decide that we need to get rid of a table in the database for some reason. In fact, it would be problematic if we cannot do so because this could create a maintenance nightmare for the DBA's. Fortunately, SQL allows us to do it, as we can use the DROP TABLE command. The syntax for DROP TABLE is

DROP TABLE "table_name"

So, if we wanted to drop the table called customer that we created in the CREATE TABLE section, we simply type

DROP TABLE customer.

13

Page 14: Reference Practicle file for RDBMS-II

(11) To view all the records of employee table.

After we have dropped the table, the data as well as the structure of the table is deleted.. so if we will perform any transaction on deleted table, the query will give an error saying that the table doesn’t exist.

(12) To make changes permanent to the database.

14

Page 15: Reference Practicle file for RDBMS-II

The COMMIT command executes a SQL COMMIT command. All changes made in your database session are committed, whether they were made through Oracle OLAP or through another form of access (such as SQL) to the database.

When you want changes that you have made in a workspace to be committed when you execute the COMMIT command, then you must first update the workspace using the UPDATE command. UPDATE moves changes from a temporary work area to the database table in which the workspace is stored. Changes that have not been moved to the table are not committed.

The COMMIT command only affects changes in workspaces that you have attached in read/write access mode. After the command returns, all committed changes are visible to other users who subsequently attach the workspace.

(13) To undo the uncommitted change in the database.15

Page 16: Reference Practicle file for RDBMS-II

The ROLLBACK aborts the current transaction.

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.

(14) To give access rights to a user on a particular database.

16

Page 17: Reference Practicle file for RDBMS-II

Once we've added users to our database, it's time to begin strengthening security by adding permissions. Our first step will be to grant appropriate database permissions to our users. We'll accomplish this through the use of the SQL GRANT statement.

Here's the syntax of the statement:

GRANT <permissions> [ON <table>]TO <user/role>[WITH GRANT OPTION]

17

Page 18: Reference Practicle file for RDBMS-II

(15) To take rights from a user on a table.

Once we've granted permissions, it often proves necessary to revoke them at a later date. Fortunately, SQL provides us with the REVOKE command to remove previously granted permissions. Here's the syntax:

REVOKE [GRANT OPTION FOR] <permissions>ON <table>FROM <user/role>

18

Page 19: Reference Practicle file for RDBMS-II

(16) To add not null constraint on the salary column of the table.

By default, a table column can hold NULL values.

The NOT NULL constraint enforces a column to NOT accept NULL values.

19

Page 20: Reference Practicle file for RDBMS-II

The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.

Now whenever we try to leave the coloumn where not null constraint is applied.

20

Page 21: Reference Practicle file for RDBMS-II

PRACTICAL NO. 2

TO USE A LOOP IN SQL

Declare

i number :=0 ;

Begin

Loop

i:=i+2;

exit when i>10;

end loop;

dbms_output.put_line(‘loop exited as the value of I has reached ‘ || to_char(i));

end;

21

Page 22: Reference Practicle file for RDBMS-II

Output:-

22

Page 23: Reference Practicle file for RDBMS-II

PRACTICAL NO. 3

TO FIND OUT THE AREA OF A CIRCLE WHEN RADIUS IS GIVEN

create table areas(radius number(5), area number(14,2));

declare

pi constant number(4,2) := 3.14;

radius number(5);

area number(14,2);

begin

radius :=3;

while radius<=7

loop

area:=pi*power(radius,2);

insert into areas values(radius,area);

radius:= radius+1;

end loop;

end;

23

Page 24: Reference Practicle file for RDBMS-II

Output:

24

Page 25: Reference Practicle file for RDBMS-II

PRACTICAL NO 4

INTRODUCTION TO VIEWS

VIEW:- A view is a virtual table which provides access to a subset of columns from one or more tables. It is a query stored as an object in the database which does not have its own data.

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 tables. The dynamic result of one or more relational operations operating on base relations to produce new relation.

Create a view:

create view v_nominees asselect nominee_no,acct_fd_no,name from from nominee_mstr;

Inserting values in a view:

insert into v_nominees values (‘n100’,’sb432’,’ram’);

25

Page 26: Reference Practicle file for RDBMS-II

Displaying view:

26

Page 27: Reference Practicle file for RDBMS-II

Updating values in view:

update v_nominees set name=’vaishali’ where name=’sharan’;

deleting values from view:-

delete from v_nominees where name=’vaishali’;

27

Page 28: Reference Practicle file for RDBMS-II

Dropping view:-

drop view v_nominees;

28

Page 29: Reference Practicle file for RDBMS-II

29

Page 30: Reference Practicle file for RDBMS-II

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>);

example: create index idxveri Empno ON acct_mstr (veri_emp_no);

composite index:

create index <index name> ON <table name> (<column name1>, <column name2>);

30

Page 31: Reference Practicle file for RDBMS-II

example: create index idxtransAcctno ON trans_mstr (trans_no, acctNo);

select <index name> from <cursor indexes>

unique index:

create unique index <index name> ON <table name> (<column name >, <column name>);

reverse key index:

create index <index name> ON <table name> (<column name>) reverse;

alter index:

example: if reverse index re built into normal index.

Alter index <index name> rebuild no reverse;

Function based index:

Create index <index name> ON <table name> (<function> (<column name>));

Example: create index idx_name ON cust_mstr (upper(fname));

31

Page 32: Reference Practicle file for RDBMS-II

Dropping an index:

Drop index<index name>;

32

Page 33: Reference Practicle file for RDBMS-II

PRACTICAL NO 6

INTRODUCTION TO PACKAGES

Create or replace package body operation as procedure insert_oper (Eno number, name varchar, job varchar, sal number, Dno number) is

Begin

Insert into emp (empno, ename, job, sal, deptno) values (Eno, name, job, sal, Dno);

End insert_oper;

Procedure retrieve (Eno in number, Name out varchar, sal out number) is

Begin

Select ename, sal into name, sal from emp where empno=Eno;

End retrieve;

Function update_oper( dno number, amount number)

Return number is

N number;

Begin

Update emp set sal=sal + amount where deptno = Dno;

N:= SQL%RowCount;

Return(N);

End update_oper;

Function Delete_oper (Eno number) return char is

Begin

Delete emp where empno=Eno;

If SQL%Found Then

33

Page 34: Reference Practicle file for RDBMS-II

Return (‘y’);

Else

Return(‘n’);

End if;

End delete_oper;

End operation;

Output:-

34

Page 35: Reference Practicle file for RDBMS-II

PRACTICAL NO 7

INTRODUCTION TO TRIGGERS

Program to explain the working of a trigger.

Create table salgrade ( grade number(5),

losal number(10),

hisal number(10),

job_classification number(10));

Create or replace trigger salary_check

Before insert or update of sal, job ON emp99 for each row

Declare

Minsal number(10);

Maxsal number(10);

Salary_out_of_range exception;

Begin

Select losal, hisal into minsal, maxsal from salgrade where job_classification =:new.job;

If (:new.sal < minsal or :new.sal > maxsal) then

Raise salary_out_of_range;

Endif;

Exception

When salary_out_of_range then

35

Page 36: Reference Practicle file for RDBMS-II

Raise_application_error(-20300,’salary’||to_char(:new.sal)||’out of range for’||’job classification’||:new.job||’for employee’||:new.Ename);

When no_data_found then

Raise _application_error(-20322,’invalid job classification’||:new.job_classification);

End;

OUTPUT:

36

Page 37: Reference Practicle file for RDBMS-II

PRACTICAL NO 8

Write a program to find salary grade using cursor

Declare

Cursor csal is

Select empno, sal from emp;

Eno emp.empno% type;

Esal emp.sal%type;

Grade varchar(2);

Begin

Epen csal;

Loop exit when csal%not found;

Fetch csal into eno, esal;

If

Esal > 2500 and esal < 3500

then grade=”C”;

endif;

If

Esal > 3500 and esal < 4500

then grade=”B”;

endif

If

Esal > 4500 and esal < 5500

37

Page 38: Reference Practicle file for RDBMS-II

then grade=”A”;

endif

If

Esal > 5500

then grade=”A+”;

endif

dbms_output.put_line(eno + ’has’ + grade + ’grade’);

endloop;

end;

OUTPUT:-

38

Page 39: Reference Practicle file for RDBMS-II

PRACTICAL NO 9(a)

CREATE OR REPLACE PROCEDURE MY_PROC AS

begin

dbms_output.put_line(‘hello world’);

end my_proc;

begin

my_proc;

end;

OUTPUT

39

Page 40: Reference Practicle file for RDBMS-II

PRACTICAL NO 9 (b)

CREATE TABLE MYTABLE (NUM_COL NUMBER, CHAR_COL VARCHAR2(60));

create or replace procedure insertIntoTemp as

v_num 1 number:=1;

v_string1 varchar2(50):= ‘hello world!’;

v_outputstr varchar2(50);

begin

insert into mytable(num_col, char_col) values (v_num1, v_string1);

select char_col into v_output str from mytable where num_col=v_num1;

dbms_output.put_line(v_output str);

end insertIntoTemp;

begin

insertIntoTemp;

end;

40

Page 41: Reference Practicle file for RDBMS-II

OUTPUT:-

41