Foreign key in MySql as composite primary key - mysql

I have the database with the name Shop with this 3 tables:
create table usr(
id_usr varchar(20) not null,
primary key(id_usr)
);
create table product(
id_product varchar(20) not null,
id_size varchar(20) not null,
price float(4,2) unsigned,
primary key(id_product,id_size)
);
create table cart(
myUser varchar(20),
mySize varchar(20),
product varchar(20),
qty int not null,
primary key(myUser,product,mySize),
FOREIGN KEY (myUser) REFERENCES usr (id_usr),
FOREIGN KEY (product) REFERENCES product (id_product),
FOREIGN KEY (mySize) REFERENCES product (id_size)
);
when I compile in sql, it gives to me this message:
1005 - Can't create table 'Shop.cart' (errno: 150)
If I try to delete the foreign key mySize (FOREIGN KEY (mySize) REFERENCES prodotto (id_size))
it works, why have I this message?

You're making a FK reference to product table but defining only part of the key. Try...
FOREIGN KEY (product, mySize) REFERENCES product (id_product, id_size),

My guess is you haven't created your prodotto table yet. This works:
create table user(
id_user varchar(20) not null,
primary key(id_user)
);
create table product(
id_product varchar(20) not null,
id_size varchar(20) not null,
price float(4,2) unsigned,
primary key(id_product,id_size)
);
create table prodotto (
id_size varchar(20) primary key
);
create table cart(
myUser varchar(20),
mySize varchar(20),
product varchar(20),
qty int not null,
primary key(myUser,product,mySize),
FOREIGN KEY (myUser) REFERENCES user (id_user),
FOREIGN KEY (product) REFERENCES product (id_product),
FOREIGN KEY (mySize) REFERENCES prodotto (id_size)
);
SQL Fiddle Demo

Related

#1005 - Can't create table `musicplayer`.`Albums` (errno: 150 "Foreign key constraint is incorrectly formed")

I'm trying to define some tables with SQL, but when I execute it I have an error. Below there is my code and the error which I receive.
CREATE TABLE Artists
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
description VARCHAR(255)
);
CREATE TABLE Albums
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
releasedate DATE,
artistid INT,
genreid INT,
picture VARCHAR(255),
CONSTRAINT `fk_albums_artists`
FOREIGN KEY (artistid) REFERENCES Artists(id),
CONSTRAINT `fk_albums_genres`
FOREIGN KEY (genreid) REFERENCES Genres(id)
);
CREATE TABLE Tracks
(
id INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
playtime INT NOT NULL,
albumid INT,
CONSTRAINT `fk_tracks_album`
FOREIGN KEY (albumid) REFERENCES Albums(id)
);
CREATE table Genres
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL
);
Error in my SQL query:
MySQL said: Documentation
1005 - Can't create table musicplayer.Albums (errno: 150 "Foreign
key
constraint is incorrectly formed") (Details…)
Declare the tables in order, so the tables are defined before they are referenced:
CREATE TABLE Artists (
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
description varchar(255)
);
CREATE table Genres(
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null
);
CREATE TABLE Albums (
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
releasedate date,
artistid int,
genreid int,
picture varchar(255),
CONSTRAINT `fk_albums_artists` FOREIGN KEY (artistid) REFERENCES Artists(id),
CONSTRAINT `fk_albums_genres` FOREIGN KEY (genreid) REFERENCES Genres(id)
);
CREATE TABLE Tracks(
id int(11) AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
playtime int not null,
albumid int,
CONSTRAINT `fk_tracks_album` FOREIGN KEY (albumid) REFERENCES Albums(id)
);
Here is a db<>fiddle.
You can still have tables that reference each other. However, you will need to declare such foreign key constraints using ALTER TABLE rather than in the CREATE TABLE statement. However, you do not need to do this for these table definitions.

How to make UserID and ProductID columns as the foreign keys in Downloads Table?

This is what I have done so far !!
Create Database MyWebDB;
Create Table Users
( UserID Int Primary Key,
EmailAddress Varchar(50) Not Null,
FirstName Varchar(50) Not Null,
LastName Varchar(50) Not Null)
Create Table Downloads
( DownloadID int Primary Key,
DownloadDate SmallDatetime Null,
FileNames Varchar(100) Not Null,
Foreign Key (UserID) References Users(UserID),
Foreign Key (ProductID) References Products(ProductID))
Create Table Products
(ProductID int primary key, ProductName Varchar(50))
My Sql query
You have to set data type for Download table UserID and ProductID
Userid int FOREIGN KEY REFERENCES Users(UserID)
Productid int FOREIGN KEY REFERENCES Products(ProductID)
or
Userid int,
Productid int,
FOREIGN KEY (Userid) REFERENCES Users(UserID),
FOREIGN KEY (Productid) REFERENCES Products(ProductID))
Like :
Create Table Downloads
( DownloadID int Primary Key,
DownloadDate SmallDatetime Null,
FileNames Varchar(100) Not Null,
Userid int,
Productid int,
FOREIGN KEY (Userid) REFERENCES Users(UserID),
FOREIGN KEY (Productid) REFERENCES Products(ProductID))

Can't add foreign key constraint, (#1215 - Cannot add foreign key constraint)

I'm trying to create a database, i have one table in my database and want to add the other one.
The table i have:
create table department(
dept_name varchar(20),
building varchar(20) not null,
budget numeric(8,2),
primary key (dept_name)
)
and i want to add this table:
create table student (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department
)
But i get this error:
#1215 - Cannot add foreign key constraint
Can you guys help me with this problem?
Thanks.
Your 2nd table should be-
create table student (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department(dept_name)
)
Note: It will be better include dept_id as int in your parent table and create reference it in 2nd table to get better performance.
create tables as per below-
create table department(
dept_id int auto_increment,
dept_name varchar(20),
building varchar(20) not null,
budget numeric(8,2),
primary key (dept_id)
);
create table student (
ID varchar(5),
name varchar(20) not null,
dept_it int,
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_id) references department(dept_id)
)

MySQL intermediary table error code 1005 (150) can't create foreign key

I have two tables which i want to join with an intermediary table for a many to many relationship
the first table is booking
CREATE TABLE booking (
bookingID INT NOT NULL AUTO_INCREMENT,
customerID INT,
runID INT,
startDate DATE,
endDate DATE,
dateBookedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (bookingID),
INDEX idx_start (startDate),
INDEX idx_end (endDate),
FOREIGN KEY (runID) REFERENCES Run(RunID),
FOREIGN KEY (customerID) REFERENCES customer(CustomerID));
the second is dog
CREATE TABLE dog(
DogID int(6) NOT NULL,
DogName varchar(15),
medicalID int (6),
Gender character(1) check(gender in ('m', 'f')),
Age int(2),
Breed varchar(15),
size character (1) check(size in ('s', 'm', 'l')),
primary key (DogID));
and the intermediary table is as follows which gives an error 1005(150) there is something wrong with the foreign key for bookingID. But can't see the problem. any help is welcomed.
CREATE TABLE isBooked(
BookingID int not null,
DogID int not null,
foreign key (DogID) references Dog(dogID),
foreign key (BookingID) references Booking(BookingID));

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)
);