简体   繁体   English

我是否必须将外键关系与与另一个表相关的表中的任何列放置? 幼虫 8,SQL

[英]Do i have to put a foreign key relation to any column in a table with relation with another table? larvavel 8, Sql

I have multiple tables, and some do have columns which are related but I am not certain if I have to put the foreign key relations to all columns which are related.我有多个表,有些确实有相关的列,但我不确定是否必须将外键关系放到所有相关的列。

Here are some of my migrations schemas.这是我的一些迁移模式。

users用户

Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->boolean('role')->nullable( );
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken()->nullable();
        $table->timestamps();
    });

bids出价

Schema::create('bids', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('loan_id');
        $table->unsignedBigInteger('user_id');
        $table->decimal('interest');
        $table->string('PayType');
        $table->integer('IntervalPay');
        $table->string('GracePeriod');
        $table->timestamps();
        $table->foreign('user_id')
        ->references('id')->on('users')->ondelete('cascade');
        $table->foreign('loan_id')
        ->references('id')->on('loan_request')->ondelete('cascade');
    });

loan_contracts贷款合同

 Schema::create('loan_contracts', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('lender_id');
        $table->unsignedBigInteger('borrower_id');
        $table->integer('LoanType');
        $table->Biginteger('amount');
        $table->decimal('interest');
        $table->string('GracePeriod');
        $table->string('PayType');
        $table->integer('IntervalPay');
        $table->timestamps();
    });

loan_request贷款请求

Schema::create('loan_request', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('users_id');
        $table->integer('LoanType');
        $table->Biginteger('amount');
        $table->string('PayType');
        $table->integer('IntervalPay');
        $table->string('GracePeriod');
        $table->timestamps();
        $table->foreign('users_id')
        ->references('id')->on('users')->ondelete('cascade');
    });

If you observe you will see common column names.如果您观察,您将看到常见的列名。

You do it because there are relations - so you can click user_id in phpmyadmin/whatever and jump to proper userid.您这样做是因为存在关系 - 因此您可以单击 phpmyadmin/whatever 中的 user_id 并跳转到正确的用户 ID。 In order to delete, you remove the user and mysql removes relations without additional harassment.为了删除,你删除用户和 mysql 删除关系没有额外的骚扰。

In short - convenience.简而言之 - 方便。 You don't need leftovers from tables that act on non-existing users.您不需要作用于不存在用户的表中的剩余部分。

Well, in fact, when you use $table->foreign('user_id') besides creating a key, Laravel is also creating the regarding index for that foreign key.好吧,事实上,当你使用$table->foreign('user_id')除了创建一个键之外,Laravel 也在为该外键创建相关索引。

The main benefit of foreign keys is that they enforce data consistency, meaning that they keep the database clean.外键的主要好处是它们强制数据一致性,这意味着它们保持数据库清洁。 It is true that foreign keys will impact INSERT, UPDATE, and DELETE statements because they are data checking, but they improve the overall performance of a database.确实,外键会影响 INSERT、UPDATE 和 DELETE 语句,因为它们是数据检查,但它们提高了数据库的整体性能。

An index is a pointer to data in a table.索引是指向表中数据的指针。 An index in a database is very similar to an index in the back of a book.数据库中的索引与书后的索引非常相似。 Searching for data based on a column that is part of the index will allow us to make use of the index to quickly access the record.基于作为索引一部分的列搜索数据将使我们能够利用索引快速访问记录。

It's not just about checking if a related record/relationship exists to keep consistency.这不仅仅是检查是否存在相关记录/关系以保持一致性。 If you work with millions of records, you will notice this improvement easily (A query performance can be improved from 20secs down to a few milliseconds).如果您使用数百万条记录,您会很容易注意到这种改进(查询性能可以从 20 秒提高到几毫秒)。

So the short answer is yes , you should define your foreign keys.所以简短的回答是肯定的,你应该定义你的外键。

There is an interesting (and accepted) answer it this post https://stackoverflow.com/a/1130/2330666这篇文章有一个有趣(并且被接受)的答案https://stackoverflow.com/a/1130/2330666

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM