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 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 Select statement for check if the table is created.

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

Select * from libary_10;

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

临时表的用法

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临时表的全部内容;

翻译自: https://www.journaldev.com/28597/sql-temp-table

sql临时表

sql临时表_SQL临时表相关推荐

  1. sql 表变量 临时表_SQL表变量概述

    sql 表变量 临时表 This article explores the SQL Table variables and their usage using different examples. ...

  2. [导入]SQL中的临时表和表变量

    我们经常使用临时表和表变量,那现在我们就对临时表和表变量进行一下讨论. 临时表 局部临时表 全局临时表 表变量 临时表 临 时表存储在TempDB数据库中,所有的使用此SQL Server 实例的用户 ...

  3. SQL Server--[转]SQL Server中临时表与表变量的区别

    http://blog.csdn.net/skyremember/archive/2009/03/05/3960687.aspx 我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是使用临 ...

  4. mysql表变量临时表_sql server 临时表详细讲解及简单示例

    一.概述 在sql server里临时表存储在TempDB库中,TempDB是一个系统数据库,它只有Simple恢复模式,也是最小日志记录操作.主要用于存放局部临时表,全局临时表,表变量,都是基于临时 ...

  5. 巧用SQL的全局临时表防止用户重复登录

    在我们开发商务软件的时候,常常会遇到这样的一个问题:怎样防止用户重复登录我们的系统?特别是对于银行或是财务部门,更是要限制用户以其工号身份多次登入. 可能会有人说在用户信息表中加一字段判断用户工号登录 ...

  6. sql 表变量 临时表_何时使用SQL临时表与表变量

    sql 表变量 临时表 It is very beneficial to store data in SQL Server temp tables rather than manipulate or ...

  7. Sql Server 创建临时表

    Sql Server 创建临时表 语法 创建临时表 方法一:create table #临时表名(字段1 约束条件,字段2 约束条件,.....)create table ##临时表名(字段1 约束条 ...

  8. SQL server之临时表

    临时表定义 SQL Server临时表是临时对象的一种,临时对象是以#或者##为前缀的, 诸如临时存储过程.临时函数等都是临时对象,临时对象都存储在tempdb数据库中. 当我们关闭数据库时,临时表会 ...

  9. Sql Server之临时表操作

    临时表: 临时表分为"本地临时表"和"全局临时表"两种. 本地临时表的名称以单个符号 (#) 打头,仅对当前的用户连接可见,当创建者从SQL Server 实例 ...

最新文章

  1. Blender中的多平面动画学习教程
  2. leetcode868
  3. GitHub真把代码冰封北极1000年!
  4. 为什么我们要做单元测试?(二)
  5. maven没有resource文件夹_maven项目中没有resource文件夹的问题
  6. iOS开发Swift篇—(三)字符串和数据类型
  7. 门禁系统产品选择与施工要点
  8. less+rem迭代适配
  9. 基于3D关节点的人体动作识别综述
  10. 在MAME里如何设置组合键
  11. 分布式高并发下,Actor模型如此优秀
  12. DC-4靶场练习—teehee提权
  13. zookeeper(1)
  14. 一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
  15. python用来占位的关键字_python-study/Readme.md at master · wchhuangya/python-study · GitHub
  16. WMS、WFS、WMTS、TMS
  17. c语言给结构体指针申请空间,结构体中的指针变量申请空间问题
  18. 【有奖征集】 | 解锁程序YUAN的1024面
  19. 【PP-1】定义生产计划参数文件
  20. MQL4内置函数--总结汇总

热门文章

  1. 大数据Kudu(七):Kudu分区策略
  2. iOS游戏开发游戏功能之外的东西
  3. 徕卡LGO输入第三方天线参数具体操作步骤
  4. 【物理应用】基于matlab白鲸算法太阳能光伏模型参数估计【含Matlab源码 2018期】
  5. Spark之SparkSQL
  6. ESP32开发 解决VS Code 中 make menuconfig 乱码问题
  7. 大学物理--光的衍射
  8. S32K144学习笔记2 - 串口配置
  9. 【Gaze】Generating Image Descriptions via Sequential Cross-Modal Alignment Guided by Human Gaze
  10. 百度地图 ( 二 ) 添加覆盖物