1

I have the following (simplified) data structure:

Property                    Product
-----------   n <----> 1    -------
product_id                  manufacturer
type                        name
value                    

Where the products table might look like this

| name         |
|--------------|
| iPad 2       |
| iPhone 6     |

and the associated properties might be

| product_id | type        | value |
|----------------------------------|
|      1     | RAM         | 16GB  |
|      1     | CPU         | A11   |
|      1     | Screen Size | 10"   |
|      2     | Cellular    | yes   |
|      2     | Screen Size | 5.5"  |

Is it possible, in MySQL, to create a constraint, so that every product can have each property type at most once (e.g., a product can't have multiple Screen Sizes associated. As far as I could understand the MySQL docs, constraints only work on one relation, but not across relations.

2
  • Also, consider storing types in a separate table Commented May 25, 2019 at 10:30
  • @Strawberry true, my example isn't normalized Commented May 25, 2019 at 13:23

1 Answer 1

3

I realized the solution immediately after posting this question. The constraint can be placed on the properties relation, so that each pair of product_id, type can only exist once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.