简体   繁体   English

sql表创建中始终需要主键吗?

[英]Is there always a need for a Primary Key in sql table creations?

I don't know if I created these tables in sql correctly. 我不知道我是否在sql中正确创建了这些表。 Can someone confirm I am doing this correctly or incorrectly? 有人可以确认我正确或不正确地这样做吗? Can a table just have a foreign key or does it have to have a primary key? 一个表可以仅具有外键还是必须具有主键? Thanks in advance. 提前致谢。

CREATE TABLE Job(
JobId integer,
ToolName varchar(40),
status varchar(100),
start_time time,
finish_time time
PRIMARY KEY(JobId));

CREATE TABLE ErrorLog(
JobId integer,
ErrCode integer,
Description varchar(200),
PRIMARY KEY(ErrCode)
FOREIGN KEY(JobId) REFERENCES Job(JobId));

CREATE TABLE BLAST(
input varchar(100),
database varchar(100),
evalue varchar(100),
JobId integer, 
FOREIGN KEY (JobId) REFERENCES Job(JobId));

CREATE TABLE MitoLoc(
input varchar(100),
specificity varchar(100),
JobId integer,
FOREIGN KEY (JobId) REFERENCES Job(JobId));

It is best practice to create a primary key column on a table in a SQL RDBMS. 最佳实践是在SQL RDBMS的表上创建主键列。 Although SQL does not require tables to have primary keys. 尽管SQL不需要表具有主键。

Primary keys allow for a unique identifier to be set for each row in the table. 主键允许为表中的每一行设置唯一标识符。 This is the only way you can uniquely identify a specific row in the table, and the only way you'll be able to utilize a foreign key relationship. 这是唯一标识表中特定行的唯一方法,也是利用外键关系的唯一方法。

Database servers, like SQL Server, also optimize both the storage and querying of tables better when they have primary keys defined. 像SQL Server这样的数据库服务器在定义了主键时,也可以更好地优化表的存储和查询。

Primary keys are a very good idea, but not required. 主键是一个很好的主意,但不是必需的。 Almost every table that I create has an automatically incremented primary key. 我创建的几乎每个表都有一个自动递增的主键。 This is in addition to several other columns that I keep around, such as CreatedAt and CreatedBy . 这是我保留的其他几列的补充,例如CreatedAtCreatedBy

Foreign key relationships are typically to primary keys but can also be to unique keys. 外键关系通常与主键有关,但也可以与唯一键有关。

Why do you want a primary key? 为什么要主键?

  • So you can delete a row that is a duplicate, easily. 因此,您可以轻松地删除重复的行。
  • So you can readily update a single row. 因此,您可以随时更新单行。
  • An auto incremented primary key gives you an idea of the order that rows were inserted into the table. 自动递增的主键使您可以了解将行插入表的顺序。
  • A single column primary key is much easier to handle with foreign key references. 单列主键更易于使用外键引用进行处理。

There are, undoubtedly, other reasons. 毫无疑问,还有其他原因。 But those are a few. 但是只有少数几个。

As for your tables, I think Mitoch and Blast should have id columns that are primary keys. 至于您的表,我认为MitochBlast应该具有作为主键的id列。 You can also declare other columns (or combinations) to be unique, if appropriate. 如果合适,还可以将其他列(或组合)声明为唯一。

Not needed. 不需要。 But It is not good for table structure. 但这对表结构不利。 you can create tables without primary key. 您可以创建没有主键的表。 and it can be have foreign key. 它可以是外键。 But some DBMS don't allow to save table without primary key if you are not using primary key then 但是,如果您不使用主键,则某些DBMS不允许在没有主键的情况下保存表

  1. It is difficult to identify each record 很难识别每条记录
  2. Its record can't references to other tables 它的记录不能引用其他表

when creating table structure some time you have to create composite primary key that mean two or more columns together (combination ) can be primary key 在创建表结构时,有时必须创建复合主键 ,这意味着两个或更多列在一起(组合)可以是主键

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

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