mysql 获取全局唯一值_MySQL
bitsCN.com
在涉及数据库存储数据的时候,经常会遇到唯一值问题,有的是主键带来的限制,有的则是业务上的需要。
下面介绍几种唯一值的获取或者生产方法:
先建一个测试用的表tbl_user,有三个字段:Id、Name、Age,其中Id为主键。1: drop table if exists `tbl_user`;
2: create table
3: `tbl_user` (
4: `Id` int(10),
5: `Name` varchar(20),
6: `Age` int(10),
7: PRIMARY KEY (`Id`)
8: )DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
插入几条数据
1: insert into tbl_user values (1000,"小猫",22);
2: insert into tbl_user values (1001,"小狗",22);
3: insert into tbl_user values (1002,"小刺猬",22);
4:
5: select * from tbl_user;
查询结果:
<img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L22201W50-23L24.jpg" class="lazy" style="max-width:90%" title="image" alt="image" style="max-width:90%" style="max-width:90%" border="0">
2.先查询表中最大的值select max(id),再加1后作为新的值。很笨的方法。
1: select max(Id) from tbl_user;
2: 查询到的最大Id为 1002
3:
4: 之后插入 1003
5:
6: insert into tbl_user values (1003,"小熊",22);
7:
此时表中数据为
3.如果是表级别的唯一,即在同一个表中某个字段唯一,可以把该字段设置为“自增(AUTO_INCREMENT)”的。这样你不必费心思去生成这个不能重复的唯一值了。但是一般应用程序是需要这个唯一值的,这个时候你就得在查询一次去获取刚才数据库自增生成的Id。比如在用户登录的时候,你要生成一个登录会话Id或者Token,这些程序一般是需要得到这个值而不是仅仅存在数据库中。生成的值,1.可以一般的select条件查询,根据条件查询刚才插入的数据。2.直接调用select @@IDENTITY 就可以得到上一次插入记录时自动产生的ID(注意是在数据库同一个连接(会话)中),用在插入后立即select @@IDENTITY 。
看例子,先将表中的Id字段设置为自增,再插入一条数据(不要插入Id值,让数据库自增得到值),select @@IDENTITY查询,最后验证看看。
1.#将Id改为自增(auto_increment)ALTER TABLE tbl_user CHANGE Id Id int not null auto_increment;#或者 先删除Id字段再添加一个Id字段alter table tbl_user auto_increment=1000;alter table tbl_user drop column Id;alter table tbl_user add Id int not null auto_increment primary key first;2.插入一条记录insert tbl_user set Name='小猴',Age=23;3.查询刚才的自增Id值select @@IDENTITY;值是1004,
验证下:select * from tbl_user;得到的当前表记录为
过刚插入的数据“小猴”id为1004,和select @@IDENTITY;结果一样。
4.使用mysql的 UUID()函数。前面的自增字段(auto_increment)只能生成”表内”的唯一值,且需要搭配使其为”唯一的主键或唯一索引”,它的值是逐步增长的。这里的UUID产生的是字符串类型值,固定长度为:36个字符。UUID生成的是在时间、空间上都独一无二的值,是“随机+规则”组合而成。
select uuid();select uuid();执行两次,结果:69ad8b74-6d47-11e3-ba6e-7446a08ee8ec69b03c16-6d47-11e3-ba6e-7446a08ee8ec
可以看到,多次调用UUID()函数得到的值不相同,它由五部分组成,并且有连字符(-)隔开,一共36个字符。其中:
前3组值是时间戳换算过来的,解决“时间上唯一”;
第4组值是暂时性保持时间戳的唯一性,重启mysql才会变动;
第5组是mac值转过来的,有助于解决“空间上的唯一”,同一个机器多实例的一般相同。如果mac值获取不到,则是一个随机值。
这些已经可以保证得到的值在时间和空间上的唯一。当然你也可以去掉连字符: select replace(uuid(),'-','')。
在MySQL 5.1.*及更高版本有一个变种的UUID()函数,UUID_SHORT(),可以生成一个17-64位无符号的整数,注意是生成的一个整数,而前面UUID()生成的是字符串。MySQL启动后第一次执行的值是通过时间戳等初始化这个值,在本次运行中再次调用的时候都加1。这个值一般比较大,可以调用right(UUID_SHORT(),9)取后面的若干位。或者,你还可以写成自定义函数,来按需生成这个值。举个例子:
#1.调用uuid_short()函数SELECT UUID_SHORT();SELECT UUID_SHORT();#执行两次得到的值递增的:2328563497408921623285634974089217#2.创建一个自定义函数,按需获取唯一值:CREATE DEFINER=`root`@`%` FUNCTION `GetUuidTest`(SysId int) RETURNS int(10)begin declare tmpID int; set tmpID = 0; #SELECT UUID_SHORT() into tmpID; #直接取值 SELECT concat(SysId,right(UUID_SHORT(),8)) into tmpID;#SysId和UUID_SHORT()后8位数拼接得到 return tmpID;end#3.调用自定义的函数GetUuidTest(int)函数:select GetUuidTest(1);select GetUuidTest(1);select GetUuidTest(2);select GetUuidTest(2);#得到结果:174089233 #1+uuid_short()后8位(74089233)组成174089234 #1+uuid_short()后8位(74089234)组成274089235 #2+uuid_short()后8位(74089235)组成274089236 #3+uuid_short()后8位(74089236)组成#uuid_short()值递增,前面在加一个Id,不同的服务器IdSysId不同。#4.在例子中调用自定义函数GetUuidTest(int) 来插入记录:这时候不需要把Id设置为自增了。insert tbl_user set Id=GetUuidTest(1),Name='小熊猫',Age=22;insert tbl_user set Id=GetUuidTest(2),Name='小鸭子',Age=21;
例子中,select * from tbl_user;得到的所有记录为
欢迎转载,方便的话,请注明出处,谢谢!
作者:子韦一
bitsCN.com
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps
