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.