SQL语句--delete

来源:互联网 发布:使命召唤 for mac 编辑:程序博客网 时间:2024/06/03 07:12

实务:

1,如何删除表中的重复数据

方法一:delete from table student where rowid not in(select min(rowid) from student group by id);

方法二:delete from student a where rowid !=(select max(rowid) from student b where a.id =b.id);

方法三:1,创建一个临时表,存放的数据与原表相同,只是没有重复记录 create table tem_student as (select distinct id,first_name,last_name,major from student);

        2,删除原表数据 TRUNCATE table student ;这里一定用TRUNCATE,速度较快

3,把临时表中的数据插到原表中insert into student select * from tem_student;

2,如何删除一张表中所有数据

方法一:常规删除delete table a; commit;DELETE语句特点:1,会产生回滚信息2,不影响高水位线3,是DML语句4,是删除整个表中数据最慢的一种方法5,对表上的约束,触发器权限等不会有太大影响

方法二:使用TRUNCATE方法 TRUNCATE table a;  TRUNCATE特点:1,会进行空间回收,降低高水位线2,是DDL语句3,不会产生回滚信息4,是删除整个表中数据最快的一种方法5,对表上的约束,触发器权限等不会有太大影响

方法三:重建这个表

1,创建一个临时表,这个表的结构和要删除的表完全相同,只是这个表中没有数据create table tem_student as select * from student where 1=0; 1=0使创建的表没有数据

2,drop 原表 drop table student;

3,重建表 create table student as select * from tem_student;

4,重建表student上的索引,约束,触发器等

重建表特点:1,会进行空间回收,降低高水位线2,DDL 语句 3,不会产生回滚信息4,是删除整个表中数据比较快的一种方法5,需要重新维护约束,触发器和权限等































0 0
原创粉丝点击