单列上的多重约束

Anu*_*Anu 4 sql oracle10g

我们可以在单个列上添加多个约束吗?

喜欢-

create table x(x varchar2(20), y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id),
constraint null_cons not null(x)
)
Run Code Online (Sandbox Code Playgroud)

此查询返回错误 ora-00904: invalid identifier....

Isw*_*San 6

创建null_cons约束时语法错误:

使用这个(表级检查约束):

CREATE TABLE x(
    x VARCHAR2(20), 
    y NUMBER(2) NOT NULL,
    CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id),
    CONSTRAINT null_cons CHECK(x IS NOT NULL)
)
Run Code Online (Sandbox Code Playgroud)

或者(NOT NULL对列使用约束):

CREATE TABLE x(
    x VARCHAR2(20) NOT NULL, 
    y NUMBER(2) NOT NULL,
    CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id)
)
Run Code Online (Sandbox Code Playgroud)

或(使用列级检查约束):

CREATE TABLE x(
    x VARCHAR2(20) CHECK (X IS NOT NULL), 
    y NUMBER(2) NOT NULL,
   CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id)
)
Run Code Online (Sandbox Code Playgroud)