简体   繁体   中英

Unable to create table in MySQL

I am unable to create these tables in MySQL. Everything looks complete to me but I keep getting errors.

Here is my 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.

The solution - create the tables without the FK's that do not exist, and add these later using the "ALTER TABLE" command.

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...):

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. 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.

The syntax you used - Foreign key NAME References TABLE (Column) -- you forgot the column-- Allows you to name the 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

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:

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. 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.

Here is a working SQL Fiddle you can use for reference though.

EDIT

I would like to also add that I do not recommend using the char datatype. Please look into using VARCHAR() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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