I am new to databases and MySQL in particular. Suppose I need to store flat key-value data structures in MySQL. Each data structure has a few required fields and a number of optional fields that are not known in advance and may change frequently.
I will need to retrieve all data structures by one of the required fields and probably delete them.
So I'd like to store these data structures in a table like that: (just a copy-paste from the Internet rather than working code)
CREATE TABLE my_data_structures (
my_data_structure_id INT NOT NULL,
my_required_field1 VARCHAR NOT NULL,
my_required_field2 INT NOT NULL,
PRIMARY KEY (my_data_structure_id)
)
CREATE TABLE my_optional_fields (
my_optional_field_name VARCHAR NOT NULL,
my_optional_field_value VARCHAR NOT NULL,
FOREIGN KEY (my_data_structure_id) REFERENCES my_data_structures(my_data_structure_id)
)
Does this approach make sense ? How to define the primary key for the second table ?