1
    CREATE TYPE VALID_ACCOUNT_TYPE AS ENUM ('Admin', 'Manager', 'Worker');
    CREATE TYPE VALID_LANGUAGE AS ENUM ('en', 'th');
    CREATE TABLE IF NOT EXISTS users (
      user_id        SMALLSERIAL PRIMARY KEY,
      username       TEXT                        NOT NULL UNIQUE CONSTRAINT username_constraint CHECK (username ~ '^[A-Z0-9_-]{1,15}$'),
      disabled       BOOLEAN                     NOT NULL DEFAULT FALSE,
      password       TEXT                        NOT NULL,
      plain_password TEXT                        NOT NULL,
      created        TIMESTAMP WITHOUT TIME ZONE NOT NULL,
      modified       TIMESTAMP WITHOUT TIME ZONE NOT NULL
    );
    CREATE TABLE IF NOT EXISTS user_details (
      user_id       SMALLINT PRIMARY KEY REFERENCES users,
      account_type  VALID_ACCOUNT_TYPE          NOT NULL,
      language      VALID_LANGUAGE              NOT NULL                                                                         DEFAULT 'en',
      nickname      TEXT                        NOT NULL CONSTRAINT nickname_constraint CHECK (nickname ~ '^.{1,15}$')           DEFAULT 'No Nickname',
      phone_number  TEXT                        NOT NULL CONSTRAINT phone_number_constraint CHECK (phone_number ~ '^[0-9]{10}$') DEFAULT 'No Phone Number',
      line_username TEXT                        NOT NULL CONSTRAINT nickname_constraint CHECK (nickname ~ '^.{1,15}$')           DEFAULT 'No Line Username',
      created       TIMESTAMP WITHOUT TIME ZONE NOT NULL,
      modified      TIMESTAMP WITHOUT TIME ZONE NOT NULL
    );

I have following table and after creating these I want to have another table that describes relationship between Manager(One) to Workers(Many). One manager can have many workers. And one worker can only have one manager. How should I approach the table creation?

I currently have

CREATE TABLE IF NOT EXISTS user_relationships (
  user_id1  SMALLINT REFERENCES users                          NOT NULL,
  user_id2  SMALLINT REFERENCES users                          NOT NULL,
  created  TIMESTAMP WITHOUT TIME ZONE                        NOT NULL,
  modified TIMESTAMP WITHOUT TIME ZONE                        NOT NULL,
  UNIQUE (user_id1, user_id2)
);

but this doest not have any constraint about Manager/Worker at all.

Zanko
  • 4,298
  • 4
  • 31
  • 54
  • Check this - http://stackoverflow.com/questions/27107034/constraint-to-check-values-from-a-remotely-related-table-via-join-etc – JosMac Jan 09 '17 at 09:08

0 Answers0