Double primary key $cordovaSQLite - javascript

how can i create a table with 2 primary key with $cordovaSQLite?
My code:
$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS mytable(id INTEGER PRIMARY KEY AUTOINCREMENT, num INTEGER PRIMARY KEY, text TEXT)');
It is possibile?

Setting two primary keys is not possible, but you can set a unique constraint on any fields you want to act like a primary key. You can emulate autoincrementing on this field by looking at this question: SQLite auto-increment non-primary key field. Hope this helps.

Related

foreign key not displaying as null after input

i have created a dabasehelper class with three tables, i want student table primary key to be the foreign key in examResult table. But when i enter data in examResult table it display the foreign key column as null
public void onCreate(SQLiteDatabase db) {
String query2 = "CREATE TABLE IF NOT EXISTS \"student_and_parent_table1\" ( `ID` integer PRIMARY KEY AUTOINCREMENT, `Name` TEXT, `FatherName` TEXT, `MotherName` TEXT, `ParentEmail` TEXT, `FatherPhoneNumber` INTEGER, `MotherPhoneNumber` INTEGER, `Address` TEXT, `Username` TEXT, `Password` TEXT)";
db.execSQL(query2);
String query3 = "CREATE TABLE IF NOT EXISTS \"teacher_table1\" ( `ID` integer PRIMARY KEY AUTOINCREMENT, `Name` TEXT, `Email` TEXT, `MobileNumber` INTEGER, `Username` TEXT, `Password` text, `Address` text, `Qualification` TEXT )";
db.execSQL(query3);
String query4 = "CREATE TABLE IF NOT EXISTS \"exam_table1\" ( `StudentID` INTEGER PRIMARY KEY AUTOINCREMENT, `StudentName` TEXT, `SubjectName` TEXT, `SubjectCode` TEXT, `MaxMarks` INTEGER, `MarksScoredByStudent` INTEGER, `Grade` TEXT, `StudentRegNo` integer, FOREIGN KEY(StudentRegNo) REFERENCES student_and_parent_table1(ID) )";
db.execSQL(query4);
There are two potential issues (due to the limited code provided it is impossible to tell exactly why NULL's are being inserted).
Issue 1 - Foreign Key Support
The first is that by default Foreign Key support is turned off and therefore needs to be turned on. To turn foreign key support on you can override the database helper's onConfigure method to call the SQLiteDatabase's setForeignKeyConstraintsEnabled method e.g.
#Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
Issue 2 - Constraints
The second is a common misunderstanding about what Foreign Keys are.
When you define a Foreign Key you are simply adding a constraint (rule) that says that a value in the child column MUST be a value that exists in the parent column. NULL being an exception/special case that indicates no relation to a parent (unless a NOT NULL constraint is applied to the child column).
A common misunderstanding is that specifying a Foreign Key automatically assigns values, it does not, it only checks that the value being inserted is a valid value, you still have to programatically determine the value to be inserted.
As such you may wish to add a NOT NULL constraint to the child (StudentRegNo) column, which would enforce the use of a valid value.
e.g. use :-
String query4 = "CREATE TABLE IF NOT EXISTS \"exam_table1\" ( `StudentID` INTEGER PRIMARY KEY AUTOINCREMENT, `StudentName` TEXT, `SubjectName` TEXT, `SubjectCode` TEXT, `MaxMarks` INTEGER, `MarksScoredByStudent` INTEGER, `Grade` TEXT, `StudentRegNo` integer NOT NULL, FOREIGN KEY(StudentRegNo) REFERENCES student_and_parent_table1(ID) )";

Sequelize Upsert without primary key value in arguments

So, I have a relation which has the autogenerated id as the primary key and I want it to be that way.
The issue is that whenever I call upsert(), it seems like I have to provide for the primary key value as well in the parameter object which is insane as I wouldn't know what is the id of the particular row I want to edit.
Instead, I am sending mandatorily the value for a unique key that I have defined, but it doesn't work.
Any workarounds or do I need to shift my primary key to some other key of whose value is known?
Example: Let's say I have an Employee table where I have the Employee Id as the PK and their emails as the UNIQUE key. Now, I believe that an upsert operation can still be carried out using a unique key, but that doesn't seem to be possible and I have to tag in the primary key for it.

Issue with mysql command

I have a table called benificiaries with following columns
custid varchar
accno varchar primarykey
name varchar
I have one record in that table with values
101
12345
john
Now I need to insert a record for another custid but same accno
var data={"custid":"102","accno":"12345","name":"john"};
con.query('insert into benificiaries set ?',data)
But it is not allowing since accno is primary key..So how can i insert this?I can have same accountno once for different custid..Any ideas?
Check out your MySQL code here:
CREATE TABLE beneficiaries (
custid INT PRIMARY KEY,
accno INT,
name varchar(30)
);
Set custid as primary keys and observe the datatypes I've used for each column.
That is not possible, you have make accno and custid as composite key
You simply can't do this, because you've defined accno as primary key.
Primary key is exactly that - the primary key of the record. It is a unique value that positively identifies that record and only that record. If you can't guarantee that data to be unique per record, it cannot be your primary key.
This table seems like a mapping table, to me. Is it?
If so, it really doesn't need a primary key. If you absolutely MUST give it a primary key, for some reason, then just give the table a surrogate key using an autoincrementing integer column and make that the primary key for this table.
How is this table being used?
I'd say screw a primary key and just index on both accno and custid columns (if you do lookups by each).
If the combination of accno and custid is unique (probably is, given what this table seems to be), then you can define your primary key as a combination of accno and custid.
If, for some reason, you need to be able to have more than one record with the same values for both of those fields, then you have no natural key in this table anyway.
Because there can be multiple entries with the same accno, accno isn't a primary key. Make a compound primary key from accno and custid, and you should be set.
CREATE TABLE beneficiaries (
custid INT,
accno INT,
name varchar
PRIMARY KEY (custid, accno)
)

JS Knex: getting foreign keys for a table

I want to drop some tables using 'knex' but I have an error Cannot delete or update a parent row: a foreign key constraint fails when I try to drop table with foreign key:
knex.schema.dropTableIfExists(name);
I can use dropForeign() function to drop foreign key but I need to know foreign key name.
How can I get foreign key names using 'knex'?
The usual foreign key index naming format in knex is : tableName_columnName_foreign.
Eg: If you have in table chat a foreign key named visitor_id then its index name will be : chat_visitor_id_foreign
That said,you wouldn't need this, unless someone has explicitly overridden the default foreign key name. In that case , search for it in the migration file or look it up in the database .

How to store current date and time in Sqlite using PhoneGap

How to store current date and time in Sqlite using PhoneGap?
tx.executeSql("CREATE TABLE IF NOT EXISTS myorder(orderid INTEGER PRIMARY KEY AUTOINCREMENT,cdatetime VARCHAR)");
tx.executeSql('INSERT INTO myorder(orderid,cdatetime) VALUES(NULL,"01/01/2012 01:00:00")',[],successOrderfunction,errorfunction);
Is it correct way to store?
You can save the current datetime as long.
Define your table column with the long-datatype and insert your value like this:
tx.executeSql("CREATE TABLE IF NOT EXISTS myorder(orderid INTEGER PRIMARY KEY, cdatetime long)");
tx.executeSql("INSERT INTO myorder(cdatetime) VALUES("+myDate.getTime()+")",[],successOrderfunction,errorfunction);
By the way. There is no need to use autoincrement. If you try to insert a null value in a primary key integer the field is automatically converted into an integer which is one greater than the largest value of that column.
I don't use sqlite or phonegap but you cannot add null in your pk table.
Add a number or don't add anything at all.
tx.executeSql('INSERT INTO myorder(cdatetime) VALUES("01/01/2012 01:00:00")',[],successOrderfunction,errorfunction);
This should work, you created an incremental pk.
tx.executeSql('INSERT INTO myorder(orderid,cdatetime) VALUES(1, "01/01/2012 01:00:00")',[],successOrderfunction,errorfunction);
This should also work, I think in sqlite you can add pk manually.
Try:
tx.executeSql('CREATE TABLE IF NOT EXISTS expens (Etype TEXT,amount REAL,created_at DATETIME DEFAULT CURRENT_TIMESTAMP)');

Categories

Resources