If I have two tables in SQL with a many-many relationship, do I need to create an additional table?

后端 未结 3 644
萌比男神i
萌比男神i 2021-01-13 15:16

Take these tables for example.

Item
    id
    description
    category

Category
    id
    description 

An item can belong to many catego

3条回答
  •  心在旅途
    2021-01-13 15:51

    Yes, you cannot form a third-normal-form many-to-many relationship between two tables with just those two tables. You can form a one-to-many (in one of the two directions) but in order to get a true many-to-many, you need something like:

    Item
        id primary key
        description
    
    Category
        id primary key
        description
    
    ItemCategory
        itemid     foreign key references Item(id)
        categoryid foreign key references Category(id)
    

    You do not need a category in the Item table unless you have some privileged category for an item which doesn't seem to be the case here. I'm also not a big fan of introducing unnecessary primary keys when there is already a "real" unique key on the joining table. The fact that the item and category IDs are already unique means that the entire record for the ItemCategory table will be unique as well.

    Simply monitor the performance of the ItemCategory table using your standard tools. You may require an index on one or more of:

    • itemid
    • categoryid
    • (itemid,categoryid)
    • (categoryid,itemid)

    depending on the queries you use to join the data (and one of the composite indexes would be the primary key).

    The actual syntax for the entire job would be along the lines of:

    create table Item (
        id            integer       not null primary key,
        description   varchar(50)
    );
    create table Category (
        id            integer       not null primary key,
        description   varchar(50)
    );
    create table ItemCategory (
        itemid        integer       references Item(id),
        categoryid    integer       references Category(id),
        primary key   (itemid,categoryid)
    );
    

    There's other sorts of things you should consider, such as making your ID columns into identity/autoincrement columns, but that's not directly relevant to the question at hand.

提交回复
热议问题