Sei sulla pagina 1di 37

DDL Commands

Definition:
Experiment:1 Date:

DDL (Data Definition Language)

DDL statements are used to alter/modify a database or table structure and schema. Thes
handle the design and storage of database objects. The statements belonging to DDL ar

1. CREATE – create a new Table, database, schema

Syntax:

CREATE TABLE <table_name> (

<column_name1> <datatype1> <constraint1> <column_name2> <datatype2> <constraint2>


<constraint-list> ) ; 2. ALTER – alter existing table, column description

Syntax:

Alter table <table name> add <column name><data type>;

3. Truncate – to remove all rows from table.


Syntax:
TRUNCATE table <table name>;

4. Drop : Deletes existing objects from database.


Syntax: Drop <table name>;

OUTPUT:
PROCEDURE:

1. Create a table named ‘student’ using the DDL command ‘create’ with the
following columns: register_no as int, name as varchar and subject as varchar.

2. Add an additional column ‘phone’ to the table using the DDL command ‘alter’.

3. Using the DDL command ‘truncate’, truncate the table.

4. Using the DDL command ‘drop’, drop the table.

Program: 1. Create table student(id int,name varchar(10),subject varchar(20)); 2. Insert into


student(id,name,subject)values(1,’A’,’physics’); 3. Select * from student; 4. Alter table stu
Add Phone int; 5. Truncate table student; 6. Drop table student;

RESULT:

The DDL commands for student database is executed and output is verified.

1. Definition:
Date:
Experiment:2

DML Commands
1.1 DML (Data Manipulation Language)

DML statements affect records in a table. These are basic operations we perform on data such
a few records from a table, inserting new records, deleting unnecessary records, and updatin
existing records. DML statements include the following:

1. SELECT – select records from a table


Syntax: SELECT column1, column2, ... FROM table_name;

2. INSERT – insert new records


Syntax:

INSERT INTO table

(column-1,column-2,...column-n)

(value-1, value-2, ... value-n);

3. UPDATE – update/Modify existing records


Syntax: UPDATE table SET column1=value1,column2=value2... WHERE condition; 4. DELETE – d
existing records
Syntax: DELETE from table WHERE condition;
OUTPUT:

5.PROCEDURE:

1. Create a table named ‘student’ using the DDL command ‘create’ with the following columns: regis
name as varchar and subject as varchar. 2. Insert 5 rows into the table created using the ‘insert’ DML
with values corresponding to each column. 3. Using the DML command ‘select’, display the created t
an additional column ‘phone’ to the table using the DDL command ‘alter’. 5. Update the table using t
command ‘update’ giving appropriate conditions for it. 6. Using the DML command ‘select’, display
table. 7. Delete rows from the table using the DML command ‘delete’. 8. Using the DDL command ‘t
truncate the table. 9. Using the DDL command ‘drop’, drop the table. Program:
6.Create table student(id int,name varchar(10),subject varchar(20));

7.insert into student(id,name,subject)

