列不能为空 Mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37698167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Column cannot be null Mysql
提问by Chapter247 Infotech
Below is my table structure
下面是我的表结构
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 ;
I am executing a query and it showing me column can not be null below is my query
我正在执行一个查询,它显示我的列不能为空 下面是我的查询
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)
Error - Column 'physician_minc' cannot be null
错误 - 列“physician_minc”不能为空
physician_minc
is not mandatory field. How can I fix this problem ?
physician_minc
不是必填字段。我该如何解决这个问题?
回答by aarju mishra
You have defined the not null value to 'physician_minc':
您已将非空值定义为“physician_minc”:
`physician_minc` varchar(20) NOT NULL,
How can you pass the null value to it.First you need to make the physician_minc
column as null then only you can pass your desired null value.
如何将空值传递给它。首先,您需要将该physician_minc
列设为空值,然后才能传递所需的空值。
`physician_minc` varchar(20) NULL,
回答by VaN
ALTER TABLE physicians CHANGE `physician_minc` `physician_minc` VARCHAR(20) NULL;
ALTER TABLE physicians CHANGE `physician_minc` `physician_minc` VARCHAR(20) NULL;
回答by user2033063
When you create your table you define a columns as mandatory with NOT NULL. You should change all columns which are not mandatory to NULL instead.
创建表时,您将列定义为 NOT NULL 的强制性列。您应该将所有非强制性的列改为 NULL。
For your insert code I assume only the first 10 fields are mandatory so to correctly insert your data your table generating code should be as follows:
对于您的插入代码,我假设只有前 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 ;
回答by user2033063
If you don't want to enter a value simply don't make the column not null, skip this statement, else it is compalsary to enter a value in it.
如果您不想输入值,只是不要使列不为空,请跳过此语句,否则需要在其中输入值。
回答by Chapter247 Infotech
Update your table as this:
更新您的表格,如下所示:
ALTER TABLE `physicians`
CHANGE `physician_minc` `physician_minc` VARCHAR(20) CHARACTER
SET latin1 COLLATE latin1_swedish_ci NULL;
回答by Kevin Anderson
Don't define physician_minc as not null
不要将physician_minc 定义为 not null
回答by Antoine
The last values of your insertion are NULL yet declared as not null in the table creation, hence the error.
If physician_minc is not mandatory then allow it to be NULL in the table creation by replacing physician_minc
varchar(20) NOT NULL, into physician_minc
varchar(20) NULL,
您插入的最后一个值是 NULL 但在表创建中声明为非空,因此出现错误。如果不是强制性的,那么通过将physician_minc
varchar(20) NOT NULL替换为physician_minc
varchar(20) NULL来允许它在表创建中 为 NULL,