检查ID是否唯一 [英] checking if ID is unique

查看:85
本文介绍了检查ID是否唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C语言开发一个Customer Orders项目。我在编写一个函数时遇到问题,该函数检查输入的ID是否唯一。有什么建议吗?

I am developing a Customer Orders project using C language. I am having a problem in coding a function that checks if the entered ID is unique. Any suggestions pls?

推荐答案

什么样的id?在什么程度上是唯一的:在你的数据库中(你使用任何DBMS?),全世界?

顺便说一下,实体ID没有输入,它们是生成的。



因此,您不应该允许用户输入ID,您应该在数据存储区中创建实体之前生成(无论它是什么)。因此,您可以确定它们是唯一的,至少在您的应用程序中。
What kind of id? Unique in what extent: in your database (are you using any DBMS?), world-wide?
By the way, entity ID''s are not entered, they are generated.

So, you should not allow users to enter an ID, you should generate before creating the entity in the datastore (whatever it is). Thus you can be sure, that they are unique, at least in your application.


从您对Zoltan解决方案的回答中我假设您将客户数据存储在一个文件中。我还假设客户ID最初是由另一个系统创建的,甚至可能是手动或纸质的。



如果您没有以编程方式生成ID,检查其唯一性的唯一方法是将新ID与所有其他现有ID进行比较。有几种方法可以在合理的时间内完成。



(a)如果您使用的是简单的文本或二进制文件,则可以在程序启动时读取该文件并创建现有ID的有序列表或树。然后在修改文件时更新该列表或树。您的内存列表或树就像所有现有ID的缓存。该方法不仅会告诉您是否已存在五位ID,还允许您从给定ID中快速查找客户记录。



(b)如果您有一个索引文件系统或数据库系统,只需在ID字段上创建一个索引。简单的索引读取将告诉您给定的ID是否已存在。换句话说:让数据库或索引文件为你工作。



希望能给你一些想法。
From your answer to Zoltan''s solution I assume that you store customer data in a file. I also assume that customer IDs originally are created by another system, perhaps even a manual or paper-based one.

If you don''t generate the IDs programmatically, the only way of checking their uniqueness is comparing a new ID with all other existing IDs. There are several ways to do that in a reasonable time.

(a) If you are using a simple text or binary file, you could read the file at program start and create an ordered list or tree of the existing IDs. Then update that list or tree whenever you modify the file. Your in-memory list or tree acts like a cache of all existing IDs. That method will not only tell you whether a fiven ID already exists, but also allow you to quickly find a customer record from a given ID.

(b) If you have an indexed file system or a database system at hand, just create an index on the ID field. A simple indexed read will tell you whether a given ID already exists. In other words: Let the database or index file do the work for you.

Hope that gives you some ideas.


这篇关于检查ID是否唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