The Wayback Machine - https://web.archive.org/web/20220831133239/https://www.geeksforgeeks.org/uuid_short-function-in-mysql/
Skip to content
Related Articles

Related Articles

UUID_SHORT() function in MySQL

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Dec, 2020
View Discussion
Improve Article
Save Article

This function in MySQL is used to return a “short” universal identifier as a 64-bit unsigned integer. The value of UUID_SHORT() is guaranteed to be unique if the following conditions hold :

  1. The server_id value of the current server is between 0 and 255 and is unique among our set of source and replica servers.
  2. We do not set back the system time for our server host between mysqld restarts.
  3. We should invoke UUID_SHORT() on average fewer than 16 million times per second between mysqld restarts.

Syntax :

UUID_SHORT()

Parameter :
This method does not any parameter.

Returns :
It returns a Universal Unique Identifier number.

Example-1 :
Generating a short Universal Unique Identifier value with the help of UUID_SHORT Function.

SELECT UUID_SHORT() AS Short_UUID_Value ;

Output :

SHORT_UUID_VALUE
99032629508046848

Example-2 :
Whenever we will use UUID_SHORT function we will get different short Universal Unique Identifier value. Lets check it –

SELECT UUID_SHORT() 
AS  SHORT_UUID_VALUE1, UUID_SHORT() 
AS SHORT_UUID_VALUE2, UUID_SHORT() 
AS SHORT_UUID_VALUE3 ;

Output :

SHORT_UUID_VALUE1SHORT_UUID_VALUE2SHORT_UUID_VALUE3
990326295080468499903262950804685099032629508046851

Example-3 :
In this example we will use UUID_SHORT as a primary key in a table. To demonstrate create a table named OrderDetails.

CREATE TABLE OrderDetails2(
  OrderId BIGINT PRIMARY KEY,
  ProductName VARCHAR(100) NOT NULL,
  Price DECIMAL(10, 2) NOT NULL,
  ExpectedDelivery DATE NOT NULL
);

Now, inserting data into OrderDetails table. Here, We will use UUID_SHORT functions to assign the value in OrderId Column.

INSERT INTO OrderDetails2(OrderId, ProductName, Price, ExpectedDelivery)
VALUES(UUID_SHORT(), 'Asus Rog', 90000.00, '2020-12-20'),
     (UUID_SHORT(), 'Acer Predator', 100000.00, '2020-12-18'),
     (UUID_SHORT(), 'Lenovo Legion', 85000.00, '2020-12-19'),
     (UUID_SHORT(), 'Hp Omen', 70000.00, '2020-12-18'),
     (UUID_SHORT(), 'Dell Inspiron', 65000.00, '2020-12-23'),
     (UUID_SHORT(), 'Acer Nitro', 60000.00, '2020-12-22'),
     (UUID_SHORT(), 'Asus Tuf', 80000.00, '2020-12-19');

Next we will use the following command to check the table.

SELECT  * from OrderDetails2;

Output :

ORDERIDPRODUCTNAMEPRICEEXPECTEDDELIVERY
99032629508046854Asus Rog90000.002020-12-20
99032629508046855Acer Predator100000.002020-12-18
99032629508046856Lenovo Legion85000.002020-12-19
99032629508046857Hp Omen70000.002020-12-18
99032629508046858Dell Inspiron65000.002020-12-23
99032629508046859Acer Nitro60000.002020-12-22
99032629508046860Asus Tuf80000.002020-12-19
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!