Here’s simple database project I completed in PostgreSQL to store and query members of my family. It works by storing each individual in a table, then establishes either a spousal or parent/child relationship in a separate table. While I have a field for gender in the individual table, I wanted to make sure it also worked when specifying a third gender, or no gender.
The schema includes the two main tables and three tables to store special codes for gender, relationship types, and the role of each individual in the relationship. It also sets up views that I used to query other types of relationships. For example, here’s the code to create the aunt/uncle view:
The first block selects biological aunts/uncles. This uses the sibling view. In short, it selects an individual as a niece/nephew if that individual (a) is a child in the database, (b) the individual’s parents have siblings, and (c) the aunt/uncle is a sibling of the individual’s parent. The subsequent unions select the spouses of the biological aunts/uncles.
Here’s another example of querying for cousin relationships: