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)
);
Related
I want to drop the primary key in father. So I set the foreign key in child to delete cascade. Why this doesnt work ? Do I need to delete the foreign key first ? If so, then what the propurse of the delete on cascade statement ?
create database table_test;
use table_test;
create table father(
cod int,
primary key (cod)
);
create table child(
cod int,
cod_father int,
primary key(cod),
constraint fk_cod_father foreign key (cod_father) references father(cod)
on delete cascade
);
alter table father
drop primary key;
ON DELETE CASCADE constraint is used in MySQL to delete the rows from
the child table automatically, when the rows from the parent table are
deleted
You want to remove primary key and in your case first you need to unlink the foreign key reference. So drop foreign key first.
I am building a database for a school project, but for some reason I cannon make a foreign key reference between 2 tables (only those 2). My project has 14 tables and it works fine for all the others.
The tables are made like:
create table degree(
title varchar(50),
idryma varchar(40),
bathmida enum('High School', 'Univercity', 'Master', 'PHD'),
constraint degree_id primary key (title, idryma)
);
create table has_degree(
degree_title varchar(50),
degree_idryma varchar(40),
employee_username varchar(12),
acquisition_year year(4),
grade float(3,1),
constraint has_degree_id primary key (degree_title, degree_idryma, employee_username)
);
And then I try to alter the table so that I make the foreign key connections:
alter table has_degree add foreign key (degree_title) references degree(title);
alter table has_degree add foreign key (degree_idryma) references degree(idryma);
But I keep on getting
Error Code: 1824. Failed to open the referenced table 'degree'
I have tried to make them like that:
create table degree(
title varchar(50),
idryma varchar(40),
bathmida enum('High School', 'Univercity', 'Master', 'PHD'),
constraint degree_id primary key (title, idryma)
);
create table has_degree(
degree_title varchar(50),
degree_idryma varchar(40),
employee_username varchar(12),
acquisition_year year(4),
grade float(3,1),
foreign key (degree_title) references degree(title),
foreign key (degree_idryma) references degree(idryma),
/*employee is an other table that I use and that works just fine*/
foreign key (employee_username) references employee(employee_username),
constraint has_degree_id primary key (degree_title, degree_idryma, employee_username)
);
But the only thing that changes is that I get
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'has_degree_ibfk_2' in the referenced table 'degree'
The columns in your foreign key in table has_degree must be the same as the columns in the primary key of the referenced table degree.
In this case, the primary key of degree consists of two varchar columns.
So the foreign key in has_degree that references it must also be only two varchar columns, and values in those columns in has_degree must match exactly the values in a row of degree.
You defined the foreign key this way:
foreign key (degree_title) references degree(title),
foreign key (degree_idryma) references degree(idryma),
But that's two foreign keys, each having a single column. You need one foreign key with two columns:
foreign key (degree_title, degree_idryma) references degree(title, idryma),
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.
Hi I am creating several tables some of which have multiple keys linking to the same table. When I try to create the foreign key links I am given:
errno: 150 "Foreign key constraint is incorrectly formed"
Here are the tables I am creating:
create table EventFeed (
EventFeedID integer auto_increment not null,
EventName varchar(100),
EventTime Timestamp,
EventLocation varchar(100),
primary key (EventFeedID)
);
create table Calendar (
MyEventID integer auto_increment not null,
EventFeedID integer,
EventName varchar(100),
EventTime Timestamp,
EventLocation varchar(100),
AttendeeID varchar(100),
primary key (MyEventID),
foreign key (EventFeedID) references EventFeed (EventFeedID),
foreign key (EventName) references EventFeed (EventName),
foreign key (EventTime) references EventFeed (EventTime),
foreign key (EventLocation) references EventFeed (EventLocation),
foreign key (AttendeeID) references Users (UserID)
);
The error seems to be coming from the foreign keys in the 'Calendar' table however some of my other tables have used similar foreign keys and given no errors
can anyone advise what I am doing wrong?
Any help is much appreciated
You cannot have a foreign key on the column which is a non-primary key except the columns with unique constraint. So you have to make EventName, EventTime, etc unique if you want it to be referred by a foreign key.
You can check here at: Technet Microsoft
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
You can see this fiddle for working example: http://sqlfiddle.com/#!9/05663
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.