MySQL中insert into语句的6种写法

MySQL中insert into语句的6种写法,第1张

insert into是mysql中最常用的插入语句,它有6种写法。

如果插入的记录是数字的话要在数字的逗号后面加n:

通过以上实例我们可以看到insert into语句只能向原表中插入于其字段对应的数据,那么能不能通过insert into语句来把其他表的数据插入到原表中呢:

在MySQL中set方法

ModifyStatement.Set Method修改语句 set方法

Sets key and value. 设置键和值。

由于insert into语句是一个插入性的语句,所以它的功能要么向指定的表插入数据

也许你看到这个SQL语句是正确的,就觉得这样应该也可以:

mysql>mysql>insert into 4a set sname=4ainall.sname

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql>insert into 4a set sname=4ainall.sname' at line 1

或者这样也可以:

mysql>mysql>insert into 4a set sname="赵六"

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql>insert into 4a set sname="赵六"' at line 1

然后这样也是不可用:

mysql>insert into 4a select * from 4ainall set sname=4ainall.sname

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'from 4ainall set sname=4ainall.sname' at line 1

可以看出由于select是作用于4inall这个表的,而set方法也只能在select语句中,这就直接导致set方法只能作用于4inall这个表,而无法作用于4a这个表。

但是如果我们不用select语句的话编译器又怎么会知道4inall表中的数据在哪里?

显然select是用于查的而set则是一个用于改的方法,两者无法结合在一起——insert into set语句当然也不能用于将其他表的数据插入到原表中了。

sql语句中,添加记录的语法为:insert into 表名 (col1,col2....coln)values(value1,value2.....valuen);

其中,如果你插入的每一列都是顺序插入,无一缺漏的话,(col1,col2...coln)可以省略。

也就是上式也可以简化为:insert into 表名 values(value1,value2.....valuen);

看了你写的sql代码,问题出在insert into 的整体语句出现在了不该出现的地方,只需做一点小改动即可解决,如下图:

解析:insert into语句需要在user表已经存在的情况下才可以使用。而你原来的语句中,将上图2中的语句插入到了create table user的语句中,致使create table user 语句未能成功执行,所以才会报错。

而将“INSERT INTO user(uid,tel) values('甲','3354986')”整条语句直接拿出来放在“ENGINE=InnoDB DEFAULT CHARSET=gbk”后面之后,整个sql就可以顺利执行了。

扩展资料:

当mysql大批量插入数据的时候就会变的非常慢, mysql提高insert into 插入速度的方法有三种:

1、第一种插入提速方法:

如果数据库中的数据已经很多(几百万条), 那么可以 加大mysql配置中的 bulk_insert_buffer_size,这个参数默认为8M

举例:bulk_insert_buffer_size=100M;

2、第二种mysql插入提速方法:

改写所有 insert into 语句为 insert delayed into

这个insert delayed不同之处在于:立即返回结果,后台进行处理插入。

3、第三个方法: 一次插入多条数据:

insert中插入多条数据,举例:

insert into table values('11','11'),('22','22'),('33','33')...


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

原文地址: https://outofmemory.cn/zaji/8546498.html

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

发表评论

登录后才能评论

评论列表(0条)

保存