Suppose you have a table A related to another table B by a one-to-many (hasMany) relation. Table B can have other hasMany relations and so on.
For instance, I am trying to do it in cakePhp, but I think it is a general SQL problem.
Suppose that in every table there is a column, user_id.
There is a way to update all the columns user_id in all related table with one SQL construct? Or rather, which is the fastest way to do it? Now, I could not find a solution but doing it by iteration, and, obviously this is quite time-consuming and server-overloading.
Related
I have multiple tables in a Laravel app with 1-to-1 relationship such as users , users_settings , user_financial
And some 1-to-many relationships such as users_histories
My questions are:
1. Should I always include incremental id at the first?
for example is the id necessary in the Table #2 below?
Table 1:
id (primary,increments) , name, email, password
Table 2:
id (primary,increments), user_id, something_extra
^ why does every guide include this? // e.g. https://appdividend.com/2017/10/12/laravel-one-to-one-eloquent-relationships/
Can't I just use user_id as primary key and skip the incremental key? because I want to auto insert it on table 2 as soon as data is inserted in table 1.
2. How should I name 1-to-1 and 1-to-many tables in Laravel? `
I searched but didn't find any naming convention for different type of relationships...
Currently I do:
users table with primary key id is the base.
1-to-1: users_settings with foreign key user_id
1-to-many: users_histories foreign_key user_id
many-to-many: users_groups foreign_key user_id
should the first two tables be named settings/setting , histories/history instead? sorry I'm a little confused here.
I actually asked a similar question around 2 days ago. Its up to you but I'd say yes. In my case if I don't auto_increment all my ids in the related tables, data won't be associated with the correct user. However, there is an argument for saying auto_increment columns should not be used in this case, but they are useful for other things. According to some, the relationships might not be as meaningful so it'd be up to you and down to the specifics of you data tables for how meaningful the relationship will be. Regardless, you should research more into the advantages of auto_incrementing all your ids in related tables, as well as possible disadvantages before deciding what you want to do. Either way is fine, but they offer different advantages and disadvantages- which you'll need to compare and what works best for your specific case.
This is a well debated topic about the primary key. IMHO, No, you shouldn't. Every column in database should have a purpose. Following this, for your example, I agree that the auto_increment id is redundant and this is simply because it doesn't have a purpose. The second table is still uniquely describing the user so that the primary key should be the user_id.
Beside the above, there is another principle for me to decide whether I need the auto_increment id: whether I can see a table as an entity. For example, user is clearly an entity, but a relationship is not (in most cases), i.e., composite key can serves the purpose. But when an relationship table is extended to have more attributes and it starts to make sense for it to have an auto_increment id.
I don't have much experience on Laravel, but the naming for a database table should not be dictated by a framework. Comparing history and user_history, what a new DBA or developer expect from the two names without looking its data? user_history describes the table more precisely
I have two preexisting tables that were created by a CMS. I've mapped them in Symfony2 and I am trying to establish a relationship between two of the tables but I am now noticing that the CMS created these tables without a foreign key. This table table_uploads has a column that every other table is related to called column_table_name and column_record_num.
So assuming we have three tables... table_students, table_uploads, and table_teachers, the table table_uploads will have a value of either students or teachers in column_table_name to show its relation to the other two tables as well as a number in column_record_num that corresponds to the other table's primary key. Neither of the three tables actually have a defined foreign key however.
In Symfony, I'd like to call a table
$students = $this->getDoctrine()->getRepository('SchoolBundle:Students')->findAll();
and then load the related records from another table in such a way like (I know select_related doesn't actually exist, but this is essentially the functionality I'm looking to create)
$students->select_related()
Is this at all possible without the existence of a foreign key? The only way I can think of doing it is grabbing the column values from $students and running another query to other table with these values and merging the two results.
You could create a custom repository method that would join unrelated tables. DQL join between unrelated entities?
Or is it enough to have just the relations defined in the model. I have been trying to connect tables users and groups and got only to a point where only last connection in array got saved ...
Just to add, I am using table users_groups as a join table. This table tas fields user_id and group_id. Find all works like a treat.
All you need to do as far as your database is concerned is to make sure you're using the correct table name and field(s). Then, as long as you have your model associations set up correctly, you should be good to go.
In your case, your table should be 'groups_users', not 'users_groups' (they should be in alphabetical order).
So I have 2 tables in my database, they are 'workouts' and 'exercises'. Workouts contains a row called exercises which is a comma-separated list of exercise IDs - from the 'exercises' table e.g. '1,2,3'.
My question is, can I write a single query to allow me to select a row from the workouts table, say one with an id of 1, and have MySQL fetch each of the exercises from the list in that row, returning them within the 'workout' row?
At the moment I'm using PHP to select the workout row, and then making individual requests for each of the exercises, resulting in serious inefficiency.
I took a look at Joining rows as array from another table for each row and also did some research into the group_concat() function, but I'm not sure that's what I'm after.
Update
Here are the 2 tables:
IMO, the best approach is to redesign your schema to have a cross-reference table called exercises_workouts (or something similar). Remove the CSV field.
Here's page that goes into more detail on implementing a many-to-many relationship:
http://www.tonymarston.net/php-mysql/many-to-many.html
Note: The linked page uses the mysql_* functions, but the general explanation of the approach stands. You'll want to look into PDO for database access.
I can't find how to insert a row that doesn't have a PK. This isn't possible with a class that is extending Zend_Db_Table_Abstract, so how should I do this?
The table I in which I want to insert records is a join table.
The only two columns in it could serve as a PK, but I don't know how I should let Zend know that neither.
Any suggestions?
Zend doesn't really care if the table have in fact a PK (or an index) or not. The primary_key option in a Zend_Db_Table_Abstract is really just to know which column the 'WHERE' clause will search for when using find and find* methods, etc. So, basically, just specify which columns to use as reference columns for your db model. I can't give you more details since I don't know the details of the said table.