简体   繁体   中英

allowing null values in already existing foreign key column mysql

I have a MySQL database with tables t1 and t2 . One of the columns in table t1 has a foreign key to t2 .

Need to allow the foreign key column to accept null values. There is already some important data so recreating the table is not an option.

Tried the usual alter table commands but it showed syntax error. Is there a way to go around it without affecting the database?

This is what I tried:

ALTER TABLE t1 MODIFY fk_column_id NULL;

The missing part is the type definition in the modify statement. With MODIFY you redefine the column, thus you need to give the new type as well. But in case you only modify that it can be null, no data will be lost.

Create referenced table and filling it :

mysql> -- Creating referenced table
mysql> create table `tUser` (
    -> `id` int auto_increment not null,
    -> `name` varchar(16),
    -> primary key (`id`)
    -> );
Query OK, 0 rows affected (0.07 sec)

mysql> -- Filling and checking referenced table
mysql> insert into `tUser` (`name`) values ("Jane"), ("John");
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from `tUser`;
+----+------+
| id | name |
+----+------+
|  1 | Jane |
|  2 | John |
+----+------+
2 rows in set (0.07 sec)

mysql> -- Creating referencing table
mysql> create table `tHoliday` (
    -> `id` int auto_increment not null,
    -> `userId` int,
    -> `date` date,
    -> primary key (`id`),
    -> foreign key (`userId`) references `tUser` (`id`)
    -> );
Query OK, 0 rows affected (0.14 sec)

mysql> -- Filling and checking referencing table
mysql> insert into `tHoliday` (`userId`, `date`) values
    -> (1, "2014-11-10"),
    -> (1, "2014-11-13"),
    -> (2, "2014-10-10"),
    -> (2, "2014-12-10");
Query OK, 4 rows affected (0.08 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from `tHoliday`;
+----+--------+------------+
| id | userId | date       |
+----+--------+------------+
|  1 |      1 | 2014-11-10 |
|  2 |      1 | 2014-11-13 |
|  3 |      2 | 2014-10-10 |
|  4 |      2 | 2014-12-10 |
+----+--------+------------+
4 rows in set (0.05 sec)

mysql> -- Updating foreign key column to allow NULL
mysql> alter table `tHoliday` modify `userId` int null;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> -- Inserting line without foreign key
mysql> insert into `tHoliday` (`date`) values ("2014-11-15");
Query OK, 1 row affected (0.06 sec)

mysql> select * from `tHoliday`;
+----+--------+------------+
| id | userId | date       |
+----+--------+------------+
|  1 |      1 | 2014-11-10 |
|  2 |      1 | 2014-11-13 |
|  3 |      2 | 2014-10-10 |
|  4 |      2 | 2014-12-10 |
|  5 |   NULL | 2014-11-15 |
+----+--------+------------+
5 rows in set (0.03 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM