2016-01-22 68 views
0

SQL錯誤完整性約束(JAVAUSER.SYS_C007925)違反 - 未找到父鍵

我不知道我做錯了什麼在這裏

這裏是我用來創建我的表的DDL

Create Table HomeState (StateAbbreviation char(2) Primary Key, 
     StateName varchar(25)); 

Create Table Country (CountryAbbreviation char(2) Primary Key, 
      CountryName varchar(35)); 

Create Table Employee (EmployeeID Integer Primary Key NOT NULL, 
         FirstName varchar(20), 
         LastName varchar(30), 
         MI char(1), 
         HomeAddress varchar(30), 
         Zip char(5), 
         DateOfBirth date, 
         HireDate date, 
         TerminationDate date, 
         AnnualSalary number(20,2), 
         LicenseDate date, 
         StateAbbreviation char(2), 
         CountryAbbreviation char(2), 
         Foreign Key (StateAbbreviation) references HomeState, 
         Foreign Key (CountryAbbreviation) references Country); 

Create Table Truck (VinNumber Integer Primary Key, 
      Make varchar(25), 
      Model varchar(30), 
      Year Integer, 
      PurchasePrice number(20,2), 
      LicenseNumber varchar(15)); 

Create Table EmployeeTruck (EmployeeID Integer, 
      VinNumber Integer, 
      Primary Key(EmployeeID,VinNumber), 
      Foreign Key (EmployeeID) references Employee, 
      Foreign Key (VinNumber) references Truck); 

Create Table Accident (AccidentID Integer Primary Key, 
      DateOfAccident date, 
      AccidentDescription varchar(200), 
      AccidentLocation varchar(100), 
      EmployeeID Integer, 
      Foreign Key (EmployeeID) references Employee); 

,這裏是我曾經嘗試在僱員表

insert into employee 
values ('1','brian','kim','j','adfasdf', 
     '1234','24-nov-1993','24-sep-1993','24-sep-1993', 
     '1234','24-sep-1993','as','as') 

填補了命令,但它總是GI我錯誤,我把這個問題的標題...

回答

0

在表「員工」,你說Foreign Key (StateAbbreviation) references HomeState,和Foreign Key (CountryAbbreviation) references Country)。在插入到Employee之前,您插入到Employee.HomeState和Employee.CountryAbbreviation中的值必須存在於HomeState和Country表中。

在插入到Employee之前,將行插入HomeState和Country。


您還有其他問題。這是一個例子。

Create Table HomeState (StateAbbreviation char(2) Primary Key, 
    StateName varchar(25)); 

insert into HomeState values ('AL', 'Alabama'); 
insert into HomeState values ('AM', 'Alabama'); 
insert into HomeState values ('AN', 'Alabama'); 

所有這些插入語句成功。他們不應該。在這種情況下,StateName也是候選鍵。

Create Table HomeState (
    StateAbbreviation char(2) Primary Key, 
    StateName varchar(25) not null unique 
); 

讓我們再來看看這一點。

Create Table HomeState (
    StateAbbreviation char(2) Primary Key, 
    StateName varchar(25) not null unique 
); 

Create Table Country (
    CountryAbbreviation char(2) Primary Key, 
    CountryName varchar(35) not null unique 
); 

insert into HomeState values ('CA', 'California'); 
insert into Country values ('AF', 'Afghanistan'); 

insert into Employee (EmployeeID, StateAbbreviation, CountryAbbreviation) 
values (-42, 'CA', 'AF'); 

州「加利福尼亞州,美國」是有道理的。狀態「加利福尼亞,阿富汗」不。

沒有名字的僱員沒有意義。聲明名字,姓氏和HireDate not null

打賭:無論你的數據庫允許什麼廢話會出現。這只是時間問題。

+0

非常感謝你! – Brian

相關問題