2013-02-20 514 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

相關問題