load导入数据用法。insertinto

load导入数据用法。insertinto,第1张

insert插入数据有以下几种方式

其中insert into是标准语法,insert是mysql简化后的语法,一般推荐使用insert into。

格式如下

insert into 表名 value (值列表)-- 插入一条信息

insert into 表名(属性列表)value(值列表)-- 插入一条数据,值列表要和属性列表一一对应

insert into 表名 (属性列表)values (值列表),(值列表),(值列表)......-- 在指定列插入多条数据,值列表和属性列表一一对应

insert into 表名 values (值列表),(值列表),(值列表)......-- 在所有列插入多条数据,值列表和表结构相符。

insert into 表名 set 列名=属性名,列名=属性名...

insert into wx_student value (null,'lili2',1313455668,22,'郑州',10)

insert into wx_student(sno,sname) value (null,'lili2')

insert into wx_student(sno,sname) values (null,'lili3'),(null,'lili4'),(null,'lili5')

insert into wx_student values (null,'lili6',1313455611,22,'郑州',10),(null,'lili7',1313455622,22,'郑州',10),

(null,'lili8',1313455633,22,'郑州',10)

insert into wx_student set sname='李四',sphone='1911412512'

登录后复制

load

load从文件中加载数据

本节介绍数据的插入,复制数据到另一张表的Sql语法,主要语法有: insert into,insert into select,select into from 等用法,下面将一一为大家详细说明:

以下面两张表进行sql脚本说明

insert into有两种语法,分别如下:

语法1:INSERT INTO table_name VALUES (value1,value2,value3,...)   --这种形式无需指定要插入数据的列名,只需提供被插入的值即可:

语法2:INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...)    --这种形式需指定要插入数据的列名,插入的值需要和列名一一对应:

eg:insert into customer values('1006','14006','王欣欣','27','深圳市')  --向表customer插入一条数据

eg:insert into customer values('1007','14007','孟一凡','27','')             --向表customer插入一条数据,最后一个值不填表示对应的值为空,非必填项可以不用插入值

eg:insert into customer (cus_id,cus_no,cus_name,cus_age,cus_adds) values('1008','14008','孔凡','26','广州市')      --向表customer插入一条数据,插入的值与列名一一对应

详解:insert into select    --表示从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。

语法1:INSERT INTO table_name2 SELECT  * FROM table_name1  --表示将表table_name1中复制所有列的数据插入到已存在的表table_name2中。被插入数据的表为table_name2,切记不要记混了。

eg:insert into customer select * from asett   --将表asett中所有列的数据插入到表customer中

语法2:INSERT INTO table_name2 (column_name(s)) SELECT column_name(s) FROM  table_name1  --指定需要复制的列,只复制制定的列插入到另一个已存在的表table_name2中:

eg:insert into customer (cus_id,cus_no) select ast_id,ast_no from asett   --将表asett中列ast_id和ast_no的数据插入到表customer对应的cus_id,cus_no列中

详解:从一个表复制数据,然后把数据插入到另一个新表中。

语法1:SELECT * INTO newtable [IN externaldb] FROM table1                               --复制所有的列插入到新表中:

eg:select * into customer from asett     --将asett表中数据插入到customer中,被插入的 表customer不存在

eg:select * into customer from asett where ast_id = '1008'    --只复制表asett中ast_id=1008的数据插入到customer中,被插入的 表customer不存在

语法2:SELECT column_name(s) INTO newtable [IN externaldb] FROM table1   --只复制指定的列插入到新表中:

eg:select ast_id,ast_no into customer from asett  --将asett表中列ast_id,ast_no数据插入到customer中,被插入的 表customer不存在

区别1:insert into customer select * from asett where ast_id='1009' --插入一行,要求表customer 必须存在

区别2:select * into customer  from asett  where ast_id='1009' --也是插入一行,要求表customer  不存在

区别3:select into from :将查询出来的数据复制到一张新表中保存,表结构与查询结构一致。

区别4:insert into select :为已经存在的表批量添加新数据。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11807044.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存