在 SQL Server 2005 中,如何设置整数列以确保值大于 0?

发布于 2024-07-06 22:57:06 字数 133 浏览 6 评论 0原文

这可能是一个简单的答案,但我找不到它。 我有一个包含整数列的表,我想确保插入行时该列中的值大于零。 我可以在代码方面执行此操作,但认为最好在桌面上强制执行。

谢谢!

我上次的评论是错误的,现在一切都很好。

This is probably a simple answer but I can't find it. I have a table with a column of integers and I want to ensure that when a row is inserted that the value in this column is greater than zero. I could do this on the code side but thought it would be best to enforce it on the table.

Thanks!

I was in error with my last comment all is good now.

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

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

发布评论

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

评论(5

也只是曾经 2024-07-13 22:57:06

您可以在列上使用检查约束。 IIRC 的语法如下所示:

create table foo (
    [...]
   ,Foobar int not null check (Foobar > 0)
    [...]
)

正如下面的海报所说(感谢 Constantin),您应该在表定义之外创建检查约束并给它一个有意义的名称,以便很明显它适用于哪一列。

alter table foo
  add constraint Foobar_NonNegative
      check (Foobar > 0)

您可以从sys.check_constraints中的系统数据字典中获取检查约束的文本:

select name
      ,description
  from sys.check_constraints
 where name = 'Foobar_NonNegative'

You can use a check constraint on the column. IIRC the syntax for this looks like:

create table foo (
    [...]
   ,Foobar int not null check (Foobar > 0)
    [...]
)

As the poster below says (thanks Constantin), you should create the check constraint outside the table definition and give it a meaningful name so it is obvious which column it applies to.

alter table foo
  add constraint Foobar_NonNegative
      check (Foobar > 0)

You can get out the text of check constraints from the system data dictionary in sys.check_constraints:

select name
      ,description
  from sys.check_constraints
 where name = 'Foobar_NonNegative'
私藏温柔 2024-07-13 22:57:06

创建数据库约束:

ALTER TABLE Table1 ADD CONSTRAINT Constraint1 CHECK (YourCol > 0)

您也可以有非常复杂的约束,涉及多个列。 例如:

ALTER TABLE Table1 ADD CONSTRAINT Constraint2 CHECK (StartDate<EndDate OR EndDate IS NULL)

Create a database constraint:

ALTER TABLE Table1 ADD CONSTRAINT Constraint1 CHECK (YourCol > 0)

You can have pretty sophisticated constraints, too, involving multiple columns. For example:

ALTER TABLE Table1 ADD CONSTRAINT Constraint2 CHECK (StartDate<EndDate OR EndDate IS NULL)
梦醒时光 2024-07-13 22:57:06

我相信您想向表字段添加一个 CONSTRAINT:

ALTER TABLE tableName WITH NOCHECK
ADD CONSTRAINT constraintName CHECK (columnName > 0)

可选的 NOCHECK 用于防止将约束应用于现有的数据行(可能包含无效数据)& 允许添加约束。

I believe you want to add a CONSTRAINT to the table field:

ALTER TABLE tableName WITH NOCHECK
ADD CONSTRAINT constraintName CHECK (columnName > 0)

That optional NOCHECK is used to keep the constraint from being applied to existing rows of data (which could contain invalid data) & to allow the constraint to be added.

路还长,别太狂 2024-07-13 22:57:06

创建表时添加 CHECK 约束

CREATE TABLE Test(
      [ID] [int]  NOT NULL,
      [MyCol] [int] NOT NULL CHECK (MyCol > 1)
)

Add a CHECK constraint when creating your table

CREATE TABLE Test(
      [ID] [int]  NOT NULL,
      [MyCol] [int] NOT NULL CHECK (MyCol > 1)
)
凉墨 2024-07-13 22:57:06

您可以更改表并添加新的约束,如下所示。

BEGIN TRANSACTION
     GO
     ALTER TABLE dbo.table1 ADD CONSTRAINT
        CK_table1_field1 CHECK (field1>0)
     GO
     ALTER TABLE dbo.table1 SET (LOCK_ESCALATION = TABLE)
     GO
COMMIT

you can alter your table and add new constraint like bellow.

BEGIN TRANSACTION
     GO
     ALTER TABLE dbo.table1 ADD CONSTRAINT
        CK_table1_field1 CHECK (field1>0)
     GO
     ALTER TABLE dbo.table1 SET (LOCK_ESCALATION = TABLE)
     GO
COMMIT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文