Every field can have multiple values - mysql

I am asked to allow users to input multiple values in EVERY field. So the option is limitless.
For example. Columns are:
CompanyID-
Company name
Website
Key_Markets
M&A_History
Highlights
Region
Comments
A scenario is a company can have multiple websites,key markets, region, etch. How would I do this professionally? I am thinking of putting every column a seperate table.

Basically there are three ways to realize this.
1) Write multiple fields into one column seperately. This would be a very bad design and you would have to handle the splitting in your application - Do not do that ;-)
2) Use one table with multiple groups to store the data. This would make sense for parameters but not really if you have different values for each customer. For example:
CompanyID
GroupID
Position
Value
Example:
108001, 'homepage', 1, 'www.mypage.com';
108001, 'homepage', 2, 'www.mysecondpage.com';
108001, 'homepage', 3, 'www.anotherpage.com';
108001, 'markets', 1, 'erp';
108001, 'markets', 2, 'software';
108001, 'region', 1, 'germany';
108001, 'region', 2, 'austria';
108001, 'region', 3, 'poland';
3) Use seperate tables for each 1:n relation! This would be the best solution for your needs I guess. This would have the advantage that you can easily extend your schema and store more data in it. For example if you decide to store the amount of users for each region or key markets etc.
Another point: Use n:m relations to avoid double content in your database! For example should the key-markets and regions be stored in a completely seperated table and you store the IDs of the customer and the key-market in a crosstab. So you do not need to store the key-markets as a string for each customer!

You would need a database structure like:
table_master_companies
- record_id
- company_name
table_websites
- record_id
- company_id
- website_address
table_key_markets
- record_id
- company_id
- key_market
etc. You would then need to use joins to concat all the information into a single recordset.

Related

How to map IDs in different formats from source to destination using SSIS

My source data is in PostgreSQL and my target data is in Common Data Service (D365).
On the source, I have employees with these IDs: 1, 2, 3, 4, ...
On target, the IDs are a little bit more complex (GUID).
How can I map these IDs? I need to know that the employee with the Id 1 (source) corresponds to the worker with the Id b15a1bd9-50e2-ea11-a81b-000d3a33f94a (target).
PS: I can't edit the source database.
I appreciate your help.
Best regards,
Ana Alves.

Is it a good practice to store SQL query in the database?

My Usecase:
We allow the users of the system to create a list(i.e segment) of contact details based on the variety of the filters from the UI. So they create the segment using a form to select say all people living in a particular state.
So the issue here is that, when the new contacts are added, I want these segments to be updated as well. The solution that I can think of is saving SQL query for each segment and update each segment when new contacts are added using these SQL queries.
I thought of saving only parameters in a json string format but complication in that case is filters available are over multiple tables and there is no way to generate the dynamic SQL query as it involves joins and different filters (i.e. 'in', 'endswith', etc).
I don't think either of them is the best solution. Any better ideas?
Any help would be appreciated, thanks in advance!
Update: JSON parameter and Query I need
{'is_buyer': ['1'], 'is_executive_member': ['1'], 'is_member': ['1'], 'survey_participant': ['1'], 'cluster': ['A'], 'state': ['state1'], 'city': ['ab', 'cd'], 'gender': ['male']}
Expected query output:
SELECT * FROM `contacts` INNER JOIN `contacts_clusters` ON (`contacts`.`KEY` = `contacts_clusters`.`KEY`) WHERE (`contacts`.`IS_BUYER` IN (1) AND `contacts`.`IS_EXECUTIVE_MEMBER` IN (1) AND `contacts`.`IS_MEMBER` IN (1) AND `contacts`.`SURVEY_ID` IS NOT NULL AND `contacts_clusters`.`CLUSTER` IN ('A') AND `contacts`.`STATE` IN ('state1') AND `contacts`.`CITY` IN ('ab', 'cd') AND `contacts`.`GENDER` IN ('male'))
This is just one of the example of segment filter. There are much more parameters and tables which can used for the creation of segment.
I think you'd be better off storing the required parameters for the segment, then when new contacts are added, you'd check if the new contacts match according to the stored segment parameters. If they do, then you'd update the segment.

How to create a view?

