Broad Network


SQL Compound Statements in MySQL

Handling MySQL Events with Triggers and Procedures Using SQL – Part 2

Division 5

Forward: In this part of the series, we look at SQL compound statements in MySQL.

By: Chrysanthus Date Published: 6 Aug 2012

Introduction

This is part 2 of my series, Handling MySQL Events with Triggers and Stored Procedures Using SQL. You must have read all the different parts of the series (or equivalent tutorials from elsewhere) up to this point. In this part of the series, we look at SQL compound statements in MySQL.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Compound Statement
We have seen individual SQL statements in the previous series. There are times when you would want to treat a group of SQL statements as one unit. In that case you would have to group them into what is called a compound statement. It is simple to do this. There is a SQL statement called the BEGIN statement. This statement has two reserved words: BEGIN and END. Just put all the SQL statements you want to treat as a unit between the BEGIN and END reserved words. You place semicolons at the end of each SQL statement as usual. You do not place a semicolon after BEGIN; you place a semicolon after END since from BEGIN to END is actually one statement, a compound statement.

The Syntax of the BEGIN statement is:

[begin_label:] BEGIN
    [statement_list]
END [end_label]

Apart from the reserved words, BEGIN and END and statement-list, every other item of the BEGIN statement is optional. We shall start by looking at the statement_list.

Use of a Compound Statement
A compound statement is used to accomplish a particular task in SQL Programming. An example of such a task is an event handler, called trigger. When you are using SQL, you are doing programming. I have not approach the study of database in terms of programming in order to make the study easy, but that is what you are doing, with SQL.

The group of SQL statements of interest forms the, statement-list, enclosed by the BEGIN and END reserved words. You can have some instructions before the word BEGIN, as the [ begin-label : ] option in the syntax. These instructions do not end in semicolons; they are still part of the compound statement. You can also have something after the END reserved word, as the [ end-label ] option in the syntax, which is still part of the compound statement.

The reserved word, END actually ends the BEGIN statement that encloses normal SQL statements.

Delimiter
Here, delimiter means what ends a SQL statement. As the SQL statements in the BEGIN compound statement ends with a semicolon, this conflicts with the semicolon of the END reserved word of the whole compound BEGIN statement. So type your compound statement like this:

Delimiter //
[begin_label:]
BEGIN
    [statement_list]
END;
//

So you begin with “Delimiter //” and you end with // as shown above.

Well, let us stop here and continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message