Oracle缺少表达式

时间:2017-09-16 05:10:05

标签: sql oracle

我现在只有这个错误..我似乎无法看到它太长的地方(页面底部的错误)

CREATE TABLE Menu_Item_Ingredient
(
  Menu_Item_Number Number(5,0)CONSTRAINT NN_MenuItemIngredient_MenuItemNumber Not null,
    CONSTRAINT FK_MenuItemIngredient_MenuItemNumber Foreign Key(Menu_Item_Number) References Bill_Item(Menu_item_Number),
  Ingredient_Number Number(5,0) CONSTRAINT NN_MenuItemIngredient_IngredientNumber Not null,
    CONSTRAINT FK_MenuItemIngredient_IngredientNumber Foreign Key(Ingredient_Number) References Ingredient(Ingredient_Number),
  Quantity_Needed Number(5,2) DEFAULT 0 CONSTRAINT NN_MenuItemIngredient_QuantityNeeded Not null
    CONSTRAINT CK_MenuItemIngredient_QuantityNeeded CHECK(Quantity_Needed >= 0),
    CONSTRAINT PK_BillItem_Ingredient Primary key(Menu_Item_Number,Ingredient_Number)
)

错误报告 - SQL错误:ORA-00972:标识符太长 00972. 00000 - “标识符太长” *原因:指定了超过30个字符的标识符。 *动作:最多指定30个字符。

2 个答案:

答案 0 :(得分:0)

执行以下操作。在您的查询中,当您在discount表格中声明bill_item列时,会丢失逗号。

CREATE TABLE Bill_Item3
(
    Bill_Number      NUMBER(6,0) CONSTRAINT NN_BillItem_BillNumber NOT NULL,
    Menu_Item_Number NUMBER(5,0) CONSTRAINT NN_BillItem_MenuItemNumber NOT NULL,
    Discount         NUMBER(5,2) CONSTRAINT N_BillItem_Discount NULL,
    --colum Discount format '%'00.00,
    Quaintity_Sold   NUMBER(3,0)CONSTRAINT NN_BillItem_QuaintitySold NOT NULL,
    Selling_Price    NUMBER(6,2) default 0 CONSTRAINT NN_BillItem_SellingPrice NOT NULL,
    CONSTRAINT CK_BillItem_SellingPrice CHECK (Selling_Price >= 0  ), 
    CONSTRAINT PK_Bill_MenuItem PRIMARY KEY (Bill_Number,Menu_Item_Number),
    CONSTRAINT FK_BillItem_Bill FOREIGN KEY (Bill_Number) REFERENCES bill (Bill_Number),
    CONSTRAINT FK_BillItem_MenuItemNumber FOREIGN KEY (Menu_Item_Number) REFERENCES Menu_Item_Ingredient(Menu_Item_Number),
    CONSTRAINT CK_BillItem_Discount CHECK (Discount BETWEEN 0 AND 100)
);

答案 1 :(得分:0)

你的问题在于约束,

  1. 外键约束将用于引用另一个表中的列,在您的查询中,您已经给出了下面的约束 -

    "CONSTRAINT FK_BillItem_Bill Foreign key (Bill_Number) References Primary(Bill_Number),你想用这个做什么?

    您需要使用的语法应为 -

    CONSTRAINT constraint_name FOREIGN KEY (column_in_current_table) REFERENCES Other_Table_Name(column_name_you_want_to_refer_in_other_table)
    
  2. 据我所知,这不起作用,你不能在检查约束中使用DEFAULT。

    constraint CK_BillItem_SellingPrice check (Selling_Price >= 0 OR DEFAULT = 0)
    

    如果要强制执行DEFAULT约束,则必须使用以下内容 -

    CREATE TABLE Persons (
        ID int NOT NULL,
        LastName varchar(255) NOT NULL,
        FirstName varchar(255),
        Age int,
        City varchar(255) DEFAULT 'Sandnes'
    );
    

    您的决赛桌可以如下 -

    CREATE TABLE Bill_Item
    (
      Bill_Number Number(6,0) constraint NN_BillItem_BillNumber not null,
      --CONSTRAINT FK_BillItem_Bill Foreign key (Bill_Number) References Primary(Bill_Number), 
      Menu_Item_Number Number(5,0) CONSTRAINT NN_BillItem_MenuItemNumber Not null,
      --CONSTRAINT FK_BillItem_MenuItemNumber Foreign Key (Menu_Item_Number) References Primary(Menu_Item_Number),
      Discount Number(5,2) CONSTRAINT N_BillItem_Discount Null
      CONSTRAINT CK_BillItem_Discount Check (Discount BETWEEN 0 and 100),
      --colum Discount format '%'00.00,
      Quaintity_Sold Number(3,0)CONSTRAINT NN_BillItem_QuaintitySold not null,
      Selling_Price Number(6,2) DEFAULT 0.0 CONSTRAINT NN_BillItem_SellingPrice not null, -- You can remove "DEFAULT 0.0" if you do not want to default your Selling_Price to 0.0
      constraint CK_BillItem_SellingPrice check (Selling_Price >= 0),
      CONSTRAINT PK_Bill_MenuItem Primary key (Bill_Number,Menu_Item_Number)
    );
    
  3. 所有其他表也有相同的问题,如上例所示,从检查约束中删除DEFAULT,并在列定义本身中指定“默认”约束。

  4. 此外,创建将在其他表中首先引用的表,例如,您的第二个表“Bill”具有引用“Waiter”表的外键约束,因此您必须创建“Waiter” “先表。

  5. 如果您仍有疑虑,请告诉我。