Open In App

UUID_SHORT() function in MySQL

Last Updated : 22 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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_VALUE1 SHORT_UUID_VALUE2 SHORT_UUID_VALUE3
99032629508046849 99032629508046850 99032629508046851

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 :

ORDERID PRODUCTNAME PRICE EXPECTEDDELIVERY
99032629508046854 Asus Rog 90000.00 2020-12-20
99032629508046855 Acer Predator 100000.00 2020-12-18
99032629508046856 Lenovo Legion 85000.00 2020-12-19
99032629508046857 Hp Omen 70000.00 2020-12-18
99032629508046858 Dell Inspiron 65000.00 2020-12-23
99032629508046859 Acer Nitro 60000.00 2020-12-22
99032629508046860 Asus Tuf 80000.00 2020-12-19

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads