Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB
ÈÈÃűêÇ©£º c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 ×îÐÂÎÄÕ : sql

SQL Serverº¯Êý´óÈ«

--¾ÛºÏº¯Êý
use pubs
go
select avg(distinct price)  --Ëãƽ¾ùÊý
from titles
where type='business'
go 
use pubs
go
select max(ytd_sales)  --×î´óÊý
from titles
go 
use pubs
go
select min(ytd_sales) --×îСÊý
from titles
go 
use pubs
go
select type,sum(price),sum(advance)  --ÇóºÍ
from titles
group by type
order by type
go 
use pubs
go 
select count(distinct city)  --Çó¸öÊý
from authors
go 
use pubs
go
select stdev(royalty) --·µ»Ø¸ø¶¨±í´ïʽÖÐËùÓÐÖµµÄͳ¼Æ±ê׼ƫ²î
from titles
go 
use pubs
go
select stdevp(royalty) --·µ»Ø±í´ïʽÖÐËùÓÐÖƵÄÌî³äͳ¼Æ±ê׼ƫ²î
from titles
go 
use pubs
go
select var(royalty) --·µ»ØËùÓÐÖµµÄͳ¼Æ·½²î
from titles
go 
use pubs
go
select varp(royalty) --·µ»ØËùÓ ......

¼òµ¥µ«ÓÐÓõÄSQL½Å±¾

ÐÐÁÐת»»
create table test(id int,name varchar(20),quarter int,profile int)
insert into test values(1,'a',1,1000)
insert into test values(1,'a',2,2000)
insert into test values(1,'a',3,4000)
insert into test values(1,'a',4,5000)
insert into test values(2,'b',1,3000)
insert into test values(2,'b',2,3500)
insert into test values(2,'b',3,4200)
insert into test values(2,'b',4,5500)
select * from test
--ÐÐתÁÐ
select id,name,
[1] as "Ò»¼¾¶È",
[2] as "¶þ¼¾¶È",
[3] as "Èý¼¾¶È",
[4] as "Ëļ¾¶È",
[5] as "5"
from
test
pivot
(
sum(profile)
for quarter in
([1],[2],[3],[4],[5])
)
as pvt
create table test2(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int)
insert into test2 values(1,'a',1000,2000,4000,5000)
insert into test2 values(2,'b',3000,3500,4200,5500)
select * from test2
--ÁÐתÐÐ
select id,name,quarter,profile
from
test2
unpivot
(
profile
for quarter in
([Q1],[Q2],[Q3],[Q4])
)
as unpvt

sqlÌæ»»×Ö·û´® substring replace
--Àý×Ó1£º
update tbPer ......

OracleÖÐSQL PLUSµÄ³£ÓÃÖ¸Áî

½øÈësql*plusÐèÒªÊäÈëÓû§Ãû¡¢¿ÚÁîºÍÖ÷»ú±êÖ¾·û
system/manager
sys/change_on_intall
scott/tiger
internal/oracle
ÒÔÉÏΪ³õʼÃÜÂë
Ö÷»ú×Ö·û´®¿Õ»òÕßÊÇ@+ÄãµÄ·þÎñÃû,ÔÚ±¾µØµÇ½²»ÐèÒª±¾µØ·þÎñÃû¡£
ÏÂÃæ¾Í½éÉÜÒ»ÏÂһЩ³£ÓõÄsql*plusÃüÁ
Ê×ÏȲ鿴µ±Ç°Ê¹ÓõÄÊý¾Ý¿âʵÀý:
select name from V$database;
Çл»Á½¸öÊý¾Ý¿âʵÀý
ÔÚsqlplusÀïconnect username/password@sid
»òhost set ORACLE_SID=sid
»òcmdÀïset ORACLE_SID=sid
»òÐÞ¸Ä×¢²á±íÀïORACLE_SIDÏî
¡ô1. Ö´ÐÐÒ»¸öSQL½Å±¾Îļþ
SQL>start file_name
SQL>@ file_name
ÎÒÃÇ¿ÉÒÔ½«¶àÌõsqlÓï¾ä±£´æÔÚÒ»¸öÎı¾ÎļþÖУ¬ÕâÑùµ±ÒªÖ´ÐÐÕâ¸öÎļþÖеÄËùÓеÄsqlÓï¾äʱ£¬ÓÃÉÏÃæµÄÈÎÒ»ÃüÁî¼´¿É£¬ÕâÀàËÆÓÚdosÖеÄÅú´¦Àí¡£
¡ô2. ¶Ôµ±Ç°µÄÊäÈë½øÐб༭
SQL>edit
¡ô3. ÖØÐÂÔËÐÐÉÏÒ»´ÎÔËÐеÄsqlÓï¾ä
SQL>/
¡ô4. ½«ÏÔʾµÄÄÚÈÝÊä³öµ½Ö¸¶¨Îļþ
SQL> SPOOL file_name
ÔÚÆÁÄ»ÉϵÄËùÓÐÄÚÈݶ¼°üº¬ÔÚ¸ÃÎļþÖУ¬°üÀ¨ÄãÊäÈëµÄsqlÓï¾ä¡£
¡ô5. ¹Ø±ÕspoolÊä³ö
SQL> SPOOL OFF
Ö»ÓйرÕspoolÊä³ö£¬²Å»áÔÚÊä³öÎļþÖп´µ½Êä³öµÄÄÚÈÝ¡£
¡ô6£®ÏÔʾһ¸ö±íµÄ½á¹¹
SQL> desc table_name
¡ô7. COLÃüÁ
Ö÷Òª¸ñ ......

