简体   繁体   中英

How to let the user to enter primary keys manually

I have a requirement for my ASP.NET website which requires me to let the user to enter primary keys manually for a particular table, and if the user entered a primary key which is already exist in the database, the system should notify the user as "Primary already exist" or some thing like that.

I am thinking to let the user enter the primary key, and when he enter a key which is already exist, the system going to throw an exception for primary key constraint violation. So I'm going to catch that exception and in the catch block I am going to display an error message to notify the user about the duplication of the primary key. However, I am not sure this is a right way to do it or is there any standard way to do this?

Yes, this is a valid approach. There are a few things that should be mentioned, though:

  • Using a surrogate key (a counter, a GUID, ...) as the primary key instead of a user-entered value (natural key) has a lot of advantages, see the linked article for details. If you have already thought about that and decided to use a natural key instead, that's fine, I just thought it should be mentioned. If you use a surrogate key, uniqueness of the user-defined value can be ensured through a unique index.

  • Throwing an exception as part of the normal control flow is usually frowned upon: Doing a SELECT for the existing key and then the INSERT would be more elegant than waiting for the exception to occur. (Use a transaction and an appropriate transaction isolation level to ensure that no row can be inserted by another client in between your SELECT and your INSERT).

Before trying to insert the new record in the database, simply do a query against the target table to see if the primary key already exists. If it does, return a message to the user.

That's pretty ugly. It's not a good idea to use exceptions for "normal" logic flow - it makes the code harder to read and maintain, and tends to be slower (though you probably won't notice the performance impact).

I'd split it up into a "validate" method, which should validate the data the user entered - is it the right data type? Does it meet minimum length requirements? Is it unique?

If the "validate" method returns a violation, show it to the user; give them the chance to fix it.

If the "validate" method does not return a violation, try to insert the record, and catch the "duplicate key" exception (someone else may have entered that very record in the time between your validation and entering the record).

I'm not gonna discuss if you should or shouldn't do it (I don't think you should by the way :p ) but this is the code:

SET IDENTITY_INSERT TABLE ON

remember that you can have only one table per database set to ON

The primary key in MS Sql Server is an unique index. In case when you violate the index constraint, database will raise an error and rollback the transaction.

If you will be able to maintain the indexes name properly, you will be able to use proposed by you solution for other cases to.

IMHO, that approach is fine. Often alternative is to ask about the existence of values in database. That eventually also end up with an Exception if was found. If you will decide to let the db to raise the exception do not forget to mention that in documentation of code. Because the usage of it without such information could cause in future some problems.

Why bother the user with making up a primary key and guessing what to enter and whether it's already taken? I mean, I know int is a wide range of numbers, but users will probably be trying 3-4 digit numbers; eventually, they'll start hitting dups. So, why?? You're complicating unnecessarily.

Have the db generate the id. Report it to the user, if necessary. Can't be simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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