简体   繁体   English

数据库中的主键如何工作?

[英]How does the primary key in a database works?

I'm using a datagridview to display the records of a database. 我正在使用datagridview显示数据库的记录。 I also have a form that collects new data to be added to the database too. 我也有一个表格,该表格也收集要添加到数据库的新数据。 What I want to know is if the primary key is generated automatically or not once the data is inserted in the database using dataAdapter-DataSets. 我想知道的是,一旦使用dataAdapter-DataSets将数据插入数据库后,主键是否自动生成。

I have a method in a class that handles all the tables for the different datagridviews. 我在一个类中有一个方法可以处理不同datagridviews的所有表。 The client method looks like this. 客户端方法如下所示。

public void AddNewClient (Client client)
        {
            try
            {
                DataRow dr = dataSet.Tables["Clients"].NewRow ();
                dr[1] = client.firstName;
                dr[2] = client.lastName;
                dr[3] = client.MI;
                dr[4] = client.address;
                dr[5] = client.city;
                dr[6] = client.state;
                dr[7] = client.zipcode;
                dr[8] = client.homePhone;
                dr[9] = client.cellPhone;
                dr[10] = client.otherPhone;
                dr[11] = client.faxNumber;
                dr[12] = client.email;
                dataSet.Tables["Clients"].Rows.Add (dr);

                adapterClients.Update (dataSet, "Clients");
            }
            catch (Exception e) { throw e; }
        } 

The dataSet.Tables["Clients"] is bind to the DataGridViewClients. dataSet.Tables [“ Clients”]绑定到DataGridViewClients。 When the new record displays in the DataGridViewClients there's no ClientID value. 当新记录显示在DataGridViewClients中时,没有ClientID值。 However, when I restart the application and the DataGridViewClients is displayed I can see how a new value to ClientID has been assigned. 但是,当我重新启动应用程序并显示DataGridViewClients时,我可以看到如何为ClientID分配新值。 So I wonder is this something that is done automatically by SQL server or ....? 所以我想知道这是由SQL Server或....自动完成的吗? I mean I don't really know. 我的意思是我真的不知道。 :) :)

The person who created the database decided at that time how the PRIMARY KEY works. 创建数据库的人当时决定了PRIMARY KEY的工作方式。

One possible (and common) way to declare a PRIMARY KEY is to make it an INTEGER column for which the database engine itself will create values when new rows are INSERTed. 声明PRIMARY KEY的一种可能(通用)方法是将其设置为INTEGER列,当插入新行时,数据库引擎本身将为其创建值。 In SQL Server this kind of column is called an IDENTITY (other products might call it an AUTOINC, AUTONUMBER, or GENERATOR column). 在SQL Server中,这种列称为IDENTITY(其他产品可能将其称为AUTOINC,AUTONUMBER或GENERATOR列)。 This is probably what's happening in your database. 这可能就是您的数据库中正在发生的事情。

Another technique is to use an INTEGER field in which the application programmer is responsible for creating a unique value. 另一种技术是使用INTEGER字段,其中应用程序程序员负责创建唯一值。 This is less practical because you have to deal with collisions between multiple simultaneous users. 这不太实用,因为您必须处理多个同时用户之间的冲突。

Finally, some designers try to identify one or more columns of meaningful data that are guaranteed to be unique for each record in the system and declare that combination of columns to be the PRIMARY KEY. 最后,一些设计人员试图确定有意义的数据的一列或多列,这些数据对于系统中的每个记录都保证是唯一的,并将这些列的组合声明为PRIMARY KEY。 In this case there's no separate numbering system for the records; 在这种情况下,记录没有单独的编号系统。 the PRIMARY KEY is made up of "real" data. 主键由“真实”数据组成。

The ClientID is probably an identity column . ClientID可能是一个标识列 That means the database automatically assigns a new number to each row. 这意味着数据库会自动为每行分配一个新数字。 Even if you wanted to, you would not be allowed to set the value of the identity column yourself. 即使您愿意,也不允许您自己设置标识列的值。

An identity column is often a primary key, but this is not always true. 身份列通常是主键,但这并不总是正确的。 A primary key can be made up of any set of columns that uniquely identifies a row. 主键可由唯一标识行的任何一组列组成。 It's easy to see that an "auto-number" or identity column meets this requirement. 很容易看到“自动编号”或标识列满足此要求。

You can list all identity columns in your database with the following query: 您可以使用以下查询列出数据库中的所有标识列:

select  t.name
,       c.name
from    sys.tables t
join    sys.columns c
on      c.object_id = t.object_id
where   c.is_identity = 1

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

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