SQL CREATE INDEXµÄÓ÷¨
Ë÷Òý (Index) ¿ÉÒÔ°ïÖúÎÒÃÇ´Ó±í¸ñÖпìËÙµØÕÒµ½ÐèÒªµÄ×ÊÁÏ¡£¾ÙÀýÀ´Ëµ£¬¼ÙÉèÎÒÃÇÒªÔÚÒ»±¾Ô°ÒÕÊéÖÐÕÒÈçºÎÖÖÖ²ÇཷµÄѶϢ¡£ÈôÕâ±¾ÊéûÓÐË÷ÒýµÄ»°£¬ÄÇÎÒÃÇÊDZØÐëÒª´ÓÍ·¿ªÊ¼¶Á£¬Ö±µ½ÎÒÃÇÕÒµ½ÓйØÖÖÖ±ÇཷµÄµØ·½ÎªÖ¹¡£ÈôÕâ±¾ÊéÓÐË÷ÒýµÄ»°£¬ÎÒÃǾͿÉÒÔÏÈÈ¥Ë÷ÒýÕÒ³öÖÖÖ²ÇཷµÄ×ÊѶÊÇÔÚÄÄÒ»Ò³£¬È»ºóÖ±½Óµ½ÄÇһҳȥÔĶÁ¡£ºÜÃ÷ÏԵأ¬ÔËÓÃË÷ÒýÊÇÒ»ÖÖÓÐЧÇÒʡʱµÄ·½Ê½¡£
´Ó×ÊÁÏ¿â±í¸ñÖÐÑ°ÕÒ×ÊÁÏÒ²ÊÇͬÑùµÄÔÀí¡£Èç¹ûÒ»¸ö±í¸ñûÓÐË÷ÒýµÄ»°£¬×ÊÁÏ¿âϵͳ¾ÍÐèÒª½«Õû¸ö±í¸ñµÄ×ÊÁ϶Á³ö (Õâ¸ö¹ý³Ì½Ð×ö'table scan')¡£ÈôÓÐÊʵ±µÄË÷Òý´æÔÚ£¬×ÊÁÏ¿âϵͳ¾Í¿ÉÒÔÏÈÓÉÕâ¸öË÷ÒýÈ¥ÕÒ³öÐèÒªµÄ×ÊÁÏÊÇÔÚ±í¸ñµÄʲôµØ·½£¬È»ºóÖ±½ÓÈ¥ÄÇЩµØ·½×¥×ÊÁÏ¡£ÕâÑù×ÓËٶȾͿì¶àÁË¡£
Òò´Ë£¬ÔÚ±í¸ñÉϽ¨Á¢Ë÷ÒýÊÇÒ»¼þÓÐÀûÓÚϵͳЧÂʵÄÊ¡£Ò»¸öË÷Òý¿ÉÒÔº¸ÇÒ»»ò¶à¸öÀ¸Î»¡£½¨Á¢Ë÷ÒýµÄÓï·¨ÈçÏ£º
CREATE INDEX "INDEX_NAME" ON "TABLE_NAME" (COLUMN_NAME)
ÏÖÔÚ¼ÙÉèÎÒÃÇÓÐÒÔÏÂÕâ¸ö±í¸ñ£¬
TABLE Customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)
ÈôÎÒ們ÒªÔÚ Last_Name 這個欄λÉϽ¨Ò»個Ë÷Òý£¬ÎÒ們¾Í´òÈëÒÔϵÄÖ¸Á
CREATE INDEX IDX_CUSTOMER_LAST_NAME
on CUSTOMER (Last_Name)
ÈôÎÒÃÇÒªÔÚ Last_Name Õâ¸öÀ¸Î»ÉϽ¨Ò»¸öË÷Òý£¬ÎÒÃǾʹòÈëÒÔϵÄÖ¸Á
CREATE INDEX IDX_CUSTOMER_LOCATION
on CUSTOMER (City, Country)
Ë÷ÒýµÄÃüÃû²¢Ã»ÓÐÒ»¸ö¹Ì¶¨µÄ·½Ê½¡£Í¨³£»áÓõķ½Ê½ÊÇÔÚÃû³ÆÇ°¼ÓÒ»¸ö×ÖÊ×£¬ÀýÈç "IDX_" £¬À´±ÜÃâÓë×ÊÁÏ¿âÖеÄÆäËûÎï¼þ»ìÏý¡£ÁíÍ⣬ÔÚË÷ÒýÃûÖ®ÄÚ°üÀ¨±í¸ñÃû¼°À¸Î»ÃûÒ²ÊÇÒ»¸öºÃµÄ·½Ê½¡£
Çë¶ÁÕß×¢Ò⣬ÿ¸ö×ÊÁÏ¿â»áÓÐËü±¾ÉíµÄ CREATE INDEX Óï·¨£¬¶ø²»Í¬×ÊÁÏ¿âµÄÓï·¨»áÓв»Í¬¡£Òò´Ë£¬ÔÚÏÂÖ¸ÁîÇ°£¬ÇëÏÈÓÉ×ÊÁÏ¿âʹÓÃÊÖ²áÖÐÈ·ÈÏÕýÈ·µÄÓï·¨¡£
Ïà¹ØÎĵµ£º
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--ÿҳÏÔʾ¼Ç¼ÌõÊý
@currentpage int output,--µÚ¼¸Ò³
@orderid nvarchar(50),--Ö÷¼üÅÅÐò
@sort int,--ÅÅÐò·½Ê½£¬1±íʾÉýÐò£¬0±íʾ½µÐòÅÅÁÐ
......
IN Õâ¸öÖ¸Áî¿ÉÒÔÈÃÎÒÃÇÒÀÕÕÒ»»òÊý¸ö²»Á¬Ðø (discrete) µÄÖµµÄÏÞÖÆÖ®ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ£¬¶ø BETWEEN ÔòÊÇÈÃÎÒÃÇ¿ÉÒÔÔËÓÃÒ»¸ö·¶Î§ (range) ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ¡£BETWEENÕâ¸ö×Ó¾äµÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from " ±í¸ñÃû"
WHERE "À¸Î»Ãû" BETWEEN 'ÖµÒ»' AND 'Öµ¶þ'
Õ⽫ѡ³öÀ¸Î»Öµ°üº¬ÔÚÖµÒ»¼°Öµ¶þÖ®¼äµÄÿһ±Ê× ......
½ÓÏÂÀ´£¬ÎÒÃÇÌÖÂÛ alias (±ðÃû) ÔÚ SQL ÉϵÄÓô¦¡£×î³£Óõ½µÄ±ðÃûÓÐÁ½ÖÖ£º À¸Î»±ðÃû¼°±í¸ñ±ðÃû¡£
¼òµ¥µØÀ´Ëµ£¬À¸Î»±ðÃûµÄÄ¿µÄÊÇΪÁËÈà SQL ²úÉúµÄ½á¹ûÒ׶Á¡£ÔÚ֮ǰµÄÀý×ÓÖУ¬ ÿµ±ÎÒÃÇÓÐÓªÒµ¶î×ܺÏʱ£¬À¸Î»Ãû¶¼ÊÇ SUM(sales)¡£ ËäÈ»ÔÚÕâ¸öÇé¿öÏÂûÓÐʲôÎÊÌ⣬¿ÉÊÇÈç¹ûÕâ¸öÀ¸Î»²»ÊÇÒ»¸ö¼òµ¥µÄ×ܺϣ¬¶øÊÇÒ»¸ö¸´ÔӵļÆË㣬 ......
ÎÒÃÇ¿ÉÒÔÔÚÒ»¸ö SQL Óï¾äÖзÅÈëÁíÒ»¸ö SQL Óï¾ä¡£µ±ÎÒÃÇÔÚ WHERE ×Ó¾ä»ò WHERE ×Ó¾ä»ò HAVING ×Ó¾äÖвåÈëÁíÒ»¸ö SQL Óï¾äʱ£¬ÎÒÃǾÍÓÐÒ»¸ö subquery µÄ¼Ü¹¹¡£ Subquery µÄ×÷ÓÃÊÇʲôÄØ£¿µÚÒ»£¬Ëü¿ÉÒÔ±»ÓÃÀ´Á¬½Ó±í¸ñ¡£ÁíÍ⣬ÓеÄʱºò subquery ÊÇΨһÄܹ»Á¬½ÓÁ½¸ö±í¸ñµÄ·½Ê½¡£
Subquery µÄÓï·¨ÈçÏ£º
SELECT "À¸Î»1"
f ......
UNION ALL Õâ¸öÖ¸ÁîµÄÄ¿µÄÒ²ÊÇÒª½«Á½¸ö SQL Óï¾äµÄ½á¹ûºÏ²¢ÔÚÒ»Æð¡£ UNION ALL ºÍ UNION ²»Í¬Ö®´¦ÔÚÓÚ UNION ALL »á½«Ã¿Ò»±Ê·ûºÏÌõ¼þµÄ×ÊÁ϶¼ÁгöÀ´£¬ÎÞÂÛ×ÊÁÏÖµÓÐÎÞÖظ´¡£
UNION ALL µÄÓï·¨ÈçÏ£º
[SQL Óï¾ä 1]
UNION ALL
[SQL Óï¾ä 2]
ÎÒÃÇÓúÍÉÏһҳͬÑùµÄÀý×ÓÀ´ÏÔʾ³ö UNION ALL ºÍ UNION µÄ²»Í¬¡£Í¬Ñù¼ÙÉèÎÒÃÇ ......