简体   繁体   English

在SQL表中选择主键

[英]Choice of a primary key in SQL table

I want to make an SQL table to keep track of notes that are added/edited/deleted. 我想制作一个SQL表来跟踪添加/编辑/删除的注释。 I want to be able to display the state of each NOTEID at this moment in a table, display log of changes of selected note and be able to delete all notes marked with a given NOTEID . 我现在希望能够在表格中显示每个NOTEID的状态,显示所选笔记的更改日志,并能够删除所有标记有给定NOTEID

create table[dbo].[NOTES]{
  NOTEID [varchar](128) NOT NULL,
  CREATEDBY [varchar](128) NOT NULL, /*is this redundant?*/
  TIMECREATED  DATE NOT NULL,          /*is this redundant?*/
  MODIFIEDBY [varchar](128) NOT NULL,
  TIMEMODIFIED  DATE NOT NULL,
  NOTE [VARCHAR}(2000) NULL,
  PRIMARY KEY ( /* undecided */ ),
}; 

What is the natural way of making this table? 制作这张桌子的自然方法是什么? Should I autogenerate the primary ID or should I use ( NOTEID , TIMEMODIFIED ) as the primary key? 我应该自动生成主ID还是应该使用( NOTEIDTIMEMODIFIED )作为主键? What kind of fool proof protection should be added? 应该添加哪种防呆保护?


I would like to be able to display all notes in a "Note history" window. 我希望能够在“便笺历史”窗口中显示所有便笺。 So, I should store note from 3 days ago, when it was created, note from 2 days ago and from today, when it was modified. 因此,我应该存储3天前创建的便笺,2天前以及今天修改后的便笺。

However, the "Notes" table will show the final state for each NOTEID. 但是,“注释”表将显示每个NOTEID的最终状态。 That is 那是

SELECT NOTE from NOTES where NOTEID = 'selected_note_id' and date = latest

The best way is create two tables. 最好的方法是创建两个表。

 NOTES (
     NOTE_ID        -- primary key and autogenerated / autonumeric
     CREATEDBY      -- only appear once
     TIMECREATED    -- only appear once
     NOTE 
 )

 NOTES_UPDATE (
     NOTES_UPDATE_ID   -- primary key and autogenerated / autonumeric
     NOTE_ID           -- Foreign Key to NOTES
     MODIFIEDBY 
     TIMEMODIFIED  
     NOTE 
 )     

You can get your notes updates 您可以更新笔记

 SELECT N.*, NU.*
 FROM NOTES N
 JOIN NOTES_UPDATE NU
   ON N.NOTE_ID = NU.NOTE_ID

and to get the last update just add 并获取最新更新,只需添加

 ORDER BY NOTE_UPDATE_ID DESC
 LIMIT 1   -- THIS is postgres sintaxis.

I think your current table design is fine, though you might want to make the NOTEID the primary key and auto increment it. 我认为您当前的表设计很好,尽管您可能希望将NOTEID作为主键并自动对其进行递增。 I don't see the point of making (NOTEID, TIMEMODIFIED) a composite primary key because a given note ID should ideally only appear once in the table. 我看不到要制作(NOTEID, TIMEMODIFIED)复合主键的意义,因为理想情况下,给定的音符ID在表中应该只出现一次。 If the modified time changes, the ID should remain the same. 如果修改的时间更改,则ID应该保持不变。

Assuming we treat notes as files on a computer, then there should be only one table (file system) which stores them. 假设我们将便笺视为计算机上的文件,则应该只有一个表(文件系统)来存储便笺。 If a given note gets modified, then the timestamp changes to reflect this. 如果给定的注释被修改,则时间戳更改以反映此情况。

SIMPLE ANSWER: 简单的答案:

The PRIMARY KEY should be the value that unique identifies each row in your table. PRIMARY KEY应该是唯一标识表中每一行的值。 In your particular case, NOTEID should be your id. 在您的特定情况下, NOTEID应该是您的ID。

ELABORATING: 阐述:

It is important to remember that a PRIMARY KEY creates an index by default, which means that whenever you do a query similar to: 重要的是要记住,默认情况下, PRIMARY KEY创建一个索引,这意味着每当您执行类似以下查询时:

SELECT * FROM table WHERE NOTEID = something

The query will execute a lot faster than without an index (which is mostly relevant for bigger tables). 查询将比没有索引时执行得快得多(这与较大的表最相关)。 The PRIMARY KEY is also forced to be unique, hence no two rows can have the same PRIMARY KEY PRIMARY KEY也被强制为唯一,因此任何两行都不能具有相同的PRIMARY KEY

A general rule is that you should have an INDEX for any value that will often be used within the WHERE ... part of the statement. 一般规则是,对于该语句的WHERE ...部分中经常使用的任何值,都应该有一个INDEX If NOTEID is not the only value you will be using in the WHERE .... part of the query, consider creating more indexes 如果NOTEID不是您将在查询的WHERE ....部分中使用的唯一值,请考虑创建更多索引

HOWEVER! 然而! TREAD WITH CAUTION. 谨慎行事。 Indexes help speed up searches with SELECT however they make UPDATE and INSERT work slower. 索引有助于通过SELECT加快搜索速度,但是它们会使UPDATEINSERT工作变慢。

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

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