GaussDB SQL basic syntax example-BOOLEAN expression

Table of contents

I. Introduction

2. Introduction to BOOLEAN expressions in GaussDB SQL

1. Concept

2. Composition

3. Grammar examples

3. Basic application in GaussDB SQL

1. Example 1, using comparison operators

2. Example 2, using logical operators

3. Example 3, using IS NOT NULL operator

4. Example 4, using the like pattern matching operator

4. Summary

I. Introduction

SQL is a standard computer language used to access and manipulate databases. SQL standards supported by GaussDB (main features of SQL2, SQL3 and SQL4 are supported by default).

This series will be introduced based on the "Cloud Database GaussDB—SQL Reference".

2. Introduction to BOOLEAN expressions in GaussDB SQL

1, concept

In the GaussDB database, the BOOLEAN expression is a very common expression type. It is used to compare two conditions to determine whether they aretrue or false a>. BOOLEAN expressions can be used for conditional judgment or in loop statements as Termination condition. The syntax is very simple and only requires the use of logical operators to compare two conditions. GaussDB SQL supports logical operators such as AND and OR, which can combine results into more complex Boolean expressions.

2, composition

  • Operators: comparison operators (such as =, <>, <, >, <=, >=) and logical operators (such as AND, OR, NOT, etc.).
  • Operand: A field value or constant used for comparison.

3, example language

The screenshot below is the SQL part of cursor use. SQL involves BOOLEA expressions for conditional judgment and < a i=3>Loop statement, please refer to:

1) Conditional judgment, see red box

"v_salary>=20000",In this example, when v_salary >= 20000, the UPDATE statement after THEN is executed.

2) Loop statement, see blue box

"%NOTFOUND", is one of the attributes of the cursor, used to control the program flow or understand the status of the program. This property is true when the most recent DML (Data Manipulation Language) operation (such as INSERT, UPDATE, DELETE, etc.) did not affect any rows. 'EXIT WHEN c1%NOTFOUND;' will be executed.

3. Basic application in GaussDB SQL

Use Boolean expressions to filter results based on specific conditions and only return data that meets the conditions. Here are some examples of using Boolean expressions in SELECT lists.

1, Example 1, use comparison operator

--Determine whether it is a high salary based on whether the salary is greater than 2w, and return TRUE or FALSE

SELECT *

               ,(salary > 20000) AS high_salary

   FROM company;

In the above SQL example, we select name, age, address, salary and a Boolean expression (salary > 20000) from the company table. This expression is used to determine whether the employee's salary is high. The high_salary column in the result set will display the Boolean value TRUE or FALSE.

2, Example 2, using logical operators

--Determine whether it is a valid age based on whether the age is between 18-60, and return TRUE or FALSE

SELECT * 

              ,(age >= 18 AND age <= 60) AS valid_age

   FROM company;

In the above SQL example, we select name, age, address, salary and a Boolean expression (age >= 18 AND age <= 60) from the company table. This expression is used to determine whether the employee's age is valid. The valid_age column in the result set will display a Boolean value of TRUE or FALSE.

3, Example 3, use IS NOT NULL operator

--Determine whether the address is empty and return TRUE or FALSE

SELECT *

             ,(address IS NOT NULL) AS null_address

   FROM company;

In the above SQL example, we select name, age, address, salary and a Boolean expression (address IS NOT NULL) from the company table. This expression is used to determine whether the employee's address is NULL. The null_address column in the result set will display the Boolean value TRUE or FALSE.

4, Example 4, using the like pattern matching operator

LIKE: Determine whether the string can match the pattern string after LIKE. The LIKE expression returns true if the string matches the provided pattern (NOT LIKE expression returns false), otherwise it returns false (NOT LIKE expression returns true) .

--Determine whether the address is CN, return TRUE or FALSE

SELECT *

              ,(address LIKE 'CN%') AS c_address

FROM company;

In the above SQL example, we select name, age, address, salary and a Boolean expression (address LIKE 'CN%') from the company table. This expression is used to determine whether the employee's address is in CN. . The c_address column in the result set will display a Boolean value of TRUE or FALSE.

Attachment: There is also a pattern matching operator SIMILAR TO in GaussDB SQL.

Description: The SIMILAR TO operator returns true or False. It is very similar to LIKE, except that it uses regular expressions defined by the SQL standard to understand patterns.

4. Summary

BOOLEAN expressions are very commonly used in SQL, they allow developers to build logical statements that are capable of complex filtering and selection of data in a table. By using Boolean expressions, query results can be narrowed to rows that meet specific criteria, or data can be aggregated and grouped based on those criteria.​  

In short, Boolean expressions can help us make logical judgments and loop control, and improve the readability of the code. Proficient in the use of BOOLEAN expressions is very important in the development process of GaussDB SQL and so on.

--Finish

Guess you like

Origin blog.csdn.net/GaussDB/article/details/134068914