1. 插入数据

通过使用insert语句,在表中添加记录。

  1. -- 通过显式地指明字段间的对应关系。
  2. insert ignore into `demo`.`customers`(first_name,second_name,country)
  3. values
  4. ('Andy','Perera','China'),
  5. ('Bob','Ven','Alustralia');
  6. -- 如果插入的字段个数与表字段完全对应,则不需要指定列名
  7. insert ignore into `demo`.`customers`
  8. values
  9. (1,'Andy','Perera','China'),
  10. (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;