Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize Foreign key Association in mysql

Shulou Source: shulou.com Published: 2022-05-31 18:58:03 09月18日 Update

How to achieve foreign key association in mysql, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.

> show create table country\G

*************************** 1. row ***************************

Table: country

Create Table: CREATE TABLE `country` (

`country_id` smallint(5) unsigned NOT NULL auto_increment,

`country` varchar(50) NOT NULL,

`last_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

PRIMARY KEY (`country_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 row in set (0.01 sec)

mysql> show create table city\G

*************************** 1. row ***************************

Table: city

Create Table: CREATE TABLE `city` (

`city_id` smallint(5) unsigned NOT NULL auto_increment,

`city` varchar(50) NOT NULL,

`country_id` smallint(5) unsigned NOT NULL,

`last_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

PRIMARY KEY (`city_id`),

KEY `country_id` (`country_id`),

CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 row in set (0.00 sec)

mysql> select * from city;

+---------+----------+------------+---------------------+

| city_id | city | country_id | last_update |

+---------+----------+------------+---------------------+

| 1 | hancheng | 1 | 2012-01-09 09:18:33 |

+---------+----------+------------+---------------------+

1 row in set (0.01 sec)

mysql> select * from country;

+------------+---------+---------------------+

| country_id | country | last_update |

+------------+---------+---------------------+

| 1 | chen | 2012-01-09 09:16:38 |

+------------+---------+---------------------+

mysql> update country set country_id=100 where country_id=1;

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test/city`, CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`))

The problem above is that the country_id field cannot be changed because there is an association.

Then I looked at the book again and found that there was no foreign key constraint (cascade,set null,no action,restrict) in my SQL statement, and I felt that this was where I went wrong.

However, how to join the association method, the Internet to find a long time there is no suitable method. Just look for it yourself, just through the method the teacher said,? help a little bit finally found a way to change, the document function is very powerful ah

| ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...)

| ADD [CONSTRAINT [symbol]]

PRIMARY KEY [index_type] (index_col_name,...)

| ADD [CONSTRAINT [symbol]]

UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)

After writing it, there are a lot of mistakes. There is no way to start.

mysql> alter table city add CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE;

ERROR 1005 (HY000): Can't create table '.\ test\#sql-ed0_37.frm' (errno: 121)

zhouqian@zhou:~$ perror 121

OS error code 121: Remote I/O error

MySQL error code 121: Duplicate key on write or update

Can't create table 'test.icity' (errno: 150)

About mysql how to achieve foreign key association questions to share the answer here, I hope the above content can have some help for everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Tags: Questions relevance methods ways more help answers appropriate powerful easy easy content function again places piles fields guys buddies that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple NVidia Shulou Technology MySQL