简体   繁体   中英

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.
  • role VARCHAR(20) Enumeration of SUPER_ADMIN and PROJECT_ADMIN

What I understood from above is status should take only Enabled or Disabled and same with role also. 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.

May I get some ideas here? Any help would be appreciated. Thank you

You need to use CHECK CONSTRAINT to limit to specific values

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.

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.

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. Create FK on User_Status and User_Role in NCT_UserRegistration to refer to the enumeration tables.

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)

This Foreign key vs check constraint for integrity post also describes few benefits of using FK over check constraint

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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