2

I have a table with the following schema :

public static final String PRIMARY_KEY = "_id";
public static final String TASK_NAME = "name";
public static final String TASK_DESCRIPTION = "description";
public static final String IS_TASK_COMPLETED = "is_completed";
public static final String IS_TASK_DELETED = "is_deleted";
public static final String TASK_START_DATE = "start_date";
public static final String TASK_START_TIME = "start_time";
public static final String ESTIMATED_TASK_END_DATE = "end_date";
public static final String TIMESTAMP = "timestamp";

In one fragment I'm inserting a row in the table using the insert() method. I know that this returns the rowId of the inserted row but in this thread I read that

If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID. You can then access the ROWID using any of four different names, the original three names described above or the name given to the INTEGER PRIMARY KEY column. All these names are aliases for one another and work equally well in any context.

So will the returning rowId be same as the primary key. But what if a row is deleted and another row is inserted in that case will the rowId be as same as the primary key value "id"?

Community
  • 1
  • 1
Paras
  • 3,191
  • 6
  • 41
  • 77
  • 1
    Your question is unclear. Are you asking what happens to the `rowId` if you delete a record and then do an `INSERT`? – Tim Biegeleisen Jul 10 '16 at 07:21
  • Yes. I mean will the value of rowId and the primary key will always be same? – Paras Jul 10 '16 at 07:39
  • Still not clear...but if you make the column integer and primary key, then `rowId` should be synonomous with the primary key. – Tim Biegeleisen Jul 10 '16 at 07:40
  • If I have a row in the table having rowId value as 8 and primary key value as 8. If I delete this row and insert another row. The value of new PK will be 9. What will be the value of new rowId? – Paras Jul 10 '16 at 07:44
  • If you had an integer PID column, then SQLite _might_ use `9` as the next value. But it could also use another value if you don't have the column set to auto increment. But all bets are off, because you have a `String` PID column. – Tim Biegeleisen Jul 10 '16 at 08:01
  • Thanks Tim, that clears my doubt. – Paras Jul 10 '16 at 08:34

1 Answers1

10

All tables (ignoring exceptions) have a row ID.

When there is no column declared as INTEGER PRIMARY KEY, the rowid is separate from the other columns:

CREATE TABLE A ( _id INT PRIMARY KEY, name TEXT ); -- not "INTEGER"

| rowid | _id | name |
+-------+-----+------+
|     1 |   1 | this |
|     2 |   2 | that |
|     3 |  30 | NULL |  -- does not need to be the same

If there is an INTEGER PRIMARY KEY column, both rowid and the declared column name are aliases for the same value:

CREATE TABLE B ( _id INTEGER PRIMARY KEY, name TEXT );

| rowid | name |
| = _id |      | 
+-------+------+
|     1 | this |
|     2 | that |
|     3 | NULL |
CL.
  • 173,858
  • 17
  • 217
  • 259