For example, I have a table positions in which I would like to create a relationship between multiple table rows. If the number of rows that need to be "linked" together is unknown, what makes the most sense with a separate table to link the ids?
Is it best to...
Create a separate table links with many columns (ie: linked_id1 [...] linked_id[n]) where the linked_ids are the id of each row and the links.id column is the id of the link. The logic is that we can create more columns than we think we will need.
OR
Create a separate table links with two columns, id and linked_ids where linked_ids is a comma separated array of the "linked" rows from positions.
OR
Do the aforementioned replacing the array with JSON data.
Each row can only be linked into one group, which hopefully simplifies things.
If each row can be included into multiple linked groups then you can create LINKS table with two fields: ID1, ID2 that will store each individual link.
If each row can be only in a single linked group then you can add GROUP_ID field to the positions table itself.
If the above does not answer your question, please clarify your requirements.
Related
I want to create an order which then inserts multiple rows into another table. Which will work like the following:
User selects one or more items from tableOfStuff then places an order which INSERTS a new order into tableOrders. When this insert is created I need to copy the ID created for that order into a TRIGGER which then creates multiple rows in tableOrderDetails. In this table I want a row created for each of the IDs from tableOfStuff and then the one new OrderID (that gets created for the order) repeated in each of these rows.
How would I achieve this in MySQL? Any tutorials or examples would be great or topic areas I should search the net for?
TableOfStuff - PK: ToS_ID
TableOrders - PK: ToO_ID
TableOfStuffOrders - FK: ToS_ID && ToO_ID
ToO_ID would only be one ID but the ToS_ID would be one or more IDs.
I'm not quite sure how to word this so I've not managed to find an answer for it!
I want to create a table, where in certain columns there is multiple rows. As shown in the picture
How do I structure this?
An example of what I'm trying to achieve is imagine a table that listed all users in an application. Each row is a user, but I want to also have a sub row for each of the photos that a user may have.
You cannot have multi-valued columns. As they do not satisfy first normal form (For detailed information about 1st Normal form: https://en.wikipedia.org/wiki/First_normal_form)
Let your table have the following columns:
(A,B,C,D)
As per the picture provided by you, I am making the following assumptions:
1) A is the primary key
2) C and D are both are multi-valued and the rows have values like (A1,B1,C1,D1),(A1,B1,C2,D2). That is for single A value we have multiple pairs of C and D.
Do comment if any of the assumption is wrong.
What you can do is make two tables.
TABLE1 (A,B)
TABLE2 (A,C,D)
Where A is the primary key in TABLE1 and foreign key in TABLE2.
As asked for a snippet,you can have tables like these:
Tables
Suppose I have a pictures table and in each row I have a column with keys to displaying elements. Each picture may have multiple displaying elements. What is the most efficient way to store them so the selection by a displaying element won't take long?
How helpful can Geometry types be in this case?
Could the option of storing the keys as a multi-point sequence be a solution for fast query?
This is called a many-to-many relation and should be mapped with an additional table
table pictures
columns id, name, ...
table elements
columns id, name, ...
table picture_elements
columns picture_id, element_id
I have 2 tables.
First table is called professions, and those are indexed by ID. So each profession now has a unique ID associated with it.
My second table is called contacts, and in there I have a profession field that right now only hold the ID that a certain profession is associated with.
My problem is that what if I have a contact that has more than one profession associated with it.
What would be the best way to query the table and ways to store the professions of a contact. I didn't want to do is create a field to just store a 0 or 1 int for each profession I have. The reason is because I want to dynamically grow the professions table and have the numbers reflect any dynamic changes on my site when I query.
You have a many-to-many relationship. To implement that in MySQL you should use a linking table. So professions and contacts should have an id in each table, but no foreign keys, and you create a new table called profession_contact_links or something, containing its own id, and profession_id and contact_id, which are both foreign keys to the respective tables. Then you can have many contacts linked with each profession, and many professions linked with each contact. To connect the two main tables together in a select you will need two joins, and what they are will depend on what exactly you want to select.
The standard solution to this modelling issue is called a link table.
Basically it is a table that contains the ids of the two tables that are linked, so you would have a link table with to columns and a primary key that is both of those columns:
(profession_id, contact_id)
or the other order... doesn't matter that much, but the order can affect performance, the key you will be searching on most often is the one you want first.
You then use either SELECT ... IN (...) or SELECT ... JOIN ... to query the data that you are after.
Depending on what you want and how you want to find it, i'd suggest rlike or in
SELECT ... FROM <table> WHERE <column name> RLIKE "^id1|id2|id3$"
This will find any cell that contains any of those three terms
or use
SELECT ... FROM <table> Where <column name> IN ('id1','id2','id3')
this will find any cell that is equals to one of those three.
for example,access example db, every student record have a nested table about guardians.
http://img101.imageshack.us/img101/9881/53882937.jpg
No. Something like that is almost always done as a single mapping table for all students, with a foreign key column pointing to the student table to specify which student a particular row relates to.
You then just filter the table to match a given student, and present that list without a student column, in the UI. It looks like a separate table to the user, but that's not actually how it's stored.
(If you did create a separate guardians table for each student, you'd make it impossible to do queries like ‘find students for a particular guardian’.)