This question already has answers here:
What are the options for storing hierarchical data in a relational database?
(7 answers)
Closed 8 months ago.
I am saving data with unknown number of branches in JSON form for now and children can have unknown number of children.
example1 - {employee_1:{employee_2:{employee_3:{}}}}
example2 - {employee_3:{employee_2:employee_1:{}}, employee_4:{employee_3:{}}}
example hierarchical structure
Now to fetch this data, i have to first fetch this hierarchy saved as json and then fetch the id's stored in this json
is there any way to fetch this in one query?
or is there better approach to store and retrieve this?
i am using MySQL
Employee Table
id
name
1
Alice
2
Bob
3
Emily
4
Brian
Example 1 Employee Relation Table
id
parent_id
child_id
1
2
3
2
1
2
Example 2 Employee Relation Table
id
parent_id
child_id
1
4
3
2
2
1
3
3
4
4
3
2
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 2 years ago.
Hello to Folks out there,
I need some suggestion on How to design MySQL Table Structure for search optimization.
I am creating a Real estate website. In that I have property table and all its associated tables.
I can design my table for saving these records in two ways.
I have amenity master table
id property_name
----------------
1 Property A
2 Property B
3 Property C
Approach 1
Property Table
id property_name
----------------
1 Property A
2 Property B
3 Property C
Property_Amenities table
id p_id amenity_id
------------------------
1 1 1
2 1 2
3 1 3
4 1 4
5 2 1
6 2 1
Approach 2
Property Table
id property_name amenity_id
----------------------------
1 Property A 1,2,3,4,5
2 Property B 1,4,7,9,12
3 Property C 3,4,7,8,9,10
Approach 1 Query : I can join tables and get the all the amenities name for a particular property. Add the required index for optimization. For A property there will be 20 amenities on averages. Suppose I have 100K property records then to get amenities for particular property, MySQL query will search in 2 millions records of Property_Amenities table.
Approach 2 Query : I can search using FIND_IN_SET or IN MySQL operator. But I was going through this search topic and it seems that this approach will be much resource intensive and cost more for same amount of data i.e 100k property records.
Any suggestion will be appropriated. What your thought on this scenario or any other approach I should follow.
Use the first approach. Period. The second is wrong, wrong, wrong. Here are some reasons:
A SQL string should not be used to store multiple values.
Integers should be stored using the proper type.
Foreign key relationships should be declared.
SQL has very poor string handling methods.
SQL has a great way to store lists; it is called a "table" not a "string".
I am pretty new to mysql and this site. I got an old mysql database (100.000 entries) to migrate to our new system. This is the old table:
CUSTOMER
Customer_ID Name Categories
1 Bob 1,2
2 Phil NULL
3 Ines 10,8
4 Carol 1
5 Rick 13,2
And i need the following structure:
CUSTOMER
Customer_ID Name
1 Bob
2 Phil
3 Ines
4 Carol
5 Rick
Category
Category_ID Category_Name
1 Biker
2 Doctors
3 Teacher
... ...
13 Drivers
CustomerHasCategory
Customer_ID Category_ID
1 1
1 2
3 10
3 8
4 1
5 13
5 2
Thanks for any help.
I also had this problem but not in MySQL. I solved it with Python using the Pandas library. So, the exact steps I followed won't be useful for you. However, I'll show you the general idea behind the solution I used.
Below is image of the original column
First, I splitted the text into columns using the comas as the delimiter.
Next, I 'stacked' the columns
Finally, I removed the artefact column(s). So, I have only the ID and the values columns. This creates a one-to-many relationship.
This question already has answers here:
Possible to do a MySQL foreign key to one of two possible tables?
(6 answers)
Closed 8 years ago.
I am fumbling my way through phpMyAdmin and mySQL. I’m creating something where a customer can register multiple products. The data coming in is:
Category > Type > Size > Color
So for example:
Cookware > Oven > 5 qt > Blue
Bakeware > Casserole > 3qt > Blue
Accessories > Textiles > N/A > Blue
Etc.
I have set up one table with categories, and 4 tables to cover each product type.
Categories
ID Category
1 Cookware
2 Bakeware
3 Accessories
4 Serveware
Cookware Table
ID Type
1 Oven
2 Skillet
3 Roaster
Bakeware Table
ID Type
1 Casserole
2 Pie Dish
3 Baker
Etc.
Then, in the registration table, I set up a foreign key to link the category to the category table. So it would look something like this:
ID CustID Category Type Size Color
1 20 2 1 11 34
1 20 1 1 9 34
(sorry the formatting is so terrible! not sure how to fix)
But, I’m stuck on how to link the product type to the correct product type table since it is dependent on which category they picked. Hopefully this makes sense. Maybe I don't even need to link them and can still request the data somehow through a query?
Merge the last 2 tables ?
ID Type Category
1 Oven 1
2 Skillet 1
3 Roaster 1
4 Casserole 2
5 Pie Dish 2
6 Baker 2
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to use comma-separated values in a DB Column?
I want to create similar articles script. My idea is that articles would be similar by ID. For example if article have ID = 1 for this article, similar article will be with similar_id = 1
Table example:
ID Similar_ID
1 0
2 0
3 1
4 1
Article with ID 1, will have 2 extra articles with ID 3 and 4.
How to model table if there are more than similar ID ?
Example:
ID Similar_ID
1 0
2 0
3 1,2
4 1,2,3
You should use two tables, the original one you have above, and a separate one that has one row per Article-Similar Article.
So your original table would be: Articles
ID Content
1 bla
2 blah
3 etc.
4 whatever
And your other table would be as you have above: ArticlesSimilar
ID Similar_ID
3 1
3 2
4 1
4 2
4 3
You would ensure that the combination ID-Similar_ID were UNIQUE.
In my application,I have the entity "Department" with Hierarchical structure.
And when I list the department in the page,I want to make them indented as their level in the hierarchical tree,so I add the deep column to identify the location of the current department.
This is the table:
department_id parent_id deep
1 NULL 1
2 1 11
3 1 12
4 2 21
5 3 31
6 2 22
7 6 221
I display them in this manner:
.indent1:{margin-left:5px;}
.indent2:{margin-left:10px;}
.indent3:{margin-left:15px;}
<#list depList as dep>
<span class="indent${dep.deep?length}">${dep.name}</span>
</#list>
Then,each time I insert a new record I have to caculate the value of its deep,I want to know which is the most efficient way?
For example:
If a new Department is created,its paren_id is 2,then the deep if this department to be inserted should be '23',and I want to know how to caculate the value 23,any suggestion?
Update:
It seems that the qustion now is realted to the sql:
I want to select the max length deep field by a given parent.
I can only get the max length deep value in the whole table:
select deep from t_department where length(deep)=(select max(length(deep)) from t_department);
But how about if under a given parent?