如何在 SQL Server 中创建是/否布尔字段?

发布于 2024-08-12 01:08:04 字数 100 浏览 4 评论 0原文

access 数据库 转换时或一般情况下,创建 yes/noBoolean 字段的最佳实践是什么?

What is the best practice for creating a yes/no i.e. Boolean field when converting from an access database or in general?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

你与清晨阳光 2024-08-19 01:08:04

等效的是 BIT 字段。

SQL 中,您使用01 来设置位字段(就像Access 中的是/否字段一样)。在 Management Studio 中,它显示为 false/true 值(至少在最新版本中)。

当通过 ASP.NET 访问数据库时,它将将该字段公开为布尔值。

The equivalent is a BIT field.

In SQL you use 0 and 1 to set a bit field (just as a yes/no field in Access). In Management Studio it displays as a false/true value (at least in recent versions).

When accessing the database through ASP.NET it will expose the field as a boolean value.

小ぇ时光︴ 2024-08-19 01:08:04

BIT 数据类型通常用于存储 boolean 值(0 表示 false1 > 为true)。

The BIT datatype is generally used to store boolean values (0 for false, 1 for true).

独行侠 2024-08-19 01:08:04

您可以使用 bit 列类型。

You can use the bit column type.

野却迷人 2024-08-19 01:08:04

您可以使用BIT字段。

要向现有表添加 BIT 列,SQL 命令如下所示:

ALTER TABLE table_name ADD yes_no BIT

如果要创建新表,可以执行以下操作:CREATE TABLE table_name ( yes_no 位)

You can use the BIT field.

For adding a BIT column to an existing table, the SQL command would look like:

ALTER TABLE table_name ADD yes_no BIT

If you want to create a new table, you could do: CREATE TABLE table_name (yes_no BIT).

入画浅相思 2024-08-19 01:08:04

已经有答案说使用 Bit 了。我将对这些答案添加更多内容。

您应该使用 bit 来表示布尔值。

MSDN 文章的评论。

位的值可以是 1、0 或 NULL。

SQL Server 数据库引擎优化了位列的存储。如果
表中有 8 位或更少的列,这些列存储为
1 字节。如果有 9 到 16 位列,则这些列是
存储为 2 个字节,依此类推。

字符串值 TRUE 和 FALSE 可以转换为位值: TRUE
转换为 1,FALSE 转换为 0。

转换为位会将任何非零值提升为 1。

参考

注意:最好仅使用数据类型 NOT NULL 将值保留为 1 和 0,

因为 Bit 具有值 1、0 和 NULL。请参阅真值表。因此,请相应地规划值。允许位数据类型使用 NULL 值可能会增加混乱。

在此输入图像描述

参考

There are already answers saying use of Bit. I will add more to these answers.

You should use bit for representing Boolean values.

Remarks from MSDN article.

Bit can take a value of 1, 0, or NULL.

The SQL Server Database Engine optimizes storage of bit columns. If
there are 8 or less bit columns in a table, the columns are stored as
1 byte. If there are from 9 up to 16 bit columns, the columns are
stored as 2 bytes, and so on.

The string values TRUE and FALSE can be converted to bit values: TRUE
is converted to 1 and FALSE is converted to 0.

Converting to bit promotes any nonzero value to 1.

Reference

Note: It is good practice to keep values as 1 and 0 only with data type NOT NULL

As Bit have values 1, 0 and NULL. See truth table for this. So plan values accordingly. It might add confusion by allowing NULL value for bit data type.

enter image description here

Reference

沙沙粒小 2024-08-19 01:08:04

您可以使用数据类型 bit

插入的值大于 0 将存储为“1”

插入的值小于 0 将存储为“1”

插入的值将存储为“0”存储为“0”

这适用于 MS SQL Server 2012 Express

You can use the data type bit

Values inserted which are greater than 0 will be stored as '1'

Values inserted which are less than 0 will be stored as '1'

Values inserted as '0' will be stored as '0'

This holds true for MS SQL Server 2012 Express

鲜血染红嫁衣 2024-08-19 01:08:04

创建表时的示例用法:

[ColumnName]     BIT   NULL   DEFAULT 0

Sample usage while creating a table:

[ColumnName]     BIT   NULL   DEFAULT 0
那片花海 2024-08-19 01:08:04

您可以使用 BIT 字段

创建新表:

CREATE TABLE Tb_Table1
(
ID              INT,
BitColumn       BIT DEFAULT 1
)

