Working with SQL in PSQL
Structured Query Language (SQL) is a database language that uses Englishlike statements to perform database operations. Both the American National Standards Institute (ANSI) and IBM have defined standards for SQL. The IBM standard is the Systems Application Architecture (SAA). The PSQL product implements most of the features of both ANSI SQL and IBM SAA SQL and provides additional extensions that neither standard specifies.
PSQL allows you to create different types of SQL statements. The following table lists the types of SQL statements you can create and the tasks you can accomplish using each type of statement:
The rest of this chapter briefly describes the SQL statements used in each statement category. For detailed information about each statement, refer to SQL Syntax Reference.
The following are the statement category overview sections found in this section:
Data Definition Statements
Data definition statements let you specify the characteristics of your database. When you execute data definition statements, PSQL stores the description of your database in a data dictionary. You must define your database in the dictionary before you can store or retrieve information.
PSQL allows you to construct data definition statements to do the following:
The following sections briefly describe the SQL statements associated with each of these tasks. For general information about defining the characteristics of your database, refer to the PSQL Programmer's Guide in the Developer Reference.
Creating, Modifying, and Deleting Tables
You can create, modify, and delete tables from a database by constructing SQL using the following statements:
Table 2
Creating and Deleting Views
You can create and delete views from a database by constructing SQL using the following statements:
Table 3
Creating and Deleting Indexes
You can create and delete indexes from a database by constructing SQL using the following statements:
Table 4
Creating and Deleting Triggers
You can create and delete triggers from a database by constructing SQL using the following statements:
Table 5
PSQL provides additional SQL control statements, which you can only use in the body of a trigger. You can use the following statements in triggers:
Table 6
Creating and Deleting Stored Procedures
A stored procedure consists of statements you can precompile and save in the dictionary. To create and delete stored procedures, construct statements using the following:
Table 7
PSQL provides additional SQL control statements, which you can only use in the body of a stored procedure. You can use the following statements in stored procedures:
Table 8
IF...THEN...ELSE
Creating and Deleting User-Defined Functions (UDF)
In addition to the built-in functions, PSQL allows you to create your own user-defined functions (UDF) and use them in PSQL statements.
A user-defined function is a database object that encapsulates one or more PSQL statements that can be reused. A user-defined function takes zero or more input arguments and evaluates a return value, which is a scalar value.
User-defined functions are always defined within the context of a database. Successful execution of this statement results in the storing of the UDF definition in the specific database. Once stored, the UDF can be modified, invoked, and deleted.
PSQL supports scalar user-defined functions.
A scalar user-defined function returns a single value of the data type specified in the RETURNS clause of the PSQL statement. A scalar UDF can contain multiple PSQL statements. You can specify any data type value for the returned data except text, ntext, image, cursor, or timestamp.
To create and delete user-defined functions, construct statements using the following:
Table 9
Data Manipulation Statements
Data manipulation statements let you access and modify the contents of your database. PSQL allows you to construct data manipulation statements to do the following:
The following sections briefly describe the SQL statements associated with each of these tasks.
Retrieving Data
All statements you use to retrieve information from a database are based on the SELECT statement.
Table 10
When you create a SELECT statement, you can use various clauses to specify different options. The types of clauses you use in a SELECT statement are as follows:
Table 11
In addition, you can use the UNION keyword to obtain a single result table from multiple SELECT queries.
Modifying Data
You can add, change, or delete data from tables and views by issuing statements such as the following:
Table 12
When you create a DELETE or UPDATE statement, you can use a WHERE clause to define search criteria that restrict the data upon which the statement acts.
Creating and Deleting Views
You can create and delete views by constructing SQL using the following statements:
Table 13
Executing Stored Procedures
A stored procedure consists of statements that you can precompile and save in the dictionary. To execute stored procedures, construct statements using the following:
Table 14
CALL or EXEC[UTE]
Executing System Stored Procedures
A system stored procedure helps you accomplish those administrative and informative tasks that are not covered by the Data Definition Language. The system stored procedures have a psp_ prefix. To execute stored procedures, construct statements using the following:
Table 15
CALL or EXEC[UTE]
For more details, see System Stored Procedures.
Executing Triggers
A trigger consists of statements you can precompile and save in the dictionary. Triggers are executed automatically by the engine when the specified conditions occur.
Data Control Statements
Data control statements let you define security for your database. When you create a dictionary, no security is defined for it until you explicitly enable security for that dictionary. PSQL allows you to construct data control statements to do the following:
*Note: If you have a Btrieve owner name set on a file that is a table in a secure database, the Master user of the database must use the owner name in any GRANT statement to grant privileges on the given table to any user, including the Master user.
The following sections briefly describe the SQL statements associated with each of these tasks.
Enabling and Disabling Security
You can enable or disable security for a database by issuing statements using the following statement:
Table 16
Creating and Deleting Users and Groups
You can create or delete users and user groups for the database by constructing SQL using the following statements:
Table 17
Granting and Revoking Rights
You can assign or remove rights from users or groups by issuing statements using the following:
Table 18