The `UUID()` expression in MySQL generates a Universal Unique Identifier (UUID), which is a 128-bit number used to uniquely identify information without significant risk of collision. It is particularly useful for generating unique keys across distributed systems.
Usage The `UUID()` expression is used when a unique identifier is needed, such as primary keys in a distributed database environment. It does not require any parameters and can be used directly in SQL statements.
This syntax generates a new, random UUID each time it is executed. The UUID generated is a version 1 UUID, which is based on a timestamp and the node's MAC address, formatted as a string of 36 characters including hyphens.
This example generates a single UUID, which can be used as a unique identifier for data entries.
2. Inserting UUID into Table
INSERTINTOusers(id, username)VALUES(UUID(),'johndoe');Offerto da
Here, a new user is added to the `users` table with a UUID as the `id`, ensuring each user has a unique identifier.
3. Creating a Table with UUID as Primary Key
CREATETABLEorders(
id CHAR(36)PRIMARYKEY,
product_name VARCHAR(255),
quantity INT);INSERTINTOorders(id, product_name, quantity)VALUES(UUID(),'Laptop',5);Offerto da
A table is created with a `UUID` as the primary key, and a new order is inserted, demonstrating the use of `UUID()` for unique key generation.
Tips and Best Practices
Use for Globally Unique Keys. Utilize `UUID()` when you need globally unique identifiers across different systems, especially in horizontally scalable architectures.
Consider Storage Impact. Storing UUIDs as `CHAR(36)` can increase storage requirements; consider using `BINARY(16)` for efficiency.
Be mindful of indexing. UUIDs do not have natural ordering, which can impact index performance. This can affect JOIN operations and query performance. Consider alternative indexing strategies or use `UUID_SHORT()` for a shorter version if applicable.
Security Considerations. Be aware that version 1 UUIDs may expose system information, such as MAC addresses. Handle them properly to mitigate security risks.
Avoid overuse in simple applications. In smaller-scale applications, `AUTO_INCREMENT` may be more efficient than `UUID()` for primary keys when distributed uniqueness is not required.
Evaluate Key Strategies. Determine when to use UUIDs versus other unique key strategies based on the need for distributed uniqueness versus local uniqueness.
Remember that `UUID()` is not the only function available; `UUID_SHORT()` can be used for generating a more compact unique identifier when applicable.
The `UUID()` expression in MySQL generates a Universal Unique Identifier (UUID), which is a 128-bit number used to uniquely identify information without significant risk of collision. It is particularly useful for generating unique keys across distributed systems.
Usage
The `UUID()` expression is used when a unique identifier is needed, such as primary keys in a distributed database environment. It does not require any parameters and can be used directly in SQL statements.
This syntax generates a new, random UUID each time it is executed. The UUID generated is a version 1 UUID, which is based on a timestamp and the node's MAC address, formatted as a string of 36 characters including hyphens.
Examples
1. Generating a UUID
This example generates a single UUID, which can be used as a unique identifier for data entries.
2. Inserting UUID into Table
Here, a new user is added to the `users` table with a UUID as the `id`, ensuring each user has a unique identifier.
3. Creating a Table with UUID as Primary Key
A table is created with a `UUID` as the primary key, and a new order is inserted, demonstrating the use of `UUID()` for unique key generation.
Tips and Best Practices
Remember that `UUID()` is not the only function available; `UUID_SHORT()` can be used for generating a more compact unique identifier when applicable.