Store data with variable columns in MySQL - EAV model

When it is necessary to store data with variable columns in MySQL, a common solution is to use the EAV (Entity-Attribute-Value) model. The EAV model allows flexible storage of different attributes of different entities, and is suitable for situations where the number of attributes is uncertain. This article will introduce how to use Java and MySQL to implement the storage and retrieval of EAV models.

What is the EAV model?
The EAV model is a data model for storing entity attributes. It consists of three tables:

Entity table: stores the basic information of the entity, such as entity ID and name.

Attribute table: Store attribute information of entities, such as attribute names.

Value table: store the specific information of the attribute value.

By storing entity IDs, property names and property values ​​in different tables, properties of entities can be easily added, removed or modified without changing the database structure.

Database table design
First, create the Entity table:

CREATE TABLE Entity (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255)
);

Then, create the Attribute table:

 
CREATE TABLE Attribute (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255)
);

Next, create the Value table:

 
CREATE TABLE Value (
  id INT PRIMARY KEY AUTO_INCREMENT,
  value VARCHAR(255)
);
最后,创建EntityAttribute表用于关联实体、属性和值: 

```java
CREATE TABLE EntityAttribute (
  entity_id INT,
  attribute_id INT,
  value_id INT,
  FOREIGN KEY (entity_id) REFERENCES Entity(id),
  FOREIGN KEY (attribute_id) REFERENCES Attribute(id),
  FOREIGN KEY (value_id) REFERENCES Value(id)
);

Now, you can execute INSERT statements to save entity, attribute and value data and relate them. For example:
- to save entity

INSERT INTO Entity (name) VALUES ('Object1');
INSERT INTO Entity (name) VALUES ('Object2');

– save properties

INSERT INTO Attribute (name) VALUES ('Attribute1');
INSERT INTO Attribute (name) VALUES ('Attribute2');

– save value

INSERT INTO Value (value) VALUES ('Value1');
INSERT INTO Value (value) VALUES ('Value2');

– Associate entities, properties and values

INSERT INTO EntityAttribute (entity_id, attribute_id, value_id)
VALUES (1, 1, 1); -- Object1 的 Attribute1 属性的值是 Value1

INSERT INTO EntityAttribute (entity_id, attribute_id, value_id)
VALUES (1, 2, 2); -- Object1 的 Attribute2 属性的值是 Value2

By executing the above SQL statement, you can save and associate entity, attribute and value data in MySQL.

For retrieving data, you can use the JOIN operation to get attributes and values ​​of a specific entity. For example, to retrieve the properties and values ​​of Object1:

SELECT a.name AS attribute, v.value
FROM Entity e
JOIN EntityAttribute ea ON e.id = ea.entity_id
JOIN Attribute a ON ea.attribute_id = a.id
JOIN Value v ON ea.value_id = v.id
WHERE e.name = 'Object1';

This will return the properties and corresponding values ​​of Object1.

Guess you like

Origin blog.csdn.net/weixin_43866043/article/details/130769319