I'm pretty new in App Maker. I want to create an app that will collect various types of request (failures, new ideas, orders and so on ). For each type of request will be separate data model. Every data model (request) contains 3 the same information: date, applicient, comments.
In addition to the dashboard's stand for each type of request, I want to make one in which all entries will be displayed with only repating records and type of request as one more record (date, applicient, comments, type of request)
I think that Calculated Model is the answer here, but despite getting acquainted with the documentation, I don't know how to implement this in my case. Could anybody halp me with this ?
Below I am presenting the display of the above description. Records 1, 2, 3 ... presents records that don't replicate in another data model.
IMAGE:
https://drive.google.com/file/d/1gi6ylZacOVSkcqtpaupRpIOrzbq7fOsT/view?usp=sharing
I tried to do relations, but I couldn't displey this in one table, what is my goal. How to conigure the SQL datasource to do this ?
You can create a new calculated SQL model with UNION:
(SELECT 'Failures' AS REQUEST_TYPE, C.* FROM Failures AS C)
UNION ALL
(SELECT 'New Ideas', C.* FROM `New ideas` AS C)
UNION ALL
(SELECT 'Orders', C.* FROM Orders AS C);
You must have corresponding fields in your datasource that match the sql column names: REQUEST_TYPE, Date, Applicient, Comments
Reference:
Cloud sql model

MySQL Select Data from multiple tables based on one id

I have a database with 2 tables that look like this:
content
id name
1 Cool Stuff
2 Even Better stuff
--
contentFields
id content label value
5 1 Rating Spectacular
6 1 Info Top Notch
7 2 Rating Poor
As you can see the content column of the contentFields table coincides with the id column of the content table.
I want to write a query that grabs all of the content and stores the applicable content fields with the right content, so that it comes out to this:
[
{
id: 1,
name: 'Cool Stuff',
contentFields: [
{label: 'Rating', value: 'Spectacular'},
{label: 'Info', value: 'Top Notch'}
]
},
{
id: 2,
name: 'Even Better Stuff',
contentFields: [
{label: 'Rating', value: 'Poor'}
]
}
]
I tried an inner join like this:
SELECT * FROM content INNER JOIN contentFields ON content.id = contentFields.content GROUP BY content.id
But that didn't do it.
*Note: I know that I could do this with 2 seperate queries, but I want to find out how to do it in one as that will dramatically improve performance.
What you are trying to achieve is not directly possible with SQL only.
As you have already stated yourself, you are looking for a table within a table. But MySQL does not know about such concepts, and as far as I know, other databases also don't. A result set is always like a table; every row of the result set has the same structure.
So either you let your GROUP BY content.id in place; then, for every row in the result set, MySQL will select a random row from the joined table which fits to that row's content.id (you even can't rely on that it is the same row every time).
Or you remove the GROUP BY; then you will get every row from the joined table, but that is not what you want as well.
When performance is an issue, I would probably choose the second option, adding ORDER BY content.id, and generate the JSON myself. You could do so by looping through the result set and begin a new JSON block every time the content.id changes.
Disclaimer The following is pure speculation.
I don't know anything about node.js and how it transforms result sets into JSON. But I strongly assume that you can configure its behavior; otherwise, it actually would not be of any use in most cases. So there must be a method to tell it how it should group the rows from a result set.
If I am right, you would first have to tell node.js how to group the result set and then let it process the rows from the second option above (i.e. without the GROUP BY).

Thumbs up/down on multiple elements

I am building a music application. In my database I have an "Artist" table, related to an "Album" table, related to a "Track" table.
Each user of my application can "like" (thumbs up) or "dislike" (thumbs down) an artist/album/track. Thus, I have to create Many-to-many relationships between users and the artists/albums/tracks with an argument "vote" which can be set to 1/-1.
My question is : Would it be more appropriate to create three "Like/Dislike" tables (user_artist_like, user_album_like, user_track_like) or only one table "user_like" with three columns (artist_id, album_id, track_id) ? Knowing that I will often have to fetch all the likes of a user.
The first option is better, because putting the data in one table implies that data points in the same row are related, which is not true. Multiple tables allows you to manage the data more easily without getting confused by the rows. For instance, how would you structure a INSERT INTO statement for the single table? It couldn't use the space effectively.
Just in case anyone else reads this, you can set up a table that has a column type to handle this.
{
user_id: 7,
object_id: 12,
type: 'album',
is_liked: 1
}
{
user_id: 7,
object_id: 8,
type: 'track',
is_liked: 0 //this as 0 means disliked
}