简体   繁体   English

在laravel / lumen中创建临时表并插入数据

[英]Creating Temporary table in laravel/lumen and insert data

I want to create temporary table in Laravel/Lumen and I made schema like this. 我想在Laravel / Lumen中创建临时表,并制作了这样的架构。

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

When I run php artisan migrate I see... 当我运行php artisan migrate我看到...

Migrating: 2017_11_25_165640_create_temp_table
Migrated:  2017_11_25_165640_create_temp_table

... but it didn't create any tables. ...但是它没有创建任何表。 What happened? 发生了什么?

Temporary tables are session based. Temporary tables是基于会话的。 It is not created on SQL Server . 它不是在SQL Server上创建的。 You can have a look in this article in laracast . 你可以在看这个文章中laracast。

Temporary table can be used in lumen too. 临时表也可用于lumen We can use Schema Builder to create table and drop table. 我们可以使用Schema Builder create表和drop表。 Let assume, we have a function for a simple request. 假设,我们有一个用于简单请求的函数。 We could use temporary table like the following- 我们可以像下面这样使用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;
}

As Temporary Table are session based, you should always free-up memory by dropping tables at the end of your work. 由于Temporary Table是基于session的,因此您应该始终在工作结束时通过dropping表来free-up memory

A temporary table is a special type of table that allows you to store a temporary result set, which you can reuse several times in a single session . 临时表是一种特殊的表类型,它允许您存储临时结果集,您可以在一个会话中重复使用多次。 So, if you are trying to find them, you probably won't get it as they are session-based. 因此,如果您试图找到它们,由于它们是基于会话的,因此您可能会找不到它。

MySQL removes the temporary table automatically when the session ends or the connection is terminated. 当会话结束或连接终止时, MySQL自动删除临时表

You should keep in mind that these tables are created without indexes, so if your goal is to improve the speed of queries, adding an index after creating the table is usually desirable. 您应记住,这些表是在没有索引的情况下创建的,因此,如果您的目标是提高查询速度,通常最好在创建表后添加索引。

You can read more about it: http://www.mysqltutorial.org/mysql-temporary-table/ 您可以阅读有关它的更多信息: http : //www.mysqltutorial.org/mysql-temporary-table/

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

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