1

我想用 MYSQL PHPMYADMIN 创建一个表关系。但没有约束。

这是我所做的:

CREATE TABLE runs (
code_teachers int(8),
code_department int(8),
primary key(code_teachers, code_department),
foreign key(code_teachers)references teachers,
foreign key(code_department)references department
);

因此,如您所见,我正在尝试在教师创建表的 code_teachers 和部门创建表的 code_department 之间创建名为运行的表关系。

但是由于一些未知的原因,当我进入设计者时,它只是没有建立关系。因此,如果有人知道我的问题的答案,欢迎您告诉我,因为我在这里发疯了。

4

1 回答 1

1

你试过了吗

FOREIGN KEY ( columnName ) REFERENCES tableName(columnName)

您的查询中似乎缺少 tableName。外键需要引用其他表中的列。

维基百科说:

  • In the context of relational databases, a foreign key is a referential constraint between two tables. A foreign key is a field in a relational table that matches a candidate key of another table. The foreign key can be used to cross-reference tables.

mySQL.com说:

  • Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table.
于 2012-11-23T10:27:51.843 回答