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语句当然也不能用于将其他表的数据插入到原表中了。

养成好习惯,插入的时候把字段表也带上;

INSERT INTO `tb_column` (`columnId`, `columnName`, `columnDisc`, `columnOrder`) 

VALUES 

('1', '新闻', '国内国外新闻', '12'), 

('2', '财经', '关注q', '7'), 

('3', '娱乐', '关注', '14'), 

('4', '体育', '关注新闻', '6'), 

('5', '读书', '读书内容', '5')

1、使用 create table 语句可完成对表的创建, create table 的创建形式:

create table 表名称(列声明)

以创建 people 表为例, 表中将存放 学号(id)、姓名(name)、性别(sex)、年龄(age) 这些内容:

create table people(

id int unsigned not null auto_increment primary key,

name char(8) not null,

sex char(4) not null,

age tinyint unsigned not null

)

其中,auto_increment就可以使Int类型的id字段每次自增1。

2、向表中插入数据使用insert 语句。

insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:

insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...)

其中 [] 内的内容是可选的, 例如, 要给上步中创建的people 表插入一条记录, 执行语句:

insert into people(name,sex,age) values( "张三", "男", 21 )

3、想要查询是否插入成功,可以通过select 查询语句。形式如下:

select * from people;

扩展资料:

当mysql大批量插入数据的时候使用insert into就会变的非常慢, 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://www.54852.com/bake/11575948.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存