[英]How can i ensure a user does not attempt to enter an already existing primary key ID?
I have an application that adds pets to an SQL database. 我有一个将宠物添加到SQL数据库的应用程序。
Currently the user must know what PetIDs are existing and so must know what is available to be added. 当前,用户必须知道存在哪些PetID,因此必须知道可以添加哪些PetID。 If the user tries to enter an already existing ID the program gives an error.
如果用户试图输入一个已经存在的ID,程序将给出一个错误。
Im thinking i need the PetID (top text box) value to be automatically decided upon page load, with a value which wont clash with an already existing value... 我想我需要在页面加载时自动确定PetID(顶部文本框)值,该值不会与已经存在的值冲突...
Can someone help? 有人可以帮忙吗? i have no idea how to do this
我不知道该怎么做
I need the page_load to automatically search the SQL database for PetIDs that are available, pick the one with the lowest value and have it in the text box automatically, ready for the user (so the user wont have to worry about picking one which isn't already taken)... 我需要page_load自动在SQL数据库中搜索可用的PetID,选择值最低的一个,并自动将其保存在文本框中,为用户准备好(这样,用户就不必担心选择一个不可用的PetID)。尚未被接受)...
How can i have an ID which is available, waiting in the top text box, upon page load? 页面加载后,如何在顶部文本框中等待可用的ID? also make it so the user cannot attempt to change it.
还可以做到这一点,以便用户无法尝试更改它。
You can create the PetId as an Identity Column as below: 您可以如下创建PetId作为标识列:
[PetId] [int] IDENTITY(10,1) NOT NULL
Thus you will not have to add it manually, each time you add a record of Pet, the PetId will be generated automatically and that will be unique. 因此,您不必手动添加它,每次添加Pet记录时,PetId都会自动生成,并且是唯一的。 So, no clashing of PetId will occur.
因此,不会发生PetId冲突。
Here 10 represents the first Id number and 1 represents how your PetIds will be incremented. 这里10代表第一个ID号,而1代表您的PetId如何增加。
Regards, Pratik 问候,Pratik
It is usual to enter your data and return the ID after the record is added to the database (using the Identity Insert on the Id
column). 将记录添加到数据库后,通常要输入数据并返回ID(使用
Id
列上的Identity Insert)。
If you want the next number to be displayed BEFORE the data is created then use an integer column for ID
without the Identity Insert option and create a SQL Server sequence. 如果要在创建数据之前显示下一个数字,请对不带“身份插入”选项的
ID
使用整数列,并创建一个SQL Server序列。 Create a stored procedure to return the next sequence number to show in your creation page 创建一个存储过程以返回下一个序列号以显示在创建页面中
One option would be to use a sequence: 一种选择是使用序列:
CREATE SEQUENCE pet_id_seq START WITH 7
I'm starting it with 7 because it is the first available pet_id
我从7开始,因为它是第一个可用的
pet_id
Then, on page load, get the next value for the sequence by executing the following query: 然后,在页面加载时,通过执行以下查询来获取序列的下一个值:
SELECT (NEXT VALUE FOR pet_id_seq) AS next_pet_id
And show the value returned by the query on the pet_id text box. 并在pet_id文本框中显示查询返回的值。
This approach has one disadvantage, it produce gaps, if you open the page, but you don't actually insert the pet row, the id is lost forever. 这种方法有一个缺点,如果您打开页面,但实际上没有插入pet行,则会产生间隙,id会永远丢失。 But I think you should don't care, there are many numbers, you are not going to exhaust them.
但是我认为您不应该在乎,有很多数字,您不会穷尽它们。
Another option is to use the following query to fill the pet_id text box: 另一种选择是使用以下查询来填充pet_id文本框:
SELECT MAX(pet_id) + 1 AS next_pet_id FROM pets
But this one has another disadvantage. 但这又有一个缺点。 It doesn't work well on concurrent scenarios.
在并发方案中无法很好地工作。 If two users open the create pet page at the same time they get the same next_pet_id and one of them will get a primary key violation error.
如果两个用户同时打开创建宠物页面,他们将获得相同的next_pet_id,其中一个将收到主键冲突错误。
Set the id column to auto increment and don't let the user insert it, let the database decide it for itself. 将id列设置为自动递增,不要让用户插入它,而由数据库自行决定。 Or you can change the id column type and use
Guid.NewGuid()
to generate a new id. 或者,您可以更改id列类型,并使用
Guid.NewGuid()
生成新的id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.