Database Design - Items and Regions - hierarchical-data

I have design problem of database structure.
The goal is to have database for offers that our clients offer per some geographical region.
Each offer can be offered in many regions.
The regions are in hierarhy - example:
subregion_1
subregion_11
region_111
region_112
subregion_12
region_121
region_122
subregion_2
subregion_21
region_221
Now I want to store in database the offer_1 and regions for that offer. I will give You 3 examples what I have to ahieve:
when my offer_1 is stored in region_111 then I would like to display this offer when user are browsing the subregion_1, subregion_11 and region_111
If offer_1 is stored in regions subregion_11 and region_121 then the offer should be displayed when user are browsing the subregion_1, subregion_11 and all branch of subregion_11, subregion_12 and region_121
when my offer_1 is stored in subregion_1 then the offer is displayed on subregion_1 page and all branch under subregion_1.
Also I have to provide a way to calculate the number of diffrent offers in each regions dynamicaly and very fast.
Does somebody have some advice how to aproach this design?
Here is what I have so Far.
Regions
------------------------------------------------------------
| id | level1 | level2 | level3 | name | level |
------------------------------------------------------------
| 02 | 02 | null | null | subregion_1 | 1 |
| 0201 | 02 | 01 | null | subregion_11 | 2 |
| 020103 | 02 | 01 | 03 | region_111 | 3 |
------------------------------------------------------------
Offers to regions
------------------------
| offer_id | region_id |
------------------------
| 1 | 020103 |
| 1 | 0202 |
------------------------
I created id for regions from concatenating level1, level2 and level3. In the table Offers_to_regions I store the offer and the region. Here I have region on level 3 (020103) and region on level 2 (0202) for offer 1.
With this design I have problems how to query the numbers of difrent offers per region, and how to query offers for regions on level1, level2 and level3 regions.

Well there is the obvious way which uses an id to point to a parent like this
CREATE TABLE Regions (
region_id INT AUTO_INCREMENT PRIMARY KEY,
parent_id INT,
region_name VARCHAR(100) NOT NULL,
FOREIGN KEY (parent_id) REFERENCES Regions(region_id)
);
But in your situation this could be considered an anti-pattern, since it is not so easy to query through the hierarchy (specially if the number of levels changes)
Another approach could be using something like Path Enumeration, where you store the hierarchy path similar to for example unix paths. E.g.
CREATE TABLE Regions (
region_id INT AUTO_INCREMENT PRIMARY KEY,
path VARCHAR(100),
region_name VARCHAR(100) NOT NULL
);
This will allow you to store your hierarchy like this
---------------------------------------------
| region_id | path | region_name |
---------------------------------------------
| 1 | 1/ | subregion_1 |
| 2 | 1/2/ | subregion_11 |
| 3 | 1/2/3/ | region_111 |
| 4 | 1/2/4/ | region_112 |
---------------------------------------------
This way, when querying your offers table (where each offer will have a ref. to the region_id), and while browsing lets say offer for the subregion_1 (with id 1) your query can look something like this.
select Offers.SOME_COLUMN, ......
from Offers, Regions
where Offers.region_id = Regions.region_id
and Regions.path like '1/%'
There are other patterns to model your hierarchical data, such as Nested Sets and Closure Table (maybe relevant) which you might be interested to look into as well. each has different pros and cons in terms of select/insert/delete performance
EDIT:
I just noticed you edited your question, also that offers could belong to more than one region. The above might need adjustments to support assigning more than one region, but the basic idea could still be applied.

Related

Know which table are affected by a connection

I want to know if there is a way to retrieve which table are affected by request made from a connection in PostgreSQL 9.5 or higher.
The purpose is to have the information in such a way that will allow me to know which table where affected, in which order and in what way.
More precisely, something like this will suffice me :
id | datetime | id_conn | id_query | table | action
---+----------+---------+----------+---------+-------
1 | ... | 2256 | 125 | user | select
2 | ... | 2256 | 125 | order | select
3 | ... | 2256 | 125 | product | select
(this will be the result of a select query from user join order join product).
I know I can retrieve id_conn througth "pg_stat_activity", and I can see if there is a running query, but I can't find an "history" of the query.
The final purpose is to debug the database when incoherent data are inserted into the table (due to a lack of constraint). Knowing which connection do the insert will lead me to find the faulty script (as I have already the script name and the id connection linked).

Is it possible to use different forms and create one row of information in a table?

