mysql> desc students; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ |sid |int(11) |YES | |NULL | | |sname |varchar(20) |YES | |NULL | | +-------+-------------+------+-----+---------+-------+ Insert into students values(1,’aaa’); Insert into students set sid=2,sname=‘bbb’; Insert into students select * from students_bak; mysql> select * from students; +------+-------+ |sid |sname| +------+-------+ | 1 | aaa | | 2 | bbb | | 3 | ccc | +------+-------+
insert...values
和insert...set
兩種語句都是將指定的數據插入到現成的表中,而insert...select
語句是將另外表中數據查出來並插入 到現成的表中Partition
子句表明能夠將數據插入到指定的表分區中Tbl_name
表明將數據插入到的目標表Col_name
表明要插入指定數據的目標表列,若是是多列則用逗號 隔開,若是目標表中的某些列沒有在Insert
語句中指定,則這些列 會插入默認值,固然可使用default
顯視指定插入默認值Values
中除了能夠指定肯定的數值以外,還可使用表達式exprINSERT INTO tbl_name (col1,col2) VALUES(15,col1*2); -- 正確 INSERT INTO tbl_name (col1,col2) VALUES(col2*2,15); -- 錯誤
mysql> insert into students(sid,sname) values(4,'ddd'); mysql> insert into students(sid) values(5); mysql> insert into students(sname) values('eee'); mysql> insert into students value(2*3,'fff'); mysql> select * from students; +------+-------+ |sid |sname| +------+-------+ | 1 |aaa | | 2 |bbb | | 3 | ccc | | 4 |ddd | | 5 | NULL | |NULL |eee | | 6 |fff | +------+-------+
INSERT INTO tbl_name(a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); Insert into students values(7,’abc’),(8,’bcd’);
CREATE TABLE `students` ( `sid` int(11) DEFAULT NULL, `sname` varchar(20) DEFAULT NULL, `gender` int(11) DEFAULT NULL, UNIQUE KEY `idx_st_sid` (`sid`), KEY `idx_st_union` (`sname`,`sex`) ) ENGINE=InnoDB DEFAULT CHARSET=latin mysql> select * from students; +------+-------+--------+ |sid|sname|gender| +------+-------+--------+ |1|abc| 1| |2|abc| 1| |3|abc| 1| mysql> insert into students values(1,'bbb',0); ERROR 1062 (23000): Duplicate entry '1' for key 'idx_st_sid' mysql> insert ignore into students values(1,'bbb',0); Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show warnings; +---------+------+------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------+ | Warning | 1062 | Duplicate entry '1' for key 'idx_st_sid' |
mysql> insert ignore into students values(1,'aa',0),(5,'bb',1); Query OK, 1 row affected, 1 warning (0.03 sec) Records: 2 Duplicates: 1 Warnings: 1 mysql> select * from students3; +------+-------+--------+ |sid |sname|gender| +------+-------+--------+ |1|a|0| |2|b|0| |4|c|1| mysql> insert ignore into students select * from students3; Query OK, 1 row affected, 2 warnings (0.01 sec) Records: 3 Duplicates: 2 Warnings: 2
用於從另外的表中查出記錄並插入到目標表中mysql
INSERT INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
當目標表和select語句中的表相同時,則會先將select語句的結果 存放在臨時表中,再插入到目標表中(注意執行順序)sql
insert into students select * from students;
在5.6.6版本以前,用來表示此插入語句當碰到其餘連接正在使用 目標表時就等待,直到目標表沒被用時再插入數據
在5.7版本時,delayed關鍵詞就再也不支持,但語句執行時不會報 錯,只會產生一個警告信息,後續版本會去掉此關鍵詞code
insert delayed into students select * from students; Query OK, 18 rows affected, 1 warning (0.00 sec) Records: 18 Duplicates: 0 Warnings: 1
當insert語句中使用on duplicate key update子句時,若是碰到當前 插入的數據違反主鍵或惟一鍵的惟一性約束,則Insert會轉變成 update語句修改對應的已經存在表中的這條數據。好比若是a字段 有惟一性約束且已經含有1這條記錄,則如下兩條語句的執行結 果相同ci
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; UPDATE table SET c=c+1 WHERE a=1;
上述例子中若是b字段也有惟一性約束,則與此語句的執行結果 相同,但通常應該避免出現對應多條的狀況it
UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
mysql> create table students2(sid int primary key,sname varchar(20),sex int); Insert into students2 values(1,’aaa’,1); -- 插入成功 Insert into students2 values(1,’bbb’,0); -- 插入失敗 mysql> insert into students2 values(1,'bbb',0); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' insert into students2 values(1,‘bbb’,0) on duplicate key update sname=‘bbb’; +-----+-------+------+ | sid | sname | sex | +-----+-------+------+ | 1 |bbb | 1 | insert into students2 values(1,‘ccc’,0) on duplicate key update sname=‘ccc’,sex=0; -- 執行成功
1,’computer science’ ; 2,’education’; 4,’accounting’
根據create table ... like語句建立teacher_backup表,並插入以下數 據:
1,’susan’,1; 2,’ruth’,4; 3,’vivian’,4
將teacher_backup表的數據經過insert...select語句插入到teacher表中io