简体   繁体   English

无法在MySQL中创建表

[英]Unable to create table in MySQL

I am unable to create these tables in MySQL. 我无法在MySQL中创建这些表。 Everything looks complete to me but I keep getting errors. 对我来说,一切看起来都很完整,但我总是出错。

Here is my SQL: 这是我的SQL:

CREATE TABLE Movies(
   title char PRIMARY KEY NOT NULL, 
   Years date NOT NULL, 
   length decimal not null, 
   genre char NOT NULL, 
   academy_award char not null, 
   FOREIGN KEY(the_name) REFERENCES Studio, 
   FOREIGN KEY(directorName) REFERENCES  Director);

CREATE TABLE StarsIn(
   FOREIGN KEY(title) REFERENCES Movies,  
   FOREIGN KEY(Years)  REFERENCES Movies,
   FOREIGN KEY(the_name) REFERENCES MovieStar);

CREATE TABLE Director(
   the_name char PRIMARY KEY NOT NULL, 
   birthdate date NOT NULL, 
   nationality char NOT NULL);

CREATE TABLE MovieStar(
   the_name char PRIMARY KEY NOT NULL,  
   birthdate date NOT NULL, 
   address char NOT NULL, 
   gender char NOT NULL);

CREATE TABLE Studio(
   the_name char PRIMARY KEY NOT NULL,  
   address char NOT NULL);

The problem you are having is that you are referencing columns in tables that do not exist. 您遇到的问题是您正在引用不存在的表中的列。

Although you create these tables later, the DBMS engine does not know they will exist. 尽管稍后创建这些表,但是DBMS引擎不知道它们将存在。

The solution - create the tables without the FK's that do not exist, and add these later using the "ALTER TABLE" command. 解决方案-创建不包含不存在的FK的表,并稍后使用“ ALTER TABLE”命令添加这些表。

It seems like you know the syntax, so I'll let you inform me if you cannot find it. 似乎您知道语法,所以如果您找不到它,我将通知您。


your syntax should look like (All I did is re-order, and change char to be archer...): 您的语法应如下所示(我所做的只是重新排序,并将char更改为archer ...):

CREATE TABLE Studio(the_name varchar(50) PRIMARY KEY NOT NULL,  address varchar(50) NOT NULL);
CREATE TABLE Director(the_name varchar(50) PRIMARY KEY NOT NULL, birthdate date NOT NULL, nationality varchar(50) NOT NULL);
CREATE TABLE Movies(title varchar(50) PRIMARY KEY NOT NULL, Years date NOT NULL, 
    length decimal not null, genre varchar(50) NOT NULL, academy_award varchar(50) not null, 
    the_name REFERENCES Studio(the_name), directorname REFERENCES  Director(the_name));
CREATE TABLE MovieStar(the_name char PRIMARY KEY NOT NULL,  birthdate date NOT NULL, address char NOT NULL, gender char NOT NULL);
CREATE TABLE StarsIn(title REFERENCES Movies(title),movie REFERENCES Movies(title), moviestar REFERENCES MovieStar(the_name));

A little note, depends on the version - you'd might have to use the FOREIGN KEY(col_name) instead of col_name. 请注意一点,具体取决于版本-您可能必须使用FOREIGN KEY(col_name)而不是col_name。 I like more the syntax without it, but some versions force you to use FOREIGN KEY(title) instead of title in the last SQL for example. 我更喜欢没有它的语法,但是例如,某些版本会强制您使用FOREIGN KEY(title)而不是最后一个SQL中的title

The syntax you used - Foreign key NAME References TABLE (Column) -- you forgot the column-- Allows you to name the FK. 您使用的语法-外键名称引用表(列)-您忘记了该列-允许您命名FK。 If you don't use the "Foreign Key" the system will randomly assign a name. 如果您不使用“外键”,系统将随机分配一个名称。 This is usually not important, unless you want the DB design to be "clean" and to be able to reference it by name. 这通常并不重要,除非您希望数据库设计“干净”并且能够按名称引用它。

PS - I did not run it, I trust you to inform if there's any other issue - I did not check the syntax, just fixed the error you reported- references that do not exist... PSPS Always check syntax... http://dev.mysql.com/doc/refman/5.1/en/create-table.html PS-我没有运行它,我相信您可以告知是否还有其他问题-我没有检查语法,只是修复了您报告的错误-引用不存在... PSPS始终检查语法... http: //dev.mysql.com/doc/refman/5.1/zh-CN/create-table.html

This isn't working because your tables are not being created in the proper order. 这不起作用,因为未按正确的顺序创建表。

The parent table must always be created first, because otherwise you will be trying to reference a column that does not exist. 必须始终先创建父表,因为否则您将尝试引用不存在的列。

For example, look at this part here: 例如,在这里看这部分:

CREATE TABLE Movies(title char PRIMARY KEY NOT NULL, Years date NOT NULL, 
length decimal not null, genre char NOT NULL, academy_award char not null, 
FOREIGN KEY(the_name) REFERENCES Studio, FOREIGN KEY(directorName) REFERENCES  Director);

You can't reference tables studio or director because they don't even exist. 您不能引用表工作室或导演,因为它们甚至不存在。

The proper order could be: 正确的顺序可能是:

Studio
Movie Star
Director
Movies
StarsIn

In addition, you have a lot of other problems going on here. 此外,这里还有许多其他问题。

You do not define any columns in the Stars IN table, you only declare foreign keys: 您没有在Stars IN表中定义任何列,只声明了外键:

CREATE TABLE StarsIn(
FOREIGN KEY(title) REFERENCES Movies,  
FOREIGN KEY(Years)  REFERENCES Movies,
FOREIGN KEY(the_name) REFERENCES MovieStar);

Not only are you not defining any columns, you aren't referencing anything. 您不仅没有定义任何列,而且没有引用任何东西。 This will also throw an error. 这也会引发错误。 Your foreign key title must reference some column in the movies table. 您的外键标题必须引用电影表中的某些列。 And in addition to that, you can't create a foreign key reference for Years. 除此之外,您不能为Years创建外键参考。 Mostly because you cannot create a foreign key to a column that is not primary key in another table, and also why should you need two foreign keys to the same table? 通常是因为您无法为不是另一个表中的主键的列创建外键,并且为什么还要在同一表中需要两个外键?

Those problems exist in other create table statements as well, so given everything I've said please go back and look at each of your create table statements. 这些问题也存在于其他create table语句中,因此鉴于我所说的一切,请回头查看每个create table语句。

Here is a working SQL Fiddle you can use for reference though. 这是一个有效的SQL提琴,您可以将其用作参考。

EDIT 编辑

I would like to also add that I do not recommend using the char datatype. 我还要补充一点,我不建议使用char数据类型。 Please look into using VARCHAR() . 请研究使用VARCHAR()

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

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