2013-02-20 513 views
2

我已经在phpmyadmin中创建了1个表,但没有设置空值,但是当我使用插入查询插入数据时仍然采用空值。
这个怎么解决?用于创建表如何使表不在phpmyadmin中的非空set字段中使用空值?

SQL:

CREATE TABLE exmp.student (id INT(12) NOT NULL AUTO_INCREMENT PRIMARY KEY , 
name VARCHAR(200) NOT NULL , 
user_name VARCHAR(200) NOT NULL , 
branch VARCHAR(200) NOT NULL) 

插入查询:

INSERT INTO exmp.student(name) VALUES ('harjeet') 

它采取的值到表中未显示错误。

回答

0

当您运行插入查询时,它将为user_namebranch列插入空字符串。我能够复制这个。

如果您运行此查询,明确设置非空列为空,那么它会返回一个错误:

INSERT INTO student(name, user_name, branch) VALUES ('harjeet', null, null) 

Data Type Default Values了MySQL 5.0参考:

As of MySQL 5.0.2, if a column definition includes no explicit DEFAULT value, MySQL determines the default value as follows:

If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause. This is the same as before 5.0.2.

If the column cannot take NULL as the value, MySQL defines the column with no explicit DEFAULT clause. Exception: If the column is defined as part of a PRIMARY KEY but not explicitly as NOT NULL, MySQL creates it as a NOT NULL column (because PRIMARY KEY columns must be NOT NULL), but also assigns it a DEFAULT clause using the implicit default value. To prevent this, include an explicit NOT NULL in the definition of any PRIMARY KEY column.

For data entry into a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time:

If strict SQL mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been inserted.

If strict mode is not enabled, MySQL sets the column to the implicit default value for the column data type.

所以它看起来像你的MySql没有启用严格模式。 varchar列的隐式默认值是空字符串,所以这是使用的内容。如果你想让它返回一个错误,那么打开字符串模式。

0

是否使用INSERT陈述真实INSERT?您创建的表格是exmp.student。 INSERT使用student(不使用exmp模式)。

可能还有另一张允许NULL值的学生表?

+0

这两个表都是一样的我编辑了我的问题,见上文。 – 2013-02-20 11:04:51

相关问题