Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Primary Keys in Android SQLite

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?

like image 873
Jaiesh_bhai Avatar asked Aug 15 '26 07:08

Jaiesh_bhai


1 Answers

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.

like image 91
Lucifer Avatar answered Aug 17 '26 22:08

Lucifer