简体   繁体   English

如何传递外键以在 php 中创建表

[英]How to pass foreign key for creating table in php

Here is my two tables, I want to pass my guest_id as a foreign key inside reservation table.这是我的两个表,我想将我的 guest_id 作为预订表中的外键传递。 but it's given error..但它给出了错误..

$sql = "CREATE TABLE MyGuests (
    guest_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    )";

// // sql to create table

$sql = "CREATE TABLE reservation (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY(guest_id) REFERENCES MyGuests(guest_id),
room INT(11) NOT NULL,
price FLOAT(11) NOT NULL,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

where is the problem?问题出在哪里?

You need also add the column guest_id to your table reservation您还需要将列 guest_id 添加到您的餐桌预订中

Like:喜欢:

$sql = "CREATE TABLE MyGuests (
    guest_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    )";

// // sql to create table

$sql = "CREATE TABLE reservation (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
guest_id INT(6) UNSIGNED,
room INT(11) NOT NULL,
price FLOAT(11) NOT NULL,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY(guest_id) REFERENCES MyGuests(guest_id)
)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM