Retrieving and saving database fields by ID - database

Data in one table is referenced by ID to another Table. For example, I have a table of States with ids 1-50. In another table "User" I have a reference to that id ie. state= 4. When I need to update data back to "User", if the state changes, should my code be aware of the numbering of the state data? In other words, if the new state is Alabama id=1, i should enumerate before submitting to database? Or should the DataAccess layer search the datatables for theid for the new state?

The specifics of this depend on what your front end is, but in general you should pass around the id as well as the state name, so when the user selects Alabama, id 1 gets pased back to your DAL so it can set StateID in the user table directly to that with no additional selects.
E.g., if the front end is a web page,
<select>
<option value=1>Alabama</option>
...
</select>

You should never need to perform an additional search check in the db, if your database has a proper foreign key on the user table column that references the states primary key column. Having this will always ensure a legit value is persisted to your user record.

Related

GeneralDetailReportQuery key field

We use GeneralDetailReportQuery to get customer "transactions" from QuickBooks desktop and save them to a mssql table. Query is called passing the From/To Date filter params to get changes only. We try to get report changes each day and keep our table in sync with QB data. TranId, customer ListId and TranType seem to be those fields which can uniquely identify each record returned by Report query. Problem starts to happen when users change one of those key fields, customer ListId for example.
It must have been a change of a customer in a transaction... ?
Anyway, how to keep data in sync, does report have some kind of primary key field, which is not changed and can be use to track changes ?
Thanks,
Slava

How to store row modification data in Postgres?

So I need to find a way to track modifications made to database rows. I'm thinking the best way to do this is to store object deltas for the row after the modification has been made to the row. So if I had this as a table:
CREATE TABLE global.users
(
id serial NOT NULL,
username text,
password text,
email text,
admin boolean, --
CONSTRAINT users_pkey PRIMARY KEY (id)
) WITH (
OIDS=FALSE
);
ALTER TABLE global.users
OWNER TO postgres;
COMMENT ON COLUMN global.users.admin IS '';
So say I updated the username field from "support" to "admin", then I'd save a JSON object like, say:
{
"username": ["support", "admin"]
}
associated with a specific row ID.
So my question is as follows: what's a nice way to organize these objects in Postgres? I'm currently debating between either a) having a deltas table for every existing table in the database (so this object would go into a table called global.users_delta or similar) or b) having a global deltas table which holds all deltas for all objects and tracks which table each is associated with.
I haven't really been able to identify any best practices for doing this sort of thing as yet, so even some direction towards preexisting documentation would be nice.
EDIT: An added requirement here is the issue of how to deal with the related data. So say, something belongs to a category, which is stored in another table. Usually that would be referenced by ID, so the delta would track the change in category ID's numeric value. That value needs to be labeled somehow, but that label can't necessarily be retroactively applied (in case the other linked value changes say). Should the labeled value be stored or should the raw value be stored? Or maybe both?

Saving ID to SQL table that it's not a Foreign Key

I have a html select tag (dropdown) that is has Products. Each Product has its respective ID in a table called Products in the database.
I also include an option in the select called "any product" so the user can select all products to be shown when a search is done. I set the option "any product" the value 0 and I'm saving with 0 in the database.
I'm saving these searches stats in a table called ProductsSearched. The question is: is it OK to save this option with ID 0? Because this won't match any ProductID from the table Products. I won't be able to use FK from this table to the ProductsSearched.
On the other side, If I add an option "Any Product" in the Products table and assign an ID it also won't be OK because it could be misused in other tables referencing a specific product ("any product" is not a product).
Is there a standard or best way to save this data in this scenario?
Many thanks!
If the table ProductSearches is used for logging I would not even bother with fk to another tabel. I think it's better to just write out the value of the search. This way you won't have troubles later on if the values in the Product table should change somehow.

Automatically Update Table From Another PHPMYADMIN

I have a existing table Sign-Up which list the users and state an option if they would like to subscribe to the news letter. So in the column (in Table Sign_Up) for newsletter it states "YES" if this has been checked.
I Also have a second table called Subscribers.. suppose i want to add all or specific(column) values of an existing Sign_Up record if the data states Yes they want to subscribe to newsletter.
The purpose of this is to have any records which have said yes to newsletter to update and save to my subscribers table.
Can this be done automatically within phpMyadmin?
Could Triggers help with this?

Realizing Three States By Storing Values in Single Column In DB Table

I Have List of Products Displayed in Web Page.
Each product is identified by unique product id in database.
Initially when the page loads its would be shown as Both tick and Cross under every product (Image 1)
Once the User click Tick Symbol for particular product I will display it as Interested (Image2)
When the User click Cross Symbol for particular product I will display it as Not Interested (Image3)
I should load the user last selected preference every time the page loads by storing the user name and productId for which the user is Interested.
This remains simple when there are only two states where the user might either be interested or not interested since i will store all the interested ProductaIds in DB and Use them to load user preference.That is I will apply class Interested for all the ProductIds which are in DB and NotInterested class for all those Ids which are not In database.
Now the third state is the one for which the user never touched - Image1.
I already have a DB Table Like Below
CREATE TABLE UserPreference(
UserId INT,
Interested_ProductsId VARCHAR(150)
);
I am Storing the Interested_ProductsId as CSV Product Ids (i.e) 5,75,2,15 are all product Ids
Now my question is it possible to realize the third state(Image1) in the CSV.I am storing only ProductsIds in which customer interested.How to realize the ProductsId in which customer is Not interested(Image3) and customer never clicked(Image1).
Thanks in Advance
Yes this is possible: Introduce a new column:
Type tinyint not null
CHECK (Type in (1, 2, 3))
And define type as one of your three states.
You can associate arbitrary data with your join table items. You basically promote them to "entities" that way.

Resources