mysql-插入、更新、刪除資料

來源:互聯網
上載者:User

標籤: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-插入、更新、刪除資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.