Values(1,’EZIO’,’COMPUTER');

8.Insert into student(id,name,subject)

Values(2,’EVIE’,’PHYSICS’);

9.insert into student(id,name,subject)

Values(3,’EDEN’,’CHEMISTRY’);

10.insert into student(id,name,subject)

Values(4,’ETHERAPE’,’CIVIL’);

11.insert into student(id,name,subject)

Values(5,’EDGAR’,’Management’);

12.Select * from student;

13.Alter table student

Add PHONE INT;

14.Update student

Set PHONE=’9442879291’ WHERE id=’05’;

15.Update student

Set PHONE=’8754426950’

Where id=’1’;
16.Select * from student;

17.Delete from student

Where id=’4’;

18.Select * from student;

19.Truncate table student;

20.Select * from student;

21.Drop table student;

RESULT:

The DML commands for student database is executed and output is verified.

Experiment:3 Date:
Select Distinct, And, Or, Order by Commands
Definition 1. SELECT DISTINCT:– The SELECT DISTINCT statement is used to
return only distinct (different) values. Syntax: SELECT DISTINCT
column1, column2, ... FROM table_name;

2. AND: – The AND operator displays a record if all the conditions


separated by AND is TRUE. Syntax: SELECT column1, column2, ... FROM table_name WHE
AND condition2 AND condition3 ...;UPDATE – update/Modify existing records

3. OR – The OR operator displays a record if any of the conditions


separated by OR is TRUE. Syntax: SELECT column1, column2, ... FROM
table_name WHERE condition1 OR condition2 OR condition3 ...;

4. ORDER BY:- The ORDER BY keyword is used to sort the result-set in


ascending or descending order. The ORDER BY keyword sorts the records in ascendin
default. To sort the records in descending order, use the DESC keyword.

Syntax: SELECT column1, column2, ... FROM table_name ORDER BY


column1, column2, ... ASC|DESC;

PROCEDURE:

1. Create a table named ‘student’ using the DDL command ‘create’ with the following columns: id as
varchar and subject as varchar. 2. Insert 10 rows into the table created using the ‘insert’ DML comma
corresponding to each column. 3. Using the DML command ‘select’, display the created table. 4. Use
Distinct, And, Or, Order by statements in the table. 5. Using the DML command ‘select’, display the
6. Using the DDL command ‘truncate’, truncate the table. 7. Using the DDL command ‘drop’, drop th

Program:

1. Create table student(id int,name varchar(10),subject varchar(20)); 2. insert into


student(id,name,subject)
Values(1,’EZIO’,’Maths’); 3. Insert into student(id,name,subject)
Values(2,’EVIE’,’PHYSICS’); 4. insert into
student(id,name,subject)
Values(3,’ETHERAPE’,’CHEMISTRY’); 5. insert into
student(id,name,subject)
Values(4,’EDGAR’,’Programming’); 6. insert into
student(id,name,subject)
Values(5,’ELLEN’,’Management’); 7. Select distinct subject from
student; 8. Select name,id from student
Where id=5 ; 9. Select name, id from student

Where id=3 or subject=’Maths’;

10. Select name, id from student


Order by name; 11. Select * from student 12. Select name, id
from student
Order by id desc; 13. Truncate table student; 14. Drop
table student;
RESULT:The Select Distinct, And, Or and Order By commands for student database is execute
is verified.

Experiment:4

Advanced Select Statements

DATE:

Aim:

Write a program to execute advanced select statements.

SYNTAX:

1. SELECT DISTINCT:– The SELECT DISTINCT statement is used to


return only distinct (different) values.

SELECT DISTINCT column1, column2, ... FROM table_name;

2. AND: – The AND operator displays a record if all the conditions


separated by AND is TRUE.

SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condi
...;UPDATE – update/Modify existing records

3. OR – The OR operator displays a record if any of the conditions


separated by OR is TRUE.

SELECT column1, column2, ... FROM table_name WHERE condition1 OR


condition2 OR condition3 ...;

4. ORDER BY:- The ORDER BY keyword is used to sort the result-set in


ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the r
descending order, use the DESC keyword.

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2,


... ASC|DESC;

OUTPUT:
Create table student(id int,name varchar(10),subject varchar(20)); insert into student
Values(1,’Arun’,’Math’);

Insert into student Values(2,’Raghav’,’ML’);

insert into student Values(3,’vivek’,’CD’);

insert into student Values(4,’shamin’,’HCI’);

insert into student Values(5,’Prarthana’,’Management’);

select * from student;

Select distinct subject from student;

Select name,id from student Where id=’5’ and subject=’ML’;

Select name, id from student Where id=’3’ or


subject=’Math’;

Select name, id from student Order by name;

Select * from student Order by id asc;


RESULT:

The Select Distinct, And, Or and Order By commands for student database is executed and
verified.
Experiment:5 Date:
Constraints

Definition:

Constraints are the rules that we can apply on the type of data in a table. That is, we can sp
limit on the type of data that can be stored in a particular column in a table using constrain
constraints used are 1. NOT NULL: This constraint tells that we cannot store a null value i
column. That is, if a column is specified as NOT NULL then we will not be able to store n
particular column any more. 2. UNIQUE: This constraint when specified with a column, te
the values in the column must be unique. That is, the values in any row of a column must n
repeated. 3. PRIMARY KEY: A primary key is a field which can uniquely identify each ro
table. And this constraint is used to specify a field in a table as primary key. 4. FOREIGN
Foreign key is a field which can uniquely identify each row in a another table. And this co
used to specify a field as Foreign key. 5. CHECK: This constraint helps to validate the val
column to meet a particular condition. That is, it helps to ensure that the value stored in a c
meets a specific condition. 6. DEFAULT: This constraint specifies a default value for the c
when no value is specified by the user. Syntax for using these constraints
are 1.CREATE TABLE table_name ( column1 datatype constraint,
column2 datatype constraint, column3 datatype constraint, .... );
OUTPUT:

PROCEDURE:

1. Create a table named ‘CUSTOMER’ using the DDL command ‘create’ with the following columns
NULL Primary Key, firstname as varchar NOT NULL, lastname as varchar NOT NULL, salary int, p
DEFAULT ‘Bombay’and CHECK whether salary>=10000 2. Insert 5 rows into the table created usin
DML command with values corresponding to each column. 3. Using the DML command ‘select’, disp
created table. 4. Using the DDL command ‘truncate’, truncate the table. 5. Using the DDL command
the table.
Program: 1. create table CUSTOMER(cid int NOT NULL PRIMARY
KEY,firstname varchar(20) NOT NULL,lasttname varchar(20) NOT NULL,salary int,p
varchar(20) DEFAULT 'Bombay',check(salary>=10000)); 2. insert into CUSTOMER
values('1','Arun','Prasad','20000','chennai'); 3. insert into CUSTOMER
values('2','raghav','orton’,'68000','bangalore'); 4. insert into CUSTOMER
values('3','Vivek','choco','10000','goa'); 5. insert into CUSTOMER
values('4','shamin','kukku','60000','kerala'); 6. insert into CUSTOMER
values('5','prarthana','ravi','55000',DEFAULT); 7. select * from customer where
salary>25000; 8. select * from customers where place=’Bombay’;

RESULT:

The constraints for customer database and product database are applied, executed and outp
verified.

Experiment:6 Date:
Join Commands

1. Definition A JOIN clause is used to combine rows from two or more tables, based on a
column between them. Different types of Join are 1. Inner Join:-
Returns records that have matching values in both tables Syntax: SELECT column_nam
FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;

2. Left (OUTER) Join:


Return all records from the left table, and the matched records from the right table. Syn
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name =
table2.column_name;
3. Right (OUTER) Join:
Return all records from the right table, and the matched records from the left table. Syntax
column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.col
4. Full (OUTER) Join:Return all records when there is a match in either
left or right table.
OUTPUT:

Syntax:

SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON


table1.column_name = table2.column_name;

5. Natural Join:
A Natural Join is a Join operation that creates an implicit join clause for you based on t
columns in the two tables being joined. Common columns are columns that have the sa
both tables. A Natural Join can be an Inner join, a Left Outer join, or a Right Outer join
default is Inner join. Syntax: TableExpression NATURAL [ { LEFT | RIGHT } [ OUTE
INNER ] JOIN { TableViewOrFunctionExpression | ( TableExpression ) }

Program: Program: 1. Create table student(id int,name varchar(10),dept varchar(20)); 2


into student(id,name,dept) Values('1,'Arun','Cse'); 3. insert into student(id,name,dept)
Values('2','Raghav','Cse'); 4. insert into student(id,name,dept)
Values('3',’shamin','Mechanical'); 5. insert into student(id,name,dept) Values('4','vivek'
6. insert into student(id,name,dept) Values('5','prarthana','Cse'); 7. Create table sport(id
int,name varchar(10),sports
varchar(20),event_type varchar(20)); 8. Insert into
sport(id,name,sports,event_type) Values('01','arun','Cycling','Individual');
9. Insert into sport(id,name,sports,event_type)
Values('02','Raghav','Volleyball','Team'); 10. Insert into
sport(id,name,sports,event_type) Values('03','shamin','Cricket','Team');
11. Insert into sport(id,name,sports,event_type)
Values('04','vivek','Football','Team'); 12. Insert into
sport(id,name,sports,event_type)
Values('05',’prarthana','Athletics','Individual');

13. Select student.id,sport.Event_type from student Inner join sport on student.id=sport


14. Select student.id,student.name,sport.Event_type from student left join sport on
student.id=sport.id; 15. Select student.id,student.name,sport.Event_type from student ri
join sport on student.id=sport.id; 16. Select student.id,student.name,sport.Event_type fr
student full join sport on student.id=sport.id; 17. Truncate table student; 18. Truncate ta
sports; 19. Drop table student; 20. Drop table sports;

RESULT:

The join statements for student and sports database are executed and output is verified.

Definition:
Date:
Experiment:7

SQL Aggregate Functions


SQL has several inbuilt mathematical functions. They are

Count: The Count function counts the number of cells that contain numbers, and cou
numbers within the list of arguments. Syntax: SELECT COUNT(column_name) FROM ta
WHERE condition;

1. AVG: The AVG() function returns the average value of a numeric


column. Syntax: SELECT AVG(column_name) FROM
table_name WHERE condition;

2. SUM: The SUM() function returns the total sum of a numeric


column. Syntax: SELECT SUM(column_name) FROM
table_name WHERE condition;

3. MAX: The MAX() function returns the maximum value of an


expression. Syntax: SELECT MAX(column_name) FROM table_name WHERE condition; 4. M
MIN() function returns the smallest value of the selected
column. Syntax: SELECT MIN(column_name) FROM
table_name WHERE condition;
OUTPUT:

PROGRAM:

1. create table student(id int primary key not null, name varchar(20), age
int, marks int); 2. insert into student values('1', 'Arun','21','80'); 3. insert into student
values('2', 'raghav','20','92'); 4. insert into student values('3', ‘vivek','21','95'); 5.
insert into student values('4', 'shamin','21','74'); 6. insert into student values('5',
'prarthana','19','75'); 7. select * from student; 8. select count(age) from student
where age>19;

9. select avg(marks) from student; 10. select sum(marks) from


student
where marks>85; 11. select max(marks) from student; 12. select
min(age) from student;

RESULT:

The SQL functions for student database is executed and output is verified.

OUTPUT:

Subqueries
Date:
Experiment:8

Definition: A Subquery or Inner query or a Nested query is a query within another SQL qu
embedded within the WHERE clause. A subquery is used to return data that will be used i
query as a condition to further restrict the data to be retrieved. Subqueries can be used with
SELECT, INSERT, UPDATE, and DELETE statements. 1. Subqueries with the SELECT
Syntax is SELECT column_name [, column_name ] FROM table1 [,
table2 ] WHERE column_name OPERATOR (SELECT column_name
[, column_name ] FROM table1 [, table2 ] [WHERE])

2. Subqueries with the INSERT Statement:


Syntax is INSERT INTO table_name [ (column1 [, column2 ]) ] SELECT [
*|column1 [, column2 ] FROM table1 [, table2 ] [ WHERE VALUE OPERATOR
]

3. Subqueries with the UPDATE Statement


Syntax is UPDATE table SET column_name = new_value [WHERE OPERATOR [ VALU
(SELECT COLUMN_NAME FROM TABLE_NAME) [WHERE) ] 4. Subqueries with th
DELETE Statement
Syntax is DELETE FROM TABLE_NAME [WHERE
OPERATOR [ VALUE ] (SELECT COLUMN_NAME FROM
TABLE_NAME)

[WHERE) ]

PROGRAM:

1. CREATE TABLE PRODUCT(P_ID INT NOT NULL,P_NAME VARCHAR(20) N


NULL,BRAND VARCHAR(20) NOT NULL,PRICE VARCHAR(20) NOT NULL,PR
KEY(P_ID)); 2. CREATE TABLE CUSTOMER(C_ID INT NOT NULL,C_P
VARCHAR(20) NOT NULL,P_ID INT NOT NULL,QUANTITY INT,PRIMARY KE
3. INSERT INTO PRODUCT
VALUES(1,'FRUITY','ITC',20); 4. INSERT INTO PRODUCT
VALUES(2,'IPHONE','APPLE',60000); 5. Insert INTO PRODUCT
VALUES(3,'J5
PRIME','SAMSUNG',13000); 6. INSERT INTO PRODUCT
VALUES(4,'BABYFOOD','CERELAC',60); 7. INSERT INTO CUSTOMER
VALUES(100,'MAGGI',5,1); 8. INSERT INTO CUSTOMER
VALUES(101,'BABYFOOD',4,2); 9. INSERT INTO CUSTOMER
VALUES(102,'FRUITY',1,1 ); 10. INSERT INTO CUSTOMER
VALUES(103,'5STAR',6,2); 11. SELECT * FROM PRODUCT; 12. SELECT * FROM CUS
13. SELECT C_ID FROM CUSTOMER WHERE P_ID IN(SELECT
P_ID FROM PRODUCT); 14. SELECT C_ID,C_P FROM CUSTOMER WHERE
QUANTITY>ALL(SELECT QUANTITY FROM CUSTOMER); 15. SELECT C_ID,C_P FR
CUSTOMER WHERE
QUANTITY>SOME(SELECT QUANTITY FROM CUSTOMER); 16. UPDATE
CUSTOMER SET P_ID=(SELECT P_ID FROM
PRODUCT WHERE BRAND='ITC'); 17. SELECT * FROM CUSTOMER; 18. DELETE FR
CUSTOMER WHERE P_ID=(SELECT P_ID
FROM PRODUCT WHERE BRAND='ITC'); 19. SELECT * FROM CUSTOMER;
20. Drop table CUSTOMER; 21. Drop table PRODUCT;
RESULT:The view commands for customer database is executed and output is verified.

OUTPUT:

Experiment:9 Date:
Views

Definition: A view is a virtual table based on the result-set of an SQL statement. A view c
rows and columns, just like a real table. The fields in a view are fields from one or more re
in the database. Syntax: CREATE VIEW view_name AS SELECT column1, column2, ... FROM tabl
WHERE condition;

PROGRAM: 1. create table CUSTOMER(cid int NOT NULL PRIMARY


KEY,firstname varchar(20) NOT NULL,lastname varchar(20) NOT NULL,salary int,plac
varchar(20) DEFAULT 'vijayawada',check(salary>=10000)); 2. insert into CUSTOMER
values('1','Arun','prasad','20000','chennai'); 3. insert into CUSTOMER
values('2','raghav','orton','18000','bangalore'); 4. insert into CUSTOMER
values('3','Vivek','choco','10000','Goa'); 5. insert into CUSTOMER
values('4','shaminn','kukku','60000','kerala'); 6. insert into CUSTOMER
values('5','prarthana','ravi','55000',DEFAULT); 7. insert into CUSTOMER
values('6','bhaskar','arubadai','11000','ASSAM'); 8. insert into CUSTOMER
values('7','Durga','siva','12000','GUJARAT'); 9. insert into CUSTOMER
values('8',’kamalesh','prakash','13500','Goa'); 10. insert into CUSTOMER
values('9','bhanu','prasad','14500','DUBAI'); 11. insert into CUSTOMER
values('10',’barry','ellen','21000','DEFAULT'); 12. create view as TOM as

select cid, firstname, salary from CUSTOMER

where salary>20000;

13. select * from TOM;


14.create or replace view TOM as

select cid, firstname,lastname, salary from CUSTOMER

where salary>20000;

15.select * from TOM;

16.Drop view tom;

RESULT:The view commands for customer database are executed and output is verified.
OUTPUT:

EXPERIMENT 10 PL/SQL PROGRAMS EX.NO:10

DATE:

Aim: To execute the PL/SQL programs. Definition:PL/SQL stands for Procedural Language extensions to SQL.
extension of Structured Query Language (SQL) that allows the programmer to write code in a procedural format.
the data manipulation power of SQL with the processing power of procedural language to create super powerful
queries.Similar to other database languages, it gives more control to the programmers by the use of loops, condit
object-oriented concepts. Syntax: DECLARE
<declarations section> BEGIN
<executable command(s)> EXCEPTION
<exception handling> END; Program 1: To write a PL/SQL program for checking if the number is a palindrom
set serveroutput on
declare n number; m number; rev number:=0; r
number; begin n:=&n; m:=n; while n>0 loop
r:=mod(n,10); rev:=(rev*10)+r; n:=trunc(n/10);

end loop; if m=rev then dbms_output.put_line('number is palindrome');

OUTPUT:

else dbms_output.put_line('number is not palindrome');

end if; / PROGRAM 2: PL/SQL Program to Find Greatest of Three Numbers

Set serveroutput on declare


a number:=10; b number:=12; c number:=5; begin

dbms_output.put_line('a='||a||' b='||b||' c='||c); if a>b AND a>c then dbms_output.put_line('a is

greatest'); else
if b>a AND b>c then dbms_output.put_line('b is greatest'); else

dbms_output.put_line('c is greatest'); end if; end if; end; /

PROGRAM 3: PL/SQL Program for Armstrong Number

Set serveroutput on;

declare
n number:=407; s number:=0; r number; len
number; m number;

begin
m:=n;

len:=length(to_char(n));

while n>0 loop


r:=mod(n,10); s:=s+power(r,len); n:=trunc(n/10); end
loop;

if m=s then
dbms_output.put_line('armstrong number'); else
dbms_output.put_line('not armstrong number'); end if;

end; / Result: The pl/Sql programs were successfully implemented and output verified succe

OUTPUT:
EXPIREMENT 11

PROCEDURES

EX.NO:

DATE:

Aim: To implement PL/SQL programs using procedures.

Definition: The PL/SQL stored procedure or simply a procedure is a PL/SQL block which performs one or more
tasks. It is just like procedures in other programming languages.

The procedure contains a header and a body.

Header: The header contains the name of the procedure and the parameters or variables passed to the proced

Body: The body contains a declaration section, execution section and exception section similar to a general
block.

Syntax:

CREATE [OR REPLACE] PROCEDURE procedure_name

[ (parameter [,parameter]) ]

IS
[declaration_section]

BEGIN
executable_section

[EXCEPTION

exception_section]

END [procedure_name];

Procedure 1 : procedure to find minimum of 2 values

DECLARE
a number;

b number;
c number;

PROCEDURE findMin(x IN number, y IN number, z OUT number) IS

BEGIN

IF x < y THEN

z:= x;

ELSE

z:= y;

END IF;

END;

BEGIN

a:= 23;

b:= 45;

findMin(a, b, c);

dbms_output.put_line(' Minimum of (23, 45) : ' || c);

END;

OUTPUT:
Procedure 2 : procedure to find square of a number

DECLARE

a number;

PROCEDURE squareNum(x IN OUT number) IS

BEGIN

x := x * x;

END;

BEGIN

a:= 23;

squareNum(a);

dbms_output.put_line(' Square of (23): ' || a);

END;

/ RESULT: PL/SQL procedures are created successfully and output is verified.

OUTPUT:

EXPERIMENT 12

PL/SQL FUNCTIONS
EX.NO:12

DATE:

Aim: To implement PL/SQL programs using functions.

Definition: The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between procedure
function is, a function must always return a value, and on the other hand a procedure may or may not return a val
this, all the other things of PL/SQL procedure are true for PL/SQL function too.

Syntax:

CREATE [OR REPLACE] FUNCTION function_name [parameters]

[(parameter_name [IN | OUT | IN OUT] type [, ...])]

RETURN return_datatype

{IS | AS}

BEGIN
< function_body >

END [function_name];

Program CREATE OR REPLACE FUNCTION

CREATE OR REPLACE FUNCTION c_to_f (degree NUMBER) RETURN


NUMBER IS buffer NUMBER; BEGIN buffer := (degree * 9/5) + 32; RETURN
buffer; END;
OUTPUT:

; show errors select c_to_f(45) from dual;

Program 2: PL/SQL function to find maximum of 2 numbers

DECLARE

a number;

b number;

c number;

FUNCTION findMax(x IN number, y IN number)

RETURN number

IS z number;

BEGIN

IF x > y THEN

z:= x;

ELSE

Z:= y;

END IF;
RETURN z;

END;

BEGIN
a:= 23;

b:= 45;

c := findMax(a, b);

dbms_output.put_line(' Maximum of (23,45): ' || c);

END;

/
RESULT: PL/SQL Functions are created successfully and output is verified successfully.

OUTPUT:

PL/SQL CURSORS

Ex No.13

Date:

Aim: To implement PL/SQL CURSORS

Definition: A cursor is a temporary work area created in the system memory when a SQL

statement is executed. A cursor contains information on a select statement and the rows of

data accessed by it. This temporary work area is used to store the data retrieved from the

database, and manipulate this data. A cursor can hold more than one row, but can process

only one row at a time. The set of rows the cursor holds is called the active set.

Cursor 1: PLSQL program to update the salary table by 500 .

DECLARE

total_rows number(2);

BEGIN

UPDATE customers

SET salary = salary + 500;

IF sql%notfound THEN

dbms_output.put_line('no customers selected');

ELSIF sql%found THEN

total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');

END IF;

END;

OUTPUT:

CURSOR 2: PLSQL program to retrieve customer’s name and address

SET SERVEROUTPUT ON SIZE 1000000;

DECLARE

CURSOR cur_cus IS

SELECT name,

age,

address

FROM employees e

INNER JOIN departments d ON d.manager_id = e.employee_id;

r_chief cur_chief%ROWTYPE;

BEGIN

OPEN cur_chief;

LOOP

FETCH cur_chief INTO r_chief;


EXIT WHEN cur_chief%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(r_chief.department_name || ' - ' ||

r_chief.first_name || ',' ||

r_chief.last_name);

END LOOP;

CLOSE cur_chief;

END;

/ RESULT: The PL/SQL program to create cursors and implement in programs have been executed succe

Output 1:

Aim:
Experiment:14 Date:
Triggers

Create PL/SQL programs for triggers.

Definition:

1. Creates a DML, DDL, or logon trigger. A trigger is a special type of


stored procedure that automatically executes when an event occurs in the database serv
triggers execute when a user tries to modify data through a data manipulation language
event. DML events are INSERT, UPDATE, or DELETE statements on a table or view.
triggers fire when any valid event is fired, regardless of whether or not any table rows a
These are created by default when DML statements like, INSERT, UPDATE, and DEL
statements are executed. They are also created when a SELECT statement that returns j
is executed. Syntax is: CREATE [ OR ALTER ] TRIGGER [ schema_name . ]trigger_n
table } [ WITH <dml_trigger_option> [ ,...n ] ] { FOR | AFTER } { [ INSERT ] [ , ] [ U
, ] [ DELETE ] } AS { sql_statement [ ; ] [ ,...n ] }

<dml_trigger_option> ::=
[ NATIVE_COMPILATION ] [ SCHEMABINDING ] [
EXECUTE AS Clause ]

Program :

Trigger:1 CREATE OR REPLACE TRIGGER LED_F BEFORE UPDATE ON LE


FOR EACH ROW WHEN (NEW.AMOUNT/OLD.AMOUNT >1.0) BEGIN

OUTPUT 2:
INSERT INTO LED_AUDIT
VALUES(:OLD.ACTIONDATE,:OLD.ITEM,:OLD.QUANTITY, :OLD:AMOUN

UPDATE LEDGER SET AMOUNT=20000; SELECT * FROM


LEDGER;

Trigger 2: CREATE OR REPLACE TRIGGER LED_T BEFORE INSERT OR UPD


OF ITEM ON LEDGER
FOR EACH ROW BEGIN :NEW.UPPERITEM:=UPPER(:NEW.ITEM); END;
ALTER TRIGGER LED_T DISABLE; UPDATE LEDGER SET ITEM=’YYY’;
SELECT * FROM LEDGER; INSERT INTO ACCOUNT
VALUES(1,’CSE’,2500); INSERT INTO ACCOUNT VALUES(2,’IT’,2000);
SELECT * FROM ACCOUNT; INSERT INTO LOAN
VALUES(‘JHON’,’CSE’,2005); INSERT INTO LOAN
VALUES(‘SMITH’,’IT’,2000); SELECT * FROM LOAN;

OUTPUT:
Trigger 3:

CREATE OR REPLACE TRIGGER Q BEFORE DELETE ON


LOAN

FOR EACH ROW

BEGIN

IF :OLD.AMOUNT>0 THEN

RAISE_APPLICATION_ERROR(-20001,’DELETION IS NOT
ALLOWED’)

END IF;

END;

SELECT * FROM LOAN;

DELETE LOAN WHERE CUSNAME=’JHON’;

Trigger 4:

CREATE OR REPLACE TRIGGER Q

AFTER INSERT OR UPDATE ON ACCOUNT


FOR EACH ROW

BEGIN

UPDATE CC SET CNT=CNT+1;

END;

DELETE AMOUNT WHERE ACC_NO=1;

RESULT: The trigger programs have been implemented successfully.


OUTPUT:

Experiment:15 Date:
Application Program

Aim:

Create a student management php application program and connect it to SQL


database.

Abstract: student management application helps us in management of student,


candidates, their position and updation of their profile. The application uses a
XAMPP software to connect the front end to the database. The front end of the
application is developed using PHP and backend is developed using SQL database.

Table information : Student table : Create table student(s_no int , reg no int , name
varchar(10), email varchar(20) ,mob no int , course varchar(7),subject
varchar(60));

Course table :

Create table course(s_no int, short_name varchar(5), Full_name varchar(30),


created_date date);

Subject Table: Create Table subject(s_no int,subject_1


varchar(15),subject_2 varchar(15), subject_3 varchar(15));

Procedure: 1. Write codes in PHP to make the front end of the


application. 2. After making front end, using XAMPP, in web
browser, go to localhost/phpmyadmin. In that, click on SQL. Then create a
database by creating two tables Student course and subject.
3. Create Database Connection File In PHP using PHP
custom function include (include ‘connection.php’) on the top of your code and call
its function and use it. 4. unzip phpmvc.zip into your target project folder. 5. create
database tables int your db. 6. In the global.php configure your db connections and
configure your application base paths. 7. Write the model classes according to the db
tables 8. Create controllers for each table. Foreach controller
create views for the Actions. 9. Now open the browser and using your
localhost/remote url access the application

Program:

Table Creation:

Student table : Create table student(s_no int , reg no int , name varchar(10), email
varchar(20) ,mob no int , course varchar(7),subject varchar(60));

Course table :

Create table course(s_no int, short_name varchar(5), Full_name varchar(30),


created_date date);
Subject Table:

Create Table subject(s_no int,subject_1 varchar(15),subject_2


varchar(15), subject_3 varchar(15));

Connection:

<?php

class Database {

private $_connection;

private static $_instance; //The single instance

private $_host = "localhost";


private $_username = "root";

private $_password = "";

private $_database = "schoolmanagement";

/*

Get an instance of the Database

@return Instance

*/

public static function getInstance() {

if(!self::$_instance) { // If no instance then make one

self::$_instance = new self();

return self::$_instance;
}

// Constructor

public function __construct() {

$this->_connection = new mysqli($this->_host, $this->_username, $this->_password,


$this- >_database);

// Error handling

if(mysqli_connect_error()) {

trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error(),

E_USER_ERROR);

// Magic method clone is empty to prevent duplication of connection


private function __clone() { }

// Get mysqli connection

public function getConnection() {

return $this->_connection;

} ?> Add Course:

<?php

session_start ();
if (! (isset ( $_SESSION ['login'] ))) {

header ( 'location:../index.php' );

} include('../config/DbFunction.php');

$obj=new DbFunction();

$rs=$obj->showCourse();

$rs1=$obj->showCountry();

$ses=$obj->showSession();

$res1=$ses->fetch_object();

//$res1->session;

if(isset($_POST['submit'])){

Potrebbero piacerti anche