【问题标题】:Creating Temporary table in laravel/lumen and insert data在 laravel/lumen 中创建临时表并插入数据
【发布时间】:2018-05-09 15:24:58
【问题描述】:

我想在 Laravel/Lumen 中创建临时表,并制作了这样的架构。

Schema::create('temp_image', function (Blueprint $table) {
    $table->increments('id');
    $table->string('link');
    $table->timestamps();
    $table->temporary();
});

当我运行php artisan migrate 时,我看到...

Migrating: 2017_11_25_165640_create_temp_table
Migrated:  2017_11_25_165640_create_temp_table

...但它没有创建任何表。发生了什么?

【问题讨论】:

标签: php mysql laravel lumen


【解决方案1】:

Temporary tables 是基于会话的。它不是在SQL Server 上创建的。您可以查看 laracast 中的 this 文章。

临时表也可以在lumen 中使用。我们可以使用Schema Buildercreate 表和drop 表。 假设,我们有一个简单请求的函数。我们可以像下面这样使用temporary table-

public function temporary_check()
{
    Schema::create('temp_message', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('sender_id');
        $table->integer('receiver_id');
        $table->string('message');
        $table->timestamps();
        $table->temporary();
    });

    DB::table('temp_message')->insert(['sender_id'=>2,'receiver_id'=>3,'message'=>'message temp check']);

    $data = DB::table('temp_message')->get();

    Schema::drop('temp_message');

    return $data;
}

由于Temporary Table 是基于session 的,因此您应该始终在工作结束时使用free-up memory by dropping 表格。

【讨论】:

  • 这些表在 lumen 中是否也可用,如果它的会话库我们需要创建模型来如何从临时表中插入和检索值
  • 看看链接,他们解释的很好。
  • 找不到任何解决方案,为什么我在这里发布 wuestion
  • 这只是一个示例,您可以以更好的方式使用它,例如创建会话开始并删除会话结束的表
  • 真的很有帮助..我在 lumen 中使用所以没有会话请告诉我这个表什么时候到期,临时表可以通过迁移创建,我们可以在模型中使用因为我厌倦了模型但我收到错误
【解决方案2】:

临时表是一种特殊类型的表,可让您存储临时结果集,您可以在单个会话中多次重复使用该结果集。所以,如果你试图找到它们,你可能不会得到它,因为它们是基于会话的。

MySQL 在会话结束或连接终止时自动删除临时表

您应该记住,这些表是在没有索引的情况下创建的,因此如果您的目标是提高查询速度,通常需要在创建表后添加索引。

您可以阅读更多相关信息:http://www.mysqltutorial.org/mysql-temporary-table/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多