0% found this document useful (0 votes)
24 views12 pages

Mitali Bhavsar - Pr-0 - PPUD

The document outlines the prerequisites for PPUD in Computer Engineering, detailing various SQL commands categorized into DDL, DML, DCL, TCL, triggers, cursors, and procedures. It provides syntax and examples for each command, including creating, altering, and dropping tables, as well as manipulating data and managing permissions. Additionally, it explains the use of triggers and cursors in SQL, along with the structure and execution of stored procedures.

Uploaded by

mitalibhavsar19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

Mitali Bhavsar - Pr-0 - PPUD

The document outlines the prerequisites for PPUD in Computer Engineering, detailing various SQL commands categorized into DDL, DML, DCL, TCL, triggers, cursors, and procedures. It provides syntax and examples for each command, including creating, altering, and dropping tables, as well as manipulating data and managing permissions. Additionally, it explains the use of triggers and cursors in SQL, along with the structure and execution of stored procedures.

Uploaded by

mitalibhavsar19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Department of Computer Engineering PPUD 196140307008

Prerequisites topics for PPUD

 DDL command :-
Data Definition Language(DDL) is a subset of SQL and a part of DBMS(Database Management
System). DDL consist of Commands to commands like CREATE, ALTER, TRUNCATE and
DROP. These commands are used to create or modify the tables in SQL.

1. Create
2. Alter
3. Truncate
4. drop

1) CREATE

CREATE statements is used to define the database structure schema:


 Syntax:

CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

 For example:

Create database university;


Create table students;
Create view for_students;

2) DROP
Drops commands remove tables and databases from RDBMS.

 Syntax

DROP TABLE ;

 For example:

Drop object_type object_name;


Drop database university;
Drop table student;

3) ALTER
Alters command allows you to alter the structure of the database.

 Syntax:

To add a new column in the table

ALTER TABLE table_name ADD column_name COLUMN-definition;

Government Polytechnic For Girls-Ahmedabad Page No. 1


Department of Computer Engineering PPUD 196140307008
To modify an existing column in the table:

ALTER TABLE MODIFY(COLUMN DEFINITION....);

 For example:

Alter table guru99 add subject varchar;

4) TRUNCATE:
This command used to delete all the rows from the table and free the space containing the table.

 Syntax:

TRUNCATE TABLE table_name;

 Example:

TRUNCATE table students;

Government Polytechnic For Girls-Ahmedabad Page No. 2


Department of Computer Engineering PPUD 196140307008

 DML command :-
Data Manipulation Language (DML) allows you to modify the database instance by inserting,
modifying, and deleting its data. It is responsible for performing all types of data modification in a
database.

1. Insert
2. Update
3. Delete

1) INSERT:
This is a statement is a SQL query. This command is used to insert data into the row of a table.

 Syntax:

INSERT INTO TABLE_NAME (col1, col2, col3,.... col N)


VALUES (value1, value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3, .... valueN);

 For example:

INSERT INTO students (RollNo, FIrstName, LastName) VALUES ('60', 'Tom', Erichsen');

2) UPDATE:
This command is used to update or modify the value of a column in the table.

 Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE


CONDITION]

 For example:

UPDATE students
SET FirstName = 'Jhon', LastName= 'Wick'
WHERE StudID = 3;

3) DELETE:
This command is used to remove one or more rows from a table.

 Syntax:

DELETE FROM table_name [WHERE condition];

Government Polytechnic For Girls-Ahmedabad Page No. 3


Department of Computer Engineering PPUD 196140307008
 For example:

DELETE FROM students


WHERE FirstName = 'Jhon';

Government Polytechnic For Girls-Ahmedabad Page No. 4


Department of Computer Engineering PPUD 196140307008

 DCL command :-
DCL (Data Control Language) includes commands like GRANT and REVOKE, which are useful to
give “rights & permissions.” Other permission controls parameters of the database system.

1. Grant
2. Revoke

1) Grant:
This command is use to give user access privileges to a database.

 Syntax:

GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

 For example:

GRANT SELECT ON Users TO'Tom'@'localhost;

2) Revoke:
It is useful to back permissions from the user.

 Syntax:

REVOKE privilege_nameON object_nameFROM {user_name |PUBLIC |role_name}

 For example:

REVOKE SELECT, UPDATE ON student FROM BCA, MCA;

Government Polytechnic For Girls-Ahmedabad Page No. 5


Department of Computer Engineering PPUD 196140307008

 TCL command :-
Transaction control language or TCL commands deal with the transaction within the database.

1. Commit
2. Rollback
3. Savepoint

1) Commit
This command is used to save all the transactions to the database.

 Syntax:

Commit;

 For example:

DELETE FROM Students


WHERE RollNo =25;
COMMIT;

2) Rollback
Rollback command allows you to undo transactions that have not already been saved to the database.

 Syntax:

ROLLBACK;

 Example:

DELETE FROM Students


WHERE RollNo =25;

3) SAVEPOINT
This command helps you to sets a savepoint within a transaction.

 Syntax:

SAVEPOINT SAVEPOINT_NAME;

 Example:

