I use Mysql and PHP and My database have constraint. I have made a function to import an sql file for a whole database by these step:
Everything is work fine. However, now I have to expand it. I have to make an import system which can import for individual functionality of website. It means I have to make an system can import individual tables and keep the constraint of them. For example, my system have tables user, article... when import article (export from local and import to development server) I have to check the user_id to make sure the application have this user (on development). I've never done anything like this before. I look like very complicated.
Therefore, I want to ask anyone how to do it? How many way to do it? and Which is the best solution to do it? Or If you have many experience, Could you please share to me where should I find to learn it or which book I should read to improve practice in database solution?
You can switch off foreign keys constraints:
-- switch off
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
-- make import
-- switch on
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
You can load your import data into a temporary table and look for invalid rows in it.
DROP TEMPORARY TABLE IF EXISTS tmp_import_article;
CREATE TEMPORARY TABLE tmp_import_article LIKE article;
-- TODO: load or insert import data into tmp_import_article
-- select invalid rows
SELECT tia.*
FROM tmp_import_article tia
LEFT JOIN `user` u ON u.user_id = tia.user_id
WHERE u.user_id IS NULL;
If you get an empty result, then everything is fine and you can insert the data from temporary table into the real one.
If you get a not empty result - show it to the user in an error message.
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.