我们是否需要指定"not null"?对于主键?甲骨文/SQL [英] Do we need to specify "not null" for primary key? Oracle/SQL
本文介绍了我们是否需要指定"not null"?对于主键?甲骨文/SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
CREATE TABLE Person(
PersonId NUM(20),
...
)
ALTER TABLE Person
ADD(CONSTRAINT personpk PRIMARY KEY(PersonId))
作为标题,我是否需要为PersonId指定"not null"?还是如果我将其设置为主键,默认情况下它不会自动为空?
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?
e.g:
CREATE TABLE Person(
PersonId NUM(20) NOT NULL,
...
推荐答案
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
因此,不,您不需要将主键列指定为NOT NULL.
So, no, you do not need to specify primary key columns as NOT NULL.
这篇关于我们是否需要指定"not null"?对于主键?甲骨文/SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文