標籤:sel row 清空 now() ace 資料 font 表示 from
1、插入: ① mysql中有三種插入:insert into、replace into、insert ignore insert into:表示插入資料,資料庫會檢查主鍵,如果出現重複會報錯; replace into:表示插入替換資料,需求表中有PrimaryKey,或者unique索引,如果資料庫已經存在資料,則用新資料替換,如果沒有資料效果則和insert into一樣; insert ignore:表示如果表中已經存在相同的記錄,則忽略當前新資料,當主鍵重複時會忽略新資料。 > insert ignore into class values(49,‘張全蛋‘,now()); Query OK, 0 rows affected, 1 warning (0.00 sec) > select * from class; +----------+------------+---------------------+ | class_id | class_name | date | +----------+------------+---------------------+ | 46 | 小強 | 2017-06-06 22:04:46 | | 47 | 小麗 | 2017-06-06 22:04:46 | | 48 | 小芳 | 2017-06-06 22:04:46 | | 49 | 小王 | 2017-06-06 22:04:46 | +----------+------------+---------------------+ > replace into class values(49,‘張全蛋‘,now()); Query OK, 2 rows affected (0.00 sec) > select * from class; +----------+------------+---------------------+ | class_id | class_name | date | +----------+------------+---------------------+ | 46 | 小強 | 2017-06-06 22:04:46 | | 47 | 小麗 | 2017-06-06 22:04:46 | | 48 | 小芳 | 2017-06-06 22:04:46 | | 49 | 張全蛋 | 2017-06-06 22:25:55 | +----------+------------+---------------------+ ② 將多行查詢結果插入到表中: 文法:select_statement語句中查詢的欄位數應該和前面要插入的欄位一致。 insert into table1_name(field_name ...) select field_name ... from table2_name where ...; insert into class(class_name) select name from asd [where id between 5 and 10]; ③ 當要匯入的資料中有重複值的時候,MYSQL會有三種方案: 方案一:使用 ignore 關鍵字 ignore(忽視) 方案二:使用 replace into 方案三:ON DUPLICATE KEY UPDATE 2、更新: update tb_name set field_name=value where field1_name=value; 3、刪除: 刪除行: delete from tb_name where field_name=value; 刪除一定範圍內的資料: delete from tb_name where field_name between value1 and value2; 清空表: delete from tb_name;
mysql-插入、更新、刪除資料