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. Asp.Net下的DataGrid的多层表头
  2. Mosquitto配置文件说明
  3. php恶意代码,php快速查找数据库中恶意代码的方法,快速查找恶意代码_PHP教程...
  4. 《“笨办法”学Python(第3版)》——习题3 数字和数学计算
  5. Tencent JDK 国产化CPU架构支持分享
  6. 分计算iv值_一文读懂评分卡的IV、KS、AUC、GINI指标
  7. oracle ora 00283,【案例】Oracle报错ORA-16433非归档丢失redo无法启动的恢复过程
  8. centos 8 安装使用配置
  9. Spring Boot工作笔记-RabbitMQ中Unsupported major.minor version 52.0问题
  10. √GMAP.NET 地图
  11. 【python】lxml-The E-factory
  12. SpringMVC通过工具类获取Request域
  13. 同时安装python2和python3
  14. SPSS之单因素方差分析
  15. 2030年中国GDP将超越美国成为世界第一?
  16. Lee滤波和Refine-Lee滤波
  17. 手机格式化的计算机原理,格式化也不靠谱?教你如何彻底删除手机上的数据
  18. Android适配全面屏,三星S8的18.9,小米mix全面屏
  19. 机器学习系列4 使用Python创建Scikit-Learn回归模型
  20. 【开源SPL】列存数据仓库怎样更高效

热门文章

  1. 关于Mac下载brew报curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
  2. 打开计算机不显示百度云管家,百度云管家怎么打不开电脑上的百度云管家打不开的解决方法...
  3. Python 导入通讯录:将.csv文件转换为.vcf文件
  4. psp记忆棒测试软件,psp记忆棒修复工具
  5. spark学习7之IDEA下搭建Spark本地编译环境并上传到集群运行
  6. 国家计算机考试报名照片编辑器,Artensoft Photo Editor(电脑照片编辑软件) V1.5 官方版...
  7. 2023年东大学机械专硕考研上岸经验分享
  8. 替换Tomcat在浏览器地址栏中的ico图标
  9. java爬虫12306_java爬虫12306,爬取所有的站点和车次,并导入postgreSQL数据库
  10. MySQL数据库基础理论