One foreign key referring Multiple column (Notification Concept) - android

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

Related

How to create a link between these two tables?

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.

Best way to enter complex table to sqlite database

I have some kind of this table.
The question is what is the best way to create this kind of table?
Should I create for each item one table is it possible to create only one table??
Updated: See comments under #Emil.
You should have 1 tables as #Emil has suggested.
This should look like, soemthing like
_id, sort, grade, diameter, length, price1_dol, price1_euros, price2_dol, price2_euros, final,
Note: I have split up prices columns up - so you have price1_dol, price1_euros, price2_dol, price2_euros.
It is indeed possible to make this data into just one table. The columns sort and grade seem to uniquely identify one row so together they might make up a candidate key. If so you could use those as your primary key, or create a new integer column that you use as the primary key.
You should definitely not create one table per item. The database schema should never change with normal use. Only when you add, remove or change the type of data you have in your database should you consider changing the schema. Otherwise you should design and normalize your database in such a way that it's possible to grow the data only by inserting new rows, not new tables.

SQLITE Android Design

I'm trying to build a chore app where the user can create a group with a password and they can then add chores to the group that they made. My question is I'm using sqlite as the database and I'm wondering if it's possible to add a database column into the table. I'm planning on creating a table inside my db with columns of id, username, password, database. database is where I will create additional databases where it will store the chore table for my app. If I can't do this, can someone lead me along the way on how I can for example, make a table named Group, then for its columns have id, username, password. Then for each Group rows, be able to somehow match it with another corresponding database or table where the table will be chores with columns say name, frequency. And however many Group rows I add, I will need to somehow make another chore table to match with it, but I need to know how to link and refer to it from just my Group table. If there's a better way to do this using SQLite I would love to know. Thank you.
If i get your question correctly, I think all u need is to use PRIMARY_KEY and FOREIGN_KEY. Hope you are aware of constraints in databases.
So basically two tables will share a KEY which is unique, the key will be the PRIMARY_KEY in table 1 and will act as a link or reference to row in second table, where it is a FOREIGN_KEY
check this for more on constraints

Saving a changing amount of Values in a SQL-Database

i am really stuck at this point of my android app development.
What i need is a way to save a changing amount of int or string-values (in a sql database). Yet im not even sure if this is the right approach, but let me explain:
In the app i am currently working on, you are able to create certain "events". Users should be able to apply for such events.
I have an external database with 2 tables:
first one for users - every user has a unique ID
second one for events - every event has a unique ID
I need each event to know what users applied for it. And i need each user to know what events they applied for.
I was thinking to save the Event-IDs in the User-Table and vice versa.
I just dont know how to do that since the amount of applicants/ID's can change. Is there a way to save Arrays in the database which can easily be edited (e.g. +/- one ID) and read?
Is this even the right way? I am very happy for any advise!
Thanks in advance!
What you seem to want is a many-to-many relationship. A user can be part of many events, and an event can have many users. That requires an additional table though:
Table: User Columns: UserId, Name, ...
Table: Event Columns: EventId, Name, ...
Table: UserEvents Columns: UserId, EventId, ...
In the new table, UserEvents, you would store the UserId's and EventId's like this:
UserEvents
UserId EventId
1 1
2 1
1 2
This means that if you selected UserId 1, the query would return EventId 1 & 2. If you selected EventId 1 the query would return that UserId 1 & 2 would be attending.
This is the standard and recommended way to deal with many-to-many. It's very flexible and can easily be scaled.
You could either use a Compound key (Composite Key) for this table, or create a column specifically as a Primary Key. The code below can be used, and manipulated, to create both your table and Compound/Composite key (I'm guessing on data types).
CREATE TABLE UserEvents
(
[UserId] INT NOT NULL,
[EventId] INT NOT NULL
CONSTRAINT PK_UserEvents PRIMARY KEY NONCLUSTERED ([UserId], [EventId])
)
I would add a third table (e.g. UserEvents) to store which events a user has applied for, along with other relevant attributes (e.g. ApplicationTime, ApplicationStatus). This association would have a foreign key relationship back to the related tables and resolve the many-to-many relationship between users and events.
What you have there is called a "many-to-many" relationship between to tables which can only be resolved by the introduction of a third table between your two tables that stores the associations.
This table would contain the User-ID and the Event-ID as foreign keys (and maybe additional information).

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.

Categories

Resources