简体   繁体   English

找出列是否在mysqli中支持“ Null”值

[英]Find out if the column supports “Null” values in mysqli

I have a mysql database of 100's of tables. 我有100个表格的mysql数据库。 Some columns allow null value, some don't. 有些列允许使用空值,有些则不允许。 I am inserting data into the table dynamically in php mysqli. 我在php mysqli中将数据动态插入表中。 I am using the following code to insert any null values: 我正在使用以下代码插入任何空值:

$value = ($value == "") ? null : $value;

But, the problem with this is I am getting Column 'XXX' cannot be null error. 但是,这样做的问题是我得到Column 'XXX' cannot be null错误。 How can I determine if the column supports Null values? 如何确定列是否支持Null值?

Please help. 请帮忙。

You can get all information you need about database structure through the database information_schema , like this one (you want the column IS_NULLABLE ): 您可以通过数据库information_schema获得所需的有关数据库结构的所有信息,就像这样(您需要IS_NULLABLE列):

SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
  AND table_schema = 'db_name'
  AND column_name = 'col_name'

Reference: http://dev.mysql.com/doc/refman/5.0/en/columns-table.html 参考: http : //dev.mysql.com/doc/refman/5.0/en/columns-table.html

In your database set the DEFAULT value "" (empty string) for fields wich IS NOT NULL. 在数据库中,为IS NOT NULL的字段设置DEFAULT值“”(空字符串)。 So if you insert NULL value the MySQL will substitute it with default "". 因此,如果您插入NULL值,MySQL将用默认的“”代替它。

OR 要么

SHOW COLUMNS FOR `table_name`;

Look at field named Null 查看名为Null字段

You can get this info from information_schema database ( IS_NULLABLE field)- 您可以从information_schema数据库( IS_NULLABLE字段)中获取此信息-

SELECT
  TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE
FROM
  information_schema.COLUMNS
WHERE
  TABLE_SCHEMA = 'db_name' AND TABLE_NAME = 'table_name'; -- specify your filter

You can run the following query: 您可以运行以下查询:

SHOW COLUMNS FROM `table_name_here`

Which will give you something like the following output: 这将为您提供以下输出:

Field  Type              Null  Key  Default  Extra
id     int(10) unsigned  NO    PRI  NULL     auto_increment
name   varchar(200)      YES        NULL

If you have access to the table information_schema , you can do: 如果您有权访问表information_schema ,则可以执行以下操作:

SELECT IS_NULLABLE
FROM   COLUMNS
WHERE  TABLE_NAME   = "table name"
AND    TABLE_SCHEMA = "db_name"
AND    COLUMN_NAME  = "column name"
LIMIT  1

(I've edited the following query to add the TABLE_SCHEMA column from Doug's accepted answer, in case anyone finds this answer later) (我编辑了以下查询,以从Doug接受的答案中添加TABLE_SCHEMA列,以防以后有人找到该答案)

I wouldn't blindly translate an empty string to mysql NULL at all. 我根本不会盲目地将空字符串转换为mysql NULL。
That's actually 2 different values, each have it's distinct features - so, swapping them may lead to unpredictable results. 这实际上是2个不同的值,每个值都有其独特的功能-因此,交换它们可能会导致不可预测的结果。

The same goes for the trading PHP's NOLL into Mysql' NULL. 将PHP的NOLL交易为Mysql的NULL也是如此。 They may have different meaning too. 它们也可能具有不同的含义。

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

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