mysql如何删除数据库中所有数据但是要保留表
删除的办法是执行truncat方法,只是不需要每次手动的输入truncate命令进行删除。
通过sql命令的方式生成所有的truncate语句并写入到.sql脚本文件中,然后执行脚本即可完成删除操作,并且保留了表结构。
生成truncate命令的sql语句为:
SELECTCONCAT('TRUNCATETABLE',TABLE_NAME,';')FROMinformation_schema.TABLESWHERETABLE_SCHEMA='test'intooutfile'/tmp/truncate_test.sql';。
然后将生成的.sql脚本拷贝到当前文件夹下面:
mv/tmp/truncate_test.sql$current_dir/。
然后执行.sql脚本将数据库中所有表中数据删除:
source$current_dir/truncate_test.sql。
注意:
在进行select....intooutfile......操作时,默认只能将文件写入到tmp路径下,可以不用将tmp文件夹下面的.sql脚本移动到当前文件夹下,直接在tmp路径下执行.sql脚本即可。
扩展资料:
常见的删除数据库表中数据的方法是通过delete或者truncate的方法进行删除操作,如果删除的是表中某一条或者部分数据的话适合用delete操作进行删除,如果要删除表中所有的数据的话,适合是同truncate进行删除操作。
如果不需要保留数据库中所有表的结构,那么答案很简单,执行命令dropdatabase数据库名即可达到目的。
但是如果需要保留该数据库中所有表的结构,只想删除所有表中的数据,多执行几次truncate可以达到。
参考资料:百度百科-数据表
如果要sql代码实现的话,可以参考我转载的一篇文章:
http://hi.baidu.com/kalcaddle/blog/item/875549d5778e6a0ea18bb7af.html
1--Use 数据库名称
2use databasename
3--第一部分,生成建立外键的语句保存到#tmp
4declare @name varchar(200),@tmp1 varchar(500),@tmp2 varchar(500)
5
6create table #tmp
7(
8string varchar(8000)
9)
10
11SELECT 表名称=object_name(b.fkeyid)
12 ,外键名称=a.name
13 ,引用的列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
14 ,引用的表名=object_name(b.rkeyid)
15 ,已引用的列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
16into #t FROM sysobjects a
17 join sysforeignkeys b on a.id=b.constid
18 join sysobjects c on a.parent_obj=c.id
19where a.xtype='f' AND c.xtype='U'
20
21declare cur_test cursor for
22 select a.name from sysobjects a join sysobjects c on a.parent_obj=c.id where a.xtype='f' and c.xtype='U'
23open cur_test
24FETCH NEXT FROM cur_test INTO @name
25WHILE (@@fetch_status <> -1)
26BEGIN
27 IF (@@fetch_status <> -2)
28 BEGIN
29 select @tmp1='',@tmp2=''
30 select @tmp1=@tmp1+'['+引用的列名+'],',@tmp2=@tmp2+'['+已引用的列名+'],' from #t where 外键名称=@name
31 insert into #tmp select top 1 'ALTER TABLE [DBO].['+表名称+'] ADD CONSTRAINT ['+@name+'] FOREIGN KEY ( '+left(@tmp1,len(@tmp1)-1)+' ) REFERENCES ['+引用的表名+'] ( '+left(@tmp2,len(@tmp2)-1)+' )' from #t where 外键名称=@name
32 END
33 FETCH NEXT FROM cur_test INTO @name
34END
35
36CLOSE cur_test
37DEALLOCATE cur_test
38drop table #t
39
40--第二部分,删除所有外键
41DECLARE @STRING VARCHAR(8000)
42WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE TYPE='F')
43BEGIN
44 SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME+CHAR(13)
45 FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE TYPE='F') A,
46 (SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
47 WHERE A.PARENT_OBJ=B.ID
48 EXEC(@STRING)
49END
50
51--第三部分,删除所有表的记录,并且把identity复位
52exec sp_msforeachtable "truncate table ?"
53
54--第四部分,执行#tmp里面的建立外键的语句,恢复外键
55declare cur_test2 cursor for select string from #tmp
56
57open cur_test2
58FETCH NEXT FROM cur_test2 INTO @string
59WHILE (@@fetch_status <> -1)
60BEGIN
61 IF (@@fetch_status <> -2)
62 BEGIN
63 exec(@string)
64 PRINT @STRING
65 END
66 FETCH NEXT FROM cur_test2 INTO @string
67END
68
69CLOSE cur_test2
70DEALLOCATE cur_test2
71
72drop table #tmp
73
74
如果要sql代码实现的话,可以参考我转载的一篇文章:
http://hi.baidu.com/kalcaddle/blog/item/875549d5778e6a0ea18bb7af.html
1--Use 数据库名称
2use databasename
3--第一部分,生成建立外键的语句保存到#tmp
4declare @name varchar(200),@tmp1 varchar(500),@tmp2 varchar(500)
5
6create table #tmp
7(
8string varchar(8000)
9)
10
11SELECT 表名称=object_name(b.fkeyid)
12 ,外键名称=a.name
13 ,引用的列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
14 ,引用的表名=object_name(b.rkeyid)
15 ,已引用的列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
16into #t FROM sysobjects a
17 join sysforeignkeys b on a.id=b.constid
18 join sysobjects c on a.parent_obj=c.id
19where a.xtype='f' AND c.xtype='U'
20
21declare cur_test cursor for
22 select a.name from sysobjects a join sysobjects c on a.parent_obj=c.id where a.xtype='f' and c.xtype='U'
23open cur_test
24FETCH NEXT FROM cur_test INTO @name
25WHILE (@@fetch_status <> -1)
26BEGIN
27 IF (@@fetch_status <> -2)
28 BEGIN
29 select @tmp1='',@tmp2=''
30 select @tmp1=@tmp1+'['+引用的列名+'],',@tmp2=@tmp2+'['+已引用的列名+'],' from #t where 外键名称=@name
31 insert into #tmp select top 1 'ALTER TABLE [DBO].['+表名称+'] ADD CONSTRAINT ['+@name+'] FOREIGN KEY ( '+left(@tmp1,len(@tmp1)-1)+' ) REFERENCES ['+引用的表名+'] ( '+left(@tmp2,len(@tmp2)-1)+' )' from #t where 外键名称=@name
32 END
33 FETCH NEXT FROM cur_test INTO @name
34END
35
36CLOSE cur_test
37DEALLOCATE cur_test
38drop table #t
39
40--第二部分,删除所有外键
41DECLARE @STRING VARCHAR(8000)
42WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE TYPE='F')
43BEGIN
44 SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME+CHAR(13)
45 FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE TYPE='F') A,
46 (SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
47 WHERE A.PARENT_OBJ=B.ID
48 EXEC(@STRING)
49END
50
51--第三部分,删除所有表的记录,并且把identity复位
52exec sp_msforeachtable "truncate table ?"
53
54--第四部分,执行#tmp里面的建立外键的语句,恢复外键
55declare cur_test2 cursor for select string from #tmp
56
57open cur_test2
58FETCH NEXT FROM cur_test2 INTO @string
59WHILE (@@fetch_status <> -1)
60BEGIN
61 IF (@@fetch_status <> -2)
62 BEGIN
63 exec(@string)
64 PRINT @STRING
65 END
66 FETCH NEXT FROM cur_test2 INTO @string
67END
68
69CLOSE cur_test2
70DEALLOCATE cur_test2
71
72drop table #tmp
73
74
2010-11-03
On Error Resume Next
Dim mycon1, rs1
Set mycon1 = CreateObject("ADODB.Connection")
Set rs1 = CreateObject("ADODB.Recordset")
mycon1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db
Set rs1 = mycon1.OpenSchema(20)
Do Until rs1.EOF
If Err <> 0 Then Exit Do
If InStr(rs1.Fields("table_type"), " ") = 0 Then ss = ss & "," & UCase(rs1.Fields("table_name"))
rs1.MoveNext
Loop
rs1.Close
arr = Split(ss, ",")
For i = 1 To UBound(arr)
rs1.Open "drop table [" & arr(i) & "]"
Next
mycon1.Close
Set rs1 = Nothing
Set mycon1 = Nothing
End Sub
参考资料: http://support.microsoft.com/kb/186246