Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto increment database column in Java derby?

Here's the SQL I've tried so far to create the table

CREATE TABLE tblPosts (nId INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
strContent VARCHAR(140) NOT NULL,
strLink VARCHAR(200),
strImage VARCHAR(200));

using

String sql = "INSERT INTO tblPosts VALUES ('" + textFieldContent.getText() + "', '" + 

textFieldLink.getText() + "', '" + textFieldImage.getText() + "')";

I get an error telling me I'm not providing the nId column value which I'm not but if the column auto-increments I'm not supposed to right?

I've also tried using the IDE to create the table on the database as described here

Alter a table column with auto increment by 1 in derby

Same response though. Any suggestions?

like image 263
luigivampa Avatar asked Mar 13 '26 23:03

luigivampa


1 Answers

You need to include Start with 1, Increment by 1 Like this

CREATE TABLE tblPosts (nId INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY(Start with 1, Increment by 1), 
strContent VARCHAR(140) NOT NULL, 
strLink VARCHAR(200)
like image 56
kelvin andre Avatar answered Mar 15 '26 12:03

kelvin andre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!