Foreign key constraint are are incompatible - mysql

I have a problem add FOREIGN KEY
CREATE TABLE Persons (
ID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
);
CREATE TABLE users (
userID int NOT NULL,
username varchar(20) NOT NULL,
password varchar(20) NOT NULL,
personID int NOT NULL,
PRIMARY KEY (userID),
FOREIGN KEY (personID) REFERENCES Persons(ID)
);
The error show:
MySQL said: Documentation
#3780 - Referencing column 'personID' and referenced column 'ID' in foreign key constraint 'users_ibfk_1' are incompatible.

Why not use the same name as in the persons table: ID instead of
personID
i.e
Change the column personID in Table users to ID:
personID ==> ID
Then try again to assign the foreign key

Related

can composite primary key be referenced as a foreign key?

I have the following table
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);
I want to add a foreign key to the following table
create table License(
LicenseID int not null,
PersonID int not null,
issue_date date,
primary key (LicenseID)
);
how should I reference it ?!
Any foreign key must have columns to match all the columns of the primary key it references.
If your parent table has a compound primary key with two columns, an int and a varchar, then the foreign key that references it must also have two columns, an int and a varchar, in the same order.

i am trying to create foreign keys but i got error 1822 .. please see my code below

CREATE TABLE employee(
empid int auto_increment primary key,
empfirstname varchar(200) not null,
emplastname varchar(200) not null,
email varchar(200) not null,
officenumber int not null
);
CREATE TABLE customer(
custid int auto_increment primary key,
firstname varchar(200) not null,
lastname varchar(200) not null,
address varchar(200) not null,
contact varchar(200)
);
CREATE TABLE product(
productid int auto_increment primary key,
productdesc varchar(500) not null,
weight int not null,
unit_cost int not null
);
CREATE TABLE productorder(
productid int,
orderid int,
primary key(productid,orderid),
constraint fk3 foreign key (productid) references product(productid),
constraint fk4 foreign key (orderid) references productorder(orderid)
);
CREATE TABLE salesorder(
salesorderid int auto_increment primary key,
empid int not null,
custid int not null,
orderdate date not null,
shippingmethod varchar (200) not null,
constraint a_fk1 foreign key (empid) references employee(empid),
constraint a_fk2 foreign key (custid) references customer(custid)
);
What is this meant to do?:
constraint fk4 foreign key (orderid) references productorder(orderid)
It's not uncommon for a table to have a foreign key back to its own primary key, such as for records which have a parent/child relationship. But that doesn't seem to be the case here.
More to the point of the error though, this isn't referencing the entire primary key for the target table. That key has two fields:
primary key(productid,orderid)
So the DBMS can't create the foreign key because its structure doesn't match the target primary key.
If you want to create that foreign key, it would need to match. Probably something like this:
constraint fk4 foreign key (productid,orderid) references productorder(productid,orderid)
But it doesn't appear that you need that foreign key at all, because it doesn't seem to make sense in your data model. Instead I suspect orderid might need to be autoincrement and just use the productid foreign key. Something like this:
CREATE TABLE productorder(
orderid int auto_increment primary key,
productid int,
constraint fk3 foreign key (productid) references product(productid)
);
(Note that there could be more changes you'd want to make to your data model. This answer doesn't purport to provide you with a complete production-ready data model, just to correct the error. Your data model is likely to change/evolve as you develop your system.)
Its normal,
The foreign key in table **productorder**
is refereing to the table itself:
constraint fk4 foreign key (orderid) references **productorder**(orderid)
In order to achieve the self-referencing constraint on the table productorder, you need to add another column with the same type and make typical referencing.
For instance :
Create table productorder (
productid int,
orderid int,
orderid_parent int,
primary key(productid,orderid),
constraint fk_self foreign key(orderid) references productorder(orderid_parent)
)

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

mysql error 1215(hy000) cannot add foreign key constraint

I keep getting this error, I used InnoDB for all tables, menuID, and customerID, are primary keys in their respective tables, and the datatypes appear to be the same.
ERROR 1215 (HY000): Cannot add foreign key constraint
Sorry if I am missing something simple, I am new to mySQL.
create table customers(
customerID int not null auto_increment primary key,
LastName varchar(255) not null,
FirstName varchar(255) not null,
email varchar(255) not null,
password varchar(255),
phone varchar(255),
creditCard varchar(255),
address varchar(255),
time timestamp)
Engine=InnoDB;
create table menu(
menuID int not null auto_increment primary key,
typeID int,
itemName varchar(255),
price varchar(255))
Engine=InnoDB;
create table orders(
orderID int not null,
customerID int not null,
menuID int not null,
PRIMARY KEY (orderID, customerID, menuID),
FOREIGN KEY (customerID) REFERENCES customers(customerID) on delete set null on update cascade,
foreign key (menuID) references menu(menuID) on delete set null on update cascade )
Engine=InnoDB;
You can't use ON DELETE SET NULL for a foreign key column that you declared NOT NULL.
See this answer for a long checklist of things to check as possible causes of foreign key errors: MySQL Creating tables with Foreign Keys giving errno: 150

Foreign key in MySql as composite primary key

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