📅  最后修改于: 2020-11-26 05:51:48             🧑  作者: Mango
在本章中,我们将讨论PL / SQL中的运算符。运算符是告诉编译器执行特定数学或逻辑运算的符号。 PL / SQL语言具有丰富的内置运算符,并提供以下类型的运算符-
在这里,我们将一一理解算术,关系,比较和逻辑运算符。字符串运算符将在后面的章节-PL / SQL-Strings中讨论。
下表显示了PL / SQL支持的所有算术运算运算符。假设变量A持有10,变量B持有5,则-
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B will give 15 |
– | Subtracts second operand from the first | A – B will give 5 |
* | Multiplies both operands | A * B will give 50 |
/ | Divides numerator by de-numerator | A / B will give 2 |
** | Exponentiation operator, raises one operand to the power of other | A ** B will give 100000 |
关系运算符比较两个表达式或值,并返回布尔结果。下表显示了PL / SQL支持的所有关系运算符。让我们假设变量A持有10,变量B持有20,那么-
Operator | Description | Example |
---|---|---|
= | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A = B) is not true. |
!= <> ~= |
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. | <= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true |
运算符用于将一个表达式与另一个表达式进行比较。结果始终为TRUE,FALSE或NULL 。
Operator | Description | Example |
---|---|---|
LIKE | The LIKE operator compares a character, string, or CLOB value to a pattern and returns TRUE if the value matches the pattern and FALSE if it does not. | If ‘Zara Ali’ like ‘Z% A_i’ returns a Boolean true, whereas, ‘Nuha Ali’ like ‘Z% A_i’ returns a Boolean false. |
BETWEEN | The BETWEEN operator tests whether a value lies in a specified range. x BETWEEN a AND b means that x >= a and x <= b. | If x = 10 then, x between 5 and 20 returns true, x between 5 and 10 returns true, but x between 11 and 20 returns false. |
IN | The IN operator tests set membership. x IN (set) means that x is equal to any member of set. | If x = ‘m’ then, x in (‘a’, ‘b’, ‘c’) returns Boolean false but x in (‘m’, ‘n’, ‘o’) returns Boolean true. |
IS NULL | The IS NULL operator returns the BOOLEAN value TRUE if its operand is NULL or FALSE if it is not NULL. Comparisons involving NULL values always yield NULL. | If x = ‘m’, then ‘x is null’ returns Boolean false. |
下表显示了PL / SQL支持的逻辑运算符。所有这些运算符都对布尔操作数起作用并产生布尔结果。让我们假设变量A成立,变量B成立,然后-
Operator | Description | Examples |
---|---|---|
and | Called the logical AND operator. If both the operands are true then condition becomes true. | (A and B) is false. |
or | Called the logical OR Operator. If any of the two operands is true then condition becomes true. | (A or B) is true. |
not | Called the logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make it false. | not (A and B) is true. |
运算符优先级确定表达式中术语的分组。这会影响表达式的求值方式。某些运算符具有更高的优先级;例如,乘法运算符的优先级高于加法运算符。
例如, x = 7 + 3 * 2 ;在这里, x被赋值为13而不是20,因为运算符*的优先级比+高,因此它首先与3 * 2相乘,然后加到7中。
在此,优先级最高的运算符出现在表格的顶部,而优先级最低的运算符出现在表格的底部。在表达式中,优先级较高的运算符将首先被评估。
运算符的优先级如下:=,<,>,<=,> =,<>,!=,〜=,^ =,IS NULL,LIKE,BETWEEN,IN。
Operator | Operation |
---|---|
** | exponentiation |
+, – | identity, negation |
*, / | multiplication, division |
+, -, || | addition, subtraction, concatenation |
comparison | |
NOT | logical negation |
AND | conjunction |
OR | inclusion |