Ini artikel lama, belum di-update. Isinya mungkin tidak relevan dengan kondisi sekarang Pada terbitan sebelumnya saya sudah pernah membahas keterbatasan AUTO_INCREMENT di MySQL. AUTO_INCREMENT sangat bergantung pada tipe datanya. Kita tidak bisa menggunakannya untuk data yang tak terhingga (unlimited), karena terbatas. Sebagai gantinya, kita harus membuat fungsi generator ID yang mampu membuat ID yang unik. Pada kesempatan yang lain, saya juga pernah membuat
generator ID yang mirip seperti ID video Youtube. Namun, saya belum yakin sepenuhnya dengan ID unik yang dihasilkannya. Mungkin saja, suatu saat nanti generator ini akan membuat ID yang sama. Seperti halnya fungsi bawaan PHP, Warning Kalau begitu, apa solusinya? UUID atau GUIDUUID merupakan singkatan dari Universally Unique Identifier. Sedangkan GUID, singkatan dari Globally Unique
Identifier. UUID dan GUID adalah hal yang sama. Perbedaanya, GUID itu merupakan implementasi dari UUID yang dilakukan oleh Microsoft. UUID terdiri dari
UUID memiliki beberapa versi, yaitu versi 1, 2, 3, 4, dan 5. Versi 4 digunakan untuk nilai acak (random). Ini bisa kita manfaatkan untuk nilai Primary Key. Menurut penjelasan Wikipedia, setelah membuat 1 miliar UUID untuk setiap detik, kemungkinan untuk terjadi duplikasi nilai yang sama untuk 100 tahun berikutnya adalah 50%. Fungsi Generator GUIDSekarang kita coba membuat sebuah fungsi generator GUID di PHP. Fungsi ini bisa kita manfaatkan untuk pengisian nilai Primary Key.
Hasil ketika mengeksekusi fungsi
tersebut: Jadi, gunakanlah UUID untuk nilai primary key bila data anda terlalu banyak, supaya tidak menjadi masalah di masa depan. Referensi
I recently had a need to use UUID as my primary key for an application that I have architected using the CodeIgniter framework with Grocery Crud on a MySQL database.
Using UUID in MySQL $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, it does not look clean. You could remove and convert the string to a 32-char key. To do that I created this function in CodeIgniter, with the CodeIgniter UUID library. 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 I have a 32-byte key, ac689561f7c94f7ebe9433c6c0fb0672 we can create the table to store it in: CREATE TABLE `type` ( `id` char(32) NOT NULL, `name` varchar(126) CHARACTER SET latin1 NOT NULL, `lastmodified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; I could have done it into a BINARY(16) column – and convert my current 32-bit key into a 16 byte which would have made it faster, I suggest that you should do that if your database is growing very large. I had a need in keeping it 32 bytes, I already have data in the database with primary keys that been generated from scratch, I do not want to keep the old keys and continue with the UUID. That’s my problem. Using Grocery Crud // uuid callback function uuid_callback($post_array){ // Build the Grocery CRUD table and form $this->load->library('uuid'); //Output a v4 UUID $id = $this->uuid->v4(); $id = str_replace('-', '', $id); $post_array['id'] = $id; return $post_array; } Here is how I set up the Grocery Crud form, which calls the uuid_callback before insert. public function type() { $uuid = new grocery_CRUD(); $uuid->set_theme('datatables'); $uuid->set_table('type'); $uuid->callback_after_insert(array($this, 'insert_timestamp', 'type')); $uuid->set_subject('Type'); $uuid->fields('name'); $uuid->fields('id','name'); $uuid->field_type('id','invisible'); $uuid->callback_before_insert(array($this,'uuid_callback')); $uuid->columns('name'); $uuid->display_as('name','Name'); $output = $uuid->render(); // Load the views $this->template->build('tables', $output); } The trick is that I have to define the primary key “id” as a field and then declare it as for hidden, before I call the function uuid_callback to get my UUID key. Conclusions |