SQL example creating table with auto-increment.
Create Table USER( id BIGINT NOT NULL auto_increment, name VARCHAR(255) NOT NULL, Primary key (id) );
UUID / GUID
A GUID is an acronym that stands for Globally Unique Identifier; they refer to as UUIDs or Universally Unique Identifiers. UUIDs are 128-bit hexadecimal numbers; the 32 hex characters, plus four dashes, are just a friendlier version for readability and are globally unique. The form is 8–4–4–4–12, with a total of 36 characters.
public static void main(String[] args) { String UUID = UUID.randomUUID().toString().replaceAll("-",""); System.out.println(UUID); }
There are four versions of UUIDs.
UUID1 uses MAC address and timestamp to generate effective uniqueness.
UUID3 and UUID 5 uses cryptographic hashing and application-provided text strings to generate UUID. (UUID 3 uses MD5 hashing, and UUID 5 uses SHA-1 hashing).
UUID4 uses pseudo-random number generators to generate UUID.
TICKET SERVER
MySQL’s Replace Into Statement can help to make MySQL auto-increment work across multiple databases, set up a dedicated database with a single table and a single record whose sole purpose is to provide unique and incremental ID.
REPLACE works exactly like INSERT, except if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new one row is inserted.
CREATE TABLE ID (
id bigint(20) unsigned NOT NULL auto_increment,
stub char(1) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY stub (stub)
) ENGINE=MyISAM
When I need a new globally unique 64-bit ID I issue the following SQL.
REPLACE INTO ID (stub) VALUES ('a'); SELECT LAST_INSERT_ID();
MULTI-MASTER REPLICATION - CENTRALIZATION
To improve the above-centralized model, we need to make some changes for high availability, like the master-slave cluster mode should be replaced. If you are afraid that a master node will hang up, you should consider a dual master mode cluster; that is, two MySQL instances can produce self increasing IDs separately.
Set the starting value and self increasing step size.
--MySQL_1 CONFIGURATION set @@auto_ increment_ Offset = 1; -- starting value set @@auto_ increment_ Increment = 2; -- step size --MySQL_2 CONFIGURATION set @@auto_ increment_ Offset = 2; -- starting value set @@auto_ increment_ Increment = 2; -- step size