0

我想创建一个列值大于 0 并且可以为空的表。

当我在创建表列时这样做

CREATE TABLE Ship
  (
    SITE_ID         INTEGER NOT NULL,
    Ship_CATEGORY  VARCHAR2(50)  CHECK (WRECK_CATEGORY IN ('Military','Government','Passenger', 'Unknown', 'Commercial')),
    Ship_TYPE      VARCHAR2(100) ,
    Ship_TONNAGE   INTEGER CHECK ((WRECK_TONNAGE>0) AND WRECK_TONNAGE IS NULL) ,
    Ship_CAUSE     VARCHAR2(100) NULL CHECK (WRECK_CAUSE IN ('Fire', 'Collision','Unknown',' Deliberate', 'Explosion',' Attack', 'Other')),
    Ship_DATESUNK  DATE NULL,
    Ship_CONDITION VARCHAR2(50) NULL CHECK (WRECK_CONDITION IN ('Excellent', 'Moderate','Poor', 'Very Poor')),
    CONSTRAINT SHIPWRECK_PK PRIMARY KEY (SITE_ID)
  );

在“Ship_TONNAGE INTEGER CHECK ((WRECK_TONNAGE>0) AND WRECK_TONNAGE IS NULL)”行中,当我向该列插入空值时,它违反了约束。我如何创建满足条件的表?谢谢

4

1 回答 1

1

将检查约束从 an 更改and为 an or

CHECK ((WRECK_TONNAGE>0) or WRECK_TONNAGE IS NULL)

您的约束引用了错误的列,WRECK* 而不是 SHIP*。试试这个定义语句:

CREATE TABLE Ship
  (
    SITE_ID         INTEGER NOT NULL,
    Ship_CATEGORY  VARCHAR2(50)  CHECK (SHIP_CATEGORY IN ('Military','Government','Passenger', 'Unknown', 'Commercial')),
    Ship_TYPE      VARCHAR2(100) ,
    Ship_TONNAGE   INTEGER CHECK ((SHIP_TONNAGE>0) or SHIP_TONNAGE IS NULL) ,
    Ship_CAUSE     VARCHAR2(100) NULL CHECK (SHIP_CAUSE IN ('Fire', 'Collision','Unknown',' Deliberate', 'Explosion',' Attack', 'Other')),
    Ship_DATESUNK  DATE NULL,
    Ship_CONDITION VARCHAR2(50) NULL CHECK (SHIP_CONDITION IN ('Excellent', 'Moderate','Poor', 'Very Poor')),
    CONSTRAINT SHIPWRECK_PK PRIMARY KEY (SITE_ID)
  );
于 2013-05-26T22:29:53.520 回答