In SQLite for Android what is the correct way to add multi primary keys?
Currently I have:
String Create_table = "CREATE TABLE project ( keyId INTEGER PRIMARY KEY, keyName TEXT PRIMARY KEY)";
The alternative method I thought of was:
String Create_table = "CREATE TABLE project (keyID INTEGER, keyName TEXT, PRIMARY KEY(keyID, keyName))";
Are both valid? If so, which is better? Also how do I disallow NULL values?
No it's not possible to create multiple primary keys for a single table. It's basic rule of any SQL. However you can use other constraint like UNIQUE with index to achieve this.
This won't be valid SQL Syntax:
String Create_table = "CREATE TABLE project ( keyId INTEGER PRIMARY KEY, keyName TEXT PRIMARY KEY)";
In other way you can create primary key for multiple column as follows:
Create Table yourTableName (col1, col2, col3, PRIMARY KEY (col1, col2));
How do I disallow NULL values?
you can use NOT NULL Constraint , it will not allow you to enter NULL values.
You may use one primary key , more foreign keys in one table
Related
I am used to creating a table in SQL like this:
String createItems = "CREATE TABLE items(id INTEGER PRIMARY KEY AUTOINCREMENT, ...)
...but in a video I was watching the tutor created a table like this with a "_":
String createItems = "CREATE TABLE items(_id INTEGER PRIMARY KEY AUTOINCREMENT, ...)
Is this the same thing, or is there any difference?
Thanks!
The convention of _id primary key naming comes from CursorAdapter that requires a column with such name in the cursor (not necessarily in the table).
Other than that, it is just an identifier for a column.
you think the wrong is in _id ?
this is a just column name you name it as you like
I created two sqlite tables on android
phone table with primary key "id"
CREATE TABLE BLOCKED_PHONES_TABLE ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, KEY_PHONE TEXT UNIQUE,KEY_IS_BLOCKED BIT )
comment table with foreign key "id"
CREATE TABLE COMMENTS_TABLE ( id INTEGER, KEY_COMMENT_TEXT TEXT, FOREIGN KEY(id) REFERENCES BLOCKED_PHONES_TABLE(id))
why does the comment table don't refer id as a foreign key?
otherwise it won't have ids that are missing in the phone table.
how can I know my sqlite version?
For backwards compatibility, foreign key checking is disabled by default.
You need to call setForeignKeyConstraintsEnabled in onConfigure.
I have that query
"CREATE TABLE CAdres (id INTEGER AUTO INCREMENT,CariID INTEGER, No INTEGER, Adres TEXT,Tel TEXT,Fax TEXT, NotTanim TEXT,PRIMARY KEY (CariID, No));"
when i inside record , id is null . why null ? normal sql increment number but sqlite come "null".
To get an autoincrementing id column, the column type must be
INTEGER PRIMARY KEY
or
INTEGER PRIMARY KEY AUTOINCREMENT
Your id column INTEGER AUTO INCREMENT is just a regular column which can store NULLs. The AUTO and INCREMENT are not keywords and don't affect anything.
You can also have only one primary key per table so remove the
,PRIMARY KEY (CariID, No)
If you need the combination to be unique, you can change that to a UNIQUE constraint.
try this.. it may work..
"CREATE TABLE CAdres (id INTEGER PRIMARY KEY AUTOINCREMENT,CariID INTEGER NOT NULL, No INTEGER NOT NULL, Adres TEXT,Tel TEXT,Fax TEXT, NotTanim TEXT ,UNIQUE (CariID, No));"
note : diff between primary key and unique is that , unique can be null and primary key cannot be null..
Mycode
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookdir TEXT , lastaddress TEXT,addresname TEXT PRIMARY KEY(bookdir,lastaddress));";
db.execSQL(createtabBook);
Logcat:
03-05 18:29:31.708: I/System.out(17160): android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error: , while compiling: CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));
i just want to create a table with composite primary key so that it is a combination of bookdir and lastaddress, also i want to make the lid as auto increment.
To get the value of any column autoincrement you need to write it or declare it as primary key.
In SQLite a column declared INTEGER PRIMARY KEY will autoincrement.
EDITED:
As you can not define more than one PRIMARY KEY in a table you have to make the bookdir,lastaddress columns as UNIQUE and define the lid columns as PRIMARY KEY as below:
Try out as below:
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER PRIMARY KEY AUTOINCREMENT,
bookdir TEXT , lastaddress TEXT,addresname TEXT, UNIQUE(bookdir,lastaddress));";
Also add "," after the column addresname TEXT, in your query.
I am using SQLite to work on an android application. What I have learned so far is that a table made in SQLite requires a column called _id. So, for instance, to create a table, I use the following SQL Statement:
CREATE TABLE IF NOT EXISTS Semesters(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_name TEXT,
is_current TEXT
GPA REAL);
However, now if I want to create a seperate table in the same database, what would I name the primary key column? For instance, this is the table I want to create:
CREATE TABLE IF NOT EXISTS Classes(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
course_name TEXT);
Now, both the semesters table and the courses table have a column called _id, and if I want to make a foreign key reference to the _id column in the semesters table, I will have to call it by the table name. Is there any way to make this simpler by using different names for ids?
Thanks.
For example, you make a third table and want to use both primary keys from your tables as foreign keys:
CREATE TABLE ClassSemester(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_id INTEGER,
class_id INTEGER,
FOREIGN KEY(semester_id) REFERENCES Semesters(_id),
FOREIGN KEY(class_id) REFERENCES Classes(_id)
);
More details in here:
http://www.sqlite.org/foreignkeys.html