我是否必须将外键关系与 table 中与另一个 table 相关的任何列关联起来?幼虫 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
我有多个表,有些确实有相关的列,但我不确定是否必须将外键关系放在所有相关的列中。
这是我的一些迁移模式。
用户
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();
});
出价
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');
});
如果您观察,您会看到常见的列名。
你这样做是因为有关系-所以你可以在phpmyadmin/whatever中单击user_id并跳转到适当的用户ID。为了删除,您删除了用户并mysql删除了没有额外骚扰的关系。
简而言之——方便。您不需要作用于 non-existing 用户的表的剩菜。
嗯,事实上,当您使用 $table->foreign('user_id')
除了创建一个键之外,Laravel 也在为该外键创建相关索引。
外键的主要好处是它们加强了数据一致性,这意味着它们保持数据库清洁。
的确,外键会影响INSERT、UPDATE、DELETE语句,因为它们是数据检查,但它们提高了数据库的整体性能。
索引是指向 table 中数据的指针。数据库中的索引与书后的索引非常相似。根据作为索引一部分的列搜索数据将使我们能够利用索引快速访问记录。
这不仅仅是检查是否存在相关 record/relationship 以保持一致性。如果您处理数百万条记录,您会很容易注意到这一改进(查询性能可以从 20 秒缩短到几毫秒)。
所以简短的回答是是,你应该定义你的外键。
有一个有趣的(并且被接受的)回答这个 post
我有多个表,有些确实有相关的列,但我不确定是否必须将外键关系放在所有相关的列中。
这是我的一些迁移模式。
用户
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();
});
出价
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');
});
如果您观察,您会看到常见的列名。
你这样做是因为有关系-所以你可以在phpmyadmin/whatever中单击user_id并跳转到适当的用户ID。为了删除,您删除了用户并mysql删除了没有额外骚扰的关系。
简而言之——方便。您不需要作用于 non-existing 用户的表的剩菜。
嗯,事实上,当您使用 $table->foreign('user_id')
除了创建一个键之外,Laravel 也在为该外键创建相关索引。
外键的主要好处是它们加强了数据一致性,这意味着它们保持数据库清洁。 的确,外键会影响INSERT、UPDATE、DELETE语句,因为它们是数据检查,但它们提高了数据库的整体性能。
索引是指向 table 中数据的指针。数据库中的索引与书后的索引非常相似。根据作为索引一部分的列搜索数据将使我们能够利用索引快速访问记录。
这不仅仅是检查是否存在相关 record/relationship 以保持一致性。如果您处理数百万条记录,您会很容易注意到这一改进(查询性能可以从 20 秒缩短到几毫秒)。
所以简短的回答是是,你应该定义你的外键。
有一个有趣的(并且被接受的)回答这个 post