Question

So basically if I had a table like so:

ID | Email | Password | PermissionGroup

could I define permission group's enum values based on all the names from the GroupName column values in a different table

ID | GroupName | PermissionNodes | Inheritance

Était-ce utile?

La solution

ENUM is not the right tool for this task.

You're describing a foreign key constraint:

CREATE TABLE groups (
  ID              SERIAL,
  GroupName       VARCHAR(255),
  PermissionNodes VARCHAR(255),  -- not sure what datatype this should be
  Inheritance     VARCHAR(255)   -- nor this
) ENGINE = InnoDB;

CREATE TABLE users  (
  ID              SERIAL,
  Email           VARCHAR(255),
  Password        CHAR(60) BINARY,
  PermissionGroup BIGINT UNSIGNED,
  FOREIGN KEY (PermissionGroup) REFERENCES groups (ID)
) ENGINE = InnoDB;

Note that the foreign key normally references the primary key of the parent table. If one wants to obtain the GroupName of a user's group, one would simply join the tables:

SELECT users.*, groups.GroupName
FROM   users JOIN groups ON groups.ID = users.PermissionGroup
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top