当前位置:首页 » 编程语言 » 清理sql

清理sql

发布时间: 2022-01-08 10:06:54

⑴ 清理数据库碎片sql语句

清除数据库日志语句
--清除sqlserver 2000/2005 数据库日志
替换下红字部分数据库名称

declare @databasename varchar(15)
select @databasename='hdjjgl_journal'
DUMP TRANSACTION @databasename WITH NO_LOG

BACKUP LOG @databasename WITH NO_LOG

DBCC SHRINKDATABASE(@databasename)

或者
USE 数据库名
DUMP TRANSACTION 数据库名 WITH NO_LOG
BACKUP LOG 数据库名 WITH NO_LOG
DBCC SHRINKFILE(2)

**********************************
--清除sqlserver 2008 数据库日志 ,执行两遍
替换下红字部分数据库名称,建立datalogbak清楚的日志备份文件夹,注意路径自定义。

use gdzjg_journal
declare @databasename varchar(100)
declare @databasenamelog varchar(100)
declare @databasenamedir varchar(100)
select @databasename='gdzjg_journal'
select @databasenamelog=@databasename+'_log'
select @databasenamedir='g:\datalogbak\'+@databasename

BACKUP LOG @databasename to disk=@databasenamedir
DBCC SHRINKFILE (@databasenamelog,1)

或者

ALTER DATABASE 数据库名称SET RECOVERY SIMPLE
ALTER DATABASE 数据库名称SET RECOVERY FULL
DBCC SHRINKDATABASE(数据库名称,0)

sqlserver 2000、2008会保存所有的数据库操作过程,将指令保存在ldf文件中,如果误删除数据,想恢复数据的话,可以通过Lumigent Log Explorer For SQLServer 软件分析ldf文件,可以看到所有执行过的insert、update、delete数据,导出来再逆向执行即可,本人曾经用过一回,确实奏效。

1、sql server 2000清理数据库日志

USE 数据库名

DUMP TRANSACTION 数据库名 WITH NO_LOG
BACKUP LOG 数据库名 WITH NO_LOG
DBCC SHRINKFILE(2)

SHRINKFILE(2),后面的2是file_id,可以在当前数据库用select * from sysfiles看得到,看数据库的log文件的fileid是多少,正常情况下log的ldf文件是2,mdf文件的fileid是1。
这个查询语句可以随时执行,不影响数据库的运行。

sql server 2005、2008清理数据库日志

USE 数据库名
ALTER DATABASE 数据库名 SET RECOVERY SIMPLE
ALTER DATABASE 数据库名 SET RECOVERY FULL
DBCC SHRINKDATABASE(数据库名,0)

这个查询语句可以随时执行,不影响数据库的运行。

清理ldf的操作可以使用sql server代理,每天自动执行一次,就不怕文件增长撑爆硬盘了。

2、一般mdf用不着收缩,收缩多了容易产生文件碎片,因为delete数据之后,mdf文件中可用页面空间会保留在那里,等下次有新数据进来时,会继续使用。如果实在要清理,可以参考下面的语句:

sql server 2000、2005、2008 收缩mdf文件

DBCC SHRINKDATABASE(数据库名)
DBCC SHRINKFILE(1,0)
DBCC UPDATEUSAGE(0)

执行上述操作后,你会发现mdf的文件减少了,ldf的文件增大了,再用上面1的日志清理操作一次即可。

3、最后再附送一个查看数据表的行数、占用文件空间、存储情况的查询语句

-- drop table #test
create table #test(
name varchar(50),
rows int,
reserved varchar(20),
data varchar(20),
index_size varchar(20),
unused varchar(20)
)
set nocount on
insert into #test
EXEC sp_MSforeachtable @command1="sp_spaceused '?'"
select * from #test order by cast(replace(reserved,'KB','') as int) desc

⑵ 如何完全清理sql软件 ,有什么软件可以清理的

1. 用360或者其他带软件管理的辅助软件,
2.手动清理把,第一步用 "控制面板"里面的系统自带的程序卸载, 卸载完成之后去目录看看相关的文件夹有木有删除,如果文件夹删除了,然后去注册表搜索所有跟你装的数据相关的项删掉。

⑶ SQL清除语句

