I'm working on a database assignment and have run into this error.
The database should look like...
Legend: **Primary Key** *Foreign Key
.
Movies (**title, year**, length, genre, *studioName, *producerID)
StarsIn(***movieTitle, *movieYear, *starName**)
MovieStar(**name**, birthdate, address, gender)
MovieMaker(**ID**, name, address)
Studio (**name**, address, *presidentID)
And my code looks like this...
CREATE TABLE Movies (
title varchar(50),
year int,
length int,
genre varchar(50),
studioName varchar(50),
producerID varchar(50),
PRIMARY KEY (title,year)
FOREIGN KEY (studioName) REFERENCES Studio(name),
FOREIGN KEY (producerID) REFERENCES MovieMaker(ID),
);
CREATE TABLE StarsIn (
movieTitle varchar(50),
movieYear int,
starName varchar(50),
PRIMARY KEY (movieTitle,movieYear,starName),
FOREIGN KEY (movieTitle) REFERENCES Movies(title),
FOREIGN KEY (movieYear) REFERENCES Movies(year),
FOREIGN KEY (starName) REFERENCES MovieStar(name)
);
CREATE TABLE MovieStar (
name varchar(50),
birthdate int,
address varchar(50),
gender varchar(50),
PRIMARY KEY (name)
);
CREATE TABLE MovieMaker (
ID varchar(50),
name varchar(50),
address varchar(50),
PRIMARY KEY (ID)
);
CREATE TABLE Studio (
name varchar(50),
address varchar(50),
presidentID varchar(50),
PRIMARY KEY (name),
FOREIGN KEY (presidentID) REFERENCES MovieMaker(ID)
);
However, I get quite a few errors stating that the syntax for my foreign keys are off. Any chance someone could lend a helping hand?
Tables referencing other tables via foreign keys may only be defined after the referenced tables already exist. You must therefore reorder your tables so that those which are referenced in FOREIGN KEY constraints get created first.
Additionally, the foreign keys referencing Movies.year and Movies.title are going to fail because you do not have indices defined on those columns individually. You do have them as a composite PRIMARY KEY, but they need their own indices.
/* MovieStar, MovieMaker are referenced by other tables
but have no FKs of their own, so create them first */
CREATE TABLE MovieStar (
name varchar(50),
birthdate int,
address varchar(50),
gender varchar(50),
PRIMARY KEY (name)
);
CREATE TABLE MovieMaker (
ID varchar(50),
name varchar(50),
address varchar(50),
PRIMARY KEY (ID)
);
/* Studio can be created next, referencing only MovieMaker */
CREATE TABLE Studio (
name varchar(50),
address varchar(50),
presidentID varchar(50),
PRIMARY KEY (name),
FOREIGN KEY (presidentID) REFERENCES MovieMaker(ID)
);
/* Movies references 2 of the above */
CREATE TABLE Movies (
title varchar(50),
year int,
length int,
genre varchar(50),
studioName varchar(50),
producerID varchar(50),
/* A comma was missing here... */
PRIMARY KEY (title,year),
FOREIGN KEY (studioName) REFERENCES Studio(name),
FOREIGN KEY (producerID) REFERENCES MovieMaker(ID),
/* Add individual indices on Movies. Omitting this would result in errno 150 */
INDEX (year),
INDEX (title)
);
CREATE TABLE StarsIn (
movieTitle varchar(50),
movieYear int,
starName varchar(50),
PRIMARY KEY (movieTitle,movieYear,starName),
FOREIGN KEY (movieTitle) REFERENCES Movies(title),
FOREIGN KEY (movieYear) REFERENCES Movies(year),
FOREIGN KEY (starName) REFERENCES MovieStar(name)
);
Here is a demo of the whole thing building properly: http://sqlfiddle.com/#!2/eaf70c
Related
How to create foreignkey constraints in this scenario?
One asterisk- primary key
Two asterisks- foreign key
Tutor table:
**tutorId
firstname
surname
telephoneNo
qualification
employmentDate
introducedBy*
Course table:
**courseCode
courseName
lengthHours
tuitonFee
tutorId*
roomNo*
This is well documented here:
https://www.w3schools.com/sql/sql_foreignkey.asp
I would recommend renaming your columns. Camelcase is one thing, but it is better to either use PascalCase or underscore as separator. Do note that firstname is two words, which should follow your standard. You've also not stated what the reference table is for roomNo or introducedBy, so you will have to edit my code with that information. If they are indeed supposed to be keys, they should probably have ID in the column name.
Fiddle:
https://www.db-fiddle.com/f/6EKuZ7DcCRsAvmrLGYcJrx/1
CREATE TABLE tutor (
tutorId INT NOT NULL,
firstname varchar(50),
surname varchar(50),
telephoneNo varchar(50),
qualification varchar(50),
employmentDate datetime,
introducedBy int,
PRIMARY KEY (tutorId)
/* FOREIGN KEY (introducedBy) REFERENCES ????? */
) ENGINE=INNODB;
CREATE TABLE courseCode (
courceCode int,
courceName varchar(50),
tutorId INT,
lenghtHours INT,
tuitonFee INT,
roomNo varchar(50),
INDEX par_ind (tutorId),
PRIMARY KEY (courceCode),
/* FOREIGN KEY (roomNo) REFERENCES ????? */
FOREIGN KEY (tutorId) REFERENCES tutor(tutorId)
ON DELETE CASCADE
) ENGINE=INNODB;
Hope somone can help me.
Session Table is here:
CREATE TABLE User (
UID int,
Name varchar(40),
Vorname varchar(15),
Titel varchar(40),
Geschlecht int,
EMailAdr varchar(40),
SHID int,
Guthaben int ,
PRIMARY KEY (UID),
FOREIGN KEY (SHID) REFERENCES Schule(SHID),
UNIQUE (EMailAdr)
);
CREATE TABLE Schule (
SHID int,
Name varchar(40),
Adr varchar(80),
PRIMARY KEY (SHID),
UNIQUE (Adr),
UNIQUE (Name)
);
CREATE TABLE Lokal (
LID int,
Name varchar(40),
Adr varchar(40),
KontoNr varchar(40),
PRIMAYR KEY (LID),
UNIQUE (Adr),
UNIQUE (Name)
);
CREATE TABLE Zahlungsarten (
ZID int,
Bezeichnung varchar(40),
PRIMARY KEY (ZID)
);
CREATE TABLE BestellteArtikel (
ArID int,
BID int,
Anzahl int,
PRIMARY KEY (ArID,BID)
);
CREATE TABLE Status (
SID int,
Bezeichnung varchar(40),
PRIMARY KEY (SID)
);
CREATE TABLE Artikel (
ArID int,
Bezeichnuhg varchar(40),
Beschreibung varchar(120),
Preis int,
LID int,
Liefervolumen int,
PRIMARY KEY (ArID),
UNIQUE (Bezeichnung)
);
CREATE TABLE Bestellungen (
BID int,
UID int,
Datum Date,
ZID int,
SID int,
SHID int,
LID int,
PRIMARY KEY (BID),
FOREIGN KEY (UID) REFERENCES User(UID),
FOREIGN KEY (ZID) REFERENCES Zahkungsarten(ZID),
FOREIGN KEY (SID) REFERENCES Status(SID),
FOREIGN KEY (SHID) REFERENCES Schule(SHID),
FOERIGN KEY (LID) REFERENCES Lokal(LID)
);
There are several problems.
In table User:
You're referencing the table Schule before you have created the latter table. The table must exist before you can declare a foreign key that references it. So this works if you create Schule before you create User (I tested it, and it works).
In table Lokal:
PRIMAYR KEY (LID),
You mistyped PRIMARY.
In table Artikel:
Bezeichnuhg varchar(40),
I think you mistyped Bezeichnung.
In table Bestellungen:
FOERIGN KEY (LID) REFERENCES Lokal(LID)
You mistyped FOREIGN.
With all due respect, you should have been able to correct these mistakes yourself without posting a question to Stack Overflow. Most of them are obvious spelling errors.
CREATE TABLE Departament
(d_nume varchar(30), d_nr int, manager varchar(30), PRIMARY KEY(d_nr),
FOREIGN KEY (manager) REFERENCES Angajat(nume));
CREATE TABLE Angajat
(nume varchar(30), prenume varchar(30), cnp char(13), d_nr int, PRIMARY KEY(cnp),
FOREIGN KEY(d_nr) REFERENCES Departament(d_nr));
"Cannot add foreign key constraint"
I use this editor online http://sqlfiddle.com/
When two tables reference each other, you can use alter table:
CREATE TABLE Departament (
d_nr int,
d_nume varchar(30),
manager char(13),
PRIMARY KEY (d_nr)
);
CREATE TABLE Angajat (
cnp char(13),
nume varchar(30),
prenume varchar(30),
d_nr int,
PRIMARY KEY(cnp),
FOREIGN KEY(d_nr) REFERENCES Departament(d_nr)
);
ALTER TABLE Departament ADD CONSTRAINT FOREIGN KEY (manager) REFERENCES Angajat(cnp);
Foreign key references should also be to the primary key, which I normally make the first column in a table. Here is a SQL Fiddle.
I have to create a table how's structure is
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key nomespec references specializzazione(nomeospe)
);
of course I've already create the tables
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
and
create table specializzazione
(
nomespe varchar (20) primary key
);
of course it doesn't work and I don't know what to do, can someone tell me how to create several differentsforeign key?
There's a few obvious things we can point out here. The column name given in the foreign key definition:
references specializzazione(nomeospe)
^
Doesn't match the column name in the table definition
... specializzazione ( nomespe ...
^^^^^^^
And the column list for the foreign key should be enclosed in parens
... foreign key (nomespec)
^ ^
You have some literal error and incorect order creating tables please check this:
create table specializzazione
(
nomespe varchar (20) primary key
);
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key (nomespec) references specializzazione(nomespe)
);
Sql wont let me create these table because of a syntax error can someone help me.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
User_password char(15),
primary key (id),
foreign key (intrest_id) references intrest(id)
);
create table Intrests(
id INT,
description char(30),
Primary key (id),
foreign key (users_id) references users(id)
);
create table User_intrest(
foreign key (users_id) references users(id),
foreign key (intrest_id) references intrest(id)
);
create table friendships(
User_1_id INT,
User_2_id INT,
description Char(20),
foreign key (users_id) references users(id)
);
create table Intrests( id INT, description char(30),
Primary key (id),
foreign key (users_id) references users(id) );
create table User_intrest( foreign key (users_id) references users(id),
foreign key (intrest_id) references intrest(id) );
For interests table, where is the column users_id defined?
Column definitions for user_interest table?
You have a cyclical FK relationship:
Users has Interests(id) as a FK, and Interests has user(id) as a FK.
You don't need EITHER of these since you have a user_intrest table to link them!
You also have several typos with table names, like intrests and intrest.
Also, you should make both your User fields in Friendships be FK to Users, I'm not sure why you want a third, unrelated Users_Id field.
The first (of many) syntax errors you have is in
create table users(
...
primary key (id),
foreign key (intrest_id) references intrest(id) <--- there is no table intrest
);
I would suggest:
But STOP! Don't just copy-paste. Try yourself to see why MySQL gives you that error message: Key column 'intrest_id' doesn't exist in table, isn't the error you get?
Try to first fix that error. What do you have to do? Add an intrest_id field in table users, not just declare it as FOREIGN KEY. (It's not a useful field to have in the end, but do it anyway).
Then rerun your query and try to fix next error. One by one. If you realy get stuck somewhere, well, you know a site to ask questions :)
So, try to fix errors one by one, until you have a script that is running without any errors at all. You'll have learned much more than simply copying and pasting any answer.
CREATE TABLE Users(
id INT,
Fname char(15),
Lname char(15),
email char(50), <-- 20 is too small for emails
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone char(20), <-- phone should be char, not INT
user_password char(15),
PRIMARY KEY (id)
); <-- You don't really need a foreign key to Interests,
<-- that's why you have the User_interest "middle" table
CREATE TABLE Interests(
id INT,
description char(30),
PRIMARY KEY (id)
); <-- You don't really need a foreign key to Users,
<-- for the same reasons.
CREATE TABLE User_interest(
user_id INT, <-- These foreign keys are
interest_id INT, <-- good, but you need to
<-- declare them as fields, too
PRIMARY KEY (user_id, interest_id), <-- It's good if all tables have PK
FOREIGN KEY (user_id) REFERENCES Users(id),
FOREIGN KEY (interest_id) REFERENCES Interests(id)
);
CREATE TABLE Friendships(
user_1_id INT,
user_2_id INT,
description char(20),
PRIMARY KEY (user_1_id, user_2_id),
FOREIGN KEY (user_1_id) REFERENCES Users(id),
FOREIGN KEY (user_2_id) REFERENCES Users(id)
);