如何在SQL语句中使用生成的标识(NetBeans JavaDB)

时间:2017-02-07 07:58:29

标签: java mysql sql netbeans

我的表设置如下:

CREATE TABLE "CAMPAIGNS"
(  "campaignId" INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
   "campaignName" VARCHAR(200),     
   "campaignLocation" VARCHAR(200),
   "campaignManager" VARCHAR(200)

);

我的INSERT语句应该如何,所以我可以确保campaignId列自动递增,然后我选择将进入表中的其余值?

5 个答案:

答案 0 :(得分:1)

只需确保campaignId INT Auto_Increment,然后您就不需要像这样在查询中添加campaignId

String query = "INSERT INTO CAMPAIGNS (campaignName, campaignLocation, campaignManager) 
                VALUES (?, ?, ?)";

答案 1 :(得分:1)

INSERT INTO CAMPAIGNS (campaignName, CampaignLocation, CampaignManager)
VALUES ('Test','Sofia','Emil')

答案 2 :(得分:1)

有时这会因数据库而异。但是将其删除或使用null通常都可以正常工作。

insert into CAMPAIGNS(campaignId, campaignName, campaignLocation, campaignManager)
values (null, 'My Campaign', 'Australia','Bob');

答案 3 :(得分:1)

PreparedStatement preparedStatement = 
    connect.prepareStatement("INSERT into CAMPAIGNS (campaignName, campaignLocation,campaignManager) VALUES (?,?,?)");

preparedStatement.setString(1, "campaignName");
preparedStatement.setString(2, "campaignLocation");
preparedStatement.setString(3, "campaignManager");

答案 4 :(得分:1)

根本没有在插入

中提及
insert into CAMPAIGNS ( campaignName, campaignLocation, campaignManager) 
values ( 'campaignName_value', 'campaignLocation_value', 'campaignManager_value')

mysql完成剩下的工作

PS没有无用的双引号