Suppose I'm trying to represent three types of things -- grandparent, parents, and children. There's a one-to-many relationship between grandparents and parents, and a one-to-many relationship between parents and children. Parents that share a grandparent must have a unique name. The same goes for children, as all children that share a grandparent must have a unique name.
Here's an example schema:
CREATE TABLE grandparent (
grandparentid TEXT PRIMARY KEY,
uniquefield1 TEXT NOT NULL
);
CREATE TABLE parent (
parentid TEXT PRIMARY KEY,
grandparentid TEXT REFERENCES grandparent,
name TEXT NOT NULL,
uniquefield2 TEXT NOT NULL,
UNIQUE (grandparentid, name)
);
CREATE TABLE child (
childid TEXT PRIMARY KEY,
grandparentid TEXT REFERENCES grandparent,
parentid TEXT REFERENCES parent,
name TEXT NOT NULL,
uniquefield3 TEXT NOT NULL,
UNIQUE (grandparentid, name)
);
I'm trying to come up with something that removes the need for the grandparentid field in the child table, because I don't want to get into a situation where it disagrees with the contents of the parentid field. However, I can't just remove it (to the best of my knowledge) because I still need a unique constraint on the pair of grandparentid and name.
Also worth noting is that each generation has unique information. It's a bit of a contrived example, because the actual use case is domain-specific and would just obfuscate the problem.