Generate GUID in MySQL for existing Data?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with databases, a common requirement is to uniquely identify each record. A Universally Unique Identifier (UUID) or Globally Unique Identifier (GUID) is often used for this purpose, providing a unique value that can be assigned to each row in a table. This article explores how to generate GUIDs in MySQL, specifically for existing data, providing both technical insights and practical examples.
Understanding GUIDs and UUIDs
GUID is a 128-bit number used to uniquely identify information in computer systems. Although GUID and UUID are terms often used interchangeably, GUID is essentially Microsoft's implementation of the UUID specification. MySQL has built-in support for UUIDs, offering a `UUID()` function which generates a new UUID.
Technical Structure
A UUID is represented as 32 hexadecimal characters, displayed in five groups separated by hyphens, for a total of 36 characters, such as:
`550e8400-e29b-41d4-a716-446655440000`
UUIDs are designed to be unique across systems, ensuring that no two UUIDs are identical, even if they are generated simultaneously on different machines.
Why Use GUID/UUID in Databases?
- Uniqueness Across Databases: GUIDs are unique across different database systems, which is crucial for applications that work in distributed environments.
- Decentralized: Since they're generated without a central authority, they're perfect for decentralized systems.
- Security: They make it difficult to guess values, which enhances security by resisting row enumeration attacks.
- Compatibility: UUIDs are becoming a standard, hence ensuring better compatibility with distributed systems.
How to Generate GUID for Existing Data in MySQL
If you're working with existing data, generating GUIDs involves a few steps. Below is a step-by-step guide with explanations and examples.
Step-by-Step Process
- Add a New Column: Create a new column to store the UUIDs. This column should be of type `CHAR(36)` to accommodate the UUID format.
- Identify Unique Rows: Use existing unique fields (like a primary key) to generate unique UUIDs.
- Resolve Duplicates: This might involve merging data or differentiating records with additional attributes.
- Sequential UUIDs: Some libraries allow creation of UUIDs that are partially time-based and thus somewhat ordered and more index-friendly.
Related reading
- Generate UUID for Cassandra in Python
- Generating a random unique 8 character string using MySQL
- Geographical redundency for database What are the options?
- Get a BigInteger attribute from Cassandra ResultSet
- Get BinData UUID from Mongo as string
- Get boolean from database using Android and SQLite
- Get current AUTO_INCREMENT value for any table
- Get first row for each partition key in Cassandra

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.