SQL Server ²éѯÓÅ»¯£¨3£©_Ë÷ÒýµÄÉè¼ÆÓëʹÓÃ
Ò»¡¢Ë÷ÒýµÄ×÷ÓÃ
1¡¢°ïÖú¼ìË÷Êý¾Ý£»
2¡¢Ìá¸ßÁª½ÓЧÂÊ£»
3¡¢½ÚÊ¡ORDER BY¡¢GROUP BYµÄʱ¼ä£»
4¡¢±£Ö¤Êý¾ÝΨһÐÔ£¨½öÏÞÓÚΨһË÷Òý£©¡£
¶þ¡¢Ë÷ÒýµÄÉè¼Æ
ÔÚÈ·¶¨Òª½¨Á¢Ò»¸öË÷Òýʱ£¬Ê×ÏÈÎÒÃÇҪȷ¶¨ËüÊǾۼ¯»¹ÊǷǾۼ¯¡¢µ¥Áл¹ÊǶàÁС¢Î¨Ò»»¹ÊÇ·ÇΨһ¡¢ÁÐÊÇÉýÐò»¹ÊǽµÐò¡¢ËüµÄ´æ´¢ÊÇÈçºÎµÄ£¬±ÈÈ磺·ÖÇø¡¢Ìî³äÒò×ӵȡ£ÏÂÃæÖðÌõÀ´¿´£º
1¡¢¾Û¼¯Ë÷Òý
£¨1£©Ê×ÏÈÖ¸³öÒ»¸öÎóÇø£¬Ö÷¼ü²¢²»Ò»¶¨ÊǾۼ¯Ë÷Òý£¬Ö»ÊÇÔÚSQL SERVERÖУ¬Î´Ã÷È·Ö¸³öµÄÇé¿öÏ£¬Ä¬ÈϽ«Ö÷¼ü¶¨ÒåΪ¾Û¼¯£¬¶øORACLEÖÐÔòĬÈÏÊǷǾۼ¯£¬ÒòΪSQL SERVERÖеÄROWID먦·ÅʹÓá£
£¨2£©¾Û¼¯Ë÷ÒýÊʺÏÓÃÓÚÐèÒª½øÐз¶Î§²éÕÒµÄÁУ¬ÒòΪ¾Û¼¯Ë÷ÒýµÄÒ¶×Ó½Úµã´æ·ÅµÄÊÇÓÐÐòµÄÊý¾ÝÐУ¬²éѯÒýÇæ¿É¸ù¾ÝWHEREÖиø³öµÄ·¶Î§£¬Ö±½Ó¶¨Î»µ½Á½¶ËµÄÒ¶×ӽڵ㣬½«Õⲿ·Ö½ÚµãÒ³µÄÊý¾Ý¸ù¾ÝÁ´±í˳ÐòÈ¡³ö¼´¿É£»
£¨3£©¾Û¼¯Ë÷Òý¾¡Á¿½¨Á¢ÔÚÖµ²»»á·¢Éú±ä¸üµÄÁÐÉÏ£¬·ñÔò»á´øÀ´·Ç¾Û¼¯Ë÷ÒýµÄά»¤£»
£¨4£©¾¡Á¿ÔÚ½¨Á¢·Ç¾Û¼¯Ë÷Òý֮ǰ½¨Á¢¾Û¼¯Ë÷Òý£¬·ñÔò»áµ¼Ö±íÉÏËùÓзǾۼ¯Ë÷ÒýµÄÖؽ¨£»
£¨5£©¾Û¼¯Ë÷ÒýÓ¦¸Ã±ÜÃ⽨Á¢ÔÚÊýÖµµ¥µ÷µÄÁÐÉÏ£¬·ñÔò¿ÉÄÜ»áÔì³ÉIOµÄ¾ºÕù£¬ÒÔ¼°BÊ÷µÄ²»Æ½ºâ£¬´Ó¶øµ¼ÖÂÊý¾Ý¿âϵͳƵ·±µÄά»¤BÊ÷µÄƽºâÐÔ¡£¾Û¼¯Ë÷ÒýµÄÁÐÖµ×îºÃÄܹ»ÔÚ±íÖоùÔÈ·Ö²¼¡£
2¡¢·Ç¾Û½¹Ë÷Òý
£¨1£©·Ç¾Û¼¯Ë÷ÒýÊʺÏÓÃÓÚÐèÒª½øÐеÈÖµ²éÕÒµÄÁУ¬ÒòΪ·Ç¾Û¼¯Ë÷ÒýµÄÒ¶×Ó½Úµã´æ·ÅµÄÊÇÓÐÐòµÄË÷ÒýÁÐÓëÊéÇ©µÄÓ³ÉäÐУ¬²éѯÒýÇæ¿É¸ù¾ÝWHEREÖиø³öµÄÖµ£¬µÃµ½ÊéÇ©£¬¼Ì¶ø¶¨Î»µ½Êý¾ÝÐУ»
£¨2£©¸²¸ÇË÷Òý£¨Covering Index£©£¬ÊǷǾۼ¯Ë÷ÒýµÄÒ»ÖÖÌØÊâÇÒ¸ßЧµÄÓ¦Ó㬾ÍÊǽ«ÐèÒª·µ»ØµÄÊý¾ÝÁÐÉè¼Æ³É×éºÏË÷Òý£¬ÔÚSELECTʱֻ²éѯË÷ÒýÖдæÔÚµÄÊý¾ÝÁУ¬ÕâÑù¾ÍÄÜÐγÉË÷Òý¸²¸Ç£¬ÒòΪË÷ÒýÐÐÖÐÒѾ°üº¬ÁËÏëµ½µÄÊý¾Ý£¬²»ÐèÒªÔÙ½øÐÐÊéÇ©²éÕÒ£»
ÔÚSQL SERVER 2005¼°ÒÔÉÏ°æ±¾ÖУ¬ÌṩÁËINCLUDED¹Ø¼ü×Ö£¬¿ÉÒÔÔڷǾۼ¯Ë÷ÒýÖаüº¬¸ü¶àÁУ¬Ò²ÊǸ²¸ÇË÷ÒýµÄÒ»¸öÓÐЧÒýÉꣻ
£¨3£©·Ç¾Û¼¯Ë÷Òý½¨Á¢ÔÚÖµ¾ßÓе¥µ÷ÐÔµÄÁÐÉÏ£¬±ÈÈ磺×ÔÔöÁУ¨µ¥µ÷µÝÔö£©£¬¿ÉÒÔ¼õÉÙË÷ÒýµÄÍⲿËéƬ¼°Ë÷Òý½á¹¹µÄά»¤£»
3¡¢¸´ºÏË÷Òý
£¨1£©¸´ºÏË÷Òý½¨Á¢ÔÚ¶à¸öÁÐÉÏ¡£ÉÏÃæÒѾ½²¹ý£¬ÔڷǾۼ¯Ë÷ÒýÖУ¬¿ÉÒÔÀûÓø²¸ÇË÷ÒýÀ´Ìá¸ß¼ìË÷µÄЧÂÊ£¬µ«Èç¹û×éºÏË÷ÒýµÄÁÐÌ«¶àµÄ»°£¬ÄÇô¶ÔÓÚÕâ¸öË÷ÒýµÄά»¤³É±¾Ò²»á¼Ó´ó£¬DMLµÄЧÂʽ«»áϽµ£¬¶øÇÒË÷ÒýµÄ²éÕÒ·¾¶»á±ä³¤£»
£¨2£©ÔÚ´´½¨¸´ºÏË÷Òýʱ£¬Ó¦¸Ã½«¸ßÑ¡ÔñÐÔµÄÁзÅÔÚÇ°Ã棬¼´×÷ΪÒýµ¼ÁУ»
4¡¢Î¨Ò»Ë÷Òý
£¨1£©ÔÙÖ¸³öÒ»¸öÎóÇø£¬¾Û¼¯Ë÷Òý²¢²»Ò»¶¨ÊÇÎ
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
¡ïAsp.netÈçºÎÁ¬½ÓSQL Server2000Êý¾Ý¿â¡ï
´ó¼ÒºÃ,ÒÔÏÂÊÇÓйØASP.netÁ¬½ÓSQL Server2000Êý¾Ý¿âµÄÀý³Ì£¬
ÔÚÕâÀïºÍ´ó¼Ò·ÖÏíһϣº
Asp.netÁ¬½ÓSQL Server2000Êý¾Ý¿âÀý³ÌÏê½â:
<%@ Import Namespace="System.Data" %>
<%@ Import NameSp ......
Ëæ×ÅB/SģʽӦÓÿª·¢µÄ·¢Õ¹£¬Ê¹ÓÃÕâÖÖģʽ±àдӦÓóÌÐòµÄ³ÌÐòÔ±Ò²Ô½À´Ô½¶à¡£µ«ÊÇÓÉÓÚ³ÌÐòÔ±µÄˮƽ¼°¾ÑéÒ²²Î²î²»Æ룬Ï൱´óÒ»²¿·Ö³ÌÐòÔ±ÔÚ±àд´úÂëµÄʱºò£¬Ã»ÓжÔÓû§ÊäÈëÊý¾ÝµÄºÏ·¨ÐÔ½øÐÐÅжϣ¬Ê¹Ó¦ÓóÌÐò´æÔÚ°²È«Òþ»¼¡£Óû§¿ÉÒÔÌá½»Ò»¶ÎÊý¾Ý¿â²éѯ´úÂ룬¸ù
¾Ý³ÌÐò·µ»ØµÄ½á¹û£¬»ñµÃijЩËûÏëµÃÖªµÄÊý¾Ý£¬Õâ¾ÍÊÇËùνµÄS ......
1.ĬÈÏÔ¼Êø£º¶¨Òå¸ÃÁÐδÊäÈëֵʱӦ¸Ã¾ßÓеÄĬÈÏÖµ
SQL-------alter table ±íÃû add constraint dt_ÁÐÃû default ĬÈÏÖµ for ÁÐÃû
2.¿ÕÖµÔ¼Êø£º¶¨Òå¸ÃÁÐÊÇ·ñÔÊÐíΪ¿ÕÖµ
SQL-------¶¨Òå±íµÄʱºòÔÚ¶ÔÓ¦ÁÐÁÐÊý¾ÝÀàÐͺóÃæÌí¼Ónot null
3.¼ì²éÔ¼Êø£ºÓÖ½ÐcheckÔ¼Êø£¬ÓÃÀ´ÏÞÖÆÁеÄÈ¡Öµ£¬Ëü¸ù¾Ý¶¨ÒåµÄÂß¼±í´ïʽÀ´Ç¿ÖÆÓòµÄÍêÕûÐÔ ......
°ïÖúÀïµÄ,Óï·¨:
BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
[ { sql_statement | statement_block } ]
END CATCH
[ ; ]
Òì³£²¿·Ö:
ÔÚ CATCH ¿éµÄ×÷ÓÃÓòÄÚ£¬¿ÉÒÔʹÓÃÒÔÏÂϵͳº¯ÊýÀ´»ñÈ¡µ¼Ö CATCH ¿éÖ´ÐеĴíÎóÏûÏ¢£º
ERROR ......