表-插入、更新、删除行

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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章