Main Tutorials

How to define one-to-one relationship in MySQL

One-to-one relationships occur when there is exactly one record in the first table that corresponds to exactly one record in the related table.

MySQL does not contains any “ready” options to define the one-to-one relationship, but, if you want to enforce it, you can add a foreign key from one primary key to the other primary key, by doing this, both tables will have the one-to-one relationship automatically.

Example

Here’s an example to define a one-to-one relationship in MySQL.

one-to-one-relationship

STOCK table is used to store the frequent use data like stock_code and stock_name, while STOCK_DETAIL is stored the company detail. Both tables contains the same Stock_Id as primary key. And, in STOCK_DETAIL table, Stock_Id is the primary key and also a foreign key to STOCK table.

Note
Another classic one-to-one example is the user and user profile design.

MySQL Script

This is the SQL statement to create tables and enforce the relationship in MySQL.


DROP TABLE IF EXISTS `mkyong`.`stock`;
CREATE TABLE  `mkyong`.`stock` (
  `STOCK_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `STOCK_CODE` varchar(10) NOT NULL,
  `STOCK_NAME` varchar(20) NOT NULL,
  PRIMARY KEY (`STOCK_ID`) USING BTREE,
  UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`),
  UNIQUE KEY `UNI_STOCK_CODE` (`STOCK_CODE`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `mkyong`.`stock_detail`;
CREATE TABLE  `mkyong`.`stock_detail` (
 `STOCK_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `COMP_NAME` varchar(100) NOT NULL,
 `COMP_DESC` varchar(255) NOT NULL,
 `REMARK` varchar(255) NOT NULL,
 `LISTED_DATE` date NOT NULL,
 PRIMARY KEY (`STOCK_ID`) USING BTREE,
 CONSTRAINT `FK_STOCK_ID` FOREIGN KEY (`STOCK_ID`) REFERENCES `stock` (`STOCK_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ammar Bozorgvar
10 years ago

thank you

Asparuh
5 years ago

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (mydb.customer, CONSTRAINT FK_id FOREIGN KEY (id) REFERENCES customer_profile (id))
Why I am getting this error when I want to update or delete with the tables which are above created?

Jon Sivert
2 years ago

I’m not well versed with constraints but I found that this works for an 1-to-1 relation:

CREATE TABLE employees(ssn CHAR(11), name VARCHAR(50), PRIMARY KEY (ssn));

CREATE TABLE departments(ssn CHAR(11), did INT, dname VARCHAR(30), PRIMARY KEY (ssn), UNIQUE KEY (did), FOREIGN KEY (ssn) REFERENCES employees(ssn));

charlie
3 years ago

i think this is not one-to-one relationship standard

Rani Abidli
8 years ago

thank you

Zeeshan Tufail
8 years ago

What if stock details has an other one-to-one relationship with another table than stock?

Majbah Habib
9 years ago

excellent example. Thanks

Johnny Phan
9 years ago

Thanks, you remind me of the correct way to define one-to-one relationship: no need auto-increment ID on the second table. It felt somehow redundant!

James Poulson
10 years ago

Nice and concise explanation. What if there are multiple child tables ?

Mohammad Najar
10 years ago

Excellent tutorial.. you rock man.

Binghong
6 years ago

This is not one-to-one but one-to-one or zero. You can have a stand alone stock without stock detail.