<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 索引創建 ### 創建唯一索引 1. 在字段定義后,鏈式調用 `unique` 方法來創建索引: ~~~php $table->string('email')->unique(); ~~~ 2. 也可以在定義完所有字段之后,單獨創建索引: ~~~php $table->unique('email'); ~~~ ### 創建復合索引 傳遞一個包含字段名的數組至索引方法: ~~~php $table->index(['account_id', 'created_at']); ~~~ ### 自定義索引名稱 Laravel 會自動生成一個合理的索引名稱,但也可以使用第二個參數來自定義: ~~~php $table->index('email', 'my_index_name'); ~~~ ### 可用的索引類型 命令 | 描述 ------------- | ------------- `$table->primary('id');` | 添加主鍵。 `$table->primary(['first', 'last']);` | 添加復合鍵。 `$table->unique('email');` | 添加唯一索引。 `$table->unique('state', 'my_index_name');` | 自定義索引名稱。 `$table->unique(['first', 'last']);` | 添加復合唯一鍵。 `$table->index('state');` | 添加基本索引。 ### 配置索引長度 Laravel 默認使用 `utf8mb4` 字符,可支持在數據庫里存儲「表情」。 如果你使用的 **MySQL 版本低于 5.7.7** 或 **MariaDB 版本低于 10.2.2**,你可能需要手動配置默認字符串長度,以順利創建索引。 方法是修改 `app/Providers/AppServiceProvider.php` 文件,調用 `Schema::defaultStringLength` 方法: ~~~php use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * 引導任何應用程序服務。 * * @return void */ public function boot() { Schema::defaultStringLength(191); } ~~~ 或者你可以為數據庫開啟 `innodb_large_prefix` 選項,有關如何正確開啟此選項的說明請查閱數據庫文檔。 ## 索引移除 >[danger] 若要移除索引,則**必須**指定索引的名稱。 ### 傳遞完整的索引名稱字符串 >[info] Laravel 默認會自動給索引分配合理的名稱:數據表名稱_索引的字段名稱_索引類型。 命令 | 描述 ------------- | ------------- `$table->dropPrimary('users_id_primary');` | 從「users」數據表移除主鍵。 `$table->dropUnique('users_email_unique');` | 從「users」數據表移除唯一索引。 `$table->dropIndex('geo_state_index');` | 從「geo」數據表移除基本索引。 ### 傳遞包含字段名的數組 >[info] 索引名稱將由 數據表名 和 字段名 拼接而成 ~~~php Schema::table('geo', function (Blueprint $table) { $table->dropIndex(['state']); // 移除索引 'geo_state_index' }); ~~~ ## 外鍵約束 ### 創建外鍵約束 有 `user_id` 字段的 `posts` 數據表,`user_id` 引用了 `users` 數據表的 `id` 字段: ~~~php Schema::table('posts', function (Blueprint $table) { $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); }); ~~~ ### 指定外鍵約束的「on delete」及「on update」行為 ~~~php $table->foreign('user_id') ->references('id')->on('users') ->onDelete('cascade'); ~~~ ### 移除外鍵約束: `dropForeign` 方法 #### 傳遞完整的外鍵約束名稱字符串 >[info] 外鍵約束與索引采用相同的命名方式:數據表名稱_約束字段_foreign。 ~~~php $table->dropForeign('posts_user_id_foreign'); ~~~ #### 傳遞一個包含字段名的數組 ~~~php $table->dropForeign(['user_id']); ~~~ > 在移除的時候,字段會按照慣例被自動轉換為對應的外鍵名稱。 #### 在遷移文件里開啟和關閉外鍵約束 ~~~php // 開啟 Schema::enableForeignKeyConstraints(); // 關閉 Schema::disableForeignKeyConstraints(); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看