首页 > 解决方案 > 尝试创建表时不断收到错误 ORA-02438

问题描述

我试图让我的代码运行,但它一直给我这个错误。我是 SQL 新手,还没有习惯它,我一直在尝试修复它,但我似乎无法让它工作。我已将这两个表放入下面的描述代码中。

 Create table Account
(
    Account_Number Number(7,0)
      Constraint PK_Account_Account_Number Primary Key
      Constraint NN_Account_Account_Number Not Null,  
    Account_Balance Number(13,2)
      Constraint NN_Account_Account_Balance Not Null
);


  Create table Investor
(
  Investor_Number Number(7,0)
      Constraint PK_Investor_Investor_Number Not Null,
  First_Name Varchar2(25)
      Constraint NN_Investor_First_Name Not Null,
  Last_Name Varchar2(30)
      Constraint NN_Investor_Last_Name Not Null,    
  Street_Address Varchar2(35)
      Constraint NL_Investor_Street_Address Null,
  City Varchar2(25)
      Constraint NL_Investor_City Null,
  Province Char(2)
      Constraint CK_Investor_Province Check (Province in ('__'))
      Constraint NL_Investor_Province Null,
  Postal_Code Varchar2(7)
      Constraint CK_Investor_Postal_Code Check (REGEXP_like(string,'[A-Z]-[0-9]-[A-Z]-[0-9]-[A-Z]-[0-9]'))
      Constraint NL_Investor_Posta_Code Null,
  Area_Code Number(3,0)
      Default '780'
      Constraint NN_Investor_Area_Code Not Null,
  Phone_Number Number(7,0) 
      Constraint NN_Investor_Phone_Number Not Null,
  Account_Number Number(7,0) Not Null,
      Constraint FK__Investor_Account_Number --Name of Constraint
      Foreign Key (Account_Number) -- Foreign Key Column name
      References Account(Account_Number)-- name of table and column trying to reference,
  );

标签: sqloracle

解决方案


中的检查约束Postal_Code是指string而不是实际的列名;它应该是:

  Postal_Code Varchar2(7)
      Constraint CK_Investor_Postal_Code Check
          (REGEXP_like(Postal_Code,'[A-Z]-[0-9]-[A-Z]-[0-9]-[A-Z]-[0-9]'))
      Constraint NL_Investor_Postal_Code Null,

db<>小提琴

您得到的错误是因为解析器将其解释string为标识符,并假设它是另一列的名称,即使它不是。但显然没有其他任何东西,所以这并非完全不合理。


推荐阅读