1. 程式人生 > >mysql-插入、更新、刪除數據

mysql-插入、更新、刪除數據

sta row code ica update tab -- affect nbsp

1、插入:

    ①  mysql中有三種插入:insert intoreplace intoinsert 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-插入、更新、刪除數據