EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL UUID
Secondary Sidebar
MySQL Tutorial
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MySQL UUID

By Aanchal SharmaAanchal Sharma

MySQL UUID

Introduction to MySQL UUID

MySQL UUID is defined as “Universally Unique Identifier” which shows the usage of a Primary key for a table. Basically, it is described on the basis of RFC 4122 that is a URN Namespace i.e. Universally Unique Identifier (UUID). Here, we will show UUID as PK (Primary Key) and its pros and cons while application of it in MySQL query. UUID is considered as a numeral which is distinct worldwide in universe and time interval. If UUID are produced on two independent severs that are associated to each other then also the both UUID values are projected to be unique.

Syntax of MySQL UUID

A UUID value is defined as a 128-bit numeral that signifies an utf8 string of a five hexadecimal digits which has the succeeding format:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,768 ratings)

For generating the UUID values, we need to apply the UUID function shown below:

UUID()

Here, the function UUID() gives a UUID value in agreement with the UUID version 1 termed in RFC 4122.

This can be illustrated by using the UUID () function as follows:

SELECT UUID();

Output:

MySQL UUID 1

It should be noted that the function is insecure for application based on statement. If this function is implemented whenever the binlog_format is fixed to statement then, it will log a warning.

How UUID works in MySQL?

UUID() function usage may be similar to a Primary key for a table in the database. Let us view the MySQL UUID comparing to Primary Key with AUTO_INCREMENT INT attributes in database.

Advantages that MySQL UUID for a primary key conveys are listed below:

  • The UUID values are distinct across databases, tables and also servers which let us to unite rows from several records or say distributed databases across MySQL servers.
  • UUID do not describe the info about any data therefore they are more secure to be used for URL. Assume that if an employee having employee id 05 is accessing his/her account by using the URL: http://www.test.com/employees/05/then, for others also it will be easy to guess and use as 07, 08, etc. and so it will provide a target for any harmful attack to breach security and increase risk.
  • To evade a round trip to the database server, UUID values can be produced anywhere. Similarly, it is a bridges logic in the application. Let us assume that to input records into a parent table as well as child tables then we need to insert into the base table i.e. parent table initially, receive the produced id and next the data will be inserted into the child tables. Thus, by applying UUID, we can create the value of primary key of the base table first and input rows at identical time into both parent and child tables within a transaction.

Drawbacks are briefed below beside the pros of using UUID relating a Primary key:

  • If we use integers i.e. 4 bytes or also big integers i.e. 8 bytes then it takes less storage capacity than UUID values i.e. storing 16 bytes.
  • Restoring can be more problematic. Like the expression WHERE Employee_ID = 05 is easy instead of WHERE Employee_ID = ‘fd7bc2fb-9ve4-10t7-49yy-p9vbc73cbxyf’.
  • When UUID values are used it may raise performance issues because of their size and unordered form.

UUID Solution in MySQL as follows:

MySQL provides you the option to store the UUID values in BINARY format or say compress form and can be displayed in human readable i.e. VARCHAR format.

This can be achieved by using the following MySQL functions related to UUID:

  • UUID_TO_BIN: This function is responsible to convert a UUID value from VARCHAR format which is human readable into a BINARY format which is compacted one for storing.
  • BIN_TO_UUID: It is just opposite to the previous one. BIN_TO_UUID function changes the UUID from condensed form or BINARY format to VARCHAR format which is human understandable for demonstrating.
  • IS_UUID: This IS_UUID function provides 1 when the argument is an effective string-format UUID. But if the argument is not legal string format UUID then, the function gives 0 as result. Also, if the argument is NULL, the IS_UUID function will return NULL.

The formats below are the valid string UUID arrangements used in MySQL:

dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh

ddddddddeeeeffffgggghhhhhhhhhhhh

{ dddddddd-eeee-ffff-gggg-hhhhhhhhhhhh }

It should be remembered that these functions, IS_UUID, UUID_TO_BIN and BIN_TO_UUID are only present or supportive in MySQL version 8.0 or say later ones.

Examples of MySQL UUID

Let us evaluate some of the examples of the working of UUID as the Primary key for a table in the MySQL database server:

Example #1

Suppose, we have shown here an experiment for UUID to have unique values at intervals on the same server so, we have executed a UUID value which gives the following result and provided a gab in time interval for 3 seconds with the SLEEP() in MySQL.

Code:

SELECT UUID(); do sleep(3); SELECT UUID();

Output:

MySQL UUID 2

Now, after that again when we have executed the UUID value then, it is found to be different like this.

it is found to be different

Example #2

We will create a fresh table in our database initially using the CREATE statement query as follows.

Code:

CREATE TABLE Employees (Employee_IDBINARY(16) PRIMARY KEY, Employee_Name VARCHAR(255));

After executing the above command a table structure will be created in the database. Next we will insert some data into it. The primary key column is specified with binary (16) with no character and no collation.

Let us input few UUID values into the column Employee_ID in the Employees table.

For implementing this we will also use the functions UUID() and UUID_TO_BIN() with the help of INSERT query below.

Code:

INSERT INTO Employees (Employee_ID, Employee_Name) VALUES
('1','Nikhil'), ('2', 'Rita'),('3', 'Sangam'), ('4', 'Hari');

Now see the contents of the table by the following query.

Code:

SELECT * FROM Employees;

Output:

contents of the table

Or, you can use this query with function in MySQL version 8.0 and above.

Code:

INSERT INTO Employees (Employee_ID, Employee_Name) VALUES
(UUID_TO_BIN(UUID()), 'Nikhil'), (UUID_TO_BIN(UUID()), 'Rita'),
(UUID_TO_BIN(UUID()), 'Sangam'), (UUID_TO_BIN(UUID()), 'Hari');

You can see that the employee id having BINARY Data type has the 16 bit digits in the table.

Now, to query info from a UUID column from the table, we will apply the function BIN_TO_UUID() that transforms the binary data value to human readable form as follows.

Code:

SELECT BIN_TO_UUID(Employee_ID) ID , Employee_Name FROM Employees;

Here, you can view that the UUID values are displayed for the Primary key column values in the table from binary digits to human character readable structure that secures the main values.

Conclusion

Though the UUID () values are planned to be distinct but they may not be essentially unpredictable one. We can create the UUID values in some further way if unpredictability is mandatory. Hence, we have seen how to apply UUID for a column with Primary key. As the UUID values are safer to be applied in a URL and can be produced anyplace that escapes a round trip to the MySQL database server.

Recommended Articles

This is a guide to MySQL UUID. Here we discuss the introduction to MySQL UUID, how UUID works, along with respective query examples. You may also have a look at the following articles to learn more –

  1. MySQL FULLTEXT
  2. MySQL Partition
  3. MySQL REVOKE
  4. MySQL Cluster
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more