创建表时是否必须明确指出主键不应该为空?即使我们不提及主键的“not null”约束,SQL 也不允许我们将 NULL 值插入到主键字段中。因此,以下哪一项是好的做法?
1) create table registration(
id int primary key,
name varchar(10)
)
2) create table registration(
id int primary key not null,
name varchar(10)
)
最佳答案
No你不知道。
A PRIMARY KEY is a unique index where all key columns must be defined as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL declares them so implicitly (and silently). A table can have only one PRIMARY KEY. The name of a PRIMARY KEY is always PRIMARY, which thus cannot be used as the name for any other kind of index.
不过,我认为明确说明是一种很好的做法 - 因为这会让事情变得更加清晰, 而且您不必每次有疑问时都查找这条规则。
关于mysql - 我们是否必须明确提及主键的非空约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16481037/