sql选择性插入

In the real world of SQL, imagine if there is a situation when you need to copy data from one table to another table. SQL has an answer for this and the answer is “SQL Insert Into Select“.

在SQL的真实世界中,想象一下是否存在需要将数据从一个表复制到另一表的情况。 SQL对此有一个答案,答案是“ SQL Insert In Select ”。

SQL插入选择 (SQL Insert Into Select)

SQL Insert Into Select clause is used when we want to copy data from one table to another table.

当我们要将数据从一个表复制到另一表时,将使用SQL Insert Into Select子句。

SQL插入选择规则 (Rules For SQL Insert Into Select)

  • INSERT INTO SELECT requires that data types in source and target tables match.INSERT INTO SELECT要求源表和目标表中的数据类型匹配。
  • The existing records in the target table are unaffected.目标表中的现有记录不受影响。

SQL插入选择语法 (SQL Insert Into Select Syntax)

INSERT INTO table2 (column1, column2, ... , columnN)
SELECT c1, c2, ... , cn FROM table1
WHERE condition;

In the above syntax, data from table1 is selected using SELECT statement and then is inserted into the table2 using INSERT statement.

在以上语法中,使用SELECT语句选择来自table1的数据,然后使用INSERT语句将其插入到table2中。

SQL插入选择示例 (SQL Insert Into Select Example)

Let us consider the following table for understanding SQL Insert Into Select Statement.

让我们考虑下表,以了解将SQL插入到Select语句中。

Teacher

老师

TeacherId TeacherName State Country
1 Amit Bengaluru India
2 Harry Texas US
3 John London UK
教师编号 老师的名字 国家
1个 阿米特 班加罗尔 印度
2 哈里 德州 我们
3 约翰 伦敦 英国

Student

学生

StudentId StudentName State Country
1 Henry Wales UK
2 Rohit Delhi India
3 Steve London UK
学生卡 学生姓名 国家
1个 亨利 威尔士 英国
2 罗希特 新德里 印度
3 史蒂夫 伦敦 英国

Query for the tables:

查询表

CREATE TABLE `teacher` (`TeacherId` INT NOT NULL,`TeacherName` VARCHAR(45) NULL,`State` VARCHAR(45) NULL,`Country` VARCHAR(45) NULL,PRIMARY KEY (`TeacherId`),UNIQUE INDEX `TeacherId_UNIQUE` (`TeacherId` ASC) VISIBLE);CREATE TABLE `student` (`StudentId` INT NOT NULL,`StudentName` VARCHAR(45) NULL,`State` VARCHAR(45) NULL,`Country` VARCHAR(45) NULL,PRIMARY KEY (`StudentId`),UNIQUE INDEX `StudentId_UNIQUE` (`StudentId` ASC) VISIBLE);Insert into Teacher(TeacherId,TeacherName,State,Country) VALUES (1, 'Amit','Bengaluru','India'), (2, 'Harry','Texas','US'), (3, 'John','London','UK');
Insert into Student(StudentId,StudentName,State,Country) VALUES (1, 'Henry','Wales','UK'), (2, 'Rohit','Delhi','India'), (3, 'Steve','London','UK');

Let us assume a case when the Student from India got a teaching job in the same Institute. In that case, the data for students from India need to be copied to the data in Teacher table.

让我们假设一个案例,即印度学生在同一所学院获得教学工作。 在这种情况下,需要将印度学生的数据复制到“教师”表中的数据。

Insert into Teacher (TeacherId,TeacherName,State,Country)
Select 4,StudentName,State,Country from Student where country = 'India';

Notice that there is already a teacher with id 2, so we are using “select 4” to use a different id for the student data that we are copying to the teacher table.

请注意,已经有一个ID为2的教师,因此我们使用“选择4”为复制到教师表的学生数据使用不同的ID。

Below image shows the teacher table data after the command execution.

下图显示了命令执行后的教师表格数据。

SQL Insert Into Select

SQL插入选择

结论 (Conclusion)

SQL insert into select clause is very helpful in copying data from one table to another. We can use it to create a selective dump of a table data. It’s supported by all the major SQL database vendors such as MySQL, Oracle, SQL Server, PostgreSQL etc.

将SQL插入select子句对于将数据从一个表复制到另一个表非常有帮助。 我们可以使用它来创建表数据的选择性转储。 所有主要SQL数据库供应商(例如MySQL,Oracle,SQL Server,PostgreSQL等)都支持它。

翻译自: https://www.journaldev.com/24344/sql-insert-into-select

sql选择性插入