I have been searching for a way to combine two or more rows of one table in a database into one row.
I am currently creating multiple web-based forms that connect to one table in my database. Is there any way to write some mysql and php code that will take separate form submissions and put them into one row of the database instead of multiple rows?
Here is an example of what is going into the database:
This is all in one table with three rows.
Form_ID represents the three different forms that I used to insert the data into the table.
Form_ID | Lot_ID| F_Name | L_Name | Date | Age
------------------------------------------------------------
1 | 1 | John | Evans | *NULL* | *NULL*
-------------------------------------------------------------
2 |*NULL* | *NULL* | *NULL* | 2017-07-06 | *NULL*
-------------------------------------------------------------
3 |*NULL* | *NULL* | *NULL* | *NULL* | 22
This is an example of three separate forms going into one table. Every time the submit button is hit the data just inserts down to the next row of information.
I need some sort of join or update once the submit button is hit to replace the preceding NULL values.
Here is what I want to do after the submit button is hit:
I want it to be combined all into one row but still in one table
Form_ID is still the three separate forms but only in one row now.
Form_ID |Lot_ID | F_Name | L_Name | Date | Age
----------------------------------------------------------
1 | 1 | John | Evans | 2017-07-06 | 22
My goal is once a one form has been submitted I want the next, different form submission to replace the NULL values in the row above it and so on to create a single row of information.
I found a way to solve this issue. I used UPDATE tablename SET columname = newColumnName WHERE Form_ID = newID
So this way when I want to update rows that have blanks spaces I have it finding the matching ID's

Getting duplicate rows when querying Cloud SQL in AppMaker

I migrated from Drive tables to a 2nd gen MySQL Google Cloud SQL data model. I was able to insert 19 rows into the following Question table in AppMaker:
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| SurveyType | varchar(64) | NO | PRI | NULL | |
| QuestionNumber | int(11) | NO | PRI | NULL | |
| QuestionType | varchar(64) | NO | | NULL | |
| Question | varchar(512) | NO | | NULL | |
| SecondaryQuestion | varchar(512) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
I queried the data from the command line and know it is good. However, when I query the data in AppMaker like this:
var newQuery = app.models.Question.newQuery();
newQuery.filters.SurveyType._equals = surveyType;
newQuery.sorting.QuestionNumber._ascending();
var allRecs = newQuery.run();
I get 19 rows with the same data (the first row) instead of the 19 different rows. Any idea what is wrong? Additionally (and possibly related) my list rows in AppMaker are not showing any data. I did notice that _key is not being set correctly in the records.
(Edit: I thought maybe having two columns as the primary key was the problem, but I tried having the PK be a single identity column, same result.)
Thanks for any tips or pointers.
You have two primary key fields in your table, which is problematic according to the App Maker Cloud SQL documentation: https://developers.google.com/appmaker/models/cloudsql
App Maker can only write to tables that have a single primary key
field—If you have an existing Google Cloud SQL table with zero or
multiple primary keys, you can still query it in App Maker, but you
can't write to it.
This may account for the inability of the view to be able to properly display each row and to properly set the _key.
I was able to get this to work by creating the table inside AppMaker rather than using a table created directly in the Cloud Shell. Not sure if existing tables are not supported or if there is a bug in AppMaker, but since it is working I am closing this.

Tastypie URL Mapping, Mapping with mysql database

I have one table:
Table Name: "blog"
version_id(Primary Key) | blog_id | version_name | title | status
1 | 21 | draft | test | 1
2 | 21 | live | test | N/A
Users will use this url:-
GET: v1/blogs/{blog_id}/draft
PUT: v1/blogs/{blog_id}/draft
but tastypie will create url according to primary key: -
GET: v1/blogs/{version_id}
PUT: v1/blogs/{version_id}
According to me Pseudo code should be:-
select version_id
from blog
where version_id = 1 and version_name = draft
and using version_id I can create:-
v1/blogs/{version_id}
But I am not able to write url mapping in tastypie,
Please help
I would setup the blog table as follows
id | blog_id | version | version_name | title | status
1 | 21 | 1 | draft | test | 1
2 | 21 | 2 | live | test | N/A
Its good practice to always have a unique identifier for a table. I'm not quite sure if thats what you meant by version_id since your naming is confusing
tastypie will use the id field, but with that information you can easily find the parent/published blog and change it (since I guess that's what you're after). I'm also not sure what blog_id refers to. Conceptially it refers to the record (ie the id field), but perhaps your setup is different. I suggest you relook at your modelling to make sure its as simple as possible.

Derived Associations with Entity Framework

I've just started with Entity Framework this week, and am struggling with a few of the concepts.
Right now, I have a database structure that I am struggling to transfer across to entity framework.
I have started with the model first, and have this:
------------------ -----------------------
| Order_Item | | Order_FetchableItem |
---------------- ---------------------
| order_id | | order_id |
| item_id | | item_id |
------------------ | fetch_url |
-----------------------
The idea is that orders contain items, and this relation is conveyed in the order_item table. HOWEVER, some (not all) of the items in an order have a URL, so this property needs to be stored too.
I can't get this working in EF, because EF detects Order_Item as a relation, and I can't derive from it. What's the best alternative for doing this?
I have considered moving the fetch_url field to the Order_Item table, but as it is a wide column, I don't want lots of NULL values in the order_item table.
Thanks, and please excuse the formatting above!