Question

i have a question about the best practices about this situation: I have a mysql table that contains (besides the other columns) two columns that are both TINYINT(1) but they can't be both on 0, they can be 0 and 1, 1 and 0, 1 and 1 but no 0 and 0. What's the best way to control this? is even a way to control this?

Edit: i think i will choose the ENUM option but how this will perform if i combine mysql with Entity Framework?

Était-ce utile?

La solution

A few options I can think of:

  • Validate in before update/insert triggers
  • Validate in application logic
  • Add a foreign key reference with acceptable states
  • Use an ENUM with all the possible combinations, and don't allow NULL

I would probably use the last approach, personally.

EDIT:

Just realized--the SET data type accomplishes this perfectly.

Autres conseils

I would suggest using an enum, combined from both values. They are stored quite efficiently. So it would be enum('0;1','1;0','1;1') not null.

EDIT:

Given the additional information, and following @landons suggestion, just for the record: the enum would be

delivery enum('physical', 'digital', 'both') not null;

You could use (also following @landon)

delivery SET('physical','digital');

But that would allow empty values.

One workaround would be to combine your two columns into a BIT(2) column, then when you perform your update you could test that you weren't updating the column to b'00'.

As for a built-in prevention, I do not believe there is a way to do it.

Perhaps you could achieve this by putting them together in 1 column.

  • first number represents 1
  • second number represents 2

Your column could then contain values 1, 2 or 3. When it is 0, they both are.

With 2 and 3 the second number would be 1. With 1 and 3 the first number would be 1.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top