Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

SQL Cheat Sheet Adv

The document discusses views, stored procedures, and transactions in SQL. Views allow representing data from tables in alternative ways. Stored procedures allow saving and reusing SQL code. Transactions allow committing or rolling back changes to a database.

Uploaded by

Aashish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

SQL Cheat Sheet Adv

The document discusses views, stored procedures, and transactions in SQL. Views allow representing data from tables in alternative ways. Stored procedures allow saving and reusing SQL code. Transactions allow committing or rolling back changes to a database.

Uploaded by

Aashish Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

28/03/2024, 16:29 about:blank

SQL Cheat Sheet: Views, Stored Procedures and Transactions


Views

Topic Syntax Description Example


CREATE VIEW view_name AS SELECT column1, A CREATE VIEW is an alternative way of CREATE VIEW EMPSALARY AS SELECT EMP_ID,
Create View column2, ... FROM table_name WHERE representing data that exists in one or more F_NAME, L_NAME, B_DATE, SEX, SALARY FROM
condition; tables. EMPLOYEES;
CREATE OR REPLACE VIEW EMPSALARY AS
CREATE OR REPLACE VIEW view_name AS The CREATE OR REPLACE VIEW command SELECT EMP_ID, F_NAME, L_NAME, B_DATE,
Update a View SELECT column1, column2, ... FROM SEX, JOB_TITLE, MIN_SALARY, MAX_SALARY
table_name WHERE condition; updates a view. FROM EMPLOYEES, JOBS WHERE
EMPLOYEES.JOB_ID = JOBS.JOB_IDENT;
Use the DROP VIEW statement to remove a view DROP VIEW EMPSALARY;
Drop a View DROP VIEW view_name;
from the database.

Stored Procedures in IBM Db2 using SQL

--#SET TERMINATOR @ CREATE PROCEDURE


RETRIEVE_ALL

LANGUAGE SQL
--#SET TERMINATOR @ CREATE PROCEDURE A stored procedure is a prepared SQL code READS SQL DATA
PROCEDURE_NAME
that you can save, so the code can be reused DYNAMIC RESULT SETS 1
LANGUAGE over and over again. BEGIN
Stored
Procedures BEGIN The default terminator for a stored procedure is DECLARE C1 CURSOR
semicolon(;). To set a different terminator we WITH RETURN FOR
END
@ use SET TERMINATOR clause followed by the SELECT * FROM PETSALE;
terminator such as ‘@’.
OPEN C1;

END
@

Stored Procedures in MySQL using phpMyAdmin

DELIMITER // DELIMITER //
A stored procedure is a prepared SQL code
CREATE PROCEDURE PROCEDURE_NAME that you can save, so the code can be reused CREATE PROCEDURE RETRIEVE_ALL()
over and over again.
BEGIN BEGIN
Stored
Procedures END // The default terminator for a stored procedure is SELECT * FROM PETSALE;
semicolon (;). To set a different terminator we
DELIMITER ; use DELIMITER clause followed by the END //
terminator such as $$ or //.
DELIMITER ;

Transactions with Db2

A COMMIT
command is
used to
persist the
changes in CREATE TABLE employee(ID INT, Name VARCHAR(20), City VARCHAR(20), Salary INT, Age INT);
COMMIT;
the database.
INSERT INTO employee( ID, Name, City, Salary, Age) VALUES( 1, ‘Priyanka pal’, ‘Nasik’, 36000, 21), (2, ‘Riy
Commit ‘Bangalor’, 82000, 29);
command The default
terminator SELECT *FROM employee;
for a COMMIT;
COMMIT
command is
semicolon
(;).
Rollback ROLLBACK; A ROLLBACK As auto-commit is enabled by default, all transactions will be committed. We need to disable this opti
command command is see how rollback works.
used to For db2, we have to disable auto-commit manually. Click the gear icon located on the right side of the
rollback the Assistant window. Next, select the “On Success” drop-down and choose “commit after the last statement
transactions script” Remember to save your changes!
which are
not saved in
the database.

The default
terminator
for a
ROLLBACK
command is
semicolon
(;).

about:blank 1/3
28/03/2024, 16:29 about:blank

INSERT INTO employee VALUES (3, ‘Swetha Tiwari’, ‘Kanpur’, 38000, 38);

SELECT *FROM employee;


ROLLBACK;
SELECT *FROM employee;

Transactions with MySQL

