简体   繁体   English

是否有可能使mysql表接受引用其他表的primary_key列的空值?

[英]Is it possible to have a mysql table accept a null value for a primary_key column referencing a different table?

I have a table that has a column which holds the id of a row in another table. 我有一个表,该表具有一列,该列保存另一个表中的行的ID。 However, when table A is being populated, table B may or may not have a row ready for table A. 但是,在填充表A时,表B可能有也可能没有为表A准备的行。

My question is, is it possible to have mysql prevent an invalid value from being entered but be ok with a NULL? 我的问题是,是否可以让mysql阻止输入无效值,但可以将其设为NULL? or does a foreign key necessitate a valid related value? 还是外键需要有效的相关值?

So... what I'm looking for (in pseudo code) is this: 所以...我正在寻找的(用伪代码)是:

Table "person" id | name

Table "people" id | group_name | person_id (foreign key id from table person)

insert into person (1, 'joe'); 

insert into people (1, 'foo', 1)//kosher
insert into people (1, 'foo', NULL)//also kosher
insert into people(1, 'foo', 7)// should fail since there is no id 7 in the person table.

The reason I need this is that I'm having a chicken and egg issue where it makes perfect sense for the rows in the people table to be created before hand (in this example, I'm creating the groups and would like them to pre-exist the people who join them). 我之所以需要这样做,是因为我遇到了一个鸡和鸡蛋的问题,这对于在人员表中手动创建行非常有意义(在此示例中,我正在创建组,并希望他们预先创建-存在加入他们的人)。 And I realize that THIS example is silly and I would just put the group id in the person table rather than vice-versa, but in my real-world problem that is not workable. 我意识到这个例子很愚蠢,我只是将组ID放在人员表中,而不是相反,但是在我的现实世界中这是行不通的。

Just curious if I need to allow any and all values in order to make this work, or if there's some way to allow for null. 只是好奇,如果我需要允许任何和所有值才能完成此工作,或者是否有某种方式允许null。

如果将列设置为可空,则即使它是引用另一个表中列的外键,它也可以包含NULL。

Foreign keys can be null. 外键可以为空。

When the row in the referenced table is entered you'll have to UPDATE that row to point to it. 输入引用表中的行后,您将必须更新该行以指向该行。

You set a foreign key column to accept nulls by setting the optionality of the column to NULL: 通过将列的可选性设置为NULL,可以将外键列设置为接受空值:

DROP TABLE IF EXISTS `example`.`tableb`;
CREATE TABLE  `example`.`tableb` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `person_id` int(10) unsigned default NULL, -- notice, !say "NOT NULL" like id
  PRIMARY KEY  (`id`),
  CONSTRAINT `FK_tableb_1` FOREIGN KEY (`person_id`) REFERENCES `tablea` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

That said, tablea has to have at least one record in it before you attempt to insert a null value into the tableb reference column. 就是说,在您尝试将空值插入到tableb引用列中之前,tablea必须至少包含一条记录。 Otherwise, MySQL will throw an error (for me anyways, on 4.1). 否则,MySQL会抛出错误(无论如何对我来说是4.1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在执行PRIMARY KEY的变更表时,mysql的“默认”列值NULL被删除 - mysql “Default” column value NULL is getting erased, on executing alter table for PRIMARY KEY MySQL表设计:可能使用主外部多列键吗? - MySQL table design: Primary Foreign Multi-Column Key possible? SQL Alchemy:表 primary_key - 意外的关键字参数错误 - SQL Alchemy : Table primary_key - unexpected keyword argument error MySQL查询返回,Schema_Name,Table_NAme,Count,Size_of_Table,Primary_Key和Auto_Increment_Key - MySQL Query To Return, Schema_Name, Table_NAme, Count, Size_of_Table, Primary_Key and Auto_Increment_Key MySQL中如何将主表的Primary Key值插入到子表的Foreign Key列? - How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL? mysql表中两列主键的最大值 - MAX value of a two column primary key in mysql table MySQL:是否可以通过使用不同的键引用另一个表来更新一个表中的两个字段? - MySQL: Is it possible to update two fields in one table by referencing another table by a different key? MySQL 子表需要有主键吗? - MySQL child table need to have primary key? MySQL两栏表作为主键 - MySQL two-column table as primary key MySQL 列的主键出现在另一个表中 - MySQL column of occurrences of the primary key in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM