简体   繁体   中英

Best practice for saving data in SQL

I am creating a survey. It is long enough that I want to give people a chance to save what they have so far. I am wondering what the best practice is for saving the data. Do I turn off foreign key constraints so if they haven't selected everything yet then foreign key constraint errors are ignored. In this example I use an ID to link the documents table to the table that holds what they have selected. If they haven't selected a document yet then a -1 is inserted as a holder. Or do I create a second table to hold the saved place data. Or is there a third option.

There is a 3rd option. You can generate the primary key right when the user begins the survey. There are two ways to do this:

  • Generate a database record and read-back the primary key (assumes it's generated by the database)
  • Change the primary key to be a GUID and simply generate a GUID in code.

OK, using -1 this way means that you have to have document in the documents table with an id of -1. If you don't a better structure would be to define the field as allowing nulls. Then you pass in a null value.

You might want to read this: Can a foreign key be NULL and/or duplicate?

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