Skip to content

Instantly share code, notes, and snippets.

@arnemart
Last active August 29, 2015 14:11
Show Gist options
  • Save arnemart/511aa4a098d3a6b67539 to your computer and use it in GitHub Desktop.
Save arnemart/511aa4a098d3a6b67539 to your computer and use it in GitHub Desktop.
The ultimate flexible database schema
CREATE TABLE "database"
(
id serial NOT NULL,
name text,
CONSTRAINT database_pkey PRIMARY KEY (id)
)
CREATE TABLE "table"
(
id serial NOT NULL,
database_id integer,
name text,
CONSTRAINT table_pkey PRIMARY KEY (id),
CONSTRAINT table_database_id_fkey FOREIGN KEY (database_id)
REFERENCES "database" (id)
)
CREATE TABLE "column"
(
id serial NOT NULL,
table_id integer,
name text,
CONSTRAINT column_pkey PRIMARY KEY (id),
CONSTRAINT column_table_id_fkey FOREIGN KEY (table_id)
REFERENCES "table" (id)
)
CREATE TABLE "row"
(
id serial NOT NULL,
table_id integer,
CONSTRAINT row_pkey PRIMARY KEY (id),
CONSTRAINT row_table_id_fkey FOREIGN KEY (table_id)
REFERENCES "table" (id)
)
CREATE TABLE "value"
(
id serial NOT NULL,
column_id integer,
row_id integer,
value text,
CONSTRAINT value_pkey PRIMARY KEY (id),
CONSTRAINT value_column_id_fkey FOREIGN KEY (column_id)
REFERENCES "column" (id)
CONSTRAINT value_row_id_fkey FOREIGN KEY (row_id)
REFERENCES "row" (id)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment