简体   繁体   English

如何为SQL表列分配2个默认值?

[英]How to assign 2 default values to SQL table column?

I am designing user registration table with below columns. 我正在设计下面列的用户注册表。

CREATE TABLE [dbo].[NCT_UserRegistration]
(
    [User_Id] [int] IDENTITY(1,1) NOT NULL,
    [User_EmailId] [varchar](255) NULL,
    [User_Password] [varchar](512) NULL,
    [User_Name] [varchar](255) NULL,
    [User_MobileNum] [varchar](20) NULL,
    [User_Status] [varchar](15) NULL,
    [User_Role] [varchar](20) NULL,
    [User_CreatedDate] [timestamp] NULL,
    [User_UpdatedDate] [datetime] NULL,
    [Name] [varchar](30) NULL
)

My requirement for the status and role as below. 我对地位和角色的要求如下。

  • status VARCHAR(15) Index, Enumeration of ENABLED, DISABLED. status VARCHAR(15)索引,ENABLED枚举,禁用。
  • role VARCHAR(20) Enumeration of SUPER_ADMIN and PROJECT_ADMIN role VARCHAR(20) SUPER_ADMIN和PROJECT_ADMIN的枚举

What I understood from above is status should take only Enabled or Disabled and same with role also. 我从上面了解到的状态应该只是EnabledDisabled ,也与角色相同。 How can I design my table to make sure it takes only those two values? 如何设计我的表以确保它只需要这两个值? Also is there any way for example if I supply 1 then it is ENABLED and 0 for DISABLED. 也有任何方式,例如,如果我提供1然后它是ENABLED和0为DISABLED。

May I get some ideas here? 我可以在这里得到一些想法吗? Any help would be appreciated. 任何帮助,将不胜感激。 Thank you 谢谢

You need to use CHECK CONSTRAINT to limit to specific values 您需要使用CHECK CONSTRAINT来限制特定值

CREATE TABLE [dbo].[NCT_UserRegistration](
    [User_Id] [int] IDENTITY(1,1) NOT NULL,
    [User_EmailId] [varchar](255) NULL,
    [User_Password] [varchar](512) NULL,
    [User_Name] [varchar](255) NULL,
    [User_MobileNum] [varchar](20) NULL,
    [User_Status] [varchar](15) NULL CONSTRAINT chk_Status CHECK ([User_Status] IN ('ENABLED', 'DISABLED')),
    [User_Role] [varchar](20) NULL CONSTRAINT chk_Role CHECK ([User_Role] IN ('SUPER_ADMIN','DISABLED')),
    [User_CreatedDate] [timestamp] NULL,
    [User_UpdatedDate] [datetime] NULL,
    [Name] [varchar](30) NULL

)

For enumeration you will have to handle at front end or while retriving values from table which is an extra step. 对于枚举,您必须在前端处理或在从表中重新执行值时处理,这是一个额外的步骤。

SELECT CASE WHEN [User_Status] = 1 THEN 'ENABLED' WHEN [User_Status] = 0 THEN 'DISABLED' END As UserStratus
FROM [dbo].[NCT_UserRegistration]

Can you try adding constraint as below for status field. 您可以尝试为状态字段添加以下约束。 If its working then apply the same to ROLE field. 如果它的工作然后将相同的应用于ROLE字段。

alter table NCT_UserRegistration
    add (STATUS VARCHAR(15) default 'ENABLED',
         constraint conSTATUS check (STATUS in ('ENABLED', 'DISABLED')))

There are two possible approaches. 有两种可能的方法。

  1. Check constraints - @mh2017 has explained this well in his answer. 检查约束 - @ mh2017在他的回答中解释了这一点。

Looking at the conversation that seems to fit your requirements better, but just for the sake of sharing idea, I will mention - 看一下似乎更符合你要求的对话,但为了分享想法,我会提到 -

  1. Foreign key constraint (FK)- If it is acceptable to modify the User_Status and User_Role columns to be of type tinyint (or similar number type), you can store just the ids in these and create enumeration (aka mapping tables) to store what the ids represent. 外键约束(FK) - 如果可以将User_Status和User_Role列修改为tinyint类型(或类似的数字类型),则可以只在这些列中存储id并创建枚举(也就是映射表)来存储ids代表。 Create FK on User_Status and User_Role in NCT_UserRegistration to refer to the enumeration tables. 在User_Status上创建FK,在NCT_UserRegistration中创建User_Role以引用枚举表。

FKC ensures that the referring column (User_Status and User_Role in NCT_UserRegistration) cannot have value other than those listed in the referred to column (the respective id columns in the enumeration tables) FKC确保引用列(NCT_UserRegistration中的User_Status和User_Role)不能具有除引用列中列出的值之外的值(枚举表中的相应id列)

This Foreign key vs check constraint for integrity post also describes few benefits of using FK over check constraint 完整性帖子的外键与检查约束也描述了使用FK而不是检查约束的一些好处

Here is a sample code showing foreign key approach 这是一个显示外键方法的示例代码

  CREATE TABLE [dbo].[NCT_UserRegistration](
    [User_Id] [int] IDENTITY(1,1) NOT NULL,
    [User_EmailId] [varchar](255) NULL,
    [User_Password] [varchar](512) NULL,
    [User_Name] [varchar](255) NULL,
    [User_MobileNum] [varchar](20) NULL,
    [User_Status] tinyint NULL, -- I changed this from varchar to tinyint
    [User_Role] tinyint  NULL, -- I changed this from varchar to tinyint
    [User_CreatedDate] [timestamp] NULL,
    [User_UpdatedDate] [datetime] NULL,
    [Name] [varchar](30) NULL
)

create table StatusEnumeration
(
    StatusId tinyint,
    Description varchar(10)
    constraint pk_StatusEnumeration__StatusId primary key clustered (StatusId)
)

insert into StatusEnumeration(StatusId, Description)
values
(0, 'Disabled'),
(1, 'Enabled')


create table RoleEnumeration
(
    RoleId tinyint,
    Description varchar(20)
    constraint pk_RoleEnumeration__RoleId primary key clustered (RoleId)
)

insert into RoleEnumeration(RoleId, Description)
values
(0, 'SUPER_ADMIN '),
(1, 'PROJECT_ADMIN')

alter table NCT_UserRegistration
add constraint fk_NCT_UserRegistration__StatusEnumeration_StatusId foreign key (User_Status)
    references StatusEnumeration (StatusId)
go

alter table NCT_UserRegistration
add constraint fk_NCT_UserRegistration__RoleEnumeration_RoleId foreign key (User_Role)
    references RoleEnumeration (RoleId)
go

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM