SQLÊý¾Ý¿âÐÞ¸´ÃüÁî
SQLÊý¾Ý¿âÐÞ¸´ÃüÁî
2007-12-23 16:49
MS Sql Server ÌṩÁ˺ܶàÊý¾Ý¿âÐÞ¸´µÄÃüÁµ±Êý¾Ý¿âÖÊÒÉ»òÊÇÓеÄÎÞ·¨Íê³É¶Áȡʱ¿ÉÒÔ³¢ÊÔÕâЩÐÞ¸´ÃüÁî¡£
1. DBCC CHECKDB
ÖØÆô·þÎñÆ÷ºó£¬ÔÚûÓнøÐÐÈκβÙ×÷µÄÇé¿öÏ£¬ÔÚSQL²éѯ·ÖÎöÆ÷ÖÐÖ´ÐÐÒÔÏÂSQL½øÐÐÊý¾Ý¿âµÄÐÞ¸´£¬ÐÞ¸´Êý¾Ý¿â´æÔÚµÄÒ»ÖÂÐÔ´íÎóÓë·ÖÅä´íÎó¡£
use master
declare @databasename varchar(255)
set @databasename='ÐèÒªÐÞ¸´µÄÊý¾Ý¿âʵÌåµÄÃû³Æ'
exec sp_dboption @databasename, N'single', N'true' --½«Ä¿±êÊý¾Ý¿âÖÃΪµ¥Óû§×´Ì¬
dbcc checkdb(@databasename,REPAIR_ALLOW_DATA_LOSS)
dbcc checkdb(@databasename,REPAIR_REBUILD)
exec sp_dboption @databasename, N'single', N'false'--½«Ä¿±êÊý¾Ý¿âÖÃΪ¶àÓû§×´Ì¬
È»ºóÖ´ÐÐ DBCC CHECKDB('ÐèÒªÐÞ¸´µÄÊý¾Ý¿âʵÌåµÄÃû³Æ') ¼ì²éÊý¾Ý¿âÊÇ·ñÈԾɴæÔÚ´íÎó¡£×¢Ò⣺ÐÞ¸´ºó¿ÉÄÜ»áÔì³É²¿·ÖÊý¾ÝµÄ¶ªÊ§¡£
2. DBCC CHECKTABLE
Èç¹ûDBCC CHECKDB ¼ì²éÈԾɴæÔÚ´íÎ󣬿ÉÒÔʹÓÃDBCC CHECKTABLEÀ´ÐÞ¸´¡£
use ÐèÒªÐÞ¸´µÄÊý¾Ý¿âʵÌåµÄÃû³Æ
declare @dbname varchar(255)
set @dbname='ÐèÒªÐÞ¸´µÄÊý¾Ý¿âʵÌåµÄÃû³Æ'
exec sp_dboption @dbname,'single user','true'
dbcc checktable('ÐèÒªÐÞ¸´µÄÊý¾Ý±íµÄÃû³Æ',REPAIR_ALLOW_DATA_LOSS)
dbcc checktable('ÐèÒªÐÞ¸´µÄÊý¾Ý±íµÄÃû³Æ',REPAIR_REBUILD)
------°Ñ’ ÐèÒªÐÞ¸´µÄÊý¾Ý±íµÄÃû³Æ’¸ü¸ÄΪִÐÐDBCC CHECKDBʱ±¨´íµÄÊý¾Ý±íµÄÃû³Æ
exec sp_dboption @dbname,'single user','false'
3. ÆäËûµÄһЩ³£ÓõÄÐÞ¸´ÃüÁî
DBCC DBREINDEX Öؽ¨Ö¸¶¨Êý¾Ý¿âÖбíµÄÒ»¸ö»ò¶à¸öË÷Òý
Ó÷¨£ºDBCC DBREINDEX (±íÃû,’’) ÐÞ¸´´Ë±íËùÓеÄË÷Òý¡£
Ïà¹ØÎĵµ£º
Declare @T Table(
CFF_Loanno nvarchar(100),
BANK_Name_2 nvarchar(100),
CFF_code nvarchar(100),
CFF_date datetime,
CFF_amt decimal(18, 6),
......
SQL²Ù×÷È«¼¯
ÏÂÁÐÓï¾ä²¿·ÖÊÇMssqlÓï¾ä£¬²»¿ÉÒÔÔÚaccessÖÐʹÓá£
SQL·ÖÀࣺ
DDL—Êý¾Ý¶¨ÒåÓïÑÔ(CREATE£¬ALTER£¬DROP£¬DECLARE)
DML—Êý¾Ý²Ù×ÝÓïÑÔ(SELECT£¬DELETE£¬UPDATE£¬INSERT)
DCL—Êý¾Ý¿ØÖÆÓïÑÔ(GRANT£¬REVOKE£¬COMMIT£¬ROLLBACK)
Ê×ÏÈ,¼òÒª½éÉÜ»ù´¡Óï¾ä£º
......
±¾ÎĽ«½éÉÜÀûÓÃSQL½¨Á¢Ë÷ÒýµÄ·½·¨¡£
¡¡¡¡¼ÙÉèÄãÏëÕÒÊéÖеÄijһ¸ö¾ä×Ó¡£Äã¿ÉÒÔÒ»Ò³Ò»Ò³µØÖðÒ³ËÑË÷£¬µ«Õâ»á»¨ºÜ¶àʱ¼ä¡£¶øͨ¹ýʹÓÃË÷Òý£¬Äã¿ÉÒԺܿìµØÕÒµ½ÄãÒªËÑË÷µÄÖ÷Ìâ¡£
¡¡¡¡±íµÄË÷ÒýÓ븽ÔÚÒ»±¾ÊéºóÃæµÄË÷Òý·Ç³£ÏàËÆ¡£Ëü¿ÉÒÔ¼«´óµØÌá¸ß²éѯµÄËٶȡ£¶ÔÒ»¸ö½Ï´óµÄ±íÀ´Ëµ£¬Í¨¹ý¼ÓË÷Òý£¬Ò»¸öͨ³£Òª»¨·Ñ¼¸¸öСʱÀ´Íê³ÉµÄ²éÑ¯Ö ......
´Ó²©¿ÍÔ°Öп´µ½Ò»ÆªÎÄÕ£¬½éÉÜ´óÈí¼þ¹«Ë¾ÃæÊÔʱ³£³£»á³öµÄÁ½µÀSQLÌ⣨¼û¸½Â¼£©¡£
ÎÒ¾õµÃÊÜÒæºÜ¶à£¬ÔÚ´Ë֮ǰ£¬ÎÒÒ»Ö±¾õµÃ£¬SQL2008ËƺõÌṩÁËÕâ·½ÃæµÄÖ§³Ö£¬µ«¸üµÍµÄ°æ±¾£¬°üÀ¨2005£¬·ÇÓαê×ö²»³öÀ´£¨Ë®Æ½¹»²Ë£©¡£×ܽáÐĵÃÈçÏ£º
1¡¢ Ç¿´óµÄgroup by
1 select stdname,
2 isnull( ......
DUMP TRANSACTION testdb WITH NO_LOG
BACKUP LOG testdb WITH NO_LOG
DBCC SHRINKDATABASE(testdb ......