I have a sqlite table and I need to keep the ids from changing when I VACUUM the database. The documentation says that VACUUM will not change the rowids of a table that has an explicit INTEGER PRIMARY KEY.
So, I created a table with
CREATE TABLE tableName (
"rowid" INTEGER PRIMARY KEY,
"updated" DATETIME DEFAULT (CURRENT_TIMESTAMP),
"description" TEXT
)
But that makes a table with two rowid columns. In SQLite Manager add-on for Firefox I see both, and when I try to access the result set in Java it says "ambiguous column: 'rowid'". Is there a way to explicitly create rowid or do I have to use a different name?
rowid is a column of every SQLite table by default
If a table has a primary key that consists of a single column, and the declared type of that column is INTEGER in any mixture of upper and lower case, then the column becomes an alias for the rowid.
Thus, your rowid column doesn't replace the default rowid. Your key is an alias for the default rowid.
Because of this, you really shouldn't name your column rowid, as it is already used, and will result in "ambiguous column" errors, as you have experienced already.
The documentation is saying that the rowid that SQLite is generating will not be changed if your table contains some INTEGER PRIMARY KEY. The PK does not need to be the rowid.
Related
I am trying to create Postgres table with Bigserial data type as a promary key. Once I am creating the table the table definition is getting changed to bigint NOT NULL DEFAULT nextval('transactions.transaction_id_seq'::regclass), to this. Please let me know why this happenning?
Thanks In Advance,
Somnath
As noted in the documentation, serials are not "real" data types, but rather convenience wrappers. If you create a serial column, you automatically get
a new sequence ('tablename_columnname_seq`)
an integer column of the appropriate type that takes its default value from the sequence.
the setup for the column to use the sequence.
To quote:
The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases).
CREATE TABLE table (BIGSERIAL id PRIMARY KEY);
is the same as
CREATE SEQUENCE table_id_seq;
CREATE TABLE table (
id bigint NOT NULL DEFAULT nextval('table_id_seq')
);
ALTER SEQUENCE table_id_seq OWNED BY table.id;
Which matches what you are getting.
I created table in Derby database that looks something like that:
create table "DATABASE".SOMETABLE (ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) CONSTRAINT PK PRIMARY KEY,
SOMETHING VARCHAR(50) not null)
Now I would like to be able to insert into that table without specifically listing all the columns I want to insert into. So I would like to do something like this:
insert into sometable values ('','something')
I don't want to insert anything into id column but I don't want to be forced to always list names of all the columns I want to insert into. However if I execute this code I get error for trying to modify auto increment primary key.
In MySQL and many other database systems I could just do this:
insert into sometable values ('','something')
and it would work but Derby gives me an error. I tried putting NULL instead of '' but I just get the same error. Is there any way to not insert anything into the primary key column with auto increment (without specifically listing all the other columns) in Derby database?
Thanks in advance for any answers.
Use:
insert into sometable values (default, 'something')
The default keyword is documented here
Specify the column you're inserting:
insert into sometable (something) values ('something')
(I would recommend specifying columns in general, then the order of the columns in your DDL does not matter)
My understanding is that the only difference between
CREATE TABLE T(ID IDENTITY PRIMARY KEY);
and
CREATE TABLE T(ID BIGINT IDENTITY);
is that the latter is more efficient since the ID column is the row id and as such corresponds the _ROWID_ pseudo-column.
Also, if I do
CREATE TABLE T(ID IDENTITY);
then ID doesn't correspond to _ROWID_ and it isn't even a primary key at all.
Are these assumptions correct?
As suggested I did some tests.
It turns out that all the three statements appear to be equivalent.
ID is always the primary key and corresponds to the _ROWID_ column.
After all, I went with a fourth variant:
CREATE TABLE T(ID BIGINT AUTO_INCREMENT, CONSTRAINT KEY_NAME PRIMARY KEY (ID));
This has the advantage that the primary key's name can be specified.
In the other cases the name was automatically generated, something along the lines of CONSTRAINT_0.
All of this with H2 1.4.190.
I've developed a software that is in production now & used by 5 clients.
I am using Derby database.
But, Now, I realized that I should've designed one table Primary Key Column with BigInt Datatype instead of Integer type.
So how can I alter the table safely, in the case when this table has relationship with other tables also through this Primary Key Column?
And, If I would like to change type of all table Primary Key Columns from Integer to BigInt, will there be any effect on performance?
Try this:
ALTER TABLE MY_TABLE ADD COLUMN NEW_COLUMN BIGINT;
UPDATE MY_TABLE SET NEW_COLUMN=MY_COLUMN;
ALTER TABLE MY_TABLE DROP COLUMN MY_COLUMN;
RENAME COLUMN MY_TABLE.NEW_COLUMN TO MY_COLUMN;
Discussed here
The problem
I have a table for some data that has an ID column of type integer (which is also the primary key).
When a new data entry is added to the table, it should get a new ID whereas the ID is not known by the application that inserts the object but it should be given by the database. For example, the IDs should be assigned like 0, 1, 2, ...
Assume that I have all other data for the new entry, how would I do the insert? Normally:
insert into T values(123, 'data');
But now I don't know what to put instead of 123
- would you create some kind of global variable NEXTID in the database that provides the IDs and query/update this value each time before inserting into T?
The questions
How to handle this kind of problem? A solution that is concurrency save is preferable.
How to achieve this with Java/myBatis? I Have a Java class that corresponds to the table structure and a new object should be added to the database, getting a new ID automatically.
Update
What I searched for was auto-increment.
Is there a standard SQL way (database independent) of declaring a column as auto-increment? I am using Apache Derby and GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) is suggested here.
How does the insert to a table that contains auto-increment columns look like?
What is the best way to get the created auto-increment value after an insert when simultaneaous access to the database is possible?
I'll accept an answer that includes explanation and SQL instructions for declaration and insertion :)
If you are using sqlserver, making column of identity type will solve the purpose something like this
.
ALTER TABLE [dbo].[T] ADD [Column1] INT identity (1, 1)
For others like oracle you can for simple database sequence.
In MySQL you can use
ALTER TABLE table_name ADD id INT AUTO_INCREMENT;
this auto increment the id column, you don't have to give in insert.