简体   繁体   English

临时表和主表-MySQL

[英]temp and main tables - mysql

I have a scenario where I need to insert the data into table temporarily and later on approval or confirmation, make it permanent. 我有一种情况,需要临时将数据插入表中,然后在批准或确认后将其永久化。 The data will be inserted by a user and approval or denial needs to be done by Super User. 数据将由用户插入,超级用户需要批准或拒绝。

What I think of now is to have two different but identical tables (temporary and main) and the user will insert the data into temp table. 我现在想到的是拥有两个不同但完全相同的表(临时表和主表),用户会将数据插入到临时表中。 After confirmation of Super User, the data will be moved to main table. 确认超级用户后,数据将移至主表。 But the problem comes when a database contains very large number of tables then this process will become more complex. 但是问题出在数据库包含大量表时,该过程将变得更加复杂。

EDIT : This implies to CREATE EDIT & DELETE commands. 编辑:这意味着要创建编辑和删除命令。

Is there any simpler or better approach of doing this? 有没有更简单或更完善的方法?

Please suggest. 请提出建议。

Using a version table (related to comment ): 使用版本表 (与comment有关):

The idea here is to have a version table; 这里的想法是有一个版本表。 when your user changes a piece of information the new version is stored in this table along with the related ID. 当您的用户更改一条信息时,新版本将与相关ID一起存储在此表中。

Then all you need to do is join on the PersonID and select the most recent accepted version. 然后,您所需要做的就是加入PersonID并选择最新接受的版本。

This means the user can make as many updates as they want but they won't show until the super user accepts them, it also means the data is never destroyed (stored in the version table) and they don't need to implement rollback as it's already there! 这意味着用户可以根据需要进行任意数量的更新,但是直到超级用户接受它们后它们才会显示,这也意味着数据永远不会被破坏(存储在版本表中),并且不需要实现回滚。它已经在那里!

See : http://sqlfiddle.com/#!3/cc77f/4 参见http : //sqlfiddle.com/#!3/cc77f/4

People Table: 人物表:

ID | Age Etc... (Info That Doesn't Change)
-----------------------
1  | 12
2  | 16
3  | 11 

People Version Table: 人物版本表:

VersionID | PersonID | Name  | Approved
-----------------------
1         | 1        | Stevz | FALSE
2         | 1        | Steve | TRUE
3         | 2        | James | TRUE
4         | 3        | Jghn  | FALSE
5         | 3        | John  | TRUE

Example table SQL 示例表SQL

CREATE TABLE People 
    (
     id int identity primary key, 
     age int
    );

CREATE TABLE PeopleVersion 
    (
     versionId int identity primary key, 
     peopleId int, 
     name varchar(30),
     approved varchar(30) 
    );

Example Query 查询范例

SELECT * FROM People p
INNER JOIN PeopleVersion v ON p.id = v.peopleID 
WHERE v.approved = 'TRUE' 
ORDER BY versionId DESC 

A further insight: 进一步的见解:

You could even have three states of Approved; 您甚至可以具有三个已批准状态。 null meaning no admin has chosen yet, TRUE meaning it was accepted and FALSE meaning it was rejected null表示尚未选择任何管理员, TRUE表示已接受, FALSE表示已被拒绝

You could show the user the most recent from null and true , show the admin all three and show the other users of the site only versions that were true 您可以向用户显示nulltrue最新信息,向管理员显示所有三个信息,并仅向网站的其他用户显示true版本


Old Comments 旧评论

Could you just add a field called approved to the table and then hide anything without the approval flag set to TRUE ? 您是否可以在表中添加一个称为认可的字段,然后隐藏任何未将认可标志设置为TRUE

It could default to FALSE and only the super user would be able to see items with the flag set to FALSE 它可以默认为FALSE ,只有超级用户才能看到标记设置为FALSE

Eg 例如

Name  | Age | Approved
-----------------------
Steve | 12  | FALSE
James | 16  | TRUE
John  | 11  | FALSE

The user would only see James , but the SuperUser would see all three listed 用户只会看到James ,但是SuperUser会看到所有列出的三个


Alternatively using your temporary and main tables is the other way of looking at this problem, though this may lead to problems as everything get's larger 另外,使用临时表和主表是解决此问题的另一种方法,尽管随着事情的发展,这可能会导致问题。

The easiest approach is a flag within the table marking an entry either approved or not-yet approved. 最简单的方法是在表中标记一个条目,该条目标记已批准或尚未批准的条目。

Then just change the retrieving logic to only show entries where that flag is set to approved. 然后,只需将检索逻辑更改为仅显示该标志设置为认可的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM