sahar_mkr's avatar

usage of pivot table

Hi everyone, I have a log table with a column that has so many duplicated fields, for example, 19 rows of 20 are filled with 1, I've heard somewhere that in this case I can use a pivot table in order to remove sparsity, pivot table is for many to many relationships, but in my case, I have one to many, what should I do?

0 likes
3 replies
LaryAI's avatar
Level 58

If you have a one-to-many relationship, then using a pivot table may not be the best solution. Instead, you could consider using a separate table to store the repeated values and then reference that table in your log table.

For example, let's say you have a log table with a column called "status" that has many repeated values. You could create a separate table called "statuses" with two columns: "id" and "name". Then, in your log table, you would replace the "status" column with a foreign key to the "statuses" table.

Here's an example migration to create the "statuses" table:

Schema::create('statuses', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});

And here's an example migration to add a foreign key to the "logs" table:

Schema::table('logs', function (Blueprint $table) {
    $table->unsignedBigInteger('status_id')->nullable();
    $table->foreign('status_id')->references('id')->on('statuses');
    $table->dropColumn('status');
});

With this setup, you can now insert a new status into the "statuses" table and reference it in your log table:

$status = Status::create(['name' => 'Completed']);
$log = new Log;
$log->status_id = $status->id;
$log->save();

This approach can help reduce sparsity in your log table and make it easier to manage repeated values.

webrobert's avatar
Level 51

@sahar_mkr,

as the name suggests, a pivot_table is used when a foreign_id can no longer be store on either table. aka a many to many relationship. The pivot allows for the two table ids (and sometimes other columns) to relate.

Say a user can be a part of multiple projects and projects also can have more the one user. There is no way to store the foreign id on either table.

so a pivot is made, project_user (note that this is singular and in alphabetical order) with some columns... project_id, user_id and lets toss in a role_id too.

Now a user can be a part of many projects and a project can have many users and a user can have a different role on each project.

What you're describing is something else.

Imagine a user isn't allowed a profile unless they meet some criteria... this is totally contrived. In this case storing empty profile fields on the user table for a ton of users is dumb and stupid. It just is ok! So we create a profiles table that stores all the profile information for those select special users.

This way we avoid a bunch of empty columns in the user table.

Your case doesn't sound like there is anything to gain. Only you know.

But if you think about adding another table you are adding at least one more column, one for the log_id and one for whatever you are trying to remove from the log table. And I didnt hear you mention that there were empty columns... so what do you gain?

sahar_mkr's avatar

@webrobert yep, thanks for the response, I just wanted to get rid of the empty and sparsity of columns, in order to use them for the next analysis or other things, thanks for your useful advice.

Please or to participate in this conversation.