doushi1957 2016-09-12 01:38
浏览 78

创建运动员表失败SQLSTATE [23000]:完整性约束违规:

i am trying to create to 2 tables and inserting data into them but i am geting this error Creating Athlete the table failed SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails

try
    {
    $dropQuery = "DROP TABLE IF EXISTS Country";
    $pdo->exec($dropQuery);
    $dropQuery = "DROP TABLE IF EXISTS Athlete";
    $pdo->exec($dropQuery);



$createQuery = "CREATE TABLE Country
    (
        countryID int(11) NOT NULL AUTO_INCREMENT,
        name varchar(30) NOT NULL,  
        population decimal(10,0),
        flagImage varchar(30) NOT NULL,
        PRIMARY KEY (countryID)
    )";


    $pdo->exec($createQuery);
}
catch(PDOException $e)
{
    $error = " Creating Country the table failed";
    include 'error.php';
    exit();
}
try
{
$createQuery = "CREATE TABLE Athlete
    (
        athleteId int(11) NOT NULL AUTO_INCREMENT,
        lastName varchar(30) NOT NULL,
        firstName varchar(30) NOT NULL,
        gender char(1) NOT NULL,
        image varchar(300) NOT NULL,
        sport varchar(30) NOT NULL,
        countryID int(11) NOT NULL,
        CONSTRAINT Athlete_Country FOREIGN KEY (countryID) REFERENCES Country(countryID),
        PRIMARY KEY (athleteId)
    )";



    $pdo->exec($createQuery);
}
catch(PDOException $e)
{
    $error = " Creating Athlete the table failed";
    include 'error.php';
    exit();
}




try
{       
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('usa',324206000,'usa.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('Hungary',9823000,'hungary.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('Jamaica',2930050,'jamaica.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('United Kindom',65341183,'uk.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('Australia',25054000,'australia.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('South Africa',54956900,'southafrica.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('Ethiopia',92206005,'ethiopia.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('Poland',38437239,'poland.jpg')";
        $pdo->exec($query);
        $query = "INSERT INTO Country (name,population,flagImage) VALUES ('China',1379442000,'china.jpg')";
        $pdo->exec($query);


}
 catch(PDOException $e)
{
    $error = "Creating Country data failed";
    include 'error.php';
    exit();
}
try
{       
        $query = "INSERT INTO Athlete VALUES (1,'Phelps','Michael','m','Phelps.jpg','Swimming',1)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (2,'Ledecky','Katie','f','Ledecky.jpg','Swimming',1)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (3,'Biles','Simone','f','Biles.jpg','Gymnastics',1)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (4,'Hosszu','Katinka','f','Hosszu.jpg','Swimming',2)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (5,'Bolt','Usain','m','Bolt.jpg','Athletics',3)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (6,'Kenny','Jason','m','Kenny.jpg','Cycling',4)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (7,'Danuta','Kozak','f','Danuta.jpg','Canoeing',2)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (8,'Murphy','Ryan','m','Murphy.jpg','Swimming',5)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (9,'Manuel','Simone','f','Manuel.jpg','Swimming',1)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (10,'Dirado','Maya','f','Dirado.jpg','Swimming',1)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (11,'van Niekirk','Wayde','m','vanNiekirk.jpg','Athletics',6)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (12,'Ayana','Almaz','f','Ayana.jpg','Athletics',7)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (13,'Wlodarczyk','Anita','f','Wlodarczyk.jpg','Athletics',8)";
        $pdo->exec($query);
        $query = "INSERT INTO Athlete VALUES (14,'Long','Qingquan','m','Long.jpg','Weightlifting',9)";
        $pdo->exec($query);


}
 catch(PDOException $e)
{
    $error = "Creating Athlete the data failed";
    include 'error.php';
    exit();
}
  • 写回答

1条回答 默认 最新

  • douchuoliu4422 2016-09-12 06:55
    关注

    There are two issues in your code: First, as Chin said, you have to execute your 'CREATE Country' stament before 'CREATE Athlete' stament. And second, you have an error in the'INSERT INTO Country Athlete VALUES ...', you need to remove 'Country' to make it work.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配