简体   繁体   中英

How should my INSERT INTO statement for GENERATED BY DEFAULT AS IDENTITY be?

I'm trying to use GENERATED BY DEFAULT AS IDENTITY key for my record id's for my tables because a user needs to register themself and the user shouldn't be able to choose their own record id. So I decided to use GENERATED BY DEFAULT AS IDENTITY but I don't know how to write my INSERT statements.

This is my user table:

CREATE TABLE USER
(
   ID_USER INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
   USERNAME VARCHAR(20) UNIQUE NOT NULL,
   FORENAME VARCHAR(20) NOT NULL,
   SURNAME VARCHAR(20) NOT NULL,
   PASSWORD VARCHAR(10) NOT NULL,
   USER_TYPE INT NOT NULL,
   PRIMARY KEY(ID_USER),
   FOREIGN KEY (USER_TYPE) REFERENCES USER_TYPES(ID_TYPE)
);

and users will be allowed to register themselves.

This is what im using for my database

When a table has a column GENERATED BY DEFAULT AS IDENTITY it means that you can insert with a value in that column if you want to but you don't have to. So then in your insert, you could instead write

INSERT INTO ARTIST (ORIGIN,ARTIST_NAME) VALUES ('USA','Nirvana');

For reference: https://www.ibm.com/support/knowledgecenter/en/SSEPEK_11.0.0/apsg/src/tpc/db2z_identitycols.html

Edit after comment:

In the case where you need to retrieve the ID, @Mathias was correct that this is a duplicate question . A possible solution taken from this answer would be:

PreparedStatement result = cnx.prepareStatement(
    "INSERT INTO ARTIST (ORIGIN,ARTIST_NAME) VALUES ('USA','Nirvana')",
    RETURN_GENERATED_KEYS);
int updated = result.executeUpdate();
if (updated == 1) {
    ResultSet generatedKeys = result.getGeneratedKeys();
    if (generatedKeys.next()) {
        int key = generatedKeys.getInt(1);
    }
}

where key has the ID that you need for your next query.

The question is slightly different from the one already answered and needs a different answer.

The OP states: user shouldn't be able to choose their own record id . In that case the column definition should be ID_USER INT NOT NULL GENERATED ALWAYS AS IDENTITY to disallow any user-supplied value.

The table name shouldn't be USER as this is a reserved word. Try USERS instead.

The insert statement shouldn't insert into the ID_USER column. Similar to the example in the other answer, it should list the columns that are being inserted. An example below:

 INSERT INTO USERS (USERNAME, FORENAME, SURNAME, PASSWORD, USER_TYPE)
   VALUES ('JohnSmith','John', 'Smith', 'apasswrdx67', 3)

The OP wants to insert the generated value into another table using the GUI DatabaseManager. This is done by using the IDENTITY() function immediatly after inserting that row. For example,

 INSERT INTO SOMETABLE (X, Y, Z) VALUES (IDENTITY(), 'some value', 'other value')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM