我正在创建一个包含多个 sql 文件的数据库1 个文件创建表.1 文件添加约束.1 个文件删除约束.
I'm creating a database with several sql files 1 file creates the tables. 1 file adds constraints. 1 file drops constraints.
主键是一个约束,但有人告诉我在表定义中定义主键,但没有给出原因.
The primary is a constraint however I've been told by someone to define your primary key in the table definition but not given a reason why.
将主键定义为可以添加和删除的约束更好还是在表定义中更好.
Is it better to define the primary key as a constraint that can be added and dropped or is it better to do it in the table definition.
我目前的想法是在表定义中执行此操作,因为将其作为可移除约束执行可能会导致重复键出现一些可怕的问题.但是删除约束无论如何都可能导致严重的问题,因此预计如果有人确实删除了主键,他们会采取适当的措施来避免问题,因为他们应该为任何其他数据输入采取措施
My current thinking is to do it in the table definition because doing it as a removable constraint could potentially lead to some horrible issues with duplicate keys. But dropping constraints could lead to serious issues anyway so it is expected that if someone did drop the primary key, they would have taken appropriate steps to avoid problems as they should have for any other data entry
推荐答案主键是约束,但约束不一定是主键.除非进行一些重大的数据库手术,否则永远不需要删除主键.
A primary key is a constraint, but a constraint is not necessarily a primary key. Short of doing some major database surgery, there should never be a need to drop a primary key, ever.
将主键与表一起定义是一种很好的做法 - 如果将表和键定义分开,则会打开导致键定义丢失或遗忘的窗口.鉴于任何体面的数据库设计都完全依赖于一致的键,因此您永远不会希望主键无法正常工作.
Defining the primary key along with the table is good practice - if you separate the table and the key definition, that opens the window to the key definition getting lost or forgotten. Given that any decent database design utterly depends on consistent keys, you don't ever want to have even the slightest chance that your primary keys aren't functioning properly.
更多推荐
SQL:应该在哪里定义主键
发布评论