I need to find a way to move or copy and delete some data from 2 tables holding temporary data related with a foreign key to two permanent tables which also are related with a foreign key. The transactions in the tables holding the temporary data shall balance before they are transferred to the permanent tables. Primary keys from the tables holding the temporary data shall of course not be transferred.
The reason for doing this is to avoid unbalanced data into the permanent tables, and deleting records in the permanent tables, when the data do not balance.
The tables I have created:
CREATE TABLE `tbl_0_accs_fin_trans` (
`fin_trans_id` int NOT NULL,
`fin_trans_tstamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`fin_trans_description` text NOT NULL,
`fin_trans_balance` decimal(20,2) DEFAULT NULL
) ;
CREATE TABLE `tbl_0_accs_fin_trans_journals` (
`journal_id` int UNSIGNED NOT NULL,
`fin_trans_id` int NOT NULL,
`acc_id` int NOT NULL,
`journal_amount` decimal(20,2) NOT NULL,
`acc_entry_type_id` int NOT NULL
) ;
CREATE TABLE `tbl_0_accs_fin_trans_tmp` (
`fin_trans_id` int NOT NULL,
`fin_trans_tstamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`fin_trans_description` text NOT NULL,
`fin_trans_balance` decimal(20,2) DEFAULT NULL
) ;
CREATE TABLE `tbl_0_accs_fin_trans_journals_tmp` (
`journal_id` int UNSIGNED NOT NULL,
`fin_trans_id` int NOT NULL,
`acc_id` int NOT NULL,
`journal_amount` decimal(20,2) NOT NULL,
`acc_entry_type_id` int NOT NULL
) ;
-
ALTER TABLE `tbl_0_accs_fin_trans`
ADD PRIMARY KEY (`fin_trans_id`);
ALTER TABLE `tbl_0_accs_fin_trans_journals`
ADD PRIMARY KEY (`journal_id`),
ADD UNIQUE KEY `journal_id` (`journal_id`),
ADD KEY `fkey_fin_trans_id` (`fin_trans_id`),
ADD KEY `fkey_acc_id` (`acc_id`);
ALTER TABLE `tbl_0_accs_fin_trans_tmp`
ADD PRIMARY KEY (`fin_trans_id`);
ALTER TABLE `tbl_0_accs_fin_trans_journals_tmp`
ADD PRIMARY KEY (`journal_id`),
ADD UNIQUE KEY `journal_id` (`journal_id`),
ADD KEY `fkey_fin_trans_id_tmp` (`fin_trans_id`),
ADD KEY `fkey_acc_id_tmp` (`acc_id`);
--
ALTER TABLE `tbl_0_accs_fin_trans_journals`
ADD CONSTRAINT `fkey_acc_id` FOREIGN KEY (`acc_id`) REFERENCES `tbl_0_accs` (`acc_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
ADD CONSTRAINT `fkey_fin_trans_id` FOREIGN KEY (`fin_trans_id`) REFERENCES `tbl_0_accs_fin_trans` (`fin_trans_id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
--
ALTER TABLE `tbl_0_accs_fin_trans_journals_tmp`
ADD CONSTRAINT `fkey_acc_id_tmp` FOREIGN KEY (`acc_id`) REFERENCES `tbl_0_accs` (`acc_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
ADD CONSTRAINT `fkey_fin_trans_id_tmp` FOREIGN KEY (`fin_trans_id`) REFERENCES `tbl_0_accs_fin_trans_tmp` (`fin_trans_id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
The result would be like following:
I have earlier used triggers to do this between 2 tables, but I have not been able to google any description or sample on my new issue.
I have not tried very much, expect from creating the tables holding the temporary data, and searching for documentation on the issue.
I am asking for a link or some guidance where to find samples or documentation on how to solve the issue.