±ÊÊÔSQLÓï¾ä——ѧϰ±Ê¼Ç
¶¨Ò壺
create table ±íÃû£¨ÁÐÃû1 ÀàÐÍ [not null] [,ÁÐÃû2 ÀàÐÍ] [not null]£¬···£© [ÆäËû²ÎÊý]
Ð޸ģº
alter table ±íÃû add ÁÐÃû ÀàÐÍ
alter table ±íÃû rename column ÔÁÐÃû to ÐÂÁÐÃû
alter table ±íÃû alter column ÁÐÃû ÀàÐÍ [£¨¿í¶È£© [£¬Ð¡Êýλ]]
alter table ±íÃû drop column ÁÐÃû
ɾ³ý£º
drop table ±íÃû
½¨Á¢ÁеÄË÷Òý£º
create [unique] index Ë÷ÒýÃû on »ù±¾±íÃû£¨ÁÐÃû [´ÎÐò] [£¬ÁÐÃû [´ÎÐò]] ···£© [ÆäËû²ÎÊý]
ÆäÖеĴÎÐò£¬ASC£¨ÉýÐò£¬È±Ê¡£© DESC£¨½µÐò£©
drop index
select Ä¿±êÁÐ from ±í£¨ÊÓͼ£© [where Ìõ¼þ±í´ïʽ] [group by ÁÐÃû1] [having ÄÚ²¿º¯Êý±í´ïʽ] [order by ÁÐÃû [ASC|DESC]]
Èô²»Í¬±íÖеÄÁÐͬÃû£¬ÔòдΪ“±íÃû.ÁÐÃû”
ÆäÖеÄÄ¿±êÁпÉʹÓÃÒÔϺ¯Êý£º
count£¨ÁÐÃû|*£©£¬sum£¨ÁÐÃû£©£¬avg£¨ÁÐÃû£©£¬max£¨ÁÐÃû£©£¬min£¨ÁÐÃû£©
¼Ç¼Ψһ£ºdistinct ÁÐÃû1 [£¬ÁÐÃû2···]
“*”±íʾÈÎÒâ×Ö·û´® “£¿”±íʾÈÎÒâ×Ö·û
¼¸¸öÀý×Ó£º
´ÓѧÉú±íÖвéѯ°à¼¶£ºselect distinct °à¼¶ from ѧÉú
ÏÈ°´°à¼¶£¬ÔÙ°´Ñ§ºÅÅÅÐò£ºselect * from ѧÉú order by °à¼¶£¬Ñ§ºÅ¡¢
select * from ѧÉú where Ìõ¼þ1 and Ìõ¼þ2 and sth IS [NOT] NULL
where °à¼¶ [NOT] in £¨’0001‘£¬’0002‘£©µÈ¼ÛÓÚ where °à¼¶ =’200101‘ or °à¼¶ =’200202‘
where ³öÉúÄê·Ý between 1982 and 1990
²éѯ2001¼¶µÄѧÉú£º
select * from ѧÉú where °à¼¶ like ’2001%‘
_£¨ÏºáÏߣ©±íʾÈÎÒâµ¥¸ö×Ö·û
%±íʾÈÎÒâ×Ö·û´®
²éѯ¿Î³Ì³¬¹ýÈýÃŵÄѧÉú£º
select ѧºÅ from ³É¼¨µ¥ group by ѧºÅ having count£¨*£©>3
×Ô¶¯Á¬½Ó£º
select ѧÉú.*£¬³É¼¨.* from ѧÉú£¬³É¼¨ where ѧÉú.ѧºÅ=³É¼¨.ѧºÅ order by ¿Î³ÌºÅ£¬·ÖÊý DESC
µÈ¼ÛÓÚ select ѧÉú.*£¬¿Î³ÌºÅ£¬·ÖÊý order by ¿Î³ÌºÅ£¬·ÖÊý DESC
ǶÌײéѯ£º
select ÐÕÃû from ѧÉú where ѧºÅ in £¨select ѧºÅ from ³É¼¨ where ¿Î³ÌºÅ=’C1‘£©
ÇóÁ½±íµÄ½»¼¯»ò²î¼¯£¨×Ö¶ÎÏàͬ£©£º
select * from ³É¼¨1 where ѧºÅ [NOT] in £¨select ѧºÅ from ³É¼¨2 where ³É¼¨1.¿Î³ÌºÅ=³É¼¨2.¿Î³ÌºÅ and ³É¼¨1.·ÖÊý=³É¼¨2.·ÖÊý£©
UNIONÁ¬½Ó£º
select ÁÐÃû1 from ±í1 where Ìõ¼þ1 UNION select ÁÐÃû2 from ±í2 where Ìõ¼þ2
Ïà¹ØÎĵµ£º
תÔØ˵Ã÷£º¸øÕýÔÚ×öϵͳºÍÏë×öϵͳµÄÈË£¬SQL²©´ó¾«ÉÄãÃÇ»áÓõ½µÄ¡£——mAysWINd
Ò»¸öÌâÄ¿Éæ¼°µ½µÄ50¸öSqlÓï¾ä
Student(S#,Sname,Sage,Ssex) ѧÉú±í
Course(C#,Cname,T#) ¿Î³Ì±í
SC(S#,C#,score) ³É¼¨±í
Teacher(T#,Tname) ½Ìʦ±í
ÎÊÌ⣺
1¡¢²éѯ“001”¿Î³Ì±È“002”¿Î³Ì³É¼¨¸ßµ ......
--µÚÒ»²½
--ÔÚmaster¿âÖн¨Á¢Ò»¸ö±¸·ÝÊý¾Ý¿âµÄ´æ´¢¹ý³Ì.
USE master
GO
CREATE PROC p
@db_name sysname, --Êý¾Ý¿âÃû
@bk_path NVARCHAR(1024) --±¸·ÝÎļþµÄ·¾¶
A ......
¸Õ¸Õ°²×°µÄÊý¾Ý¿âϵͳ£¬°´ÕÕĬÈÏ°²×°µÄ»°£¬ºÜ¿ÉÄÜÔÚ½øÐÐÔ¶³ÌÁ¬½Óʱ±¨´í£¬Í¨³£ÊÇ´íÎó:"ÔÚÁ¬½Óµ½ SQL Server 2005 ʱ£¬ÔÚĬÈϵÄÉèÖÃÏ SQL Server ²»ÔÊÐí½øÐÐÔ¶³ÌÁ¬½Ó¿ÉÄܻᵼÖ´Ëʧ°Ü¡£ (provider: ÃüÃû¹ÜµÀÌṩ³ÌÐò, error: 40 - ÎÞ·¨´ò¿ªµ½ SQL Server µÄÁ¬½Ó) "ËÑMSDN£¬ÉÏÃæÓÐһƬ»úÆ÷·ÒëµÄÎÄÕ£¬ÊÇÔÚÈÃÈËÄÑÒÔÃ÷°×£¬ÏÖÔÚ ......
×î½üºÜ棬ÓиöÏîÄ¿ÂíÉÏÒªÕб꣬һ¸öÏîÄ¿µÈ׏¤£¬Èô¸ÉËöËéµÄʽøÐÐÖУ¬ÓÐÒ»¶Îʱ¼äû¸üÐÂЩÓÐÓªÑøµÄ¶«Î÷ÁË
˵¸öÌâÍâ»°ÏÈ¡£
½ñÌ쿪»ú×¼±¸°Ñ×òÌìµÄ¶«Î÷debugһϣ¬ºÜÏ°¹ßµØÓÒ¼üÏîÄ¿µÄÆô¶¯Îļþ¿ªÊ¼debug£¬»úÆ÷ͻȻÀ¶ÆÁÖØÆô¡£¿ªÊ¼ÒÔΪÓÖÊÇÄÚ´æÔÚ͵͵³¬Æµ£¬¼ì²éÁËÒ»ÏÂbios£¬·¢ÏÖûʲôÎÊÌ⣬ҲûÔõôÔÚÒ⣬ËíÖØпªÆôvs2008¼ÌÐø ......