OracleÖÐSQL PLUSµÄ³£ÓÃÖ¸Áî

½øÈësql*plusÐèÒªÊäÈëÓû§Ãû¡¢¿ÚÁîºÍÖ÷»ú±êÖ¾·û
system/manager
sys/change_on_intall
scott/tiger
internal/oracle
ÒÔÉÏΪ³õʼÃÜÂë
Ö÷»ú×Ö·û´®¿Õ»òÕßÊÇ@+ÄãµÄ·þÎñÃû,ÔÚ±¾µØµÇ½²»ÐèÒª±¾µØ·þÎñÃû¡£
ÏÂÃæ¾Í½éÉÜÒ»ÏÂһЩ³£ÓõÄsql*plusÃüÁ
Ê×ÏȲ鿴µ±Ç°Ê¹ÓõÄÊý¾Ý¿âʵÀý:
select name from V$database;
Çл»Á½¸öÊý¾Ý¿âʵÀý
ÔÚsqlplusÀïconnect username/password@sid
»òhost set ORACLE_SID=sid
»òcmdÀïset ORACLE_SID=sid
»òÐÞ¸Ä×¢²á±íÀïORACLE_SIDÏî
¡ô1. Ö´ÐÐÒ»¸öSQL½Å±¾Îļþ
SQL>start file_name
SQL>@ file_name
ÎÒÃÇ¿ÉÒÔ½«¶àÌõsqlÓï¾ä±£´æÔÚÒ»¸öÎı¾ÎļþÖУ¬ÕâÑùµ±ÒªÖ´ÐÐÕâ¸öÎļþÖеÄËùÓеÄsqlÓï¾äʱ£¬ÓÃÉÏÃæµÄÈÎÒ»ÃüÁî¼´¿É£¬ÕâÀàËÆÓÚdosÖеÄÅú´¦Àí¡£
¡ô2. ¶Ôµ±Ç°µÄÊäÈë½øÐб༭
SQL>edit
¡ô3. ÖØÐÂÔËÐÐÉÏÒ»´ÎÔËÐеÄsqlÓï¾ä
SQL>/
¡ô4. ½«ÏÔʾµÄÄÚÈÝÊä³öµ½Ö¸¶¨Îļþ
SQL> SPOOL file_name
ÔÚÆÁÄ»ÉϵÄËùÓÐÄÚÈݶ¼°üº¬ÔÚ¸ÃÎļþÖУ¬°üÀ¨ÄãÊäÈëµÄsqlÓï¾ä¡£
¡ô5. ¹Ø±ÕspoolÊä³ö
SQL> SPOOL OFF
Ö»ÓйرÕspoolÊä³ö£¬²Å»áÔÚÊä³öÎļþÖп´µ½Êä³öµÄÄÚÈÝ¡£
¡ô6£®ÏÔʾһ¸ö±íµÄ½á¹¹
SQL> desc table_name
¡ô7. COLÃüÁ
Ö÷Òª¸ñ ......

SQL²åÈëÀýÌâ

--´´½¨±íTongXunLu
CREATE TABLE TongXunLu
 (
 [tName] nvarchar(30),
 [tAddress] nvarchar(50),
 [tEmail] varchar(50)
 )
--´´½¨±í students
CREATE TABLE students
 (
 [sId] int IDENTITY (1, 1) primary key NOT NULL ,
 [sName] varchar (50)   NOT NULL ,
 [sAddress] varchar (50) default 'daxing'   ,
 [sEmail] varchar (50) check([sEmail] like '%@%')   ,
 [sSex] bit NOT NULL
 )
insert into students (sname,saddress,semail,ssex) values ('ÕŹûÀÏ',default,'a@a',1)
insert into [students] (sname,saddress,semail,ssex) values ('ÀîÑ°»¶',null,'b@b',1)
insert into [students] (sname,saddress,semail,ssex) values ('Áîºü³å',default,'c@c',1)
insert into [students] (sname,saddress,semail,ssex) values ('³ÌÁéËØ',null,'d@f',0)
insert into [students] (sname,saddress,semail,ssex) values ('Ц¹þ¹þ','beijing','e@e',0)
insert into [students] (sname,saddress,semail,ssex) values ('ÀֺǺÇ','neimenggu','f@f',1)
in ......

SQL round()Ïê½â


