mysqlinsert的简单介绍

http://www.itjxue.com  2023-01-26 19:06  来源:未知  点击次数: 

mysql insert语句注意什么

1. 基础的Insert语句示例

下面的语句向员工表插入一条新记录。在这个例子中,后的“values”指定要插入到表中的所有字段对应的值。

INSERT INTO employee VALUES(100,'Thomas','Sales',5000);

用SELECT语句来验证数据是否插入成功。

SELECT * FROM employee;

2. 只针对选定的字段插入值

如果你只想向选定的字段插入值,你需要在INSERT语句中明确指定要插入的字段名字。 下面的代码仅插入id和name列的数据

INSERT INTO employee(id,name) VALUES(200,'Jason');

以上代码中,我们未对dept和salary列指定任何值。所以,以上两个字段的值为NULL,当我们用SELECT语句检索数据时。需要注意的是,NULL不是指"NULL"字符串,SELECT语句使用"NULL"来表示这个字段的值是空的而已。

mysql SELECT * FROM employee;+-----+--------+-------+--------+| id | name | dept | salary |+-----+--------+-------+--------+| 100 | Thomas | Sales | 5000 || 200 | Jason | NULL | NULL |+-----+--------+-------+--------+2 rows in set (0.00 sec)

3. INSERT SET示例

INSERT语句不仅仅可以使用"VALUE"关键字,也可以使用"SET"关键字。下面的例子与上面的例子结果一样,但是使用的是SET关键字。

mysql INSERT INTO employee SET id=300, name='Mayla';mysql select * from employee;+-----+--------+-------+--------+| id | name | dept | salary |+-----+--------+-------+--------+| 100 | Thomas | Sales | 5000 || 200 | Jason | NULL | NULL || 300 | Mayla | NULL | NULL |+-----+--------+-------+--------+

4.从另一个表中检索数据插入

下面的例子中,我们会使用INSERT ... SELECT语句,这个语句可以从其他表中检索数据,并插入到目标表。 下面的代码从Contractor表中检索所有数据并插入到Employee表。

INSERT INTO employee SELECT * FROM contractor;

SELECT语句中可以根据需要使用WHERE子句,用来检索需要的从Contractor表插入到Employee表的数据。

INSERT INTO employee SELECT * FROM contractor WHERE salary = 7000;

注意:如果你使用oracle数据库,你的SQL应该这样写"INSERT INTO employee AS SELECT * FROM contractor"。需要注意在MySQL中不适用AS关键字。

5.从其他表中选择部分字段插入

当然,你也可以从其他表中选择部分字段的数据插入到你的表中。 下面的例子将从contractor表中选择id,name字段的所有数据插入到employee表。

INSERT INTO employee(id,name) SELECT id,name FROM contractor;

跟前面的例子一样,可以使用WHERE子句过滤数据。

INSERT INTO employee(id,name) SELECT id,name FROM contractor WHERE salary = 7000;

注意:如果employee表中已存在相同主键(这里的主键是id)的记录,你会得到一条错误信息。下面是一个示例错误信息,指出employee表中已经存在id为100的记录。

ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'

6. 插入数据到指定分区

如果创建了分区表(分区类型为range)并希望将输入插入到指定分区,可以参考下面代码。 将数据插入到分区p1中的employee表。

INSERT INTO employee PARTITION (p1) VALUES(100,'Thomas','Sales',5000);

注意:如果该分区的employee表中已包含相应的数据(这里是id=100的employee数据),你会得到如下错误信息。

ERROR 1729 (HY000): Found a row not matching the given partition set

注意: 分区仅在MySQL5.6及以上版本可用。

7. 插入数据到多个分区

可以使用单条SQL语句将数据插入到多个分区。下面的INSERT语句将id为100的记录插入到p1分区,将id为200的记录插入到p2分区。

INSERT INTO employee PARTITION (p1, p2) VALUES(100,'Thomas','Sales',5000), (200,'Jason','Technology',5500);

注意: 在上例中,如果MySQL往任意分区插入数据失败,那么整个INSERT语句将失败。当然,这同样仅能在MySQL5.6及以上版本使用。

8. 在插入过程中忽略错误

在一些场景中(比如:批处理),你可能希望忽略插入过程中MySQL产生的错误信息,你可以使用INSERT IGNORE语句。 比如:下面的例子会抛出错误信息,提示数据已经存在。

mysql INSERT INTO employee VALUES(100,'Thomas','Sales',5000);ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'

要忽略上面的错误信息,可以使用INSERT IGNORE来替换INSERT语句(执行前请确保这条对应的主键已经存在)。

mysql INSERT IGNORE INTO employee VALUES(100,'Thomas','Sales',5000);Query OK, 0 rows affected (0.00 sec)

INSERT IGNORE仅仅只是忽略错误信息,不会进行其他任何处理。

9. INSERT语句的默认值

如果MySQL运行在strict模式下,并且在我们的INSERT语句中并未指定默认值,那么MySQL将抛出错误信息。 不过,如果MySQL并未启用strict模式(这个是默认启用的)时,同时INSERT语句没有为字段指定值,那么MySQL会为该字段使用字段类型的默认值。 比如,bonus表的所有字段都不允许为空(not null)。

mysql DESC bonus;+--------+---------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+---------+------+-----+---------+-------+| id | int(11) | NO | | NULL | || amount | int(11) | NO | | NULL | |+--------+---------+------+-----+---------+-------+

我们先执行以下语句,插入一条数据(id=100)

INSERT INTO bonus(id) VALUES(100);

查看刚插入的数据,可以看到amount字段被设置为默认值0

SELECT * FROM bonus;+-----+--------+| id | amount |+-----+--------+| 100 | 0 |+-----+--------+

如果,SQL语句中同时不指定id和amount字段的值,那么他们都会被自动设置为默认值0。参考下面例子。

INSERT INTO bonus VALUES();mysql select *

mysql 怎么用insert批量插入数据

一次插入多行数据

insert

into

表名[(字段列表)]

values(值列表1),(值列表2),...(值列表n);

例如:

insert

into

students(sid,sname,dob)

values

('001','张三','2001-02-03'),

('002','李四','2002-02-03'),

('003','王五','2003-02-03');

将一张表或查询中的数据插入到另一张表里

insert

into

表名(字段列表)

select

(字段列表)

from

源表

where

筛选表达式;

例如将表2中的记录全部插入到表1,假设它们的结构一样

insert

into

表1(*)

select

*

from

表2;

六、MySQL数据库之数据插入(insert into)

本节介绍数据的插入,复制数据到另一张表的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?:为已经存在的表批量添加新数据。

MySQL中insert into语句的6种写法

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

(责任编辑:IT教学网)

更多

推荐时间特效文章