For example, the article Article table has an Article Tag association table Article_tag, an article, there are multiple tags.
In updating the article, need to update the association table, I think there are two ways, but I feel very uncomfortable.
1. When updating the article, when updating the associated table, delete all the data in the associated table and re-write the new associated data. This is more secure, but the drawback is that every time you update an article, you have to delete the associated table and write data, resulting in waste, and if there are multiple fields in the associated table, each time you update each field to be re-written.
The association table sets the primary key, when updating article, according to the primary key of the associated table to find the records of the associated table, the existence of the primary key to update operations, does not exist on the new. This is also troublesome, each time to update the article, but also maintain the primary key of the association table inside the form, trouble. But this is a safer situation, but if it's like the above, it's just a hassle to keep a tag for multiple articles. For example, the form submitted by the article tag is a string, you also convert the string into an array, and then corresponding to the primary key of the associated table.
Do not set the primary key of the associated table, set a record according to the correlation ID and need to save the field, according to whether the record is present to update or new. For example select * from article_tag where aid = xxx and tag = 'xxx' , it is troublesome to locate a record, write other fields, or update the operation.
So is there a more formal or better way?
Reply content:
For example, the article Article table has an Article Tag association table Article_tag, an article, there are multiple tags.
In updating the article, need to update the association table, I think there are two ways, but I feel very uncomfortable.
1. When updating the article, when updating the associated table, delete all the data in the associated table and re-write the new associated data. This is more secure, but the drawback is that every time you update an article, you have to delete the associated table and write data, resulting in waste, and if there are multiple fields in the associated table, each time you update each field to be re-written.
The association table sets the primary key, when updating article, according to the primary key of the associated table to find the records of the associated table, the existence of the primary key to update operations, does not exist on the new. This is also troublesome, each time to update the article, but also maintain the primary key of the association table inside the form, trouble. But this is a safer situation, but if it's like the above, it's just a hassle to keep a tag for multiple articles. For example, the form submitted by the article tag is a string, you also convert the string into an array, and then corresponding to the primary key of the associated table.
Do not set the primary key of the associated table, set a record according to the correlation ID and need to save the field, according to whether the record is present to update or new. For example select * from article_tag where aid = xxx and tag = 'xxx' , it is troublesome to locate a record, write other fields, or update the operation.
So is there a more formal or better way?
As you said, "When updating the article, when updating the associated table, all the data in the associated table is deleted and the new associated data is re-written." "I feel so good." It should be more troublesome to compare the differences between the original and present labels every time.
When updating article, the new tag generates an array, the old tag reads arrays, two arrays are compared, and then deletes and adds.