PL/SQL Basics: Variables and Syntax
PL/SQL Basics: Variables and Syntax
PL/SQL does not support arrays as a native data structure type. Instead, alternatives like associative arrays (also known as index-by tables) can be used, which are essentially key-value pairs that can be indexed by integers or strings. These associative arrays allow for similar functionalities as arrays in other languages, though with differences due to not natively supporting ordered collections .
PL/SQL supports transaction control operations via statements such as COMMIT, ROLLBACK [TO], SAVEPOINT, and LOCK TABLE. Each serves to manage the state and durability of transactions performed on the database. COMMIT permanently saves changes, ROLLBACK undoes them, SAVEPOINT allows rollback to specific points, and LOCK TABLE controls concurrency by securing table access. These features ensure transactional integrity and data consistency in concurrent environments .
Values can be assigned to PL/SQL variables through direct assignments using the := operator or by executing a SELECT statement with INTO clause. In direct assignments, the value is directly calculated from expressions or other variables, e.g., 'tax := price * tax_rate;' . The SELECT INTO allows for database values to be directly assigned to a variable, e.g., 'SELECT SAL INTO CUR_SAL FROM EMP WHERE EID = ‘E12345’;'. This method fetches data from the database, providing a mechanism to link database operations with PL/SQL procedural logic .
PL/SQL syntax rules dictate that all commands must terminate with a semicolon. Additionally, statements are coded in a free field format, meaning they do not need to be aligned in specific columns. The case of commands is irrelevant, meaning they can be written in uppercase or lowercase. Comments can be added using -- for single-line comments and /* */ for block comments .
PL/SQL supports several logical operations and predicates which include simple logic operations and logical operators like AND, OR, NOT, as well as predicates like BETWEEN, IS NULL, LIKE, EXISTS, ANY, and ALL. These operations and predicates enhance the ability to form complex conditional expressions in PL/SQL programs, thereby increasing their control flow capabilities .
Using the %type attribute is advantageous because it abstracts the datatype to always match the definition used in the database schema. This minimizes errors and maintenance efforts if the underlying schema changes. For instance, if a column's datatype changes in the database, variables using %type automatically adapt to this change without needing modification, ensuring consistency across code and database schema .
The %type attribute allows a variable to be declared with the same datatype as a column in a database table without needing to know the column's exact type. This provides flexibility and reduces maintenance burden, as changes to the column type do not require altering the variable declaration .
The %rowtype attribute is used to declare a record in PL/SQL that corresponds to the row structure of a database table or view. This is significant because it allows developers to work with complete rows as a single entity, ensuring that the record has the same types and structure as the SELECT command used for retrieval. This approach is advantageous for data integrity and reduces the complexity of handling individual columns explicitly when processing database rows .
Values can be assigned to PL/SQL variables using direct assignments with the := operator, which is a straightforward expression evaluation like ‘tax := price * tax_rate’ . Another method is through a SELECT statement, like ‘SELECT SAL INTO CUR_SAL FROM EMP WHERE EID = ‘E12345’’, which retrieves a database value and assigns it to a variable . The main difference is that direct assignment uses expressions and existing variable values, while SELECT-based assignments obtain data directly from the database.
A constant in PL/SQL is defined similarly to a variable but includes the keyword CONSTANT and must be assigned an initial value at the time of declaration, which cannot be changed later. For example, a constant is declared as 'bonus_multiplier CONSTANT NUMBER (3,2) := 0.10;', indicating that the value of 'bonus_multiplier' cannot be altered after its declaration .