Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i prevent the duplicate id from the imported table in MariaDB?

(Before that, i apologize for my bad English) I have study cases like this:

I am currently having a trouble with my Web Application. I made a Web application for a certain company. I made the app using CodeIgniter 3.

I built the database using Maria DB. For the id in each table, i am using Auto-increment id for my application database for each table. I usually deploy the web app to the cloud server (sometimes the company have their own dedicated server, but sometimes haven't ). One day, there is a company that they don't want to deploy the app that i have made before to the cloud ( for the security purposes they said ).

This company wanted to deploy the app to the employee's PC personally in the office, while the pc for each employee not connected to each other ( i.e stand alone pc/personal computer/employee's Laptop ). They said, for every 5 months, they would collect all of the data from employee's personal computer to company's data center, and of course the data center are no connected to the internet. I told them that's not the good way to store their data. ( because the data will be duplicate when i am trying to merge all of the data into one, since my column id for every table are in auto-increment id, and it's a primary key). Unfortunately, The company still want to kept the app that way, and i don't know how to solved this.

They have at least 10 employees that would used this web app. According that, I have to deploy the app to the 10 PC personally.

Additional info : Each employee have their own unique id which they got from the company, and i made the auto_increment id for each employee, just like the table below:

id   |  employee_id  | employee_name   |
1    | 156901010     |  emp1
2    | 156901039     |  emp2
3    | 156901019     |  emp3
4    | 156901015     |  emp4
5    | 156901009     |  emp5
6    | 156901038     |  emp6

The problem is whenever they fill the form from that application, some of the table are not stored the employee's id but the new id that come from increment id.

For example electronic_parts table. They have the attribute like below:

| id  |  electronic_part_name  |  kind_of_electronic_part_id  |

if the emp1 fill the form from the web app , the table's content would like below.

| id  |  electronic_part_name  |  kind_of_electronic_part_id  |
|  1  |   switch               |           1               |

and if the emp2 fill the form from the web app , the table's content would like below.

| id  |  electronic_part_name  |  kind_of_electronic_part_id  |
|  1  |      duct tape         |           10              |

When i tried to merge the contents of the table into the data center it would falling apart because the duplicate id.

It's getting worst when i think about my foreign key in other tables.. like for example the customer_order table.

The table for customer_order column looks like below (just a sample, not the actual table, but similar).

|id  |  customer_name   |  electronic_parts_id  |  cashier(a.k.a employee_id, the increment id one, not the id that employee got from a company as i described above )  |
| 1  |   Henry          |           1           |              10              |
| 2  |   Julie          |           2           |              9               |

Does anyone know how to solved this problem ? or can someone suggest/recommend me some good way to solved this ?

NOTE: Each Employees have their own database for their app, so the database is not centralized, it's a stand-alone database, that means, i have to installed the database to the employee's pc one by one

like image 865
Gagantous Avatar asked Jan 24 '18 07:01

Gagantous


People also ask

How do you prevent duplicates in MySQL table?

Preventing Duplicates from Occurring in a Table. You can use a PRIMARY KEY or a UNIQUE Index on a table with the appropriate fields to stop duplicate records. Let us take an example – The following table contains no such index or primary key, so it would allow duplicate records for first_name and last_name.

How do I remove duplicates in Mariadb?

Fish for them with JOIN, and delete them with a temporary table. Use INSERT... ON DUPLICATE KEY UPDATE to update on discovery of a duplicate. Use DISTINCT to prune the results of a SELECT statement and remove duplicates.


1 Answers

This is an unconventional situation and you can have an unconventional solution.

I can suggest you two methods to solve this issue.

  1. Instead of using autoincrement for primary key generate a UUID and use it as the primary key. Regarding the probability of duplicates in random UUIDs: Only after generating 1 billion UUIDs every second for the next 100 years

    In CodeIgniter you could do this with the following code snippet.

    $this->db->set('id', 'UUID', FALSE);
    

    This generates a 36 characters hexadecimal key (with 4 dashes included).

    ac689561-f7c9-4f7e-be94-33c6c0fb0672
    

    As you can see it has dashes in the string, using the CodeIgniter DB function will insert this in the database with the dashes, it still will work. If it does not look at clean, you could remove and convert the string to a 32-char key.

    You can use the following function with the help of [CodeIgniter UUID library][1].

    function uuid_key {
            $this->load->library('uuid');
            //Output a v4 UUID 
            $id = $this->uuid->v4();
            $id = str_replace('-', '', $id);
            $this->db->set('id', $id, FALSE);
    }
    

    Now we have a 32-byte key,

    ac689561f7c94f7ebe9433c6c0fb0672
    
  2. An alternate unconventional method to tackle the situation is by adding function to log all Insert, Update, Delete queries processed in the site to a file locally. By this way, in each local implementation will generate a log file with an actual list of queries that modify the DB over time in the right sequential order.

    At any point in time, the state of the database is the result of the set of all those queries happened in the past till that date.

    So in every 5 months when you are ready to collect data from employees personal computer, instead of taking data dump, take this file with all query log.(Note: Such a query log won't have auto-increment id as it will be created only in the real time when it is executed towards a Database. )

    Use such files to import data to your datacenter. This will not conflict as it will generate autoincrements in your data center in real time. (Hope you do not have to link your local to data center at any point of time in future)

    [1]: https://github.com/Repox/codeigniter-uuid

like image 153
Deepu S Nath Avatar answered Nov 19 '22 13:11

Deepu S Nath