Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a bitwise flag for a primary key?

I am designing a database and was thinking about the need for a one to many relationship. Traditionally I have done the normal PK (as a GUID) and set up the relationship, but I was wondering instead if doing that why not use a bitwise flag as the PK.

The relationship would be lost but the data itself would describe the relationship.

Example - I have a table of groups and a table of users. Users can have 1 or more groups:

+------------------------+
| Groups                 |
+------------------------+
| PK      | Display Name |
+---------+--------------+
| 1       | Group A      |
| 2       | Group B      |
| 4       | Group C      |
+---------+--------------+

+------------------------+
| Users                  |
+------------------------+
| Name    | Groups       |
+---------+--------------+
| Fred    | 1            | // Fred is only in Group A
| Jim     | 3            | // Jim is in Groups A & B
| Sam     | 7            | // Sam is in all Groups
+---------+--------------+

Thoughts, comments and suggestions on this design please?

like image 882
Robert MacLean Avatar asked Dec 08 '22 06:12

Robert MacLean


1 Answers

I'd discourage using bit flags like this. For one thing, you've broken the ability to easily join these tables, so determining group membership will a) take longer, b) be more difficult, and c) probably involve more full-table scans or at least index scans.

like image 102
GalacticCowboy Avatar answered Jan 08 '23 08:01

GalacticCowboy