CREATE TABLE employee(ID INT, Name


VARCHAR(20), City VARCHAR(20), Salary
INT, Age INT);
A COMMIT command is used to persist the changes START TRANSACTION;
COMMIT; in the database.
Commit INSERT INTO employee( ID, Name, City,
command The default terminator for a COMMIT Salary, Age) VALUES( 1, ‘Priyanka pal’,
‘Nasik’, 36000, 21), (2, ‘Riya
command is semicolon (;). chowdary’, ‘Bangalor’, 82000, 29);

SELECT *FROM employee;


COMMIT;
As auto-commit is enabled by
default, all transactions
will be committed. We need to
disable this option to see
A ROLLBACK command is used to rollback the how rollback works. For MySQL
ROLLBACK; transactions which are not saved in the database. use the command “SET
Rollback autocommit = 0;”
command The default terminator for a ROLLBACK INSERT INTO employee VALUES (3, ‘Swetha
command is semicolon (;). Tiwari’, ‘Kanpur’, 38000, 38);

SELECT *FROM employee;


ROLLBACK;
SELECT *FROM employee;

Db2 Transactions using Stored Procedure

--#SET TERMINATOR @ CREATE PROCEDURE


TRANSACTION_ROSE LANGUAGE SQL MODIFIES
SQL DATA

BEGIN

DECLARE SQLCODE INTEGER DEFAULT 0;


DECLARE retcode INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR
–#SET TERMINATOR @ SQLEXCEPTION
SET retcode = SQLCODE;
CREATE PROCEDURE PROCEDURE_NAME
A COMMIT command is used to persist the changes UPDATE BankAccounts
BEGIN SET Balance = Balance-200
Commit in the database.
WHERE AccountName = ‘Rose’;
command COMMIT;
The default terminator for a COMMIT UPDATE BankAccounts
END command is semicolon (;). SET Balance = Balance-300
@ WHERE AccountName = ‘Rose’;

IF retcode < 0 THEN


ROLLBACK WORK;

ELSE
COMMIT WORK;

END IF;

END
@
Rollback --#SET TERMINATOR @ A ROLLBACK command is used to rollback the --#SET TERMINATOR @ CREATE PROCEDURE
command transactions which are not saved in the database. TRANSACTION_ROSE LANGUAGE SQL MODIFIES
CREATE PROCEDURE PROCEDURE_NAME

about:blank 2/3
28/03/2024, 16:29 about:blank
BEGIN The default terminator for a ROLLBACK SQL DATA

ROLLBACK;
command is semicolon (;). BEGIN

COMMIT; DECLARE SQLCODE INTEGER DEFAULT 0;


DECLARE retcode INTEGER DEFAULT 0;
END DECLARE CONTINUE HANDLER FOR
@ SQLEXCEPTION
SET retcode = SQLCODE;

UPDATE BankAccounts
SET Balance = Balance-200
WHERE AccountName = ‘Rose’;

UPDATE BankAccounts
SET Balance = Balance-300
WHERE AccountName = ‘Rose’;

IF retcode < 0 THEN


ROLLBACK WORK;

ELSE
COMMIT WORK;

END IF;

END
@

MySQL Transactions using Stored Procedure

DELIMITER //

CREATE PROCEDURE TRANSACTION_ROSE()

BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION


DELIMITER // BEGIN
ROLLBACK;
CREATE PROCEDURE PROCEDURE_NAME
RESIGNAL;
BEGIN A COMMIT command is used to persist the changesEND;

Commit in the database. START TRANSACTION;


COMMIT;
command UPDATE BankAccounts
END //
The default terminator for a COMMIT SET Balance = Balance-200
command is semicolon (;). WHERE AccountName = ‘Rose’;
DELIMITER ;
UPDATE BankAccounts
SET Balance = Balance-300
WHERE AccountName = ‘Rose’;

COMMIT;

END //

DELIMITER ;
DELIMITER //

CREATE PROCEDURE TRANSACTION_ROSE()

BEGIN
DELIMITER // DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
CREATE PROCEDURE PROCEDURE_NAME ROLLBACK;
RESIGNAL;
BEGIN
A ROLLBACK command is used to rollback the END;
ROLLBACK; transactions which are not saved in the database. START TRANSACTION;
Rollback
command COMMIT; UPDATE BankAccounts
The default terminator for a ROLLBACK SET Balance = Balance-200
END // command is semicolon (;). WHERE AccountName = ‘Rose’;

DELIMITER ; UPDATE BankAccounts


SET Balance = Balance-300
WHERE AccountName = ‘Rose’;

COMMIT;

END //

DELIMITER ;

Author(s)
D.M Naidu

Changelog
Date Version Changed by Change Description
2022-10-04 1.0 D.M.Naidu Initial Version

about:blank 3/3

You might also like