INSERT INTO SELECT语句用法

SQL INSERT INTO SELECT 语句

INSERT INTO

插入一行数据可以是

INSERT INTO Persons (LastName, Address) VALUES ('cway', 'Hometown')

插入多行可以使用INSERT INTO SELECT将一个表数据复制到另一个表

INSERT INTO Websites (name, country)
SELECT app_name, country FROM apps;

SELECT INTO

select into from 和 insert into select 都是用来复制表两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建;insert into select from 要求目标表存在。

\1. 复制表结构及其数据:

create table table_name_new as select * from table_name_old

\2. 只复制表结构:

create table table_name_new as select * from table_name_old where 1=2;

或者:

create table table_name_new like table_name_old

\3. 只复制表数据:

如果两个表结构一样:

insert into table_name_new select * from table_name_old

如果两个表结构不一样:

insert into table_name_new(column1,column2...) select column1,column2... from table_name_old

\4.SELECT INTO

SELECT LastName,Firstname
INTO Persons_backup
FROM Persons
WHERE City='Beijing
目录导航
  • SQL INSERT INTO SELECT 语句
    • INSERT INTO
    • SELECT INTO