问题
I\'m coming from MySQL world, please help.
Is it possible to create autoincrement key from NetBeans IDE in JavaDB?
Do you use some more advanced db clients, which?
Thanks.
回答1:
Found a way of setting auto increment in netbeans 8.0.1 here on StackoOverflow Screenshot below:
回答2:
This may help you:
CREATE TABLE "custinf"
(
"CUST_ID" INT not null primary key
GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
"FNAME" VARCHAR(50),
"LNAME" VARCHAR(50),
"ADDR" VARCHAR(100),
"SUBURB" VARCHAR(20),
"PCODE" INTEGER,
"PHONE" INTEGER,
"MOB" INTEGER,
"EMAIL" VARCHAR(100),
"COMM" VARCHAR(450)
);
That's how i got mine to work... to ages to get the frigging thing to actually understand me but that's the nature of code :D
BTW!- There is a way to do it in the ide interface goto the services window, expand your connection, expand your projects name, expand tables, right click indexes and select add index... the rest of the process speaks for itself really...
回答3:
If you look at this url: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/
this part of the schema may be what you are looking for.
ID INTEGER NOT NULL
PRIMARY KEY GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
回答4:
I couldn't get the accepted answer to work using the Netbeans IDE "Create Table" GUI, and I'm on Netbeans 8.2. To get it to working, create the id column with the following options e.g.
and then use 'New Entity Classes from Database' option to generate the entity for the table (I created a simple table called PERSON with an ID column created exactly as above and a NAME column which is simple varchar(255) column). These generated entities leave it to the user to add the auto generated id mechanism.
GENERATION.AUTO seems to try and use sequences which Derby doesn't seem to like (error stating failed to generate sequence/sequence does not exist), GENERATION.SEQUENCE therefore doesn't work either, GENERATION.IDENTITY doesn't work (get error stating ID is null), so that leaves GENERATION.TABLE.
Set your persistence unit's 'Table Generation Strategy' button to Create. This will create tables that don't exist in the DB when your jar is run (loaded?) i.e. the table your PU needs to create in order to store ID increments. In your entity replace the generated annotations above your id field with the following...
I also created a controller for my entity class using 'JPA Controller Classes from Entity Classes' option. I then create a simple main class to test the id was auto generated i.e.
The result is that the PERSON_ID_TABLE is generated correctly and my PERSON table has two PERSON entries in it with correct, auto generated ids.
回答5:
It's not possible right now, on Netbeans 7.0.1 . The GUI tool to create columns on a tables is very limited and does not exist a plugin that offer that feature.
回答6:
If you want to use Netbeans to define tables read this https://codezone4.wordpress.com/2012/06/19/java-database-application-using-javadb-part-1/ Simply define column as integer and create database, then grab structure to a temporary file, then delete table. Right clik to tables folder and select recreate table, select saved file and edit script for auto increment.
回答7:
- Add a new column in the table using the interface
- Write the name of column and fill other information as well
- In check field, don't uncheck it and write "INCREMENT BY 1" in it.
Voila!!
来源:https://stackoverflow.com/questions/3308329/create-autoincrement-key-in-java-db-using-netbeans-ide