Sei sulla pagina 1di 41

Guru Nanak Dev Engineering Collage, Ludhiana.

Submitted To

Submitted By

Er. Gunjan Oberoi (Deptt. Of CSE )

Nitish Kamal D3 CSE (A2) 95065/90370305200

INDEX

S.no
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));

P.no
02 19 21 23 28 31 33 35 37 38

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

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

(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

(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 Weather VALUES ('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
7

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

(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:

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 Table1 SET Column1 = Value1, Column2 = Value2,

(7) To

increase the salary of all the employees by Rs.12000.

10

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.

11

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.

12

(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.

13

(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.
14

(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 doesnt exist.

15

(12)

To make changes permanent to the database.

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.

16

(13)

To undo the uncommitted change in the database.

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.


17

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]

(15)

To take rights from a user on a table.


18

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>

(16)

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


19

By default, a table column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values.

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.
20

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

PRACTICAL NO. 2

21

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;

Output:-

22

PRACTICAL NO. 3
23

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;

Output:

24

PRACTICAL NO 4
25

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 as select nominee_no,acct_fd_no,name from from nominee_mstr;

Inserting values in a view:


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

26

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;

27

Dropping view:drop view v_nominees;

28

29

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>); example: create index idxtransAcctno ON trans_mstr (trans_no, acctNo);

30

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

Dropping an index:

Drop index<index name>; 31

PRACTICAL NO 6
INTRODUCTION TO PACKAGES
32

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 Return (y); Else Return(n); End if;
33

End delete_oper; End operation;

Output:-

PRACTICAL NO 7

INTRODUCTION TO TRIGGERS

34

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

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

OUTPUT:

PRACTICAL NO 8

Write a program to find salary grade using cursor

Declare
36

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 then grade=A; endif If Esal > 5500 then grade=A+; endif dbms_output.put_line(eno + has + grade + grade);
37

endloop; end;

OUTPUT:-

PRACTICAL NO 9(a)

CREATE OR REPLACE PROCEDURE MY_PROC AS


begin dbms_output.put_line(hello world);
38

end my_proc; begin my_proc; end; OUTPUT

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
39

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;

OUTPUT:-

40

41

Potrebbero piacerti anche