I'm new to projecting databases.
I have 3 tables:
users:
+--------+--------+---------+----------+--------+
| userid | name | surname | password | admin |
+--------+--------+---------+----------+--------+
| value1 | value2 | value3 | value4 | value5 |
+--------+--------+---------+----------+--------+
users_attributes:
+--------+--------+--------+
| id | name | type |
+--------+--------+--------+
| value1 | value2 | value3 |
+--------+--------+--------+
users_attributes_values (with foreign key userid and attributeid):
+--------+-------------+----------------+
| userid | attributeid | attributevalue |
+--------+-------------+----------------+
| value1 | value2 | value3 |
+--------+-------------+----------------+
The relationship of the users with the attributes is one to many , example: If there are 5 users, and I create 3 attributes:
then each of the 5 will have these 3 attributes.
When a new attribute is inserted in the table users_attributes , I want that in the table users_attributes_values the references to that attribute for each user present are automatically inserted. Creo / Insert 2 attributes:
INSERT INTO users_attributes(
id, name, type)
VALUES ('1', 'Peso', 'Int');
INSERT INTO users_attributes(
id, name, type)
VALUES ('2', 'Estatura', 'Int');
by inserting them, the trigger will insert in users_attributes_value the id of the present users and the id of the attributes just created, Example, there are 3 user present:
+--------+-------------+----------------+
| userid | attributeid | attributevalue |
+--------+-------------+----------------+
| 1 | 1 | |
| 1 | 2 | |
| 2 | 1 | |
| 2 | 2 | |
| 3 | 1 | |
| 3 | 2 | |
| | | |
+--------+-------------+----------------+
This is the expected result after creating new attributes, how could you get this result using triggers?