English 中文(简体)
GUID 和自动检索作为LQ数据库的主要关键
原标题:GUID. and automatic id as primary key in SQL databases
SELECT COUNT(*) FROM table_name;

我的算法是:

  • check count
  • count+1 is the new primary key starting point
  • Then keep on incrementing before every insert operation

但是,该准则是什么? 页: 1 服务器提供某种自动产生和加固的主要钥匙?

最佳回答

我不敢肯定,你是否也询问过身份识别特征,而是一个独一无二的识别特征,即(几乎)保证是独一无二的。 它可以在主要钥匙上使用,但建议采用斜线,除非你重新开展脱线工作或规划合并数据库。

例如,“热”,主要关键是:

1   Jason
2   Jake
3   Mike

当与另一个数据库合并时,该数据库即为:

1   Lisa
2   John
3   Sam

.。 你们必须重新打下几栏,确保你的FKs符合规定等等。 利用国际统一数据系统,数据似乎如此,很容易合并:

1FB74D3F-2C84-43A6-9FB6-0EFC7092F4CE    Jason
845D5184-6383-473F-A5D6-4DE98DBFBC39    Jake
8F515331-4457-49D0-A9F5-5814EE7F50BA    Mike    
CE789C89-E01F-4BCE-AC05-CBDF10419E78    Lisa
4D51B568-107C-4B63-9F7F-24592704118F    John
7FA4ED64-7356-4013-A78A-C8CCAB329954    Sam

请注意,国际统一私法协会占用的空间远远大于国际自由工会联合会,因此建议使用国际通信公司作为主要关键,除非你绝对需要。

问题回答

有3项选择

CREATE TABLE A
(
ID INT IDENTITY(1,1) PRIMARY KEY,
... Other Columns
)

CREATE TABLE B
(
ID UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
... Other Columns
)

CREATE TABLE C
(
ID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
... Other Columns
)

如果你使用“ID,那么你更喜欢C而不是B。

create table your table (id int indentity(1,1) primary key, col1 varchar(10) )

will automatically create the primary key for you. Check GUID in the T-SQL, don t have it at hand right now.

The issue with using count , then count +1 as key, is that were you to delete a record from the middle, you would end up with a duplicate key generated. EG:

Key   Data    
1     A          
2     B
3     C
4     D

删除B(现改为3),并插入E。 这试图使已经存在的第4条成为新的关键。

Key   Data    
1        A          
3        C
4        D   <--After delete count = 3 here
4        E   <--Attempted insert with key 4    

你可以利用主要的关键和自动加固,确保你不会有这个问题。

CREATE TABLE myTable
(
  P_Id int NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (P_Id)
)

或者,你可以使用GUID。 GUID是如何通过创建128个借方(代表32个char轴)来开展工作的。

Key                                      Data
24EC84E0-36AA-B489-0C7B-074837BCEA5D     A
.
.

这导致2^128的可能价值(相对较大),因此,一台计算机产生的类似价值的可能性很小。 除此以外,还有一些算法来帮助尝试并确保这样做。 因此,对于钥匙也是相当好的选择。

至于你是否使用ger或GU,通常取决于申请、政策等。





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签