Flags in a database rows, best practices

前端 未结 8 1543
南旧
南旧 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)
    );
    
    0 讨论(0)
  • 2020-12-16 10:07

    A Very Relational Approach

    For databases without the set type, you could open a new table to represent the set of entities for which each flag is set.

    E.g. for a Table "Students" you could have tables "RegisteredStudents", "SickStudents", TroublesomeStudents etc. Each table will have only one column: the student_id. This would actually be very fast if all you want to know is which students are "Registered" or "Sick", and would work the same way in every DBMS.

    0 讨论(0)
  • 2020-12-16 10:07

    If there are more than just a few flags, or likely to be so in the future, I'll use a separate table of flags and a many-to-many table between them.

    If there are a handful of flags and I'm never going to use them in a WHERE, I'll use a SET() or bitfield or whatever. They're easy to read and more compact, but a pain to query and sometimes even more of a headache with an ORM.

    If there are only a few flags -- and only ever going to be a few flags -- then I'll just make a couple BIT/BOOLEAN/etc columns.

    0 讨论(0)
  • 2020-12-16 10:08

    If the flags have very different meanings and are used directly in SQL queries or VIEWS, then using multiple columns of type BOOLEAN might be a good idea.

    Put each flag into an extra column, because you'll read and modify them separately anyway. If you want to group the flags, just give their column names a common prefix, i.e. instead of:

    CREATE TABLE ... (
        warnings INTEGER,
        errors   INTEGER,
        ...
    )
    

    you should use:

    CREATE TABLE ... (
        warning_foo BOOLEAN,
        warning_bar BOOLEAN,
        warning_...
        error_foo   BOOLEAN,
        error_bar   BOOLEAN,
        error_...   BOOLEAN,
        ...
    )
    

    Although MySQL doesn't have a BOOLEAN type, you can use the quasi standard TINYINT(1) for that purpose, and set it only to 0 or 1.

    0 讨论(0)
  • 2020-12-16 10:09

    For many cases, it depends on a lot of things - like your database backend. If you're using MySQL, for example, the SET datatype is exactly what you want.

    Basically, it's just a bitmask, with values assigned to each bit. MySQL supports up to 64-bit values (meaning 64 different toggles). If you only need 8, then it only takes a byte per row, which is pretty awesome savings.

    If you honestly have more than 64 values in a single field, your field might be getting more complicated. You may want to expand then to the BLOB datatype, which is just a raw set of bits that MySQL has no inherent understanding of. Using this, you can create an arbitrary number of bit fields that MySQL is happy to treat as binary, hex, or decimal values, however you need. If you need more than 64 options, create as many fields as is appropriate for your application. The downside is that is is difficult to make the field human readable. The BIT datatype is also limited to 64.

    0 讨论(0)
  • 2020-12-16 10:11

    Came across this when I was pondering best way to store bitmask flags (similar to OP's original use of integers) in a database.

    The other answers are all valid solutions, but I think its worth mentioning that you may not have to resign yourself to horrible query problems if you choose to store bitmasks directly in the database.

    If you are working on an application that uses bitmasks and you really want the convenience of storing them in the database as one integer or byte column, go ahead and do that. Down the road, you can write yourself a little utility that will generate another table of flags (in whatever pattern of rows/columns you choose) from the bitmasks in your primary working table. You can then do ordinary SQL queries on that computed/derived table.

    This way your application gets the convenience of only reading/writing the bitmask field/column. But you can still use SQL to really dive into your data if that becomes necessary at a later time.

    0 讨论(0)
提交回复
热议问题