Joining a Table to Itself

Certain real-life phenomena require a database to store information about relationships within a single table. There are several examples of these types of tables. Manufacturers and parts distributors work with parts, but the parts can be gathered into kits, and the kits can often become assemblies. All those entities (parts, kits, and assemblies) are usually stored in a single table, which might be called Products. Table 5.1 lists a two-level parts and kits example.

Table 5.1. Table Containing Data in a Two-Level Structure
ItemNoDescriptionPartOf
1blue shirt10
2blue pants10
3red cape10
4yellow belt10
5red boots10
10Superman Suitnull

The key to the table is the PartOf column, which contains the self-referencing key. PartOf

Get Sams Teach Yourself Transact-SQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.