I'm new to SQL, but managed to import my CSV data into an Apache Derby DB using ij.
My table contains no primary key, and no existing field is suitable, so I'd like to create a new field, generate unique numbers to fill it, and set it as the primary key. How can I do this?
For example, I tried
ALTER TABLE myTable ADD pk AUTO_INCREMENT PRIMARY KEY
but I got a syntax error on AUTO_INCREMENT.
Also, when I google for this sort of thing, I find lots of SQL tips, but few of them seem to apply to Derby. Is there a particular variant of SQL that I should be searching for?
I had the same problem, and as far as I can tell, there is no way to create a new primary key to an existing Apache Derby database. Derby does not allow you to add a column with GENERATED as an attribute.
Here's the method I found has worked the best.
Based on the original table:
Which looks like:
Create a new table with the same structure, in addition to having the primary key.
Breaking down the primary_key field. The first two are standard SQL keywords. The key will be an integer, and can't be null. Apache DB uses the keyword generated as their AUTO_GENERATED. There's two keywords that can follow generated ALWAYS and AS DEFAULT. Always means that it can't be manually entered, AS DEFAULT means it will be auto, unless you specify a number. Per the Apache spec
Copy data into new table via a
INSERTNow remove the original contacts table
Rename contactsTemp to contacts