Paras
Paras

Reputation: 3481

In Android Sqlite is ROWID same as primary Key?

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"?

Upvotes: 2

Views: 1370

Answers (1)

CL.
CL.

Reputation: 180080

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 |

Upvotes: 10

Related Questions