SQL語句-INSERT語句

Insert語句

Insert語句三種寫法:

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...valuesinsert...set兩種語句都是將指定的數據插入到現成的表中,而insert...select語句是將另外表中數據查出來並插入 到現成的表中
  • Partition子句表明能夠將數據插入到指定的表分區中
  • Tbl_name表明將數據插入到的目標表
  • Col_name表明要插入指定數據的目標表列,若是是多列則用逗號 隔開,若是目標表中的某些列沒有在Insert語句中指定,則這些列 會插入默認值,固然可使用default顯視指定插入默認值
  • Values中除了能夠指定肯定的數值以外,還可使用表達式expr
INSERT 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...values語句不光能夠插入一條數據,也能夠插入多條數據
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’);
  • Insert...values和insert...select語句的執行結果以下
  • Records: 100 Duplicates: 0 Warnings: 0
  • Records表明此語句操做了多少行數據,但不必定是多少行被插入的數據,由於若是存 在相同的行數據且違反了某個惟一性,則duplicates會顯示非0數值,warning表明語句執 行過程當中的一些警告信息
  • low_priority關鍵詞表明若是有其餘連接正在讀取目標表數據,則此insert語句須要等待讀取完成
  • low_priority和high_priority關鍵詞僅在MyISAM, MEMORY, and MERGE三種存儲引擎下才生效
  • Ignore關鍵詞表明insert語句若是違反主鍵和惟一鍵的約束條件,則不報錯而只產生警告信息,違反的行被丟棄,而不是整個語句回退;在數據類型轉換
    有問題時若是有ignore則只產生警告信息,而不是語句回退
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

Insert...select語句詳解

用於從另外的表中查出記錄並插入到目標表中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;

Insert delayed語句詳解

在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語句中使用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;

On duplicate key update子句後面能夠跟多個修改,用逗號隔開

上述例子中若是b字段也有惟一性約束,則與此語句的執行結果 相同,但通常應該避免出現對應多條的狀況it

UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;

Insert on duplicate key update語句詳解

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;      -- 執行成功

將以下數據插入到dept表中

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

相關文章
相關標籤/搜索