简体   繁体   中英

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 ):

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.

Then all you need to do is join on the PersonID and select the most recent accepted version.

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

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

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

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


Old Comments

Could you just add a field called approved to the table and then hide anything without the approval flag set to TRUE ?

It could default to FALSE and only the super user would be able to see items with the flag set to 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


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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