sql选择性插入_SQL插入选择相关推荐

  1. sql在插入语句使用子查询_SQL插入查询

    sql在插入语句使用子查询 SQL INSERT Query also referred as SQL INSERT INTO is the command used for inserting da ...

  2. mysql中sql批量插入_sql中insert如何批量插入多条记录?

    sql中insert如何批量插入多条记录? sql中insert批量插入多条记录的方法: 常见的insert语句,向数据库中,一条语句只能插入一条数据:insert into persons (id_ ...

  3. 在SQL 2005中用T-SQL插入中文数据时出现的问号或乱码的解决方案[转]

    在SQL 2005中用T-SQL插入中文数据时出现的问号或乱码的解决方案 病症表现为:主要表现为用T-sql语句插入中文数据时数据库显示全是问号"???"  解决办法: 第一种办法 ...

  4. oracle数据库开多线程,学习笔记:Oracle表数据导入 DBA常用单线程插入 多线程插入 sql loader三种表数据导入案例...

    天萃荷净 oracle之数据导入,汇总开发DBA在向表中导入大量数据的案例,如:单线程向数据库中插入数据,多线程向数据表中插入数据,使用sql loader数据表中导入数据案例 1.Oracle数据库 ...

  5. sql server死锁_SQL Server如何选择死锁受害者

    sql server死锁 In the article on Deadlock Analysis and Prevention, we saw how deadlock occurs. We saw ...

  6. 解决Chrome中UEditor插入图片的选择框加载过慢问题

    解决Chrome中UEditor插入图片的选择框加载过慢问题 ../resources/plugins/ueditor/ueditor.all.js 中line24489/24498中的 accept ...

  7. SQL查询数据并插入新表

    SQL查询数据并插入新表 --如果接受数据导入的表已经存在 insert into 表 select * from tablename--如果导入数据并生成表 select * into 表 from ...

  8. 使用sql语句向SDO_Geometry插入要素

    环境描述: 使用ArcGIS10.1直连数据库,以SDO_Geometry导入Featureclass,注册数据库,发布动态服务. js(arcgis api for js + dojo + .net ...

  9. 用Java向SQL Server数据库中插入float数据报错An error occurred while converting the Float value to JDBC data type

    作者:翁松秀 用Java向SQL Server数据库中插入float数据报错 用Java向SQL Server数据库中插入float数据报错 报错信息: 报错原因: 解决方案: 报错信息: An er ...

  10. SQL语句中批量插入

    SQL语句中批量插入 1.介绍说明 ​ 在程序中需要同时插入多条数据的写法,其中mysql和oracle两种写法存在一些差异. oracle数据库 insert into <tableName& ...

最新文章

  1. SpringBoot-web开发(一): 静态资源的导入(源码分析)
  2. 阿里钉钉陈航发布10亿“春雨计划”,推进企业级市场服务创新
  3. Best practice for JVM Tuning[转]
  4. Ironport对邮件主题中包括特殊字符的过滤
  5. 树上倍增一些理解和写法
  6. spring技术小结
  7. 一文看尽科大讯飞年度发布会:医疗,是这家A股AI公司的新赛道
  8. 快准狠才叫爽!六款智能电视语音识别大比拼
  9. 请简要概括linux与windows在文件系统方面存在的不同点,简要回答下列与网络操作系统、网络安全和数据存储相关的问题,将解答填入答题纸对应栏内。br 【问题1】(10分)br (a)Win...
  10. 微信浏览器真不愧移动届ie啊
  11. Linux网络属性配置—修改配置文件
  12. C#netdxf库读、写、绘制CAD的dxf文件
  13. 如何从hibernate官网下载各个版本的hibernate
  14. 凯恩帝k1000ti参数设置_K1000TII标准程序调试参数说明
  15. Python经典面试题解析:实现斐波那契数列
  16. kill -HUP重启mysql_kill的用法和例句,包括kill常用短语解释和词组意思翻译,同义词,反义词【澳典网ODict.Net】...
  17. 笔记本电脑无法在插电时睡眠(续)
  18. c语言源码什么意思,请问C语言源代码什么意思?
  19. mysql删除某张表三个月前的数据
  20. ubuntu 下怎么查看内存条状况

热门文章

  1. 频谱仪的更改ip_通过局域网(LAN)读取频谱分析仪图像的方法
  2. 安装vs产生的垃圾文件(eula.1028.txt,install.res.1028.dll,VC_RED.MSI等)
  3. 使用计时器setInterval实现倒计时
  4. DreamScene for Windows 7梦幻桌面使用教程
  5. word排版教程(标题设置,自动生成目录)
  6. PDF417打印后扫描不出来(记录)
  7. 矩阵乘法 算法训练 试题_计算方法练习题及答案汇总(共八套).docx
  8. 有符号二进制加法溢出判断以及溢出后该如何计算正确答案
  9. 斜齿轮重合度计算公式_齿轮强度校核及重合度计算(已优化)
  10. 基于weber的齿轮啮合刚度计算