Sql 简明教程
SQL - BOOLEAN
Boolean 是存储 true 或 false 值的通用数据类型。在表列中定义变量时使用它。
A Boolean is a universal data type which stores true or false values. It is used when we define a variable in a column of the table.
例如,客户想要一张所有 red 汽车的列表。因此,我们可以使用 BOOLEAN 运算符找到它,如下表所示 −
For instance, a customer wants a list of all the red cars. So, we can find this using the BOOLEAN operator as given in the below table −
此处, IS_RED 是 BOOLEAN 列,根据汽车颜色返回 TRUE 或 FALSE 值。
Here, IS_RED is the BOOLEAN column that returns either TRUE or FALSE values based on the color of the cars.
Boolean in MySQL
MySQL 提供了多种处理布尔数据的方法。您可以使用 BOOL、BOOLEAN 或 TINYINT 表示布尔值。
MySQL provides various options for handling Boolean data. You can use BOOL, BOOLEAN, or TINYINT to represent Boolean values.
使用 BOOL 或 BOOLEAN 时,MySQL 会在内部将它们转换为 TINYINT。类似于 PHP、C 和 C++ 等许多编程语言,MySQL 将 TRUE 字面值表示为 1,将 FALSE 字面值表示为 0。
When you use BOOL or BOOLEAN, MySQL internally converts them into TINYINT. Similar to many programming languages like PHP, C, and C++, MySQL represents the TRUE literal as 1 and the FALSE literal as 0.
Example
此处,我们正在使用列 BOOLEAN 创建一个“CARS”表。创建表的查询如下 −
Here, we are creating a table 'CARS' with column BOOLEAN. The query to create a table is as follows −
CREATE TABLE CARS (
ID INT NOT NULL,
Name VARCHAR(150),
IsRed BOOLEAN
);
在以上示例中,使用 BOOLEAN 列 IsRed 创建了一个表。您可以在此列中将 TRUE 插入为 1 或将 FALSE 插入为 0,以表示相应的布尔值。
In the above example, a table is created with a BOOLEAN column IsRed. You can insert TRUE as 1 or FALSE as 0 in this column to represent the corresponding Boolean values.
Boolean in MS SQL Server
在 MS SQL Server 中,没有直接的 BOOLEAN 数据类型。相反,可以使用 BIT 数据类型表示布尔值,其中 0 表示 FALSE,1 表示 TRUE。BIT 数据类型也可以接受 NULL 值。
In MS SQL Server, there is no direct BOOLEAN data type. Instead, you can use the BIT data type to represent Boolean values, where 0 represents FALSE and 1 represents TRUE. The BIT data type can also accept NULL values.
Example
以下是一个在 SQL Server 中使用 BOOLEAN 列创建表的示例 −
Following is an example to create a table with a BOOLEAN column in SQL Server −
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
Name VARCHAR(150),
IsAvailable BIT
);
在上面的示例中,创建名为 CUSTOMERS 的表,其中 BOOLEAN 列 IsAvailable 表示为 BIT 数据类型。您可以为该列插入 0 表示 FALSE、1 表示 TRUE 或 NULL 表示未知值。
In the example above, a table named CUSTOMERS is created with a BOOLEAN column IsAvailable represented as a BIT data type. You can insert 0 for FALSE, 1 for TRUE, or NULL for an unknown value in this column.
Filtering Boolean Data
您可以在 SQL 中根据布尔型列筛选数据。例如,在 MySQL 中,要查找所有红色汽车,可以使用 BOOLEAN 列“IsRed”筛选 TRUE 值,如下所示:
You can filter data based on Boolean columns in SQL. For example, in MySQL, to find all the red cars, you can use the BOOLEAN column 'IsRed' to filter for TRUE values as shown below −
SELECT * FROM CARS WHERE IsRed = TRUE;
在 SQL Server 中,要查找红色的汽车,可以按以下方式筛选 TRUE 值(IsRed = 1):
In SQL Server, to find cars that are red, you can filter for TRUE values (IsRed = 1) as follows −
SELECT * FROM CARS WHERE IsRed = 1;
Negating Boolean Conditions
您还可以否定布尔条件以查找 NOT TRUE 的记录。例如,要在 MySQL 中查找不是红色的汽车,请使用以下查询:
You can also negate Boolean conditions to find records that are NOT TRUE. For example, to find cars that are not red, use the following query in MySQL −
SELECT * FROM CARS WHERE IsRed = 0;
以下是在 SQL Server 中的查询:
Following is the query in SQL Server −
SELECT * FROM CARS WHERE IsRed = FALSE;
Working with NULL Values
您还可以在 SQL 中处理布尔数据的 NULL 值。如前所述,SQL Server 中的 BIT 数据类型和 MySQL 中的 BOOL/BOOLEAN 数据类型可以接受 NULL 值,其中可以表示未知或未指定条件。
You can handle NULL values of Boolean data in SQL as well. As mentioned earlier, the BIT data type in SQL Server and the BOOL/BOOLEAN data types in MySQL can accept NULL values, which can represent unknown or unspecified conditions.
要筛选布尔型列中具有 NULL 值的记录,您可以在 MySQL 和 SQL Server 中使用 IS NULL 或 IS NOT NULL 条件:
To filter records with NULL values in a Boolean column, you can use the IS NULL or IS NOT NULL condition in both MySQL and SQL Server −
-- Finding cars with unspecified availability
SELECT * FROM CARS WHERE IsAvailable IS NULL;
-- Finding cars with specified availability
SELECT * FROM CARS WHERE IsAvailable IS NOT NULL;
在上述查询中,我们根据 IsAvailable 列是 NULL 还是 NOT NULL 来筛选汽车。
In the queries above, we filter cars based on whether their IsAvailable column is NULL or not NULL.
Updating Boolean Values
您还可以在 SQL 表中更新布尔值。要更改 MySQL 中布尔型列的值,可以使用 UPDATE 语句,如下所示:
You can also update Boolean values in your SQL tables. To change the value of a Boolean column in MySQL, you can use the UPDATE statement as shown below −
-- Changing IsRed to TRUE for car with ID 123
UPDATE CARS SET IsRed = TRUE WHERE ID = 123;
在上述示例中,我们更新了 ID 为 123 的特定汽车的 IsRed 列,将其设置为 TRUE。
In the above example, we updated the IsRed column for a specific car with the ID of 123, setting it to TRUE.
要在 SQL Server 中更新布尔值,请使用以下查询:
To update Boolean values in SQL Server, use the following query −
-- Changing IsRed to TRUE for car with ID 123
UPDATE CARS SET IsRed = 1 WHERE ID = 123;