你好,所以我試圖在 java 中創建一個插入查詢到我的 sql 服務器,但問題是它一直要求欄位用戶 id 的值,即使我已經定義了 AUTO_INCREMENT,我也嘗試將值設定為 NULL,但是它說“列用戶 ID 不能為空”
呃,根據我在 sql 中所知道的,您不必為自動增量型別定義值,對嗎?
詢問:
String query = "INSERT INTO
user(userId,username,password,gender,country,role)
VALUES(NULL,'" uu "', '" pp "', '" gg "','" cc "', '" rr "')";
錯誤:
java.sql.SQLIntegrityConstraintViolationException: Column 'userId' cannot be null
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1333)
at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2106)
at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1243)
at main.Connect.updateData(Connect.java:43)
at main.Regis.actionPerformed(Regis.java:189)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
表定義:
|Field |Type |Null|key|Default|Extra|
|--------|-------------|----|---|-------|-----|
|userId |int |NO |___|NULL | |
|username|varchar(255) |NO |___|NULL | |
|password|varchar(255) |NO |___|NULL | |
|gender |varchar(255) |NO |___|NULL | |
|country |varchar(255) |NO |___|NULL | |
|role |varchar(255) |NO |___|NULL | |
注意:從我得到的 .sql 檔案中,有表用戶的更改表代碼
ALTER TABLE `user`
MODIFY `userId` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
uj5u.com熱心網友回復:
除非列上有鍵(即索引),否則不能將列設為 AUTO_INCREMENT。最好是 PRIMARY KEY 或 UNIQUE KEY。
但是我在您的描述表示例中看到,該列不是鍵:
|Field |Type |Null|key|Default|Extra|
|--------|-------------|----|---|-------|-----|
|userId |int |NO |___|NULL | |
^^^ ideally this should say "PRI"
所以你的 ALTER TABLE 使列 AUTO_INCREMENT 可能失敗了。您可以確認這一點:
SHOW CREATE TABLE `user`\G
您看到列AUTO_INCREMENT旁邊的選項了userId嗎?我不認為你會。
您可以再次嘗試將該列設為主鍵并將其設為AUTO_INCREMENT:
ALTER TABLE `user`
ADD PRIMARY KEY (`userId`),
MODIFY `userId` INT AUTO_INCREMENT;
不要打擾INT(255)。整數的長度引數是一個常見的混淆源。它幾乎沒有任何目的或作用,并且在 MySQL 8.0 中已被棄用。
不要費心制作專欄NOT NULL。當您添加 PRIMARY KEY 約束時,這將自動發生。
不要打擾設定AUTO_INCREMENT=8. 下一個 AI 值將自動設定為該列中的最大值加一。它永遠不會小于該列中的最大值。
uj5u.com熱心網友回復:
試試這個代碼,如果你已經定義了 userId 是一個 auto_increament,它必須作業。
String query = "INSERT INTO user(username,password,gender,country,role) VALUES('" uu "', '" pp "', '" gg "','" cc "', '" rr "')";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/399815.html
下一篇:在新表中插入多行的觸發器
