How to create a link between these two tables? - android

I have two tables that I have to fill in after scanning a NFC tag.
SCAN:
-IdScan
-Data
-Date
-Geolocation
-GEOLOCATION:
-Latitude
-Longitude
-Country
-PostalCode
-etc.
My question is : how to link these two tables ?
When I insert a new scan into Scan table, I'd like to create a link between Scan's location field and table LOCATION
I heard about foreign key but I don't know if it's useful in this case...

I am assuming (Geolocation) in SCAN table has the values Latitude and Longitude. Please refer to this link(though it's PostgreSQL, concepts should be applicable to any RDBMS too like sqlite.
DDL Constraints for Foreign Key
TL;DR: A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. We say this maintains the referential integrity between two related tables.
So in case you want to make (Geolocation) as Foreign Key - You must find a Primary key in Geolocation table so may be adding a date and making (date,lat,lon) as a composite primary key would make it a fit for referential integrity.

Related

Update database schema while preserving data (and relationships)

I have a simple database schema containing 5 tables and a few foreign key relationships. Now I want to update the schema without losing the relationships.
SQLite does not support the operation I want to perform. I want to mark a column AUTOINCREMENT. So I searched and found that I need to recreate the tables.
In all the relationships, foreign key is the auto-generated row ID. This will change when I re-insert the data (right?). And the best case scenario would be having mismatched relationships. The worst case (I think) would be having foreign keys that correspond to nothing. How do I avoid this?
One way that I can think of doing this is through dumping all data into a special model designed for this very exercise. This special model will encapsulate all the relationships. Then I can start creating new tables in order of ascending dependence, from least to most. Table that has no foreign keys (but other tables use its ID) goes in first. And on inserting rows, I update the model instance.
Is there a better way of doing this? Thank you for reading.
Values in an autoincrementing column can be set to an explicit value:
INSERT INTO MyTable(MyAutoincID, Name) VALUES(42, 'hello')
Just copy the ID together with the other column values.
Furthermore, as long as you do not enable foreign key checking with PRAGMA foreign_keys or setForeignKeyConstraintsEnabled(), your DB is allowed to have inconsistent data temporarily.

One foreign key referring Multiple column (Notification Concept)

I am new in programming, and I want to ask question regarding database schema (I'm using SQLite Database for Android Development)
I have some table, let say :
MsMember
MemberId
Password
MsGroup
GroupId
GroupName
MsAnnouncement
AnnouncementId
AnnouncementName
MsComment
CommentId
CommentContent
MsTodolist
TodolistId
TodolistTitle
And I want everytime a new row has been inserted to (at least one of) all five tables above, it will create a notification to user, as far as I know, with this concept, I should create a table to store every detail of notification then shows it to user..
And my best opinion so far is I create a table let say MsNotification, then to connect all five tables with this MsNotification I should have foreign key referring to each table..
My Question is would it be possible (and effective) to have a column that has more than one references?
Example :
Foreign key (SourceId) Referring MsMember (MemberId),
Foreign key(SourceId) referring MsComment (CommentId),
Foreign key (SourceId) referring MsAnnouncement (AnnouncementId), and so on.
or is there any better way to implement this concept?
Thank you in advance
No ,you can not assign single foreign key to multiple column .
But you can put multiple foreign key in single table

SQLite-how to compare two column

friends,
I am doing an Android project in my company, still some small work is remaining, I need your help to complete the task.
The problem is...
I have created two tables in which, table1 has an empty column, for purpose for saving name...
The table2 has a list of names, the objective is only the names from this list should be should be saved in the table1's empty column other than that it shouldn't accept any of the name typed manually.
You appear to want to make the list of names a validation: if the user wishes to save a name to table1, the name must already exist in table2.
Typically this would be done as in the following example, in which only the products listed in PRIZEPRODUCTS can be entered into PRIZEWINNERS table: someone could not win a Boat, for example, given the data below:
PRIZEPRODUCTS
id
productname
1|TV
2|iPad
3|backpack
PRIZEWINNERS
id
productid
winner
ALTER TABLE PRIZEWINNERS
ADD CONSTRAINT PRIZEWINNERS_PRIZEPRODUCTS_FK
FOREIGN KEY(productid) REFERENCES PRIZEPRODUCTS(id)
SQLite doesn't create the foreign key using ALTER TABLE but as part of the create-table statement. See here for the syntax. For enabling foreign key support in Android (2.2), see here.
Now, you can establish the foreign key on the [productname] column if [productname] were the key of PRIZEPRODUCTS. In other words, you could make person-name the key of the table rather than having a PersonID. But if that name is changed in the validation table, it can break the foreign key relationship, unless ON UPDATE CASCADE is enabled, but I am not sure if this is supported in Android.
I hope below query will work for you.
insert into table1(name) values (select name from table2 where id=?).
Thanks.

Sqlite different column names, but with same values

Here is my current SQLite table:
CREATE TABLE formula (_id INTEGER PRIMARY KEY AUTOINCREMENT,suggest_text_1 TEXT,formula TEXT,category TEXT);
I want to integrate search suggestions in an Android app, but I need a SUGGEST_COLUMN_INTENT_DATA_ID and I want my _id to represent it. But I also need a _id for the content provider. So is there a way where I could have SUGGEST_COLUMN_INTENT_DATA_ID and store the current and future values of _id in it? Would I use an alias?
Would I use an alias?
This is possible but it depends on your exact requirements. See my answer to this question to see if this applies to you. Android: column '_id' does not exist problem

Android composite primary key?

Can anyone tell me how to declare a composite primary key in Android 1.6 which includes an autoincrement _id column? I'm not sure of the syntax. I've ended up just enforcing it in Java when I try to add values (where registrationNumber + date has to be unique in the table):
Cursor fuelUpsCursor = getFuelUps(registrationNumber, date);
if(!fuelUpsCursor.moveToNext())
{
//add registrationNumber and date
}
I don't really need the _id column but it can make life tricky if tables don't have one.
Cheers,
Barry
Your question does not make much sense. Your subject line asks for a "composite foreign key", your first sentence asks for a "composite primary key" with an AUTOINCREMENT that your sample code then ignores.
I am going to interpret your question this way: You want an _ID INTEGER PRIMARY KEY AUTOINCREMENT column in your table to be able to use Android's CursorAdapter, but you want to also make sure that the combination of two other columns is unique.
In that case, I think that you want to use a UNIQUE constraint:
Multiple Unique Columns in SQLite
SQLite table constraint - unique on multiple columns
http://sqlite.org/lang_createtable.html

Categories

Resources