Flags in a database rows, best practices

前端 未结 8 1578
南旧
南旧 2020-12-16 09:34

I am asking this out of a curiosity. Basically my question is when you have a database which needs a row entry to have things which act like flags, what is the best practice

8条回答
  •  无人及你
    2020-12-16 09:56

    Generally speaking, I avoid bitmask fields. They're difficult to read in the future and they require a much more in-depth knowledge of the data to understanding.

    The relational solution has been proposed previously. Given the example you outlined, I would create something like this (in SQL Server):

    
    CREATE TABLE Users (
      UserId INT IDENTITY(1, 1) PRIMARY KEY,
      FirstName VARCHAR(50),
      LastName VARCHAR(50),
      EmailAddress VARCHAR(255)
    );
    
    CREATE TABLE Badges (
      BadgeId INT IDENTITY(1, 1) PRIMARY KEY,
      [Name] VARCHAR(50),
      [Description] VARCHAR(255)
    );
    
    CREATE TABLE UserBadges (
      UserId INT REFERENCES Users(UserId),
      BadgeId INT REFERENCES Badges(BadgeId)
    );
    

提交回复
热议问题