为什么 NOT NULL 约束只能在 COLUMN LEVEL 和 ALTER LEVEL 中添加 为什么我们不能在 TABLE LEVEL 中添加?
Why NOT NULL constraint can be added only in COLUMN LEVEL and ALTER LEVEL why can't we add in TABLE LEVEL?
我试过使用 alter 命令,它会像
改变 TABLE 修改不为空;及其工作
那么,为什么我们不能添加 table 级别,例如 (CREATE TABLE (COL1 DATATYPE(SIZE),COL2 DATATYPE(SIZE), CONSTRAINT TN_COL1_NN NOT NULL(COL1));?
What i meant to say is for example primary key constraint can be added in three levels (COLUMN,TABLE AND ALTER).similarly we can add ALL rest of the constraints in three level why can't we add NOT NULL?
我认为您要问的是在修改或创建 table 模式时如何在列上指定 NULL
或 NOT NULL
约束。答案是可以。
-- when you define/create the table
CREATE TABLE MyTable (Name VARCHAR(100) NOT NULL)
-- when you add a new column to an existing table
ALTER MyTable ADD Title VARCHAR(100) NOT NULL
-- when you alter an existing column on an existing table
ALTER MyTable ALTER COLUMN Title VARCHAR(100) NOT NULL
如果您想在以上任何内容中允许空值,请删除单词 NOT
。
我试过使用 alter 命令,它会像 改变 TABLE 修改不为空;及其工作 那么,为什么我们不能添加 table 级别,例如 (CREATE TABLE (COL1 DATATYPE(SIZE),COL2 DATATYPE(SIZE), CONSTRAINT TN_COL1_NN NOT NULL(COL1));?
What i meant to say is for example primary key constraint can be added in three levels (COLUMN,TABLE AND ALTER).similarly we can add ALL rest of the constraints in three level why can't we add NOT NULL?
我认为您要问的是在修改或创建 table 模式时如何在列上指定 NULL
或 NOT NULL
约束。答案是可以。
-- when you define/create the table
CREATE TABLE MyTable (Name VARCHAR(100) NOT NULL)
-- when you add a new column to an existing table
ALTER MyTable ADD Title VARCHAR(100) NOT NULL
-- when you alter an existing column on an existing table
ALTER MyTable ALTER COLUMN Title VARCHAR(100) NOT NULL
如果您想在以上任何内容中允许空值,请删除单词 NOT
。