首先,创建一张表以备以下语句使用

create table t2(id int not null auto_increment,name varchar(10),city varchar(10),primary key(`id`),index index_city(`city`))engine=innodb charset=utf8;


1、insert into t2(id,name,city) values(1,'刘','长沙');

写入数据


2、insert into t2 values (2,'朱','上海');

每行都写入数据,可不写字段名


3、insert into t2(name,city) values ('张','北京'),('王','上海');

一次写入多行数据(由于id为自增,因此如果id未指定值将自动分配,分配的值为表中原有数据的id最大值加1)


4、create table t3 like t2;

insert into t3 select * from t2;

把一个表里的所有数据全部写入另一个表中


5、select * from t2;

查看表中所有数据

select * from t2 where id=2;

按照条件查询数据


6、update t2 set name='李' where id=1;

更新id为1的名字


7、delete from t2 where id=3;

删除表中id为3的数据


8、truncate t3;

delete from t2;

删除表中所有数据

truncate的原理为先drop表,再创建表;而delete为逐行删除。