SQL Server 2005创建表与列默认值范围

发布于 2024-09-04 08:30:01 字数 483 浏览 5 评论 0原文

试图完成一些作业并遇到创建表格的问题。如何声明一系列数字的列默认值。它的内容是:“Column Building(默认为 1,但可以是 1-10)”我似乎找不到……或不知道在哪里查找此信息。

CREATE TABLE tblDepartment
(
Department_ID int NOT NULL IDENTITY,
Department_Name varchar(255) NOT NULL,
Division_Name varchar(255) NOT NULL,
City varchar(255) default 'spokane' NOT NULL,
Building int default 1 NOT NULL,
Phone varchar(255)
)

我尝试 Building int default 1 Between 1 AND 10 NOT NULL,但没有成功 我尝试 Building int default 1-10,该表已创建,但我认为它不正确。

Trying to finish up some homework and ran into a issue for creating tables. How do you declare a column default for a range of numbers. Its reads: "Column Building (default to 1 but can be 1-10)" I can't seem to find ...or know where to look for this information.

CREATE TABLE tblDepartment
(
Department_ID int NOT NULL IDENTITY,
Department_Name varchar(255) NOT NULL,
Division_Name varchar(255) NOT NULL,
City varchar(255) default 'spokane' NOT NULL,
Building int default 1 NOT NULL,
Phone varchar(255)
)

I tried Building int default 1 Between 1 AND 10 NOT NULL, that didn't work out
I tried Building int default 1-10, the table was created but I don't think its correct.

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

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

发布评论

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

评论(1

霞映澄塘 2024-09-11 08:30:01

您需要将CHECK Constraint添加到该列。

ALTER TABLE tblDepartment
ADD CONSTRAINT chkbuilding CHECK (Building >=1 AND Building <= 10 );

You need to add CHECK Constraint to the column.

ALTER TABLE tblDepartment
ADD CONSTRAINT chkbuilding CHECK (Building >=1 AND Building <= 10 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文