SAVEPOINT RollNo;

Government Polytechnic For Girls-Ahmedabad Page No. 6


Department of Computer Engineering PPUD 196140307008

 Trigger :-
A SQL trigger is a database object which fires when an event occurs in a database. We can execute a
SQL query that will "do something" in a database when a change occurs on a database table such as a
record is inserted or updated or deleted. For example, a trigger can be set on a record insert in a
database table. For example, if you want to increase the count of blogs in the Reports table when a
new record is inserted in the Blogs table, we can create a trigger on the Blogs' table on INSERT and
update the Reports table by increasing blog count to 1.

 Types of Trigger :-

1. BEFORE Trigger: BEFORE triggers run the trigger action before the triggering
statement is run.

2. AFTER Trigger: AFTER triggers run the trigger action after the triggering statement is
run.

 Syntax of creating Trigger :-

 create trigger [trigger_name]


[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]

 create trigger [trigger_name]: Creates or replaces an existing trigger with the


trigger_name.
 [before | after]: This specifies when the trigger will be executed.
 {insert | update | delete}: This specifies the DML operation.
 on [table_name]: This specifies the name of the table associated with the trigger.
 [for each row]: This specifies a row-level trigger, i.e., the trigger will be executed for
each row being affected.
 [trigger_body]: This provides the operation to be performed as trigger is fired

 Example :-

Create a row level trigger for the customers table that would fire for INSERT or UPDATE or
DELETE operations performed on the CUSTOMERS table. This trigger will display the salary
difference between the old values and new values:

 create or replace trigger display_salary_changes


before delete or insert or update on customer1
for each row

Government Polytechnic For Girls-Ahmedabad Page No. 7


Department of Computer Engineering PPUD 196140307008
when (new.id > 0)
declare
sal_diff number;
begin
sal_diff := :new.salary - :old.salary;
dbms_output.put_line('old salary: ' || :old.salary);
dbms_output.put_line('new salary: ' || :new.salary);
dbms_output.put_line('salary difference: ' || sal_diff);
end;
/

 update customer1 set salary=4000 where id=1;

Government Polytechnic For Girls-Ahmedabad Page No. 8


Department of Computer Engineering PPUD 196140307008

 Cursor :-
A cursor in SQL is a temporary work area created in system memory when a SQL statement is
executed. A SQL cursor is a set of rows together with a pointer that identifies a current row. It is a
database object to retrieve data from a result set one row at a time.

 Types of Cursor :-

1. Implicit Cursor: If the Oracle engine opened a cursor for its internal processing it is
known as an Implicit Cursor. It is created “automatically” for the user by Oracle when a
query is executed and is simpler to code.

2. Explicit Cursor: A Cursor can also be opened for processing data through a PL/SQL
block, on demand. Such a user-defined cursor is known as an Explicit Cursor.

 Syntax of creating Cursor:-

 CURSOR cursor_name IS select_statement;


cursor_name – A suitable name for the cursor.
select_statement – A select query which returns multiple rows

 Example :-

Write a PL/SQL code to calculate the total and the percentage of marks of the students in four
subjects from the table- Student with the schema given below. STUDENT ( RNO , S1 , S2, S3,
S4, total, percentage)

 declare
t_total student_.total%type;
p_per student_.percentage%type;
cursor stu is select * from student_;
rw stu%rowtype;
begin
open stu;
loop
fetch stu into rw;
exit when stu%notfound;
t_total:=rw.s1+rw.s2+rw.s3+rw.s4;
p_per:=t_total/4;
update student_ set total=t_total,percentage=p_per where rno=rw.rno;
end loop;
close stu;
end;
/

Government Polytechnic For Girls-Ahmedabad Page No. 9


Department of Computer Engineering PPUD 196140307008

Government Polytechnic For Girls-Ahmedabad Page No. 10


Department of Computer Engineering PPUD 196140307008

 Procedure :-
The PL/SQL stored procedure or simply a procedure is a PL/SQL block which performs one or more
specific tasks. It is just like procedures in other programming languages. The procedure contains a
header and a body.

 Syntax of creating Procedure:-

 CREATE OR REPLACE PROCEDURE name


[(parameter[, parameter, ...])]
AS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception handlers]
END [name];

 Example :-

Write PL/SQL code block to find out the maximum value of three numbers using procedure.

Create procedure :-

create or replace procedure maxvalue(a in number,b in number,c in number) is


begin
if (a>b) and (a>c) then
dbms_output.put_line('A is greatest');
elsif (b>a) and (b>c) then
dbms_output.put_line('B is greatest');
else
dbms_output.put_line('C is greatest');
end if;
end;
/

Execute procedure :-

declare
a number;
b number;
c number;
begin
a:=3;
b:=4;
c:=7;
maxvalue(a,b,c);
dbms_output.put_line('Maximum value is: '||c);
end;
/

Government Polytechnic For Girls-Ahmedabad Page No. 11


Department of Computer Engineering PPUD 196140307008

Government Polytechnic For Girls-Ahmedabad Page No. 12

You might also like