gpt4 book ai didi

sql-server - 如何在 SQL Server 中要求列中的最小字符数?

转载 作者:行者123 更新时间:2023-12-02 21:23:24 24 4
gpt4 key购买 nike

CREATE TABLE [dbo].[AdminTest] (
[AdminTestId] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (100) NOT NULL,
[CreatedBy] INT NOT NULL,
[CreatedDate] DATETIME NOT NULL,
[ModifiedBy] INT NOT NULL,
[ModifiedDate] DATETIME NOT NULL,
[TestLevelId] INT NOT NULL,
[TestStatusId] INT NOT NULL,
[ExamId] INT NOT NULL,
[Text] NVARCHAR (MAX) NULL,
[Sequence] INT DEFAULT ((1)) NOT NULL,
[Release] NVARCHAR (50) DEFAULT ((1)) NOT NULL,
[Version] ROWVERSION NOT NULL,
[Price] MONEY DEFAULT ((0)) NOT NULL,
[ReleaseDate] DATETIME NULL,
[Code] VARCHAR (10) DEFAULT (LEFT(newid(), (5))) NOT NULL
);

我将“代码”列设置为默认值,但如何才能使“代码”列的值至少包含 5 个字符?

最佳答案

创建检查约束:

ALTER TABLE AdminTest
ADD CONSTRAINT CK_AdminTest_Code_Length CHECK (LEN(Code) >= 5);
GO

引用号:CHECK Constraints

关于sql-server - 如何在 SQL Server 中要求列中的最小字符数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26201578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com