日期:2014-05-16 浏览次数:20524 次
清空表 truncate及delete区别
如何使用truncate命令,以及delete 与truncate的区别
下面看一款删除mysql一个表中所有数据实例。
truncate table mytable;
利用truncate 清空表,表的id从1开始。
下面测试实例
create table `user` (?
`id` int(11) not null auto_increment,
`name` varchar(100) default null,
?primary key (`id`))
?
插入几条数据
insert into user (name) values ('bob');
insert into user (name) values ('mark');
insert into user (name) values ('alex');
insert into user (name) values ('julia');
查看一下看数据
select * from user; the result is:id???????? name??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 1????????? bob??????? 2????????? mark
??3????????? alex??????? 4????????? julia?
看一下truncate实例
truncate table user;
插入一条数据
insert into user (name) values ('bill');
查看结果
select * from user;
the result is:id???????? name????????????? 1????????? bill
正如你可以看到旧的记录被删除除,而新的得到的id字段1