在现有表中添加列:

ALTER TABLE Tb_Table1 ADD BitColumn  BIT DEFAULT 1

插入记录:

INSERT Tb_Table1 VALUES(11,0)

You can use the BIT field

To create new table:

CREATE TABLE Tb_Table1
(
ID              INT,
BitColumn       BIT DEFAULT 1
)

Adding Column in existing Table:

ALTER TABLE Tb_Table1 ADD BitColumn  BIT DEFAULT 1

To Insert record:

INSERT Tb_Table1 VALUES(11,0)
清晰传感 2024-08-19 01:08:04

bit 将是最简单的,并且占用的空间也最少。与“Y/N”相比,不是很冗长,但我对此很满意。

bit will be the simplest and also takes up the least space. Not very verbose compared to "Y/N" but I am fine with it.

日暮斜阳 2024-08-19 01:08:04

bit 是最合适的选项。否则我曾经使用int来达到这个目的。 1 表示 true & 0 表示 false

bit is the most suitable option. Otherwise I once used int for that purpose. 1 for true & 0 for false.

左秋 2024-08-19 01:08:04

在任何版本的 SQL Server Management Studio 中,都使用 BIT 作为数据类型

,这将为您提供 TrueFalse 值选项。如果您只想使用 10 那么您可以使用此方法:

CREATE TABLE SampleBit(
    bar int NOT NULL CONSTRAINT CK_foo_bar CHECK (bar IN (-1, 0, 1))
)

但我会严格建议 BIT 作为 BEST选项。希望完全可以帮助别人。

In SQL Server Management Studio of Any Version, Use BIT as Data Type

which will provide you with True or False Value options. in case you want to use Only 1 or 0 then you can use this method:

CREATE TABLE SampleBit(
    bar int NOT NULL CONSTRAINT CK_foo_bar CHECK (bar IN (-1, 0, 1))
)

But I will strictly advise BIT as The BEST Option. Hope fully it's help someone.

黑色毁心梦 2024-08-19 01:08:04

您可以使用 BIT 类型,可以有 10,如果 也可以有 NULL允许 >NULL

BIT 类型 转换:

  • 0 之外的任何整数值到 1
  • "0"1 之外的任何整数字符串值。
  • “0”0

然后,您可以使用 BIT type 如下所示:

CREATE TABLE doctor (
  id INT IDENTITY,
  name NVARCHAR(50),
  on_call BIT, -- Here
  PRIMARY KEY(id)
)
GO

然后,插入行,如下所示:

INSERT INTO doctor 
VALUES ("John", 1), ("Tom", 0), ("Lisa", "-23"), ("Kai", "0"), ("Bob", NULL)
GO
1> SELECT * FROM doctor
2> GO
id name on_call
-- ---- -------
 1 John       1 <- 1
 2 Tom        0 <- 0
 3 Lisa       1 <- "-23"
 4 Kai        0 <- "0"
 5 Bob     NULL <- NULL

You can use BIT type which can have 1 or 0, or also NULL if NULL is allowed.

BIT type converts:

  • Any integer values except 0 to 1.
  • Any integer string values except "0" to 1.
  • "0" to 0.

Then, you can create a table with BIT type as shown below:

CREATE TABLE doctor (
  id INT IDENTITY,
  name NVARCHAR(50),
  on_call BIT, -- Here
  PRIMARY KEY(id)
)
GO

Then, insert rows as shown below:

INSERT INTO doctor 
VALUES ("John", 1), ("Tom", 0), ("Lisa", "-23"), ("Kai", "0"), ("Bob", NULL)
GO
1> SELECT * FROM doctor
2> GO
id name on_call
-- ---- -------
 1 John       1 <- 1
 2 Tom        0 <- 0
 3 Lisa       1 <- "-23"
 4 Kai        0 <- "0"
 5 Bob     NULL <- NULL
毁梦 2024-08-19 01:08:04

在布尔值使用什么类型的数据类型的数据库列表下方

-> 甲骨文 ->数字(1)

-> SQL Server ->位

-> MySql -> BIT 或 TINYINT

->postgreSQL -> boolean

我希望这真的很有帮助,谢谢。

Below the List of database where what type of datatype is use for Boolean

-> Oracle -> Number(1)

-> SQL Server -> BIT

-> MySql -> BIT or TINYINT

->postgreSQL -> boolean

I hope this is really helpful thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文