简体   繁体   English

SQL主键生成

[英]SQL Primary Key Generation

Used SQL Server = MySQL 使用的SQL Server = MySQL
Programming language = irrelevant, but I stick to Java and C# 编程语言=不相关,但我坚持使用Java和C#

I have a theoretical question regarding the best way to go about primary key generation for SQL databases which are then used by another program that I write, (let's assume it is not web-based.) 我有一个理论上的问题,关于为SQL数据库生成主键的最佳方法,然后再由我编写的另一个程序使用它(假设它不是基于Web的。)

I know that a primary key must be unique, and I prefer primary keys where I can also immediately tell where they are coming from when I see them, either in my eclipse or windows console when I use a database, as well as in relationship tables. 我知道主键必须是唯一的,并且我更喜欢主键,当我使用数据库时以及在关系表中时,无论是在Eclipse还是Windows控制台中,我都可以在看到主键时立即知道它们的来源。 。 For that reason, I generally create my own primary key as an alphanumeric string unless a specific unique value is available such as an ISBN or SS num. 因此,除非可以提供特定的唯一值(例如ISBN或SS num),否则我通常将自己的主键创建为字母数字字符串。 For a table Actors, a primary key could then look like a1, and in a table Movies m1020 (Assuming titles are not unique such as different versions of the movie 'Return to witch Mountain'). 对于Table Actors,主键可能看起来像a1,并且在Movies m1020表中(假定标题不是唯一的,例如电影“ Return to witch Mountain”的不同版本)。

So my question then is, how is a primary key best generated (in my program or in the db itself as a procedure)? 所以我的问题是,如何最好地生成主键(在程序中还是在程序本身中的db中)? For such a scheme, is it best to use two columns, one with a constant string such as 'a' for actors and a single running count? 对于这样的方案,是否最好使用两列,其中一列具有常量字符串(例如“ a”)用于参与者,并且具有单个运行计数? (In that case i need to research how to reference a table whose PK spans multiple columns) What is the most professional way of handling such a task? (在那种情况下,我需要研究如何引用一个PK跨越多列的表)处理这种任务的最专业方法是什么?

Thank you for your time. 感谢您的时间。

A best practice is to let your DB engine generate the primary key as an auto-increment NUMBER. 最佳实践是让您的数据库引擎生成主键作为auto-increment NUMBER。 Alphanumeric string are not a good way, even if it seems too "abstract" for you. 即使对于您来说似乎太“抽象”,字母数字字符串也不是一个好方法。 Then, you don't have to worry about your primary key in your program (Java, C#, anything else) ; 然后,您不必担心程序中的主键(Java,C#或其他任何东西); at each line inserted in your Database, an unique primary key is automatially inserted 在数据库中插入的每一行,都会自动插入一个唯一的主键

By the way, with your solution, I'm not sure you manage the case where two rows are inserted simultaneously... Are you sure in absolutely no case, your primary key can be duplicated ? 顺便说一句,对于您的解决方案,我不确定您是否可以管理同时插入两行的情况...您确定绝对不会在任何情况下都可以复制主键吗?

Your first line says:- 您的第一行说:-

SQL Server = MySQL SQL Server = MySQL

Thats not true. 这不是真的。 They are different. 他们是不同的。

how is a primary key best generated (in my program or in the db itself as a procedure)? 如何最好地生成主键(在我的程序中或在数据库本身中作为一个过程)?

Primary keys are generated by MYSQL when you specify the column with primary key constraint on it. 当您指定带有主键约束的列时,MYSQL将生成主键。 The primary keys are automatically generated and they are automatically incremented. 主键是自动生成的,并且会自动递增。

If you want your primary key as alphanumeric(which I personally will not recommend) then you may try like this:- 如果您希望主键为字母数字(我个人不建议这样做),则可以这样尝试:-

CREATE TABLE A(
id INT NOT NULL AUTO_INCREMENT,
prefix CHAR(30) NOT NULL,
PRIMARY KEY (id, prefix),

I would recommend you to have Primary key as Integer as that would help you to make your selction a bit easier and optimal.For MyIsam tables you can create a multi-column index and put auto_increment field on secondary column 我建议您将主键设置为Integer,因为这将使您的选择更容易和更优化。对于MyIsam表,您可以创建多列索引并将auto_increment字段放在辅助列上

For MySQL there's a best way - set AUTO_INCREMENT property for your primary key table field. 对于MySQL,有一种最佳方法-为主键表字段设置AUTO_INCREMENT属性。 You can get the generated id later with last_insert_id function or it's java or c# analog. 您可以稍后使用last_insert_id函数获取生成的ID,或者它是Java或C#模拟。

I don't know why you would use "alphanumeric" values - why not just a plain number? 我不知道为什么要使用“字母数字”值-为什么不只是一个普通数字?

Anyway, use whatever auto-increment functionality is available in whichever DB-system you are using, and stick with that. 无论如何,无论使用哪种数据库系统,都可以使用任何可用的自动增量功能,并坚持使用。 Do not create primary keys outside of the DB - you can't know when / how two systems might access the DB at the same time, which could cause problems if the two create the same PK value, and attempt to insert it. 不要创建数据库之外的主键-你可以不知道什么时候/如何两个系统可能会在同一时间,这可能会造成问题,如果两个创建相同的PK值,并尝试将其插入访问数据库。

Also, in my view, a PK should just be an ID (in a single column) for a specific row, and nothing more - if you need a field indicating that a record concerns data of type "actor" for instance, then that should be a separate field, and have nothing to do with the primary key (why would it?) 另外,在我看来,PK应该只是特定行的ID(在单列中),仅此而已-如果您需要一个字段来指示记录涉及例如“ actor”类型的数据,则应该是一个单独的字段,并且与主键无关(为什么?)

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

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