尊旭网
当前位置: 尊旭网 > 知识 >

selectinto

时间:2024-08-29 21:08:11 编辑:阿旭

insert into from和insert into select有什么区别?

select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建。insert into select from 要求目标表存在。备份表数据: create table emp as select * from scott.emp还原表数据:insert into emp select * from scott.emp复制表结构及其数据:create table table_name_new as select * from table_name_old只复制表结构:create table table_name_new as select * from table_name_old where 1=2;或者:create table table_name_new like table_name_old只复制表数据:如果两个表结构一样:insert into table_name_new select * from table_name_old如果两个表结构不一样:insert into table_name_new(column1,column2...) select column1,column2...from table_name_old pasting

什么是select into from和insert into select?

select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建。insert into select from 要求目标表存在。备份表数据: create table emp as select * from scott.emp还原表数据:insert into emp select * from scott.emp复制表结构及其数据:create table table_name_new as select * from table_name_old只复制表结构:create table table_name_new as select * from table_name_old where 1=2;或者:create table table_name_new like table_name_old只复制表数据:如果两个表结构一样:insert into table_name_new select * from table_name_old如果两个表结构不一样:insert into table_name_new(column1,column2...) select column1,column2...from table_name_old pasting

如何使用select into 进行备份mysql数据库

使用SELECT INTO进行备份与MYSQLDUMP很相似,同样是把数据库备份到一个指定的文件中。其结果文件只能被建立在MySQL服务器上,而不是任何其他主机。SELECT INTO语句的语法格式如下:

  Select … into outfile 'path_and_filename' ;
  示例:

  使用SELECT INTO语句查询数据库“mr_mysql”中的“mr_gly”表,把该表备份到“D:\\gly.txt”目录下,文件的名称是“gly.txt”。
  mysql> use mr_mysql
  Database changed
  mysql> select * from mr_gly into outfile "D:\\gly.txt";
  Query OK, 5 rows affected (0.00 sec)
  下面的这些参数是SELECT INTO语句的非默认参数。
  [fields
  [terminated by '\t' ] //设置输出文件以什么作为分界标识
  [enclosed by '' ] //指定的字符包围了所有的域
  [[optionally] enclosed by '' ] //指定只有字符域被包括
  [escaped by '\\'] ]
  [lines terminated by '\n' ] //设置长行的中断被什么字符代替

  下面是应用了SELECT INTO语句非默认参数的几个示例。

  示例:
  在每个域之间,默认的制表符被字符“|”代替。
  mysql> use tpsc
  Database changed
  mysql> select * from jtsr into outfile "D:\\user1.txt" fields terminated by '|' ;
  Query OK, 5 rows affected (0.00 sec)
  示例:
  Enclosed关键字用指定的字符“双引号”包围了所有的域。
  mysql> select * from jtsr into outfile "D:\\user2.txt" fields terminated by '|' enclosed by '"';
  Query OK, 5 rows affected (0.02 sec)
  示例:
  Optionally关键字的使用,导致了只有字符域被双引号包括。
  mysql> select * from jtsr into outfile "D:\\user3.txt" fields terminated by '|' optionally enclosed by '"' ;
  Query OK, 5 rows affected (0.02 sec)
  示例:
  lines terminated的使用,使每行之间的中断被字符“\n”

代替。
  mysql> select * from jtsr into outfile "D:\\user4.txt" fields terminated by '|' lines terminated by '\n' ;
  Query OK, 5 rows affected (0.02 sec)
  示例:

  综合使用这些参数。

  mysql> select * from jtsr into outfile "D:\\user5.txt" fields terminated by '|' optionally enclosed
  by '"' lines terminated by '\n' ;
  Query OK, 5 rows affected (0.02 sec)
  示例:

  使用SELECT语句中的条件进行备份。

  mysql> select * from jtsr where id>3 into outfile "D:\\user6.txt" fields terminated by '|' optionall
  y enclosed by '"' lines terminated by '\n' ;
  Query OK, 2 rows affected (0.01 sec)
  注意:在使用SELECT INTO语句时,为备份的文件命名时切忌不要重写已存在的文件;在编写文件输出的位置时不要忘记使用换码符“\”。