SQLÖÐround£¨£©º¯ÊýÓ÷¨
SQL round()Ïê½â
roundÓÐÁ½¸öÖØÔØ,Ò»¸öÓдøÓÐÁ½¸ö²ÎÊýµÄ,Ò»¸öÊÇ´øÓÐÈý¸ö²ÎÊýµÄ,
ÿһ¸ö²ÎÊý¶¼ÏàͬÊÇÒª´¦ÀíµÄÊý,
1.´øÓÐÁ½¸ö²ÎÊý.ÿ¶þ¸ö²ÎÊýÊÇСÊýµãµÄ×ó±ßµÚ¼¸Î»»òÓұߵڼ¸Î»,·Ö±ðÓÃÕý¸º±íʾ.×ó±ßΪ¸º,ÓÒ±ßΪ¸º.ΪËÄÉáÎåÈë.
select round(748.585929,-1) 750.000000
select round(748.585929,2) 748.590000
2.´øÈý¸ö²ÎÊý.µÚ¶þ¸ö²ÎÊýͬÉÏ.µÚÈý¸ö²ÎÊýÓÐÁ½ÖÖ¿ÉÄÜ,Ò»ÖÖÊÇÈ¡0ΪËÄÉáÎåÈë.ÁíÒ»ÖÖÊÇÕý¸ºÖµ,ÔòΪÉáÈ¥
 select round(748.585929,3,0)  748.586000
select round(748.585929,3,1)  748.585000
select round(748.585929,3,-1)  748.585000
---------------------------------------------
TEST@ORA1] SQL>select round(4321,-3) from dual;
ROUND(4321,-3)
--------------
          4000
[TEST@ORA1] SQL>select round(20,-3) from dual;
ROUND(20,-3)
------------
           0
[TEST@ORA1] SQL>select round(4500,-3) from dual;
ROUND(4500,-3)
------------ ......

SQL UnionºÍUnion AllµÄʹÓ÷½·¨

¡¡UNIONÖ¸ÁîµÄÄ¿µÄÊǽ«Á½¸öSQLÓï¾äµÄ½á¹ûºÏ²¢ÆðÀ´¡£´ÓÕâ¸ö½Ç¶ÈÀ´¿´£¬ ÎÒÃÇ»á²úÉúÕâÑùµÄ¸Ð¾õ£¬UNION¸úJOINËƺõÓÐЩÐíÀàËÆ£¬ÒòΪÕâÁ½¸öÖ¸Á¿ÉÒÔÓɶà¸ö±í¸ñÖÐߢȡ×ÊÁÏ¡£ UNIONµÄÒ»¸öÏÞÖÆÊÇÁ½¸ö SQL Óï¾äËù²úÉúµÄÀ¸Î»ÐèÒªÊÇͬÑùµÄ×ÊÁÏÖÖÀà¡£ÁíÍ⣬µ±ÎÒÃÇÓà UNIONÕâ¸öÖ¸Áîʱ£¬ÎÒÃÇÖ»»á¿´µ½²»Í¬µÄ×ÊÁÏÖµ (ÀàËÆ SELECT DISTINCT)¡£ unionÖ»Êǽ«Á½¸ö½á¹ûÁª½áÆðÀ´Ò»ÆðÏÔʾ£¬²¢²»ÊÇÁª½áÁ½¸ö±í 
¡¡¡¡UNION µÄÓï·¨ÈçÏ£º [SQL Óï¾ä 1] 
¡¡¡¡UNION 
¡¡¡¡[SQL Óï¾ä 2] ¼ÙÉèÎÒÃÇÓÐÒÔϵÄÁ½¸ö±í¸ñ£¬ 
¡¡¡¡Store_Information ±í¸ñ store_name Sales Date 
¡¡¡¡Los Angeles $1500 Jan-05-1999 
¡¡¡¡San Diego $250 Jan-07-1999 
¡¡¡¡Los Angeles $300 Jan-08-1999 
¡¡¡¡Boston $700 Jan-08-1999 
¡¡¡¡Internet Sales ±í¸ñ Date Sales 
¡¡¡¡Jan-07-1999 $250 
¡¡¡¡Jan-10-1999 $535 
¡¡¡¡Jan-11-1999 $320 
¡¡¡¡Jan-12-1999 $750 
¡¡¡¡¶øÎÒÃÇÒªÕÒ³öÀ´ËùÓÐÓÐÓªÒµ¶î (sales) µÄÈÕ×Ó¡£Òª´ïµ½Õâ¸öÄ¿µÄ£¬ÎÒÃÇÓÃÒÔÏ嵀 SQL Óï¾ä£º 
¡¡¡¡SELECT Date from Store_Information 
¡¡¡¡UNION 
¡¡¡¡ ......
×ܼǼÊý:4346; ×ÜÒ³Êý:725; ÿҳ6 Ìõ; Ê×Ò³ ÉÏÒ»Ò³ [1] [2] 3 [4] [5] [6] [7] [8] [9] [10]  ÏÂÒ»Ò³ βҳ
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