CREATE SNAPSHOT [schema.]snapshot
[ [PCTFREE integer] [PCTUSED integer]
[INITRANS integer] [MAXTRANS integer]
[TABLESPACE tablespace]
[STORAGE storage_clause]

[ USING INDEX [ PCTFREE integer | TABLESPACE tablespace
| INITTRANS integer | MAXTRANS integer
| STORAGE storage_clause ] ...
| [CLUSTER cluster (column [, column]...)] ]
[ REFRESH [FAST | COMPLETE | FORCE] [START WITH date] [NEXT date]]
AS subquery

schema
is the schema to contain the snapshot. If you omit schema, Oracle

creates the snapshot in your schema.

snapshot
is the name of the snapshot to be created.

Oracle chooses names for the table, views, and index used to
maintain the snapshot by prefixing the snapshot name. To limit
these names to 30 bytes and allow them to contain the entire
snapshot name, Oracle Corporation recommends that you limit your
snapshot names to 23 bytes.

PCTFREE
PCTUSED
INITRANS
MAXTRANS
establishes values for these parameters for the internal table
Oracle uses to maintain the snapshot's data.

TABLESPACE
specifies the tablespace in which the snapshot is to be created. If
you omit this option, Oracle creates the snapshot in the default
tablespace of the owner of the snapshot's schema.

STORAGE
establishes storage characteristics for the table Oracle uses to

maintain the snapshot's data.

USING INDEX
specifies the storage characteristics for the index on a simple
snapshot. If the USING INDEX clause not specified, the index is
create with the same tablespace and storage parameters as the
snapshot.

CLUSTER
creates the snapshot as part of the specified cluster. Since a
clustered snapshot uses the cluster's space allocation, do not use

the PCTFREE, PCTUSED, INITRANS, or MAXTRANS parameters, the
TABLESPACE option, or the STORAGE clause in conjunction with the
CLUSTER option.

REFRESH
specifies how and when Oracle automatically refreshes the snapshot:
FAST
specifies a fast refresh, or a refresh using only the
updated data stored in the snapshot log associated
with the master table.

COMPLETE
specifies a complete refresh, or a refresh that re-
executes the snapshot's query.
FORCE
specifies a fast refresh if one is possible or
complete refresh if a fast refresh is not possible.
Oracle decides whether a fast refresh is possible at
refresh time.

If you omit the FAST, COMPLETE, and FORCE options,
Oracle uses FORCE by default.
START WITH
specifies a date expression for the first automatic
refresh time.
NEXT
specifies a date expression for calculating the
interval between automatic refreshes.

Both the START WITH and NEXT values must evaluate to a time in the

future. If you omit the START WITH value, Oracle determines the
first automatic refresh time by evaluating the NEXT expression when
you create the snapshot. If you specify a START WITH value but omit
the NEXT value, Oracle refreshes the snapshot only once. If you
omit both the START WITH and NEXT values or if you omit the REFRESH
clause entirely, Oracle does not automatically refresh the snapshot.

AS subquery
specifies the snapshot query. When you create the snapshot, Oracle
executes this query and places the results in the snapshot. The
select list can contain up to 253 expressions. A snapshot query is
subject to the same restrictions as a view query.

PREREQUISITES:

To create a snapshot in your own schema, you must have CREATE
SNAPSHOT system privilege. To create a snapshot in another user's

schema, you must have CREATE ANY SNAPSHOT system privilege.

Before a snapshot can be created, the user SYS must run the SQL
script DBMSSNAP.SQL on both the database to contain the snapshot and
the database(s) containing the tables and views of the snapshot's
query. This script creates the package SNAPSHOT which contains both
public and private stored proceres used for refreshing the
snapshot and purging the snapshot log. The exact name and location

of this script may vary depending on your operating system.

When you create a snapshot, Oracle creates a table, two views, and
an index in the schema of the snapshot. Oracle uses these objects
to maintain the snapshot's data. You must have the privileges
necessary to create these objects. For information on these
privileges, see the CREATE TABLE, CREATE VIEW, and CREATE INDEX

commands.

The owner of the schema containing the snapshot must have either
space quota on the tablespace to contain the snapshot or UNLIMITED
TABLESPACE system privilege. Also, both you (the creator) and the
owner must also have the privileges necessary to issue the
snapshot's query.

To create a snapshot, you must be using Oracle with the proceral
option. To create a snapshot on a remote table or view, you must

also be using the distributed option.

⑷ 怎么清理sql server 的数据库

方法/步骤

1.依次打开 控制面板-->程序-->卸载程序,将带有SQL Server选项的相关组件全部卸载。

2.打开Windows Install Clean Up进行深度清理。注意:该工具的四个按键功能分别为(Select all)查找全部,(Clear all)清除全部,(Remove)删除选中项,(Exit)退出。使用时只需选择带有SQL 部分进行Remove即可。

完:通过上述简单便捷的操作即将SQL Server数据库安装失败所残留的文件彻底清除,无需用户花费大量时间去逐个排查寻找,更无需重新安装操作系统即可再次安装SQL Server数据库。该方法同样适用于其他Microsoft软件的彻底删除与卸载。

⑸ 如何清除SQL数据库中的数据

  1. 单击开始---->所有程序---->Microsoft SQL Server 2014,选择SQL Server 2014 Management Studio选项,打开软件。

(5)清理sql扩展阅读:

SQL是Structured Query Language(结构化查询语言)的缩写。SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言。在使用它时,只需要发出“做什么”的命令,“怎么做”是不用使用者考虑的。SQL功能强大、简单易学、使用方便,已经成为了数据库操作的基础,并且现在几乎所有的数据库均支持SQL。

SQL数据库的数据体系结构基本上是三级结构,但使用术语与传统关系模型术语不同。在SQL中,关系模式(模式)称为“基本表”(base table);存储模式(内模式)称为“存储文件”(stored file);子模式(外模式)称为“视图”(view);元组称为“行”(row);属性称为“列”(column)。名称对称如^00100009a^:

SQL包括了所有对数据库的操作,主要是由4个部分组成:

  1. 数据定义:这一部分又称为“SQL DDL”,定义数据库的逻辑结构,包括定义数据库、基本表、视图和索引4部分。

  2. 数据操纵:这一部分又称为“SQL DML”,其中包括数据查询和数据更新两大类操作,其中数据更新又包括插入、删除和更新三种操作。

  3. 数据控制:对用户访问数据的控制有基本表和视图的授权、完整性规则的描述,事务控制语句等。

  4. 嵌入式SQL语言的使用规定:规定SQL语句在宿主语言的程序中使用的规则。


参考资料:IT专家网-微软SQL

⑹ SQL怎么删除干净

微软的东西就这样,很傻瓜的
尤其是SQL这类,必须要严格按照步骤做
格式化吧,别的没办法

⑺ 如何清除SQL数据库啊

先把标的架构导出再删除所有表,之后在把架构导入即可。

企业管理器-->选中所有表,点击右键-->所有任务-->生成SQL脚本(G)...-->点击选项标签-->表选项的所有复选框都打上勾-->点击确定
生成的脚本文件放到查询分析器执行就OK

⑻ sql数据库怎么清理

思路:1,把数据库文件路径找到。2,分离数据库。3,删除对应数据库文件的日志文件.ldf。4,附加对应数据库文件.mdf。 详解:先看“jita”
数据库文件放在什么地方在D:\Data路径下。再把“jita”
数据库分离 然后把日志.ldf
删除,再附加数据.mdf 此时sql
server会重新生成一个很小的.ldf
日志。

⑼ 如何清空SQL数据库

truncate
table
+
要清的表名,则可将表内容完全删除而保留表结构

⑽ sql数据库满了怎么清理

-- 清空日志
--压缩日志及数据库文件大小

/*--特别注意
请按步骤进行,未进行前面的步骤,请不要做后面的步骤
否则可能损坏你的数据库.
--*/
select*fromsysfiles
--1.清空日志
DUMPTRANSACTIONusernameWITHNO_LOG

--2.截断事务日志:
BACKUPLOGusernameWITHNO_LOG

--3.收缩数据库文件(如果不压缩,数据库的文件不会减小
-- 企业管理器--右键你要压缩的数据库--所有任务--收缩数据库--收缩文件
--选择日志文件--在收缩方式里选择收缩至XXM,这里会给出一个允许收缩到的最小M数,直接输入这个数,确定就可以了
--选择数据文件--在收缩方式里选择收缩至XXM,这里会给出一个允许收缩到的最小M数,直接输入这个数,确定就可以了

-- 也可以用SQL语句来完成
--收缩数据库
DBCCSHRINKDATABASE(username)

--收缩指定数据文件,1是文件号,可以通过这个语句查询到:select*fromsysfiles

DBCCSHRINKFILE(2)

--4.为了最大化的缩小日志文件(如果是sql7.0,这步只能在查询分析器中进行)
-- a.分离数据库:
-- 企业管理器--服务器--数据库--右键--分离数据库

-- b.在我的电脑中删除LOG文件

-- c.附加数据库:
-- 企业管理器--服务器--数据库--右键--附加数据库

-- 此法将生成新的LOG,大小只有500多K

-- 或用代码:
-- 下面的示例分离username,然后将username中的一个文件附加到当前服务器。

execsp_dboptionusername,'singleuser',true
a.分离
EXECsp_detach_db@dbname='username'

b.删除日志文件
execmaster..xp_cmdshell'delD:\ProgramFiles\SQL\database\username_LOG.ldf'

c.再附加
EXECsp_attach_single_file_db@dbname='username',
@physname='D:\ProgramFiles\SQL\database\username_Data.MDF'

--5.为了以后能自动收缩,做如下设置:
-- 企业管理器--服务器--右键数据库--属性--选项--选择"自动收缩"

--SQL语句设置方式:
EXECsp_dboption'数据库名','autoshrink','TRUE'

--6.如果想以后不让它日志增长得太大
-- 企业管理器--服务器--右键数据库--属性--事务日志
--将文件增长限制为xM(x是你允许的最大数据文件大小)

--SQL语句的设置方式:
alterdatabase数据库名modifyfile(name=逻辑文件名,maxsize=20)

热点内容
gon引擎自动回收脚本 发布:2024-09-20 05:39:39 浏览:246
好医生连锁店密码多少 发布:2024-09-20 05:09:38 浏览:15
魔兽脚本代理 发布:2024-09-20 05:09:35 浏览:98
python登陆网页 发布:2024-09-20 05:08:39 浏览:757
安卓qq飞车如何转苹果 发布:2024-09-20 04:54:30 浏览:178
存储过程中in什么意思 发布:2024-09-20 04:24:20 浏览:315
php显示数据 发布:2024-09-20 03:48:38 浏览:501
源码安装软件 发布:2024-09-20 03:44:31 浏览:354
入门编程游戏的书 发布:2024-09-20 03:31:26 浏览:236
e盒的算法 发布:2024-09-20 03:30:52 浏览:144