Cannot add foreign key here? - mysql

So I'm trying to create tables and I can't for the life of me understand why i keep getting and error saying "Cannot add foreign key to constraint".
The types are the same, the parent is a primary key, and they're NULLness is the same.
The problem is in the line in the create table for CDSingers where it says:
foreign key (track_num) references CDTracks (track_num),
(it's near the end)
It's the only table that won't be created and it's because of this line.
Please help.
(some of the other tables have been ommitted since they aren't connected)
create table CD
(
num int NOT NULL,
producer varchar(100) NOT NULL,
cd_number varchar(100) NOT NULL,
title varchar(100) NOT NULL,
type varchar(100) ,
band_name varchar(100) ,
production_date DATE NOT NULL,
price double CHECK (price >= 0),
foreign key (type) references MusicType (type),
foreign key (band_name) references Band (band_name),
primary key (num),
unique (producer, cd_number)
);
create table CDTracks
(
num int NOT NULL,
track_num int NOT NULL,
song_name varchar(100) NOT NULL,
minute int NOT NULL,
foreign key (num) references CD (num),
primary key (num, track_num)
);
create table Singer
(
id int NOT NULL,
singer_firstname varchar(100) NOT NULL,
singer_lastname varchar(100) NOT NULL,
primary key (id)
);
create table CDSingers
(
num int NOT NULL,
track_num int NOT NULL,
singer_id int NOT NULL,
foreign key (num) references CDTracks (num),
foreign key (track_num) references CDTracks (track_num),
foreign key (singer_id) references Singer (id),
primary key (num, track_num, singer_id)
);

You declare composite foreign keys like this:
foreign key (num, track_num) references CDTracks (num, track_num),

Related

Bug in sytaxis in MysqlServer : the foreign key refers to an invalid table

I spent hours to find the bug on my script. MysqlServer shows me the error: the foreign key refers to an invalid table. Some help? Thanks
--SCRIPT_MER_MOCHILAS ENTIDADES FUERTES:MODELO, DUEÑO,MOCHILA, MATERIAL, DISEÑO, COLOR, TIPO_MOCHILA, MARCA.ENTIDADES DEBILES: DU_MO, CO_DI, MA_DI --
CREATE TABLE TAB_DUENO
(
ID_DUENO INT PRIMARY KEY IDENTITY (1,1),
DUENO VARCHAR(200) NOT NULL,
NOMBRE VARCHAR(200) NOT NULL,
APELLIDOP VARCHAR(200) NOT NULL,
APELLIDOM VARCHAR(200) NOT NULL
)
CREATE TABLE CAT_MODELO
(
ID_MODELO INT PRIMARY KEY IDENTITY (1,1),
MODELO VARCHAR(200) NOT NULL,
)
CREATE TABLE TAB_MOCHILA
(
ID_MOCHILA INT PRIMARY KEY IDENTITY (1,1),
MOCHILA VARCHAR(200) NOT NULL,
CONSTRAINT FK_MODELO_MOCHILA
FOREIGN KEY (ID_MODELO)REFERENCES CAT_MODELO(ID_MODELO),
CONSTRAINT FK_DISENO_MOCHILA
FOREIGN KEY (ID_DISEÑO)REFERENCES CAT_DISENO(ID_DISENO),
CONSTRAINT FK_TIPOMOCHILA_MOCHILA
FOREIGN KEY (ID_TIPOMOCHILA)REFERENCES CAT_TIPOMOCHILA(ID_TIPOMOCHILA),
CONSTRAINT FK_MARCA_MOCHILA
FOREIGN KEY (ID_MARCA)REFERENCES CAT_MARCA(ID_MARCA),
)
CREATE TABLE CAT_MATERIAL
(
ID_MATERIAL INT PRIMARY KEY IDENTITY (1,1),
MATERIAL VARCHAR (200) NOT NULL
)
CREATE TABLE CAT_COLOR
(
ID_COLOR INT PRIMARY KEY IDENTITY (1,1),
COLOR VARCHAR (200) NOT NULL
)
CREATE TABLE CAT_TIPOMOCHILA
(
ID_TIPOMOCHILA INT PRIMARY KEY IDENTITY (1,1),
TIPOMOCHILA VARCHAR(200) NOT NULL,
)
CREATE TABLE CAT_DISEÑO
(
ID_DISEÑO INT PRIMARY KEY IDENTITY (1,1),
DISEÑO VARCHAR(200) NOT NULL,
)
CREATE TABLE CAT_MARCA
(
ID_MARCA INT PRIMARY KEY IDENTITY (1,1),
MARCA VARCHAR(200) NOT NULL,
)
--AHORA VAN LAS ENTIDADES DEBILES//
CREATE TABLE DUENO_MOCHILA
(
ID_DUENOMOCHILA INT PRIMARY KEY IDENTITY (1,1),
DUENOMOCHILA VARCHAR(200) NOT NULL,
CONSTRAINT FK_DUEÑO_DUENOMOCHILA
FOREIGN KEY (ID_DUEÑO)REFERENCES CAT_DUENO(ID_DUENO),
CONSTRAINT FK_MOCHILA_DUENOMOCHILA
FOREIGN KEY (ID_MOCHILA)REFERENCES CAT_MOCHILA(ID_MOCHILA),
)
CREATE TABLE COLORDISENO
(
ID_COLORDISENO INT PRIMARY KEY IDENTITY (1,1),
COLORDISENO VARCHAR(200) NOT NULL,
CONSTRAINT FK_COLOR_COLORDISEÑO
FOREIGN KEY (ID_COLOR)REFERENCES CAT_COLOR(ID_COLOR),
CONSTRAINT FK_DISENO_COLORDISEÑO
FOREIGN KEY (ID_DISENO)REFERENCES CAT_DISENO(ID_DISENO),
)
CREATE TABLE MATERIALDISENO
(
ID_MATERIALDISENO INT PRIMARY KEY IDENTITY (1,1),
MATERIALDISENO VARCHAR(200) NOT NULL,
CONSTRAINT FK_MATERIAL_MATERIALDISENO
FOREIGN KEY (ID_MATERIAL)REFERENCES CAT_MATERIAL(ID_MATERIAL),
CONSTRAINT FK_DISENO_MATERIALDISENO
FOREIGN KEY (ID_DISENO)REFERENCES CAT_DISENO(ID_DISENO),
)
You have not understood foreign keys the syntax is correct but the italicised bit isn't CONSTRAINT FK_MODELO_MOCHILA FOREIGN KEY (ID_MODELO)REFERENCES CAT_MODELO(ID_MODELO) this column should be the name of the field in TAB_MOCHILA that you want to relate to CAT_MODELO(ID_MODELO) .

MySQL error:1251 cannot add foreign key

The first 4 table are created fine, the transactions tables run into problem. I get the 1215 error: cannot add foreign key. I've checked an rechecked the data types, and made sure all FK are PK of their own tables. What's wrong here?
CREATE SCHEMA FinalDB;
CREATE TABLE `User` (
userId int not null auto_increment primary key,
first_name varchar(255) not null,
last_name varchar(255) not null,
address varchar(255) null,
DOB date not null,
availableBalance int not null default 0,
currency varchar(20)
);
CREATE TABLE Verifications(
userId int not null primary key,
passport int null,
ssn int null,
license int null,
constraint
foreign key (userId)
references User(userId)
);
CREATE TABLE Linked_Account(
account_Id int not null,
userId int not null,
routing int null,
swift int null,
primary key (userId, account_Id),
constraint
foreign key (userId)
references User(userId)
);
CREATE TABLE Wallet (
userId int not null,
walletId varchar(5) not null,
coinAmount int not null default 0,
netWorth int not null default 0,
primary key(userId, walletId),
constraint
foreign key (userId)
references `User`(userId)
);
CREATE TABLE Transactions (
transactionId int not null primary key auto_increment,
userId int not null,
type varchar(30) not null,
walletId varchar(5) not null,
payment_method int null, #optional
total int null, #optional
quantity int not null,
fee int null, #optional
`date` date not null,
sender varchar(50) null, #optional
reciever varchar(50) null, #optional
status varchar(20) not null,
notes varchar(200) null, #optional
constraint
foreign key (userId)
references `User`(userId)
ON DELETE CASCADE ON UPDATE CASCADE,
constraint
foreign key (walletId)
references Wallet(walletId)
ON DELETE CASCADE ON UPDATE CASCADE,
constraint
foreign key (payment_method)
references Linked_Account(account_id)
);
CREATE TABLE TransactionsExchange(
transactionId int not null auto_increment primary key,
userId int not null,
currencyFrom int not null,
currencyFromAmount int not null,
currencyInto int not null,
currencyIntoEquivalent int not null,
notes varchar(200) null,
`date` date not null,
constraint
foreign key (userId)
references User(userId),
constraint
foreign key (currencyFrom)
references Wallet(walletId),
constraint
foreign key (currencyInto)
references Wallet(walletId)
);
I've look online for possible answer, but it's usually having to do with inconsistent data types or undeclared PK's. I'm basically trying to make a transactions table to log various different data in different compositions. Using backend logic to handle what is required and what is not, aside from a few defaults.
To use a compound Primary Key as Foreign Key, you'll have to add the
same number of columns (that compose the PK) with same datatypes to
the child table and then use the combination of these columns in the
FOREIGN KEY definition.
see related post here https://stackoverflow.com/a/10566463/4904726
Try this 'Transactions' table creating query:
CREATE TABLE Transactions (
transactionId int not null primary key auto_increment,
userId int not null,
type varchar(30) not null,
walletId varchar(5) not null,
payment_method int null, #optional
total int null, #optional
quantity int not null,
fee int null, #optional
`date` date not null,
sender varchar(50) null, #optional
reciever varchar(50) null, #optional
status varchar(20) not null,
notes varchar(200) null, #optional
constraint
foreign key (userId)
references `User`(userId)
ON DELETE CASCADE ON UPDATE CASCADE,
constraint
foreign key (userId, walletId)
references Wallet(userId, walletId)
ON DELETE CASCADE ON UPDATE CASCADE,
constraint
foreign key (userId, payment_method)
references Linked_Account(userId, account_id)
);

Mysql code runs but the foreign key values are not showing in table in phpmyadmin

My code runs in phpmyadmin but the foreign key values are not showing in table and when I update it the message shows "0 rows affected"
my table schema is:
CREATE TABLE fee_report (
fr_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
school_id INT NOT NULL,
branch_id INT NOT NULL,
student_id INT NOT NULL,
ledger VARCHAR(55),
bill_no VARCHAR(255) NOT NULL,
fdate date NOT NULL,
receipt_no VARCHAR(255) NOT NULL,
dr float NOT NULL,
cr float NOT NULL,
balance float NOT NULL,
particulars VARCHAR(155),
updated TIMESTAMP NOT NULL,
CONSTRAINT fk_frschool FOREIGN KEY (school_id)
REFERENCES school(school_id),
CONSTRAINT fk_frbranch FOREIGN KEY (branch_id)
REFERENCES branch(branch_id),
CONSTRAINT fk_frstudent FOREIGN KEY (student_id)
REFERENCES student(student_id)
) ENGINE = INNODB;
INSERT INTO fee_report(school_id,branch_id,student_id,ledger,bill_no,fdate,dr,balance,particulars,feetype)
VALUES(1,1,13,'TGS007','SESNOV1020',CURDATE(),'2850','-14450','11','Tuition Fee,Transport Fee,Smart Class Fee,SA-1 Exam Fee')
Why foreign keys school_id, branch_id and student_id are not showing in table
and when I remove foreign key constraint the values are visible in table

MySQL error: Cannot add foreign key constraint?

There always be the error:"Cannot add foreign key constraint" when I create my last table.
System: Mac OS X 10.9
DB : MySQL 5.6.14
DBM : Sequel Pro
CREATE TABLE users (
uid INT AUTO_INCREMENT PRIMARY KEY,
uname VARCHAR(20) NOT NULL,
uemail VARCHAR(20) NOT NULL,
ucity VARCHAR(20),
upassw VARCHAR(20) NOT NULL
);
CREATE TABLE songs(
sid INT AUTO_INCREMENT PRIMARY KEY,
sname VARCHAR(20) NOT NULL,
srldate DATE NOT NULL
);
CREATE TABLE albums (
albid INT AUTO_INCREMENT PRIMARY KEY,
albname VARCHAR(20) NOT NULL,
albrldate DATE NOT NULL,
albrltime TIME NOT NULL
);
CREATE TABLE artists (
aid INT AUTO_INCREMENT PRIMARY KEY,
aname VARCHAR(20) NOT NULL
);
CREATE TABLE genres (
gid INT AUTO_INCREMENT PRIMARY KEY,
gname VARCHAR(20) NOT NULL
);
CREATE TABLE playlists (
uid INT NOT NULL,
sid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, sid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(sid) REFERENCES songs(sid)
);
CREATE TABLE u_like_a (
aid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(aid, uid),
FOREIGN KEY(aid) REFERENCES artists(aid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE be_fan (
aid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(aid, uid),
FOREIGN KEY(aid) REFERENCES artists(aid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE follow (
uid INT NOT NULL,
to_uid INT NOT NULL,
PRIMARY KEY(uid, to_uid),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(to_uid) REFERENCES users(uid)
);
CREATE TABLE u_like_g (
gid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(gid, uid),
FOREIGN KEY(gid) REFERENCES genres(gid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE u_share_pl(
uid INT NOT NULL,
from_uid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, from_uid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(from_uid) REFERENCES users(uid),
FOREIGN KEY(plname) REFERENCES playlists(plname),
FOREIGN KEY(plmdate) REFERENCES playlists(plmdate),
FOREIGN KEY(plmtime) REFERENCES playlists(plmtime)
); #####---> This is the last table.
ERROR comes from here. I really don't why.
I have check the type for all attributes. The type and name of attributes have no problem.
But the mysql always say "Cannot add foreign key constraint"
Here is you reference wrong forgien REFERENCES users(from_uid) in last table.
FOREIGN KEY(from_uid) REFERENCES users(from_uid)
from_uid not belong to users
This should be
FOREIGN KEY(from_uid) REFERENCES users(uid)
your playLists table has primary key combination of four columns, so you should supply all these four columns as forieng key in u_share_pl table.
Another composite key as a reference should be a single constraint like
FOREIGN KEY(from_uid,sid,plname,plmdate,plmtime) REFERENCES playlists(uid,sid,plname,plmdate,plmtime)
Your last table Create should be:
CREATE TABLE u_share_pl(
uid INT NOT NULL,
from_uid INT NOT NULL,
sid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, from_uid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(from_uid,sid,plname,plmdate,plmtime) REFERENCES playlists(uid,sid,plname,plmdate,plmtime)
);

Mysql, cannot add foreign key constraint

Why does this:
Create table Kaart (
aantal_geel int not null,
aantal_rood int not null,
Primary key (aantal_geel, aantal_rood));
Create table Wedstrijd (
datum date not null,
aantal_geel int not null,
aantal_rood int not null,
Primary key (datum),
Foreign key (aantal_geel) REFERENCES kaart(aantal_geel),
Foreign key (aantal_rood) REFERENCES kaart(aantal_rood));
Gives: Error Code: 1215. Cannot add foreign key constraint
You need to reference the combination of the key columns, not each key column individually:
Create table Wedstrijd
(
datum date not null,
aantal_geel int not null,
aantal_rood int not null,
Primary key (datum),
Foreign key (aantal_geel,aantal_rood)
REFERENCES kaart(aantal_geel,aantal_rood)
);