博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql临时表_SQL临时表
阅读量:2533 次
发布时间:2019-05-11

本文共 3682 字,大约阅读时间需要 12 分钟。

sql临时表

In real time scenario, consider that you have read-only access on a table and you have to manipulate some data in it. It is always useful to create a temp table when you want to use the table in the current session.

在实时方案中,请考虑您对表具有只读访问权限,并且必须操纵其中的某些数据。 当您要在当前会话中使用临时表时,创建临时表总是很有用的。

介绍 (Introduction)

In this tutorial, we will discuss the temporary table in MySQL. Following are some features of the temporary table.

在本教程中,我们将讨论MySQL中的临时表。 以下是临时表的一些功能。

  • MySQL removes a temporary table when we close the session or if we close the connection.

    当我们关闭会话或关闭连接时,MySQL会删除一个临时表。
  • A temporary table is only visible to the client who has created it.

    临时表仅对创建它的客户端可见。
  • A temporary table can share the same name as a normal table.

    临时表可以与普通表共享相同的名称。
  • Two temporary tables can also have the same name if there are in different sessions.

    如果两个临时表处于不同的会话中,则它们也可以具有相同的名称。
  • If the temporary table and the existing normal table shares the same name. The normal table becomes inaccessible until the temporary table is not removed.

    如果临时表和现有的普通表共享相同的名称。 在不删除临时表之前,普通表将无法访问。

We will now consider creation, use and removal of a temporary table in MySQL.

现在,我们将考虑在MySQL中创建,使用和删除临时表。

创建一个临时表 (Create a Temporary Table)

Syntax for Create Temporary table: –

创建临时表的语法:–

CREATE TEMPORARY TABLE table_name SELECT column(s) FROM existing_table;

In the above syntax, due to the keyword Temporary, a temporary table gets created.

在上述语法中,由于关键字Temporary,将创建一个临时表。

Let us create a Library table and we will use the same table for the creation of a temporary table.

让我们创建一个Library表,并将使用相同的表创建一个临时表。

CREATE TABLE `library` (`idLibrary` int(11) NOT NULL,`BookTitle` varchar(45) DEFAULT NULL,`BookQuantity` int(11) DEFAULT NULL,`Author` varchar(45) DEFAULT NULL,`BookPrice` float DEFAULT NULL,PRIMARY KEY (`idLibrary`),UNIQUE KEY `idLibrary_UNIQUE` (`idLibrary`))

The below-mentioned query will be used for data insertion.

以下查询将用于数据插入。

INSERT INTO `test`.`library`(`idLibrary`,`BookTitle`,`BookQuantity`,`Author`,`BookPrice`)VALUES(1,'The Chamber of Secrets',10,'J K Rowling',20.99);INSERT INTO `test`.`library`(`idLibrary`,`BookTitle`,`BookQuantity`,`Author`,`BookPrice`)VALUES(2,'One night at the call center',13,'Chetan Bhagat',100.99);INSERT INTO `test`.`library`(`idLibrary`,`BookTitle`,`BookQuantity`,`Author`,`BookPrice`)VALUES(3,'The God of Small things',11,'Arundhati Roy',120.99);INSERT INTO `test`.`library`(`idLibrary`,`BookTitle`,`BookQuantity`,`Author`,`BookPrice`)VALUES(4,'War and Peace',5,'Leo Tolstoy',80.00);
SQL Table Before Copy

SQL Library Table 

SQL库表

We will create a temporary table with book quantity of more than 10.

我们将创建一个图书数量大于10的临时表。

Create temporary table libary_10 select * from library where bookquantity>10;

Now we will use statement for check if the table is created.

现在,我们将使用语句检查表是否已创建。

Select * from libary_10;
Temp Table Created

Temp Table Created

临时表已创建

临时表的用法 (Usage of Temporary Table)

Now we have the table created, assume we would like to get the highest book price. We will use the below mentioned query to get the result set.

现在我们已经创建了表格,假设我们想获得最高的书价。 我们将使用下面提到的查询来获取结果集。

Select max(bookprice) from libary_10;
Usage Of Temp Table

Usage Of Temp Table

临时表的用法

Now we will understand how to remove a temporary table.

现在,我们将了解如何删除临时表。

删除临时表 (Remove Temporary Table)

Syntax for Drop Temporary Table:

删除临时表的语法:

Drop Temporary Table temp_table_name;

The Temporary keyword is used to make sure that by mistake the permanent table is not deleted. If we try to remove a permanent table using the above-mentioned syntax, we will get an error.

Temporary关键字用于确保错误地删除了永久表。 如果我们尝试使用上述语法删除永久表,则会收到错误消息。

Let us see how to drop the temporary table.

让我们看看如何删除临时表。

Drop Temporary Table libary_10;

That’s all for SQL Temporary table;

这就是SQL临时表的全部内容;

翻译自:

sql临时表

转载地址:http://cnlzd.baihongyu.com/

你可能感兴趣的文章
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>