Error when inserting multiple MySQL foreign keys - mysql

When I try to insert the current table into my table in SQL I get an error (Products table):
CREATE TABLE parent(
Barcode INT(9),
PRIMARY KEY (Barcode)
) ENGINE=INNODB;
CREATE TABLE SuppliedBy(
Onr CHAR(10),
OrgNR INT(10),
Date DATE NOT NULL,
PRIMARY KEY (Onr),
FOREIGN KEY (OrgNR) REFERENCES Supplier(OrgNR)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=INNODB;
CREATE TABLE Products(
Onr CHAR(10),
Barcode INT(9),
Quantity INT(10) DEFAULT 0
CHECK (Quantity >= 0),
PRIMARY KEY (Onr, Barcode),
FOREIGN KEY (Onr) REFERENCES SuppliedBy(SSN)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Barcode) REFERENCES parent(Barcode)
ON DELETE CASCADE
ON UPDATE CASCADE
)ENGINE=INNODB;
I get the following message:
#1005 - Can't create table '.\db_project\#sql-b58_6d.frm' (errno: 150)
I'm sure it has to do with the several foreign keys in this relation, I searched around on the internet, but can't find the solution.

There is no column SuppliedBy.SSN.
FOREIGN KEY (Onr) REFERENCES SuppliedBy(SSN)
Perhaps you meant
FOREIGN KEY (Onr) REFERENCES SuppliedBy(Onr)
ON DELETE CASCADE
ON UPDATE CASCADE,

I believe the issue is likely that one of the tables you're defining the FOREIGN KEYS to point to does not have an index foreign key field you're pointing to.
For FOREIGN KEY's to work, the field you're pointing to needs to have an index on it.
See Mysql. Can't create table errno 150
Also, check that you meet all the criteria for creating the key. The columns in both tables must:
Be the same datatype
Be the same size or length
Have indexes defined.

Related

FOREIGN KEY (`shiftid`, `groupid`, `sectionid`) REFERENCES `tbl_academic`(`id`, `id`, `id`) is not working in mysql database

