In the below SQL code is 'NOT NULL' redundant since f_ID is a primary key and thus can't be null for any record?
在下面的SQL代码中,由于f_ID是主键,所以它不是NULL,因此对于任何记录都不能为NULL ?
CREATE TABLE t_Activities(
f_ID varchar(50) NOT NULL,
PRIMARY KEY (f_ID))
3 个解决方案
#1
4
What if you later drop the primary key constraint? Should the column allow nulls or not after that? Yes, no, maybe? The two constraints are different. PK constraint requires a NOT NULL, but does not implies one.
如果您后来放弃了主键约束怎么办?在那之后,列是否允许null ?是的,不,也许?这两个约束是不同的。PK约束要求不为空,但不表示为空。
#2
1
It may be redundant, but I prefer this method of creating a primary key
(or Identity
column). It shows that the person making the table understands and intends the column to be NOT NULL
as well as the Primary Key
for the table.
它可能是多余的,但是我更喜欢这种创建主键(或标识列)的方法。它表明,创建表的人理解并希望列不为NULL,以及表的主键。
#3
0
That is correct. If you had:
这是正确的。如果你有:
CREATE TABLE t_Activities(
f_ID varchar(50),
PRIMARY KEY (f_ID))
It would be the equivalen. The Primary Key constraint does not allow NULL
s.
它是相等的。主键约束不允许null。
#1
4
What if you later drop the primary key constraint? Should the column allow nulls or not after that? Yes, no, maybe? The two constraints are different. PK constraint requires a NOT NULL, but does not implies one.
如果您后来放弃了主键约束怎么办?在那之后,列是否允许null ?是的,不,也许?这两个约束是不同的。PK约束要求不为空,但不表示为空。
#2
1
It may be redundant, but I prefer this method of creating a primary key
(or Identity
column). It shows that the person making the table understands and intends the column to be NOT NULL
as well as the Primary Key
for the table.
它可能是多余的,但是我更喜欢这种创建主键(或标识列)的方法。它表明,创建表的人理解并希望列不为NULL,以及表的主键。
#3
0
That is correct. If you had:
这是正确的。如果你有:
CREATE TABLE t_Activities(
f_ID varchar(50),
PRIMARY KEY (f_ID))
It would be the equivalen. The Primary Key constraint does not allow NULL
s.
它是相等的。主键约束不允许null。