->insert into t5(name) values('chuan'),('test'),('an'); ->select last_insert_id();还有个问题就是如果insert into是用类似上面的格式多条记录同时插入。没插入之前id为10 插入三条记录后id为13 可是这select last_insert_id();却只能得到11而不是13 因为假如你使用一条INSERT语句插入多个行, LAST_INSERT_ID() 只返回插入的第一行数据时产生的值。 所以个人觉得还是用select max(id) from table_name 比较保险,毕竟id是auto_increment的。
多行同时插入时,用last_insert_id()只能取到此次插入的第一条记录的auto_increment值最好从系统表里面取:select auto_increment from information_schema.tables where table_schema='db_csdn' and table_name='tb_test_auto'
select auto_increment from information_schema.tables where table_schema='库名' and table_name='表名'
select max(id) from table_name 只有在数据库是一个人操作的时候才正确,如果在你插入之后又有别人插入了 查询到的结果是错误的 还是用last_insert_id比如好 你在这种情况下完全可以不用多行插入
三种方法的比较 IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.@@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.The IDENT_CURRENT function returns NULL when the function is invoked on an empty table. SQL Server 2005提供的新方法 CREATE TABLE Employees( Id int IDENTITY(1,1) NOT NULL, -- Other columns , CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED (Id ASC) )DECLARE @InsertedRows AS TABLE (Id int) DECLARE @NewId AS INT INSERT INTO Employees ( /* column names */) OUTPUT Inserted.Id INTO @InsertedRows VALUES (/* column values */) SELECT @NewId = Id FROM @InsertedRowsThis code is not only concurrency-safe, but it allows us to get the values of other columns that could have been generated (e.g. by DEFAULTs or TRIGGERs)
mysql 已经做了处理,select last_insert_id();只返回你最后插入的id,别人在期间插入的记录不影响它的返回值。
SELECT ID = scope_identity();
output inserted.ID values (
有并发根本不可能知道谁是最后插入的吧!!?select @@identity 当表没有任何记录的时候不行吧 select case when max(id) is null then 0 else max(id) end as LastInsert from table;
对楼主这样的学习态度我不禁想破口大骂:"你个XXX!你不熟不会翻看帮助文档啊?" 文档里明确写明: 产生的ID 每次连接后保存在服务器中。这意味着函数向一个给定客户端返回的值是该客户端产生对影响AUTO_INCREMENT列的最新语句第一个 AUTO_INCREMENT值的。这个值不能被其它客户端影响,即使它们产生它们自己的 AUTO_INCREMENT值。这个行为保证了你能够找回自己的 ID 而不用担心其它客户端的活动,而且不需要加锁或处理。 人家这么基本的东西都想不到还怎么开发数据库? 另外: select last_insert_id() + row_count() - 1 from T limit 1; 不加limit 1会返回多行,楼上的一部分出错就因这个问题. 文档里没有明确说明row_count()保存在连接中,不过,"人家这么基本的东西都想不到还怎么开发数据库?", 万一出错我不负责后果.
当你表中记录为空的时候是获取不到的
确实可以。
不过如果你是在insert into时如果id是自己指定的话。这里select last_insert_id()就会得到0
也就是说id要自动插入的才可以得到last_insert_id()
->insert into t5(name) values('chuan'),('test'),('an');
->select last_insert_id();还有个问题就是如果insert into是用类似上面的格式多条记录同时插入。没插入之前id为10 插入三条记录后id为13
可是这select last_insert_id();却只能得到11而不是13
因为假如你使用一条INSERT语句插入多个行, LAST_INSERT_ID() 只返回插入的第一行数据时产生的值。
所以个人觉得还是用select max(id) from table_name 比较保险,毕竟id是auto_increment的。
只有在数据库是一个人操作的时候才正确,如果在你插入之后又有别人插入了
查询到的结果是错误的
还是用last_insert_id比如好
你在这种情况下完全可以不用多行插入
得到的就是最后插入的ID不过我感觉设计的时候最后不要设计成这个样子,
还是通过一个专门的表来管理这些字段比较好。比如加一个“番号表”
表中存储了当前最大的ID,
插入之前去这个表中取ID+1
来使用,并更新这个最大ID,这样更方便一些。
SELECT ID = scope_identity();
获得当前会话的自动增长的ID,不会有并发问题
RETURN @@Identity
MYSQL是在5.0以后的版本中引入触发器的。CREATE TRIGGER <触发器名称> <--
{ BEFORE | AFTER }
{ INSERT | UPDATE | DELETE }
ON <表名称>
FOR EACH ROW
<触发器SQL语句> 触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中其他对象的命名方式基本相象.触发时间:
Before表示在事件发生之前执行触发器,After表示在事件发生之后执行触发器;触发事件:
三个事件:insert, update, delete触发器与表的关系:
触发器是属于一个表的,当在这个表上执行insert, update, delete操作时,就会导致相应的触发器被激活;
不能给同一个表的同一个操作创建两个不同的触发器。触发间隔:
FOR EACH ROW 子句通知触发器每隔一行执行一次动作,而不是对整下表执行一次。
把这个表增加一个字段,时间字段,这个字段插入 当前时间的字符串,这个是唯一的 既
Long.toString(new Date().getTime());然后你在根绝这个字符串查找对应的id
如果你要考虑到并发,应该这样提问:
"怎么知道当前最后一个insert进去的记录的ID"
谁都知道MAX(ID)完了,这么简单谁还问!@@!
Query OK, 0 rows affected (0.13 sec)mysql> insert into t_csdn1111(v) values ('A1');
Query OK, 1 row affected (0.06 sec)mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> select * from t_csdn1111 where id is null;
+----+------+
| id | v |
+----+------+
| 1 | A1 |
+----+------+
1 row in set (0.00 sec)mysql> select auto_increment from information_schema.tables where table_schema='
csdn' and table_name='t_csdn1111';
+----------------+
| auto_increment |
+----------------+
| 2 |
+----------------+
1 row in set (0.08 sec)mysql>
mysql> insert into t_csdn1111(v) values ('A21'),('A22'),('A23');
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)mysql> select * from t_csdn1111 where id is null;
+----+------+
| id | v |
+----+------+
| 2 | A21 |
+----+------+
1 row in set (0.00 sec)mysql> select auto_increment from information_schema.tables where table_schema='
csdn' and table_name='t_csdn1111';
+----------------+
| auto_increment |
+----------------+
| 5 |
+----------------+
1 row in set (0.00 sec)mysql>
感觉SCOPE_IDENTITY()比较像些。
IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值。
@@IDENTITY 返回为当前会话的所有作用域中的任何表最后生成的标识值。
SCOPE_IDENTITY 返回为当前会话和当前作用域中的任何表最后生成的标识值。
{
string strsql = "insert into KSXT_subject(Sub_Name,Sub_Note,insertname) values ('" + Sub_Name + "','" + Sub_Note + "','" + insertname + "') select @@IDENTITY as insertid ";
DBAccess mydb = new DBAccess();
return(Convert.ToInt32( mydb.ExecuteScalar(strsql))); }调用如下:
Subject mysub = new Subject();
int a= mysub.InsertSubjectreturn ("计算机文化基础", "2008级通修", "郝永生");
SELECT MAX(ID) FROM T1 WHERE V=?;
这些值都是基于本连接的,本连接最近的一次AUTO_INCREMENT值如果你要查最新的值,假如有并发操作的话,那就SELECT MAX(ID) FROM XXXX, 如果要严格的话前面需加个锁
//返回主键自动
PreparedStatement pre=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pre.executeUpdate();
ResultSet rs = pre.getGeneratedKeys();
int m=0;
if(rs.next()){
m=rs.getInt(1);
}
select last_insert_id();
//——————–获取刚刚插入的ID—-
long user_id=0;
res=pre1.getGeneratedKeys();
if(res.next()){
user_id=res.getLong(1);
}
select scope_identity()
IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.@@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.The IDENT_CURRENT function returns NULL when the function is invoked on an empty table.
SQL Server 2005提供的新方法
CREATE TABLE Employees(
Id int IDENTITY(1,1) NOT NULL, -- Other columns
, CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED (Id ASC) )DECLARE @InsertedRows AS TABLE (Id int)
DECLARE @NewId AS INT INSERT INTO Employees
( /* column names */)
OUTPUT Inserted.Id INTO @InsertedRows
VALUES (/* column values */)
SELECT @NewId = Id FROM @InsertedRowsThis code is not only concurrency-safe, but it allows us to get the values of other columns that could have been generated (e.g. by DEFAULTs or TRIGGERs)
mysql 已经做了处理,select last_insert_id();只返回你最后插入的id,别人在期间插入的记录不影响它的返回值。
select case when max(id) is null then 0 else max(id) end as LastInsert from table;
文档里明确写明:
产生的ID 每次连接后保存在服务器中。这意味着函数向一个给定客户端返回的值是该客户端产生对影响AUTO_INCREMENT列的最新语句第一个 AUTO_INCREMENT值的。这个值不能被其它客户端影响,即使它们产生它们自己的 AUTO_INCREMENT值。这个行为保证了你能够找回自己的 ID 而不用担心其它客户端的活动,而且不需要加锁或处理。
人家这么基本的东西都想不到还怎么开发数据库?
另外:
select last_insert_id() + row_count() - 1 from T limit 1;
不加limit 1会返回多行,楼上的一部分出错就因这个问题.
文档里没有明确说明row_count()保存在连接中,不过,"人家这么基本的东西都想不到还怎么开发数据库?",
万一出错我不负责后果.