ALTER TABLE `tbl_acc_payable` ADD CONSTRAINT `FK_APAY_SGS` FOREIGN KEY (`shiftid`, `groupid`, `sectionid`) REFERENCES `tbl_academic`(`id`, `id`, `id`) ON DELETE SET NULL ON UPDATE CASCADE
why I cannot apply multiple columns references another table multiple column in MySQL database?
return Error Message:
#1005 - Can't create table fastpay.tbl_acc_payable (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
A composite foreign key should reference a composite primary key. As you can't create a primary key on triple ID column, you should create 3 foreign keys, each of them referencing the same primary key (on the tbl_academic.id column).
FK references used the whole expression value or its prefix. The value - not the column name.
You need to reference to the values in 3 different rows which cannot be provided by composite index. So create 3 separate foreign keys referred to the same index tbl_academic(id):
ALTER TABLE tbl_acc_payable
ADD CONSTRAINT FK_APAY_SGS_1
FOREIGN KEY (shiftid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_2
FOREIGN KEY (groupid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_3
FOREIGN KEY (sectionid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE;

Foreign Key on MySQL

I am new to SQL and I started building my own project. I am having issues creating a foreign key on my second table. . Please let me know what I am missing here.
The second CREATE TABLE statement should be:
CREATE TABLE entry (
issuer_id INT AUTO_INCREMENT PRIMARY KEY,
issuer_name VARCHAR(20) NOT NULL,
fine INT,
book_id INT,
due_date DATE,
FOREIGN KEY (book_id)
REFERENCES book_table (book_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (due_date)
REFERENCES book_table (due_date)
ON DELETE CASCADE
ON UPDATE CASCADE
);
A foreign key is a column or set of columns in one table that links to a unique key (typically the primary key) in another table. The columns must exist in both tables. They must match in type and the order within the key declarations. They must constitute a unique key in the foreign table.

I keep getting "foreign key references invalid column?

I'm referencing the primary key of the table, which is also a foreign key. The exact error I'm getting is this:
"Foreign key 'customer_username' references invalid column 'customer_username' in referencing table 'CustomerAddstoCartProduct'."
create table Customers(
username varchar(20) PRIMARY KEY,
points int
FOREIGN KEY(username) REFERENCES Users ON DELETE CASCADE ON UPDATE CASCADE
)
create table CustomerAddstoCartProduct (
serial_no int,
customer_name varchar (20)
PRIMARY KEY(serial_no, customer_name)
FOREIGN KEY(serial_no) REFERENCES Products ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(customer_username) REFERENCES Customers ON DELETE CASCADE ON UPDATE CASCADE
)
All of your foreign keys declarations are malformed, they are missing the referred column in the target table.
Eg, in table CustomerAddstoCartProduct, you should have:
FOREIGN KEY(customer_username) REFERENCES Customers(username)
ON DELETE CASCADE ON UPDATE CASCADE
Instead of:
FOREIGN KEY(customer_username) REFERENCES Customers
ON DELETE CASCADE ON UPDATE CASCADE
Another thing that is akward is that, in table Customers, your primary key column has a foreign key constraint. While this might work, this probably indicates a design issue. If you have a 1:1 relationship between Customers and Users, you should probably be storing all columns in the same table.
Include the column being referenced and use the correct column name:
create table CustomerAddstoCartProduct (
serial_no int,
customer_name varchar(20)
PRIMARY KEY(serial_no, customer_name)
FOREIGN KEY(serial_no) REFERENCES Products (serial_no) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (customer_name) REFERENCES Customers (username) ON DELETE CASCADE ON UPDATE CASCADE
);
Here is a db<>fiddle -- with the extraneous table references commented out.

MySQL failing to create a new table using Foreign Keys

I only get an error 150 when I run this. The other tables are InnoDB, the types are primary key, and the data types match. So I can't see what I'm doing wrong, (and I see no obvious syntax errors). Any ideas?
CREATE TABLE grouptosite (
groups_id BIGINT(20),
usertogroup_groupID BIGINT(20),
usertogroup_userID BIGINT(20),
usertosite_id INT(10),
gts_id INT(10) AUTO_INCREMENT NOT NULL,
PRIMARY KEY (gts_id),
index (gts_id),
index (groups_id),
index (usertogroup_groupID),
index (usertogroup_userID),
index (usertosite_id),
FOREIGN KEY (groups_id)
REFERENCES groups(id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (usertogroup_groupID, usertogroup_userID)
REFERENCES usertogroup(groupID, userID)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (usertosite_id)
REFERENCES usertosite(id)
ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=INNODB;
Are you sure your columns are the same type? I first created the usertosite table with the id of BIGINT (following the same pattern of the other tables), but the grouptosite.usertosite_id column was INT: http://sqlfiddle.com/#!2/d821a.
As mentioned in another comment, datatypes for foreign keys need to be the same.

Cannot add foreign key constraint on a date field

I have the following SQL statements:
CREATE TABLE patient(
Name varchar(255),
Geburtsdatum date,
CONSTRAINT pk_patient PRIMARY KEY (Name,Geburtsdatum)
);
CREATE TABLE fake(
Name varchar(255),
PName varchar(255),
PGeburtsdatum date,
CONSTRAINT pk_fake PRIMARY KEY (Name,PName,PGeburtsdatum),
CONSTRAINT fk_PName2 FOREIGN KEY (PName) REFERENCES patient(Name) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_PGeburtsdatum FOREIGN KEY (PGeburtsdatum) REFERENCES patient(Geburtsdatum) ON DELETE CASCADE ON UPDATE CASCADE
);
This gives me the error "#1215 - Cannot add foreign key constraint". If I remove the last constraint in the second table creation everything works. All my other foreign key constraints work exactly the same way. What am I missing here?
Not sure why you tagged a mysql question as db2. Anyway, the MySQL documentation states:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan
So add an index in the Geburtsdatum column:
CREATE TABLE patient(
Name varchar(255),
Geburtsdatum date,
INDEX (Geburtsdatum),
CONSTRAINT pk_patient PRIMARY KEY (Name,Geburtsdatum)
);