[英]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.