Yes, you cannot form a many-to-many relationship between two tables with these three tables. You can form one-to-many (in one of two directions), but in order to get 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 Element table unless you have any privileged category for an element that does not look here. I'm also not a big fan of introducing unnecessary primary keys when there is already a βrealβ unique key in the join table. The fact that item and category identifiers are already unique means that the entire entry for the ItemCategory table will also be unique.
Just control the performance of the ItemCategory table using standard tools. You may need an index for one or more of:
- Itemid
- CategoryID
- (Itemid, CategoryID)
- (CategoryID, Itemid)
depending on the queries you use to combine the data (and one of the composite indexes will be the primary key).
The actual syntax for the entire job will look like this:
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) );
Other things you should consider, for example, so that your identifier columns are in the identification / auto-increment columns, but not directly related to the issue in question.
source share