當前位置:首頁 » 編程語言 » 清理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)

熱點內容
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:651
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:477
電腦主伺服器怎麼開機 發布:2024-09-20 07:19:07 瀏覽:728
2022款瑞虎升級哪些配置 發布:2024-09-20 06:59:07 瀏覽:264
資料庫與asp 發布:2024-09-20 06:55:25 瀏覽:727
python解釋編譯 發布:2024-09-20 06:52:57 瀏覽:648
舞蹈豐收腳本 發布:2024-09-20 06:36:26 瀏覽:595
linux進程埠號 發布:2024-09-20 06:36:11 瀏覽:80
派派怎麼改密碼忘了 發布:2024-09-20 06:25:49 瀏覽:780
linux虛擬地址物理地址 發布:2024-09-20 06:23:29 瀏覽:564