列不能为null Mysql

时间:2016-06-08 09:13:36

标签: mysql sql

以下是我的表结构

CREATE TABLE IF NOT EXISTS `physicians` (
  `physician_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) NOT NULL,
  `physician_gender` varchar(10) NOT NULL,
  `physician_dob_date` int(11) NOT NULL,
  `physician_dob_month` int(11) NOT NULL,
  `physician_dob_year` year(4) NOT NULL,
  `physician_profession` varchar(50) NOT NULL,
  `medical_specialty` varchar(100) NOT NULL,
  `medical_school` varchar(100) NOT NULL,
  `traning_year` year(4) NOT NULL,
  `physician_minc` varchar(20) NOT NULL,
  `physician_country` varchar(100) NOT NULL,
  `physician_state` varchar(60) NOT NULL,
  `physician_city` varchar(60) NOT NULL,
  `physician_address` varchar(100) NOT NULL,
  `physician_postal_code` varchar(10) NOT NULL,
  `physician_phone_number` varchar(20) NOT NULL,
  `physician_default_msg` longtext NOT NULL,
  PRIMARY KEY (`physician_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

我正在执行一个查询,它显示我的列不能为空,下面是我的查询

INSERT INTO `physicians` (`user_id`, `physician_gender`, `physician_dob_date`, `physician_dob_month`, `physician_dob_year`, `physician_profession`, `medical_specialty`, `medical_school`, `traning_year`, `physician_minc`, `physician_country`, `physician_state`, `physician_city`, `physician_address`, `physician_postal_code`, `physician_phone_number`, `physician_default_msg`) VALUES (4, 'Female', '5', '7', '1955', '3', '2', '1', '1965', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

错误 - 列' physician_minc'不能为空

physician_minc不是必填字段。我该如何解决这个问题?

8 个答案:

答案 0 :(得分:4)

您已将非空值定义为' physician_minc':

`physician_minc` varchar(20) NOT NULL,

如何将null值传递给它。首先,您需要将physician_minc列设为null,然后才能传递所需的空值。

`physician_minc` varchar(20) NULL,

答案 1 :(得分:2)

创建表时,使用NOT NULL将列定义为必需列。您应该将所有非强制的列更改为NULL。

对于您的插入代码,我假设只有前10个字段是必需的,因此要正确插入数据,您的表生成代码应如下所示:

CREATE TABLE IF NOT EXISTS `physicians` (
 `physician_id` bigint(20) NOT NULL AUTO_INCREMENT,
 `user_id` bigint(20) NOT NULL,
 `physician_gender` varchar(10) NOT NULL,
 `physician_dob_date` int(11) NOT NULL,
  `physician_dob_month` int(11) NOT NULL,
  `physician_dob_year` year(4) NOT NULL,
  `physician_profession` varchar(50) NOT NULL,
  `medical_specialty` varchar(100) NOT NULL,
  `medical_school` varchar(100) NOT NULL,
  `traning_year` year(4) NOT NULL,
  `physician_minc` varchar(20) NULL,
  `physician_country` varchar(100) NULL,
  `physician_state` varchar(60)  NULL,
  `physician_city` varchar(60) NULL,
  `physician_address` varchar(100)  NULL,
  `physician_postal_code` varchar(10)  NULL,
  `physician_phone_number` varchar(20)  NULL,
  `physician_default_msg` longtext  NULL,
  PRIMARY KEY (`physician_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

答案 2 :(得分:1)

不要将physician_minc定义为not null

答案 3 :(得分:1)

插入的最后一个值是NULL,但在表创建中声明为非null,因此出错。 如果physician_minc不是必需的,那么在表创建中通过将physician_minc varchar(20)NOT NULL替换为physician_minc varchar(20)NULL来允许它为NULL,

答案 4 :(得分:1)

ALTER TABLE physicians CHANGE `physician_minc` `physician_minc` VARCHAR(20) NULL;

答案 5 :(得分:1)

如果您不想输入值,只是不要使该列不为空,请跳过此语句,否则在其中输入值是比较的。

答案 6 :(得分:1)

更新您的表格:

ALTER TABLE `physicians` 
CHANGE `physician_minc` `physician_minc` VARCHAR(20) CHARACTER 
SET latin1 COLLATE latin1_swedish_ci NULL;

答案 7 :(得分:0)

与查询不同,如果列不支持空值,则此查询将用空字符串替换null。:

INSERT INTO `physicians` (`user_id`, `physician_gender`, `physician_dob_date`, `physician_dob_month`, `physician_dob_year`, `physician_profession`, `medical_specialty`, `medical_school`, `traning_year`, `physician_minc`, `physician_country`, `physician_state`, `physician_city`, `physician_address`, `physician_postal_code`, `physician_phone_number`, `physician_default_msg`) SELECT 4, 'Female', '5', '7', '1955', '3', '2', '1', '1965', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL;