SQL - CONSTRAINT to link two tables - mysql

I don't understand how to link two tables together. This is an example:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
status_id int(11) DEFAULT NULL,
CONSTRAINT `fk_id` FOREIGN KEY (`id`) REFERENCES `itemStatus` (`id`),
);
I'm calling the row "status_id" but I don't reference this anywhere, so it can't link the two. For this example, should should my "CONSTRAINT" line read in order to be correct?

A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Take a look at:
http://www.w3schools.com/sql/sql_foreignkey.asp
So it should be:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
status_id int(11) DEFAULT NULL,
CONSTRAINT `fk_id` FOREIGN KEY (`status_id`) REFERENCES `itemStatus` (`id`)
);
FOREIGN KEY (status_id) => Field in the table will REFERENCES itemStatus (id)
Constraint can't have "," when it's the last:
(...) REFERENCES itemStatus (id),
So the structure should be:
CONSTRAINT <<CONSTRAINT_NAME>> FOREIGN KEY (<<COLUMN_IN_THE_TABLE>>) REFERENCES `<<ANOTHER_TABLE>>` (`<<ANOTHER_TABLE_COLUMN_ID>>`)

Looks like you're very close. Try this instead:
CONSTRAINT `fk_id` FOREIGN KEY (`status_id`) REFERENCES `itemStatus` (`id`)

Your constraint is linking two primary keys (id of table 1 with id of table 2).
It should be something like this:
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
status_id int(11) DEFAULT NULL,
CONSTRAINT `fk_id` FOREIGN KEY (`status_id`) REFERENCES `itemStatus` (`id`)
);

Related

Error while trying to add multiple foreign keys to single table

I am trying to create a child table that constraints 3 foreign keys from the parent but I receive an error 1215: cannot add foreign key constraint
parent table:
CREATE TABLE `Availability` (
`time_of_day` varchar(20) NOT NULL,
`day_of_week` varchar(20) NOT NULL,
`email` varchar(60) NOT NULL,
PRIMARY KEY (`time_of_day`,`day_of_week`,`email`),
KEY `email` (`email`),
CONSTRAINT `Availability_ibfk_1` FOREIGN KEY (`email`) REFERENCES `service_provider` (`email_service_provider`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
child table (which I cant build due to error mentioned above):
CREATE TABLE TEST1
(
num_request INT NOT NULL,
time_of_day VARCHAR(20) NOT NULL,
day_of_week VARCHAR(20) NOT NULL,
email VARCHAR(60) NOT NULL,
PRIMARY KEY (num_request),
Foreign key (time_of_day) references Availability(time_of_day),
Foreign key (day_of_week) references Availability(day_of_week),
Foreign key (email) references Availability(email)
);
Please show me what I'm doing wrong... Thank you all.
When you're making a foreign key to a table with a composite primary key (i.e. a key of multiple columns), you should make the foreign key composite as well.
CREATE TABLE TEST1
(
num_request INT NOT NULL,
time_of_day VARCHAR(20) NOT NULL,
day_of_week VARCHAR(20) NOT NULL,
email VARCHAR(60) NOT NULL,
PRIMARY KEY (num_request),
Foreign key (time_of_day, day_of_week, email) references Availability(time_of_day, day_of_week, email)
)
The columns of the foreign key should match the columns of the primary or unique key they reference. They should have the same number of columns, in the same order.
What you tried to do was create three separate constraints of one column each.

1072 - Key column 'trax_zone_id' doesn't exist in table

this is the parent table
CREATE TABLE trax_zone(
trax_zone_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
trax_zone_name VARCHAR(255) NOT NULL,
)ENGINE=InnoDB;
this is the child table code
CREATE TABLE city(
city_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
city_name VARCHAR(255) NOT NULL,
FOREIGN KEY fk_trax(trax_zone_id) REFERENCES trax_zone(trax_zone_id) ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY fk_zoop(zoop_zone_id) REFERENCES zoop_zone(zoop_zone_id) ON UPDATE CASCADE ON DELETE RESTRICT
)ENGINE=InnoDB;
i am new to mysql , kindly guide me

mysql creating table foreign key

I've created a table :
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY
,uName VARCHAR(50)
,uSecondName VARCHAR(50)
,eMail VARCHAR(50)
)
After this I even insert some data without any problems. But when I've tried to create new table with FOREIGN KEY referenced to users.id I've got an error:
CREATE TABLE posts(
id INT(6) AUTO_INCREMENT NOT NULL
,pTitle VARCHAR(155) NOT NULL DEFAULT 'not_set'
,pText TEXT
,pAuthor INT(6)
,PRIMARY KEY(id)
,CONSTRAINT fk_PerAuthor FOREIGN KEY (pAuthor)
REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);
Did I miss something?

Trouble constraining one database table to another

I'm new to SQL and trying to learn how to reference on table to another. This is what I have:
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
sector_id int(11) DEFAULT NULL,
status_id int(11) DEFAULT NULL,
locations_id int(11) DEFAULT NULL,
payments_id int(11) DEFAULT NULL,
type_id int(11) DEFAULT NULL,
CONSTRAINT `fk_sector_id` FOREIGN KEY (sector_id) REFERENCES `sector` (`sector_id`),
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`),
CONSTRAINT `fk_locations_id` FOREIGN KEY (`locations_id`) REFERENCES `location` (`locations_id`),
CONSTRAINT `fk_payments_id` FOREIGN KEY (`payments_id`) REFERENCES `payments` (`payments_id`),
CONSTRAINT `fk_type_id` FOREIGN KEY (`type_id`) REFERENCES `type` (`type_id`)
);
Then I have my reference table for example:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
This doesn't seem to validate, can someone tell me where I have gone wrong please?
You probably need to change your table definition like this:
CREATE TABLE IF NOT EXISTS itemStatus (
status_id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
as the constraint definition in your table is like this:
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `itemstatus` (`status_id`),
You need to update the correct table in the constraint.
Also if you dont want to change the table definition then change the constraint like this:
CONSTRAINT `fk_id` FOREIGN KEY (`id`) REFERENCES `itemstatus` (`id`)
Here's your constraint:
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`)
Here's the table definition:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
There's no column named status_id in that table.
Either rename the primary key column to status_id or change the constraint to point to id.

Can two columns from one table have a foreign key to the same column in another table?

I have two tables in a database, Person and Pet.
CREATE TABLE Person (
id INT NOT NULL,
PRIMARY KEY (id)
)
CREATE TABLE Pet (
id INT NOT NULL,
original_owner INT NOT NULL,
current_owner INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (original_owner)
REFERENCES Person(id),
FOREIGN KEY (current_owner)
REFERENCES Person(id)
)
I am trying to reference the previous owner, and the current owner for each pet. I have also tried
CREATE TABLE Pet (
id INT NOT NULL,
original_owner INT NOT NULL,
current_owner INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (original_owner, current_owner)
REFERENCES Person(id, id)
)
and
CREATE TABLE Pet (
id INT NOT NULL,
original_owner INT NOT NULL,
current_owner INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (original_owner, current_owner)
REFERENCES Person(id)
)
but I get the following error:
Error Code: 1215. Cannot add foreign key constraint
Is this even possible to accomplish? Or would I have to create some sort of bridge table to accommodate this?
Please try the following:
CREATE TABLE IF NOT EXISTS `pet` (
`id` int(11) NOT NULL,
`original_owner` int(11) NOT NULL,
`current_owner` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `origin` (`original_owner`),
KEY `current` (`current_owner`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `pet`
ADD CONSTRAINT `pet_ibfk_2` FOREIGN KEY (`current_owner`) REFERENCES `person` (`id`),
ADD CONSTRAINT `pet_ibfk_1` FOREIGN KEY (`original_owner`) REFERENCES `person` (`id`);
The problem was the order I had the tables defined in the script. I had Pet come before Person. Once I put Person above Pet it worked fine. The example in the question was written from my head, as I didn't have the actual code handy when I posted it.