创建新表时如何添加外键?

发布于 2024-07-08 03:12:12 字数 333 浏览 11 评论 0原文

我有这两个 CREATE TABLE 语句:

CREATE TABLE GUEST (
  id int(15) not null auto_increment PRIMARY KEY,
  GuestName char(25) not null
);

CREATE TABLE PAYMENT (
  id int(15) not null auto_increment
  Foreign Key(id) references GUEST(id),
  BillNr int(15) not null
);

第二个语句有什么问题? 它没有创建新表。

I have these two CREATE TABLE statements:

CREATE TABLE GUEST (
  id int(15) not null auto_increment PRIMARY KEY,
  GuestName char(25) not null
);

CREATE TABLE PAYMENT (
  id int(15) not null auto_increment
  Foreign Key(id) references GUEST(id),
  BillNr int(15) not null
);

What is the problem in the second statement? It did not create a new table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放飞的风筝 2024-07-15 03:12:12

您的问题的答案与 这个 的答案几乎相同。

您需要在包含外键的表中指定包含主键的表的名称以及主键字段的名称(使用“引用”)。

有一些代码展示了如何自己创建外键,并在 CREATE TABLE 中创建。

这是其中一个更简单的示例:

CREATE TABLE parent (id INT NOT NULL,
   PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
   INDEX par_ind (parent_id),
   FOREIGN KEY (parent_id) REFERENCES parent(id)
   ON DELETE CASCADE
) ENGINE=INNODB;

The answer to your question is almost the same as the answer to this one .

You need to specify in the table containing the foreign key the name of the table containing the primary key, and the name of the primary key field (using "references").

This has some code showing how to create foreign keys by themselves, and in CREATE TABLE.

Here's one of the simpler examples from that:

CREATE TABLE parent (id INT NOT NULL,
   PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
   INDEX par_ind (parent_id),
   FOREIGN KEY (parent_id) REFERENCES parent(id)
   ON DELETE CASCADE
) ENGINE=INNODB;
梨涡少年 2024-07-15 03:12:12

我建议为付款表设置一个唯一的密钥。 另一方面,外键不应该是 auto_increment,因为它引用已经存在的键。

CREATE TABLE GUEST(
    id int(15) not null auto_increment PRIMARY KEY, 
    GuestName char(25) not null
)  ENGINE=INNODB;

CREATE TABLE PAYMENT(
    id int(15)not null auto_increment, 
    Guest_id int(15) not null, 
    INDEX G_id (Guest_id), 
    Foreign Key(Guest_id) references GUEST(id),
    BillNr int(15) not null
)  ENGINE=INNODB;

I will suggest having a unique key for the payment table. On it's side, the foreign key should not be auto_increment as it refer to an already existing key.

CREATE TABLE GUEST(
    id int(15) not null auto_increment PRIMARY KEY, 
    GuestName char(25) not null
)  ENGINE=INNODB;

CREATE TABLE PAYMENT(
    id int(15)not null auto_increment, 
    Guest_id int(15) not null, 
    INDEX G_id (Guest_id), 
    Foreign Key(Guest_id) references GUEST(id),
    BillNr int(15) not null
)  ENGINE=INNODB;
泪冰清 2024-07-15 03:12:12

确保您对数据库或两个表使用 InnoDB 引擎。 来自 MySQL 参考:

对于InnoDB以外的存储引擎,
MySQL服务器解析FOREIGN KEY
CREATE TABLE 语句中的语法,但是
不使用或存储它。

Make sure you're using the InnoDB engine for either the database, or for both tables. From the MySQL Reference:

For storage engines other than InnoDB,
MySQL Server parses the FOREIGN KEY
syntax in CREATE TABLE statements, but
does not use or store it.

述情 2024-07-15 03:12:12
create table course(ccode int(2) primary key,course varchar(10));

create table student1(rollno int(5) primary key,name varchar(10),coursecode int(2) not 
null,mark1 int(3),mark2 int(3),foreign key(coursecode) references course(ccode));
create table course(ccode int(2) primary key,course varchar(10));

create table student1(rollno int(5) primary key,name varchar(10),coursecode int(2) not 
null,mark1 int(3),mark2 int(3),foreign key(coursecode) references course(ccode));
我的鱼塘能养鲲 2024-07-15 03:12:12

int(15)not null 之间应该有空格

There should be space between int(15) and not null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文