我们需要为主键指定“非空”吗?甲骨文/SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13665394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:24:15  来源:igfitidea点击:

Do we need to specify "not null" for primary key? Oracle/SQL

sqloracle

提问by Mr.Y

CREATE TABLE Person(
    PersonId NUM(20),
    ...
    )

ALTER TABLE Person
ADD(CONSTRAINT personpk PRIMARY KEY(PersonId))

As title, do I need to specify "not null" for PersonId? Or if I set it to primary key, it is automatically not null by default?

作为标题,我是否需要为 PersonId 指定“非空”?或者如果我把它设置为主键,默认情况下它自动不为空?

e.g: 
CREATE TABLE Person(
PersonId NUM(20) NOT NULL,
...

回答by eaolson

create table mytable (
  col1 number primary key,
  col2 number,
  col3 number not null
);

table MYTABLE created.

select table_name, column_name, nullable 
from user_tab_cols where table_name = 'MYTABLE';

TABLE_NAME                     COLUMN_NAME                    NULLABLE
------------------------------ ------------------------------ --------
MYTABLE                        COL1                           N        
MYTABLE                        COL2                           Y        
MYTABLE                        COL3                           N        

So, no, you do not need to specify primary key columns as NOT NULL.

因此,不,您不需要将主键列指定为 NOT NULL。

回答by wolφi

Yes, as @eaolson said, you don't need to specify NOT NULL for primary key columns, they are set automatically to NOT NULL.

是的,正如@eaolson 所说,您不需要为主键列指定 NOT NULL,它们会自动设置为 NOT NULL。

However, Oracle keeps track that you didn't specify NOT NULL explicitly in case the primary key is disabled or dropped later on:

但是,Oracle 会跟踪您没有明确指定 NOT NULL,以防主键稍后被禁用或删除:

create table mytable (
  col1 number,
  col2 number not null
);

select table_name, column_name, nullable
  from user_tab_columns where table_name = 'MYTABLE';

TABLE_NAME   COLUMN_NAME  NULLABLE
------------ ------------ ---------
MYTABLE      COL1         Y
MYTABLE      COL2         N

As expected, col1 is nullable and col2 NOT NULL. A primary key changes both columns to NOT NULL:

正如预期的那样,col1 可以为空,而 col2 不是 NULL。主键将两列都更改为 NOT NULL:

alter table mytable add primary key (col1, col2);

select table_name, column_name, nullable
  from user_tab_columns where table_name = 'MYTABLE';

TABLE_NAME   COLUMN_NAME  NULLABLE
------------ ------------ ---------
MYTABLE      COL1         N
MYTABLE      COL2         N

If you disable or drop the primary key, both columns revert to the original state, co1 becomes nullable again:

如果禁用或删除主键,两列都恢复到原始状态,co1 再次变为可空:

alter table mytable disable primary key;

select table_name, column_name, nullable
  from user_tab_columns where table_name = 'MYTABLE';

TABLE_NAME   COLUMN_NAME  NULLABLE
------------ ------------ ---------
MYTABLE      COL1         Y
MYTABLE      COL2         N

回答by Mudassir Hasan

Primary key by definition can never be Null. Primary key purpose is to uniquely identify records. A primary key is a combination of columns which uniquely specify a row.

根据定义,主键永远不能为 Null。主键的目的是唯一标识记录。主键是唯一指定行的列的组合。

A Null value represents lack of value. Even if two records have NULL in same column , the column values are not considered equal.

Null 值表示缺少值。即使两个记录在同一列中为 NULL,列值也不被视为相等。

回答by Ross

In most DBMS, since its a primary key (and the definition of is that it must be unique within the table) then it most certainly cannot be null.

在大多数 DBMS 中,由于它是一个主键(并且定义是它在表中必须是唯一的),那么它肯定不能为空。