简体   繁体   English

如何从mdb的表中检索最后一个主要ID?

[英]How to retrieve last primary Id from mdb's table?

I got table with next columns: Id, Name, Age, Class 我得到带有下一列的表格: Id, Name, Age, Class

I am trying to insert new row in db like this: 我试图像这样在数据库中插入新行:

INSERT INTO MyTable (Name, Age, Class)
VALUES (@name, @age, @class)

And get an exeption: 并得到一个例外:

"Index or primary key cannot contain a Null value."

The question is how to add a new row without knowing next primary Id, or maybe there is a way to get this Id from the table with the help of another query ? 问题是如何在不知道下一个主要ID的情况下添加新行,或者也许有一种方法可以借助另一个查询从表中获取此ID?

您可以将“ ID”列的数据类型更改为“自动编号”,而不是“整数”。

不确定Access,但MySQL语法为:

INSERT INTO MyTable (Id, Name, Age, Class) VALUES (NULL, @name, @age, @class);

try this 尝试这个

INSERT INTO MyTable (id,Name, Age, Class)
 VALUES ((select max(id)+1 from MyTable),@name, @age, @class)

Please make the "id" column as Auto number in the Mytable and then try 请在Mytable中将“ id”列设为自动编号,然后尝试

OR else 要不然

INSERT INTO MyTable (id,Name, Age, Class)
select max(id)+1,@name, @age, @class FROM MyTable 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM