/**
* Generates an UUID
*
* @author Anis uddin Ahmad
* @param string a optional prefix
* @return string The formatted UUID
/function uuid ($prefix = ')
{
$chars = MD5 ( Uniqid (Mt_rand (), true);
$uuid = substr ($chars, 0,8). '-';
$uuid. = substr ($chars, 8,4). '-';
$uuid. = substr ($chars, 12,4). '-';
$uuid. = substr ($chars, 16,4). '-';
$uuid. = substr ($chars, 20,12);
Return $prefix. $uuid;
}
Example of using the function-
//using without prefix.
echo uuid (); Returns like ' 1225c695-cfb8-4ebb-aaaa-80da344e8352′
//using with prefix
echo uuid (' urn:uuid: '); Returns like ' urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344e8352′
*************************************************************************************
You can also use SQL statements directly to generate such as:
Insert a UUID directly into the INSERT statement as a primary key (simple):
INSERT INTO price (Name, uuid, price, BID) VALUES (' Feifei_test ', uuid (), 32, 3);
************************************************************************************
PHP generates a UUID to represent what the unique code UUID is. What the GUID is. How does PHP generate UUID? (Google search related content using UUID + MySQL to get more results than uuid+php)
Here's a description of the UUID, pretty detailed.
http://mlxia.javaeye.com/blog/279059
The following sections are reproduced:
I am the only familiar database even MySQL, probably using MySQL, more than 99% of people will use the AutoIncrement ID key, this is understandable, because MySQL's self-increasing ID efficiency is very high, the use is also very convenient. So the remaining 1% people use what is the key to the decision. It may be a keygenerator of your own making, or it may be the UUID we are going to say below.
It is said that in Oracle's circle, if who uses the ID key is to be despised, the most natural choice of primary key is UUID. I don't know about Oracle, whether these hearsay conclusions are correct or not.
So let's take a look at the UUID first. In short, a UUID is a number generated on a single machine that guarantees that all machines in the same space-time are unique. In the UUID algorithm, information such as the MAC address of the NIC, IP, hostname, process ID can be used to ensure its independence.
If your MySQL version is not too old, type SELECT UUID (); The output is the UUID, as follows:
Mysql> select UUID ();
+--------------------------------------+
| UUID () |
+--------------------------------------+
| 54b4c01f-dce0-102a-a4e0-462c07a00c5e |
+--------------------------------------+
Now you should have a more intuitive understanding of the UUID, and let's look at what the advantages and disadvantages of the UUID are respectively.
Advantages:
Can guarantee independence, the program can migrate between different databases, the effect is not affected.
Ensure that the generated IDs are not only table independent, but also library independent, which is especially important when you want to slice the database.
Disadvantages:
Compared with the int type, it takes more space to store a UUID than to take up the place.
After using the UUID, the URL appears verbose and not friendly enough.
The following for the shortcomings of the UUID said my opinion, compared to occupy the place of this shortcoming I do not care, now the most worthless is the hard disk, skip this shortcoming as well. As for the use of UUID, URL is not friendly, I think this is how much your int complex caused by the inertia of thinking, in fact, and int type, UUID is the most natural primary key choice, note that I used here is the nature of this adjective, carefully understand that you can see my meaning. In addition, many times, the URL itself does not need to be friendly, such as an E-commerce site, according to the int friendly URL, her order URL is probably the following form:/order.php/id/123, I would like to explain that this is very friendly, but some too friendly, Friendly and not even safe, for example, I am the next order in the morning, found that the URL is/order.php/id/1000, at night the next order found that the URL is/order.php/id/2000, then I can estimate that the site of the day the number of orders is approximately 1000, It can even roughly estimate its sales, which are often important business secrets. There is no such concern with UUID.
Efficiency.
If the above mentioned UUID's so-called shortcomings are not tenable, then whether to use the UUID key, the only problem is efficiency. It is said that in the database, such as PostgreSQL, there is a special type of UUID, in such a database, the use of UUID key, efficiency without any problems, but there is no such a field in MySQL, if you want to save the UUID in MySQL key, is generally the use of char (36) To simulate, because it is not a native UUID type, so the efficiency of the primary key remains to be tested, and the efficiency of the UUID key is also related to the algorithm implementation of the UUID itself.
I wanted to insert 1 million data on my own computer to test it. Unfortunately, a test, hard drive lights have been bright, let me very worried that it will hang, although the hard disk is not valuable, but my important data are on top, once the bad, the loss is big, so, test had to give up.
As for the use of the UUID on MySQL (with char (36) storage) master key, efficiency in the end, I do not know, sorry-_-!!!
How to generate a UUID. The following method generates a seemingly not UUID, because MD5 is actually possible to have duplicate values (reference http://www.phpx.com/happy/archiver/tid-56636.html), Moreover, the use of random is not to avoid duplication. So you should directly use the UUID function in MySQL to generate
de> function uuid ($prefix = ')
{
$chars = MD5 (Uniqid (Mt_rand (), true);
$uuid = substr ($chars, 0,8). '-';
$uuid. = substr ($chars, 8,4). '-';
$uuid. = substr ($chars, 12,4). '-';
$uuid. = substr ($chars, 16,4). '-';
$uuid. = substr ($chars, 20,12);
Return $prefix. $uuid;
} de>
Insert uuid in MySQL use MySQL's uuid () function
INSERT into Table (ID,..) VALUES (UUID (), ...)
Of course, you can also use the SELECT uuid () to get a UUID value before inserting it into the
Outside the question:
may be compared to the use of integer key, the efficiency is slightly worse,
Another problem is that it may cause URLs to be too long, such as displaying categories under an ID
It's usually category.php?cid=2, but it's probably category.php?uuid=a93f16c5-9634-102c-824f-3ea0651c5b77.
Whether you can change to an integer key