Solution: Create an Intersection Table
Instead of storing the account_id in the Products table, store it in a separate table, so each individual value of that attribute occupies a separate row. This new table Contacts implements a many-to-many relationship between Products and Accounts:
â | âCREATEâ âTABLEâ Contacts ( |
â | product_id âBIGINTâ âUNSIGNEDâ âNOTâ âNULLâ, |
â | account_id âBIGINTâ âUNSIGNEDâ âNOTâ âNULLâ, |
â | âPRIMARYâ âKEYâ (product_id, account_id), |
â | âFOREIGNâ âKEYâ (product_id) âREFERENCESâ Products(product_id), |
â | âFOREIGNâ âKEYâ (account_id) âREFERENCESâ Accounts(account_id) |
â | ); |
â | |
â | âINSERTâ âINTOâ Contacts ... |
Get SQL Antipatterns, Volume 1 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.