Sql 单列上的多个约束
我们可以在单个列上添加多个约束吗 像- 此查询返回错误ora-00904:无效标识符….Sql 单列上的多个约束,sql,oracle10g,Sql,Oracle10g,我们可以在单个列上添加多个约束吗 像- 此查询返回错误ora-00904:无效标识符…. 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) ) 创建表x(x varchar2(20),y编号(2)
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)
)
创建表x(x varchar2(20),y编号(2)不为空,
约束fk约束外键(x)引用用户信息(用户id),
约束空检查(x不为空)
)
创建null\u约束时语法错误
约束:
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)
)
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)
)
使用此(表级检查约束):
或(在列上使用非NULL
约束):
或(使用列级别检查约束):
如果RDBMS是SQL Server,您可以通过这样做在列中定义多个约束(我更改了一些列名,以使约束命名策略更加明显-命名是主观的):
我假设user\u info表存在,并且该表中存在user\u id列?
CREATE TABLE x(
x VARCHAR2(20) NOT NULL,
y NUMBER(2) NOT NULL,
CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id)
)
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)
)
create table x(x varchar2(20) not null, y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id)
)
CREATE TABLE SomeTable(
User_Id VARCHAR(20) CONSTRAINT FK_SomeTable_User_Id FOREIGN KEY REFERENCES dbo.User_Info(User_Id) CONSTRAINT UIX_NC_SomeTable_User_id NONCLUSTERED NOT NULL,
SomeOtherColumn DECIMAL(2, 0) NOT NULL
)