Take you to understand the BOOLEAN expression in GaussDB SQL

This article is shared from the Huawei Cloud Community " GaussDB SQL Basic Syntax Example-BOOLEAN Expression " by Gauss Squirrel Club Assistant 2.

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 are true or false . BOOLEAN expressions can be used for conditional judgment or as termination conditions in loop statements . 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. Grammar examples

The screenshot below is the SQL part of cursor use. SQL involves BOOLEA expressions for conditional judgment and loop statements . Please refer to:

1) Conditional judgment , see red box

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

2) Loop statement , see blue box

"%NOTFOUND" is one of the attributes of the cursor and is 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, using comparison operators

--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, using 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 depending on whether its pattern matches the given string . 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.

 

Click to follow and learn about Huawei Cloud’s new technologies as soon as possible~

 

High school students create their own open source programming language as a coming-of-age ceremony - sharp comments from netizens: Relying on the defense, Apple released the M4 chip RustDesk. Domestic services were suspended due to rampant fraud. Yunfeng resigned from Alibaba. In the future, he plans to produce an independent game on Windows platform Taobao (taobao.com) Restart web version optimization work, programmers’ destination, Visual Studio Code 1.89 releases Java 17, the most commonly used Java LTS version, Windows 10 has a market share of 70%, Windows 11 continues to decline Open Source Daily | Google supports Hongmeng to take over; open source Rabbit R1; Docker supported Android phones; Microsoft’s anxiety and ambitions; Haier Electric has shut down the open platform
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/11105717