表-插入、更新、刪除行

1. 插入數據

通過使用insert語句,在表中添加記錄。

-- 通過顯式地指明字段間的對應關係。
insert ignore into `demo`.`customers`(first_name,second_name,country)
values
('Andy','Perera','China'),
('Bob','Ven','Alustralia');
-- 如果插入的字段個數與表字段完全對應,則不需要指定列名
insert ignore into `demo`.`customers`
values
(1,'Andy','Perera','China'),
(2,'Bob','Ven','Alustralia');

2. 更新數據

update 表名 set 字段名 = 更新後的值 where 條件

update customers
set first_name = 'kangkang' , country = 'China'
where id = 3;

這裏的where字句並非是強制的,但如果不加where條件則會將所有數據進行更新,在實際工作中通常是需要加where條件過濾的。

3. 刪除數據

delete from 表名 where 條件

delete from customers
where id = 3;

這裏的where字句也不是強制的,但如果不加where,則會把表中的所有行都刪除。這樣刪除整個表的話會需要很長時間,因爲Mysql是逐行執行刪除的。刪除表的所有行(保留表結構)最快的方法是使用truncate table語句。
truncate table 是DDL操作,也就是說數據一旦被清空,就不能被回滾。

truncate table customers;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章