Upload
anil-bharti
View
950
Download
4
Tags:
Embed Size (px)
Citation preview
ANIL KUMAR BHARTIMCA II
SPUFI COMMAND
MADURAI KAMARAJ UNIVERSITY
Contents:
1 SPUFI . 2 Mode of use. 3 SQL command.
3.1 DDL Command.
3.2 DML Command.
3.3 TCL Command.
3.4 DCL Command.
3.5 JOIN.
3.6 VIEW.
3.7 CURSOR.
3.8 TRIGER.
3.9 FUNCTION.
3.10 PROCEDURE.
SPUFI
The DSN subcommand SPUFI executes the SQL processor using file input.SQL Processing Using File Input is a database facility invented by IBM for interfacing with their DB2 system. It is accessed from within TSO/ISPF from the DB2I Primary Option menu.SPUFI allows direct input of SQL commands in the TSO environment, rather than having them embedded within a program.
2. Mode of use
Although it is essentially an interactive tool, SPUFI operates using a pair of datasets. (A dataset on z/OS is equivalent to a file on other operating systems.) In the main SPUFI screen one specifies an input dataset and an output dataset; these can be specified once and then reused repeatedly. When the user moves on from the main screen, the standard ISPF editor is opened on the input dataset. At this point the user can enter the required SQL statements using the familiar editor.
3. SQL COMMAND
3.1. DDL (Data Definition Language). Create. Alter Drop Truncate
TABLE CREATION Syntax
CREATE TABLE <table name> ( <column_namel> <data type>((<width>) [constraint <constraint name> <constraint type>), <column_name2> <data type>((<width>), <column_name3> <data type>((<width>), <column_name4> <data typ>((cwidth>), <column_nameN> <data type>[(<width>) );
CONTINUE……..
Example CREATE TABLE Employee( empno number(4) constraint pk_empno
primary key, ename varchar2(50) constraint nn_ename
not null, salary number(10,2), hire_date date, gender char(1) constraint chk_gen
check(gender in (‘M’, ‘F’, ‘m’, ‘f’)), email varchar2(50) unique );
ALTER TABLESyntax ALTER TABLE <table name> ADD CONSTRAINT
<constraint name> <constraint type> <column name>;Syntax to add referential integrity ALTER TABLE <table name> ADD CONSTRAINT
<constraint name> FOREIGN KEY(<foreign key column>)REFERNCES
<parent table name>(<primary key column>);Example ALTER TABLE Employee ADD CONSTRAINT nn_hdate
NOT NULL hire_date; ALTER TABLE Employee ADD CONSTRAINT Ref_dept
FOREIGN KEY (deptno) REFERENCES Department(deptno);
DROPSyntax DROP <OBJECT> <object name>;Example DROP PROCEDURE ins_emp; DROP TABLE Employee; TRUNCATE Syntax TRUNCATE TABLE <table-name>;Example TRUNCATE TABLE Employee;
3.2 DML (Data Manipulation Language). Select Insert Update Delete INSERT Using this command we can append data into tables.Syntax INSERT INTO <table name>(<column_name1>,
<column_name2>, ...)VALUES (column1_value, column2_value, ...);
INSERT INTO <table-name>(<column_name2>, <column_name1>, ...) VALUES (column2-value, column1-value, ...);
INSERT INTO <table-name> VALUES (value1, value2, ...);
Example INSERT INTO Employee(empno, ename,
salary, hire_date, gender, email) VALUES(1234, ‘JOHN’, 8000, ’18-AUG-80, ‘M’, ‘[email protected]’);
INSERT INTO Employee(email , ename, empno, hire_date, gender, salary) VALUES(‘[email protected]’, ‘RHONDA’, 1235, ’24-JUL-81’, ‘F’, 7500);
INSERT INTO Employee VALUES(1236, ‘JACK’, 15000, ’23-SEP-79’, ‘m’, ‘[email protected]’);
UPDATE This command is used to modify the data existing in the
tables.Syntax UPDATE <table-name> SET <column-name> = <value>; UPDATE <table-name> SET <column-name> = <value>
WHERE <condition>; Example UPDATE Employee SET salary = 15000; UPDATE employee SET salary = 15000 WHERE empno = 1235; DELETE his command is used to remove the data from the tables.Syntax DELETE FROM <table-name>; DELETE FROM <table-name>WHERE <condition>;Example DELETE FROM Employee; DELETE FROM Employee WHERE empno = 1236;
3.3 TCL (Transaction Control Language). These commands are used to maintain the
consistency of the database. Commit Rollback Save point Commit This command is used to make the transaction
permanent. Rollback This command is used to undo the recent
transaction. Save Point This is used to rollback a transaction to a
specified point.
3.4 DCL (Data Control Language). Commit Rollback Commit This command is used to make the
transaction permanent. Rollback This command is used to undo the
recent transaction.
3.5 JOIN These are used to retrieve the data from more than one table. To
retrieve the data from more than one table the data types of fields which related to different tables need not be same while using the joins.
Types of joins: 1) Inner Join. 2) Outer Join. a) Left Outer Join. b) Right Outer Join.Examples and Description: 1 Emp EmployeeID EmployeeName
1 Ramesh 2 Sukumar 3 Ravi 4 Kalyani
2.Products: ProductID EmployeeID Productname
1 1 2 Pen 12 3 Pencil 1 2 3 Eraser 1 3 6 Book
1) Inner Join This join returns all the rows from the both
the tables where there is a match. The result set consists of only matched rows.
Syntax: select E. Employeeid,E.EmployeeName,
P.ProductName from Employees E inner join Products on E.EmployeeID=P.EmployeeID;
ResultEmployeeID EmployeeName
Productname 2 Sukumar Pen 3 Ravi Pencil 3 Ravi Eraser
2) Outer Join In outer join the resulting table may have empty columns.a) Left Outer Join Here left means first table. it returns all the rows from the first
table even though it does not have the matches in Second table. But it returns only the matched rows from the second table.
Syntax
select E. Employeeid,E.EmployeeName,P.ProductName from Employees E left join Products on E.EmployeeID=P.EmployeeID;
Result EmployeeID EmployeeName Productname
2 Sukumar Pen 3 Ravi Pencil 3 Ravi Eraser
1 Ramesh null 4 Kalyani null
b) Right Outer Join Here Right means Second table. It returns all the rows
from the second table even though it does not have the matches in First table. But it returns only the matched rows from the First table.
Syntax select E. Employeeid,E.EmployeeName, P.ProductName
from Employees E right join Products on E.EmployeeID=P.EmployeeID;
Result EmployeeID EmployeeName Productname
2 Sukumar Pen 3 Ravi Pencil 3 Ravi Eraser
6 null Book
3.6 VIEWA view is a logical or virtual table, which does not, exists
physically in the database. Views are also called as Dictionary Objects.
AdvantagesSecurity – The confidential columns can be suppressed from
viewing and manipulation. Complexity can be avoided. Logical columns can be created. Application design becomes easier. Views can be classified into two categories on the way they
a re created. 1. Simple View 2. Complex View1.Simple View If the view is created from a single table, it is called as a
simple view. We can do DML Operation, if it is Simple View.2.Complex View If the view is created on multiple tables, it is called as a
complex view.
Syntax CREATE [OR REPLACE] VIEW <view name> AS
<query> Example1 CREATE OR REPLACE VIEW data_emp AS SELECT
ename, salary FROM Employee;Example2 CREATE OR REPLACE VIEW dup_emp AS SELECT
ename, salary, salary*0.2 comm FROM Employee;DROPPING A VIEW Only the owner of the view can drop it.Syntax DROP VIEW <view name>;Example DROP VIEW data_emp;
3.7 CURSORCursors are classified in two ways 1. Implicit Cursors 2. Explicit Cursors1. Implicit Cursors For SQL Queries returning single rows PL/SQL declares
implicit cursors. For SQL Queries returning single rows PL/SQL declares
implicit cursors. These are the simple SQL statements and are written in
the BEGIN block of the PL/SQL. PL/SQL implicitly declares cursors for all DML
statements.2. Explicit Cursors Explicit cursors are used in queries that return multiple
rows. The set of rows fetched by a query is called active set. The size of the active set meets the search criteria in the select statement. Explicit cursor is declared in the DECLARE section of PL/SQL program
Declaring Cursor CURSOR <cursor-name> IS <select statement> CURSOR emp_cur IS SELECT ename FR OM EMP;Opening CursorSyntax OPEN <cursor-name>;Example OPEN emp_cur;Fetching data from CursorSyntax FETCH <cursor-name> INTO <variables>;Example FETCH emp_cur INTO ena; Closing a CursorSyntax CLOSE <cursor-name>; Example CLOSE emp_cur;
Example for a EXPLICIT CURSOR (DB2) :DECLARE ena EMP.ENAME%TYPE esa EMP.SAL%TYPE CURSOR c1 IS SELECT ename, sal FROM EMP; BEGIN OPEN c1; FETCH c1 INTO ena, esa; DBMS_OUTPUT.PUT_LINE(ena || ‘ Salary is Rs ‘ || esa); FETCH c1 INTO ena, esa; DBMS_OUTPUT.PUT_LINE(ena || ‘ Salary is Rs ‘ || esa); FETCH c1 INTO ena, esa; DBMS_OUTPUT.PUT_LINE(ena || ‘ Salary is Rs ‘ || esa); CLOSE c1; END; /
3.8 Triger A trigger is a block of statements, which are fired automatically
when a DML operation takes place. These are always associated with the database tables.
Triggers are classified as two types: 1. Row Level Triggers. 2. Statement Level Triggers.1.Row Level Triggers These triggers will be fired for each row with proportionate to
the number of rows those are begin effected the DML statement.2. Statement Level Triggers These triggers are fired once for each statement regardless with
the number of rows those are being affected by the DML statement. In DB2 triggers can be classified into two more categories such as
a) Before Triggers. b) After Triggers.a) Before Triggers These triggers are fired before the execution of the DML
statements.b) After Triggers These triggers are fired after the execution of the DML
statements.
Syntax of a Trigger(DB2) CREATE TRIGGER <trigger name> before/after <event1 or event2 event3> ON <table> [for each row] BEGIN ATOMIC <action block>;Example (DB2) DROP TRIGGER Trig_Insert go CREATE TRIGGER Trig_Insert after insert ON Employee REFERENCING new AS nnn old AS ooo BEGIN ATOMIC begin declare p_empno numeric; set p_empno = nnn; select p_empno from sysibm.sysdummy1; end; @ nnn/ooo – These are the alias names given to the inserted/updated
values.
3.9 Function It is sub-program, which is stored in the database and
whenever it is called, it does something and return some value.
Example (DB2) -- Function to multiply two numbers. create function product (a integer, b integer) returns integer language sql no external action not deterministic begin atomic declare v_pro integer; set v_pro = a*b; return v_pro ; end @
3.10 Procedure It is sub-program, which is stored in the database and whenever it is
called, it does something but doesn’t return any value. But they could return indirectly through OUT parameters. Using the stored procedures you can reduce the number of calls to the database and decrease network traffic.
Syntax of a Stored Procedure (DB2) CREATE PROCEDURE <procedure name>( [<parameter mode>]<parameter1> <data type> [<width>], [<parameter mode>]<parameter2> <data type> [<width>], [<parameter mode>]<parameter3> <data type> [<width>], [<parameter mode>]<parameter4> <data type> [<width>], . . [<parameter mode>]<parameterN> <data type> [<width>] [DYNAMIC RESULT SETS LANGUAGE <language specifier>] BEGIN <sql statements>; END <special symbol other than ‘;’> -- block terminator
Example (DB2)Procedure to insert a new row into the Employee table. DROP PROCEDURE INS_EMP -- to drop the procedure if already exist. @ CREATE PROCEDURE INS_EMP( p_empno numeric, -- by default all are IN mode parameters. p_ename varchar(50), p_salary numeric(10,2), p_hire_date datetime, P_gender char(1), P_email varchar(50), OUT p_out_param numeric -- OUT parameter declaration. ) AS LANGUAGE SQL BEGIN INSERT INTO Employee(empno, ename, salary, hire_date, gender, email) VALUES(p_empno, p_ename, p_salary, p_hire_date, p_gender, p_email); SET p_out_param=p_empno; -- Assigning the employee number to out
parameter. END @
THANK YOU
Query