I want to make a foreign key from a primary key and got this error '#1005 - Can't create table'
Here is the DDL used...
CREATE TABLE Invoice
(
InvoiceID SMALLINT,
TaskID SMALLINT,
FOREIGN KEY Invoice(InvoiceID) REFERENCES XTABLE(InvoiceID),
PRIMARY KEY (InvoiceID, TaskID)
);
Make sure the key columns in XTABLE and Invoice are marked as the PRIMARY KEY and NOT NULL.
CREATE TABLE XTABLE(InvoiceID SMALLINT NOT NULL, PRIMARY KEY(InvoiceID));
CREATE TABLE Invoice (
InvoiceID SMALLINT NOT NULL,
TaskID SMALLINT NOT NULL,
PRIMARY KEY (InvoiceID, TaskID),
CONSTRAINT FOREIGN KEY Invoice(InvoiceID) REFERENCES XTABLE(InvoiceID)
);
SQL FIDDLE: http://sqlfiddle.com/#!2/5a56f
Related
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)
)
So I'm trying to create a table 'Prerequisite_to', which is basically a relation that shows what classes are considered as a prerequisite for a specific class. Here are my SQL tables defections:
CREATE TABLE Class(
infs CHAR(4) NOT NULL,
course_number CHAR(3) NOT NULL,
PRIMARY KEY (infs,course_number));
CREATE TABLE Prerequisite_to(
infs CHAR(4),
course_number CHAR(3),
PRIMARY KEY (infs,course_number),
FOREIGN KEY (infs) REFERENCES Class(infs),
FOREIGN KEY (course_number) REFERENCES Class(course_number)
)
However, when I execute the script I get this error:
17:29:43 CREATE TABLE Prerequisite_to( infs CHAR(4), course_number CHAR(3), PRIMARY KEY (infs,course_number), FOREIGN KEY (infs) REFERENCES Class(infs), FOREIGN KEY (course_number) REFERENCES Class(course_number) ) Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'prerequisite_to_ibfk_2' in the referenced table 'Class' 0.00038 sec
You have a composite primary key, so you need a composite foreign key:
CREATE TABLE Prerequisite_to(
infs CHAR(4),
course_number CHAR(3),
PRIMARY KEY (infs,course_number),
FOREIGN KEY (infs, course_number) REFERENCES Class(infs, course_number)
);
Just for the record, I'm not a fan of composite primary keys. I also think prerequisites need two course references. So:
CREATE TABLE Classes (
class_id int auto_increment primary key,
infs CHAR(4) NOT NULL,
course_number CHAR(3) NOT NULL,
unique (infs, course_number)
);
CREATE TABLE Prerequisites (
preresequisite_id int auto_increment primary key,
class_id int,
prerequisite_class_id int,
FOREIGN KEY (class_id) REFERENCES Classes(class_id),
FOREIGN KEY (prerequisite_class_id) REFERENCES Classes(class_id)
);
Getting an
Error Code: 1822. Failed to add the foreign key constraint. Missing
index for constraint 'subject_ibfk_1' in the referenced table
'enrolment'
when attempting to create the subject table. The problem is, the error does not occur on the previous table student. The data types are the same, and the primary keys are defined.
This error occurs for both the enrolment and grade tables.
create table enrolment(
stud_id char(9) not null,
subj_code char(8) not null,
semester tinyint unsigned not null,
year smallint unsigned not null,
comment text,
primary key (stud_id, subj_code, semester, year)
);
create table grade(
stud_id char(9) not null,
subj_code char(8) not null,
semester tinyint unsigned not null,
year smallint unsigned not null,
grade tinyint unsigned,
primary key (stud_id, subj_code, semester, year)
);
create table student(
stud_id char(9) not null,
stud_name char(30),
stud_phone char(12),
stud_date_of_birth date,
stud_city char(26),
stud_address char(30),
stud_postcode char(4),
primary key (stud_id),
foreign key (stud_id)
references grade(stud_id),
foreign key (stud_id)
references enrolment(stud_id)
);
create table subject(
subj_code char(8) not null,
subj_title char(40),
primary key (subj_code),
foreign key (subj_code)
references enrolment(subj_code),
foreign key (subj_code)
references grade(subj_code)
);
The problem is due to the fact that the foreign key, subj_code, is part of a multi-column primary key (PK) in the referenced table enrolment:
primary key (stud_id, subj_code, semester, year)
where this column (subj_code) is not the leftmost one.
Table student does not have this problem because its foreign key column stud_id is the leftmost column of the PK in the referenced table.
To resolve this you can create a new index for the referened column:
ALTER TABLE enrolment ADD INDEX subj_code_idx (subj_code);
Note: You have to do the same for referenced table grade in the other foreign key.
Demo here
FOREIGN KEY of one table is a PRIMARY key of another table. its a mapping key or column between two table.
First of all: you don't make subject_id as primary key in CLO table.
subject_id is act as a primary key in subject table and foreign key in clo table
ALTER TABLE `clo` DROP PRIMARY KEY, ADD PRIMARY KEY(`clo_id`);
ALTER TABLE `subject` DROP PRIMARY KEY, ADD PRIMARY KEY(`subject_id`);
ALTER TABLE clo ADD FOREIGN KEY (subject_id) REFERENCES subject(subject_id)
I'm getting "Error Code: 1215. Cannot add foreign key constraint" when trying to create the "orders" table.
credit_cards table:
CREATE TABLE credit_cards (
customer VARCHAR(30),
card_no CHAR(16),
PRIMARY KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card CHAR(16) NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card)
REFERENCES credit_cards (card_no));
The report from SHOW ENGINE INNODB STATUS says that the problem is FOREIGN KEY (credit_card) REFERENCES credit_cards(card_no))
I've read a bunch of resolved questions and still can't figure it out. I'm using MySQL Workbench. Thanks.
You need to reference all the keys in a primary key (or unique key) relationship. I would recommend:
CREATE TABLE credit_cards (
credit_card_id int auto_increment primary key,
customer VARCHAR(30),
card_no CHAR(16),
UNIQUE KEY (customer, card_no),
FOREIGN KEY (customer) REFERENCES customers(username));
orders table:
CREATE TABLE orders (
order_no INT AUTO_INCREMENT,
customer VARCHAR(30) NOT NULL,
date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
credit_card_id int NOT NULL,
PRIMARY KEY (order_no),
FOREIGN KEY (customer)
REFERENCES customers (username),
FOREIGN KEY (credit_card_id)
REFERENCES credit_cards (credit_card_id));
create table buys (
user_id INT(11) not null,
software_id INT(11) not null,
associated_id INT(11) not null,
primary key (associated_id),
foreign key (software_id) references software,
foreign key (user_id) references users,
foreign key (associated_id) references associated);
I have a ternary relationship associated table having total participation and a key constraint but cannot add foreign key constraints
Check the following query:
CREATE TABLE products(
prd_id int not null auto_increment primary key,
cat_id int not null,
FOREIGN KEY fk_cat(cat_id)
REFERENCES categories(cat_id)
)ENGINE=InnoDB;
In your case:
foreign key (software_id) references software -> here column name is missing in software table