dongxing2692 2017-09-30 05:22
浏览 70
已采纳

如何使用外键填充子表 - MySql

I am working on MySQL database. I am new to it that is why I am facing a problem. The problem is populating the child table with foreign key which is referencing to the parent table. I have two tables employee which contains following columns

  • id as a primary key,
  • first_name
  • last_name
  • birth_date

and a borrowed table which contains following columns

  • ref as a primary key
  • employId as a foreign key
  • book

The employeeId is referencing the primary key id of the employee table. So simply it means the one employee with same id can borrow multiple books. When I insert some data into the employee table It get inserted, but when I have to insert data into the borrowed table, I have to manually insert the value in employeeId column. Isn't it supposed to be populated automatically. or I am misunderstanding the concept of the foreign key.

My SQL Code

  $uname = "root";
  $pass = "";
  $sname ="localhost";
  $db ="nady";
   //Making database connection
  $con = mysqli_connect($sname,$uname,$pass,$db);

 $t1 = "CREATE TABLE IF NOT EXISTS employee (
    id smallint(5) unsigned AUTO_INCREMENT NOT NULL,
    firstname varchar(30),
    lastname varchar(30),
    birthdate date,
    PRIMARY KEY (id)
) ENGINE=InnoDB";
$con->query($t1);



$t2 = "CREATE TABLE IF NOT EXISTS borrowed (
    ref int(10) unsigned NOT NULL auto_increment,
    employeeid smallint(5) unsigned NOT NULL,
    book varchar(50),
    PRIMARY KEY (ref),
    FOREIGN KEY (employeeid) REFERENCES employee(id) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB";
$con->query($t2);
if(!$con->query($t2)){
    echo $con->error;
}


$i1 = "INSERT INTO employee VALUES(NULL,\"Nadeem\",\"Ahmad\",22)";
$con->query($i1);

$i2 = "INSERT INTO borrowed VALUES(NULL,1,\"Ahmad\")";
$con->query($i2);
if(!$con->query($i2)){
    echo $con->error;
}

Simple what I need is ; For example an employee with id 1. Who borrowed 3 books. So in the borrowed table the employeeId column will have three rows with values 1 and different books name. My point is how would I populate the employeeId column when I am inserting the data into it. Let say, John have have borrowed three books and have id 1 then how would I insert data to borrowed table with employeeId of john. I need the query for it. and also query to retrieve the books borrowed by john.

</div>
  • 写回答

2条回答 默认 最新

  • douzi4766 2017-09-30 07:02
    关注

    The foreign key is used to link two tables, indicating that the field in a column (employId from borrowed, in your case) refers to the PRIMARY KEY of another table (id from employee).

    When you're inserting a new line in borrowed, you have to indicate the user that is taking that book, to insert it in that line. You have to know the user that is doing it. If you have foreign key, you need the id of that user, which is supposed to be his unique identifier. To insert that John has taken a book, you need to know that John's id is 1.

    If the user is already in your employee table and you know his first and last name, you can get the id with a simple select...

    SELECT id FROM employee WHERE first_name='John' AND last_name='Smith'
    

    ... and then you can do the insert with the id obtained.

    If it's new user, you need to add the user first to employee, then get the new id and then insert the new line in borrowed, to do this without having to re-query to employee table to get the new id, you can use the PHP mysqli::$insert_id/mysqli_insert_id function, that gives you the PRIMARY key of the last query. For example...

    $con->query("INSERT INTO employee (first_name,last_name) VALUES ('Mark','Whatever')");
    $newemployeeid = $con->insert_id;
    $con->query("INSERT INTO borrowed (employeeid,book) VALUES (".$newemployeeid.",'Awesome Book Title')");
    

    I hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目