What would be the most maintainable solution to define tables that have many columns in common? - postgresql

I have the following schema (which is one approach):
CONTACTS
--------
|id |
|name |--------------------------
-------- \ \
| \ \
| \ \
^ ^ ^
PHONE_NUMBERS ADDRESSES EMAILS
-------------- -------------- --------------
|id | |id | |id |
|FK(contacts)| |FK(contacts)| |FK(contacts)|
|preferred | |preferred | |preferred |
|type | |type | |type |
|inserted_at | |inserted_at | |inserted_at |
| ---------- | | ---------- | | ---------- |
|phone_no | |address | |email |
-------------- |city | --------------
|(...) |
The other two solution I came up with was (1) inheritence and (2) dumping all of them in one table which is probably the ugliest. (Or maybe I am doing something fundamentally wrong.)

This is the right approach when designing OLTP systems. Please remember, that having multiple tables in your database don't make reading from them slower and is called database normalisation.
It's better to have a dictionary table contacts and manage them within one table and add references to it using foreign keys in different tables implementing 1:N relationships between entities.
You could dig deeper into normalisation and also create a table for lookup on your type column for each table to avoid data redundancy.
You could think of table inheritance but try to model it the way your business realities are represented. Avoid second approach which is "the ugliest" as you've already noticed.
Inheritance might not be what you're looking for - inspect FROM ONLY clause. You still have different types per entity.

Related

PostgreSQL arabic case insensitive

I am looking for how to search a database using Arabic text. In Arabic there are some letters that can be written in different ways but in the results they should all show up if one of them is included in the where clause.
The famous example for this would be:
SELECT * FROM persons WHERE name = "اسامة";
+----+--------------+
| id | name |
+----+--------------+
| 3 | أسامه |
| 4 | أسامة |
| 5 | اسامه |
| 6 | اسَامه |
+----+--------------+
4 rows in set (0.00 sec)
I found a good and probably most performant way to do this by creating a custom collation on MySQL in this article but I have no idea how that is done or if it is possible at all in PostgreSQL.
Other ways that include changing the query itself to use Regex are not useful for my use case.
Can someone please guide me how to do the same

Postgres DB Schema - multiple columns vs one json column

I have a db that contains username with 3 different phone numbers and 3 different ids. also we will have 3 different type of notes for each username.
I am using postgres and data are planned to increase to millions of rows. querying and inserting new data process are really important to be fastest way.
which schema would be better for that:
username | no1(string) | no2(string) | no3(string) | id1(string) | id2(string) | | id3(string) | note1(string) | note2(string) | note3(string)
OR
username | no(JSON) | id(JSON) | note(JSON)

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.

Is it possible to get 3 select query results by executing only one stored procedure?

I have to display data in the following format
-----------------------------------------------------------
| Group Name | Description | Assigned Users | Super Groups|
-----------------------------------------------------------
|Group1 | Blah Blah | User1 | SPG1 |
| | | User2 | SPG3 |
| | | User3 | |
-----------------------------------------------------------
| Group2 | More Blah | User1 | SPG5 |
| | | User13 | |
-----------------------------------------------------------
Assigned users and Super groups data are coming from unrelated tables. Now I wonder whether is it possible to get 3 select query results in one shot (i.e. the same procedures returns 3 results). Otherwise I'm going to query the groups and users first, get the group IDs then query super groups.
So again, Is it possible to get 3 select query results by executing only one stored procedure?
Yes, just include 3 select statements.
If you're consuming these in .net and storing them in a DataSet you'll have 3 tables in the DataSet.
Example:
create procedure test
as
select 1 as res1;
select 2 as res2;
select 3 as res3
exec test
Yes. You'll have to include the three statements in your stored procedure. Take a look at this post.

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!