SQL²Ù×÷È«¼¯
SQL·ÖÀࣺ
DDL—Êý¾Ý¶¨ÒåÓïÑÔ(CREATE£¬ALTER£¬DROP£¬DECLARE)
DML—Êý¾Ý²Ù×ÝÓïÑÔ(SELECT£¬DELETE£¬UPDATE£¬INSERT)
DCL—Êý¾Ý¿ØÖÆÓïÑÔ(GRANT£¬REVOKE£¬COMMIT£¬ROLLBACK)
Ê×ÏÈ,¼òÒª½éÉÜ»ù´¡Óï¾ä£º
1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
CREATE DATABASE database-name
2¡¢ËµÃ÷£ºÉ¾³ýÊý¾Ý¿â
drop database dbname
3¡¢ËµÃ÷£º±¸·Ýsql server
--- ´´½¨ ±¸·ÝÊý¾ÝµÄ device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- ¿ªÊ¼ ±¸·Ý
BACKUP DATABASE pubs TO testBack
4¡¢ËµÃ÷£º´´½¨Ð±í
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
¸ù¾ÝÒÑÓÐµÄ±í´´½¨ÐÂ±í£º
A£ºcreate table tab_new like tab_old (ʹÓÃ¾É±í´´½¨Ð±í)
B£ºcreate table tab_new as select col1,col2… from tab_old definition only
5¡¢ËµÃ÷£ºÉ¾³ýбídrop table tabname
6¡¢ËµÃ÷£ºÔö¼ÓÒ»¸öÁÐ
Alter table tabname add column col type
×¢£ºÁÐÔö¼Óºó½«²»ÄÜɾ³ý¡£DB2ÖÐÁмÓÉϺóÊý¾ÝÀàÐÍÒ²²»Äܸı䣬ΨһÄܸıäµÄÊÇÔö¼ÓvarcharÀàÐ͵ij¤¶È¡£
7¡¢ËµÃ÷£ºÌí¼ÓÖ÷¼ü£º Alter table tabname add primary key(col)
˵Ã÷£ºÉ¾³ýÖ÷¼ü£º Alter table tabname drop primary key(col)
8¡¢ËµÃ÷£º´´½¨Ë÷Òý£ºcreate [unique] index idxname on tabname(col….)
ɾ³ýË÷Òý£ºdrop index idxname
×¢£ºË÷ÒýÊDz»¿É¸ü¸ÄµÄ£¬Ïë¸ü¸Ä±ØÐëɾ³ýÖØн¨¡£
9¡¢ËµÃ÷£º´´½¨ÊÓͼ£ºcreate view viewname as select statement
ɾ³ýÊÓͼ£ºdrop view viewname
10¡¢ËµÃ÷£º¼¸¸ö¼òµ¥µÄ»ù±¾µÄsqlÓï¾ä
Ñ¡Ôñ£ºselect * from table1 where ·¶Î§
²åÈ룺insert into table1(field1,field2) values(value1,value2)
ɾ³ý£ºdelete from table1 where ·¶Î§
¸üУºupdate table1 set field1=value1 where ·¶Î§
²éÕÒ£ºselect * from table1 where field1 like ’%value1%’ ---likeµÄÓï·¨ºÜ¾«Ã²é×ÊÁÏ!
ÅÅÐò£ºselect * from table1 order by field1,field2 [desc]
×ÜÊý£ºselect count * as totalcount from table1
ÇóºÍ£ºselect sum(field1) as sumvalue from table1
ƽ¾ù£ºselect avg(field1) as avgvalue from table1
×î´ó£ºselect max(field1) as maxvalue from table1
×îС£ºselect min(field1) as minvalue from table1
11¡¢ËµÃ÷£º¼¸¸ö¸ß¼¶
Ïà¹ØÎĵµ£º
PL/SQL×÷Ϊoracle¶ÔÓÚ´«Í³SQLÓï·¨µÄÀ©Õ¹,ÔÚ¿çϵͳÊÂÎñ´¦ÀíÒÔ¼°¶ÔÊý¾Ý¿â´óÅúÁ¿Êý¾Ý´¦Àí·½Ãæ×÷ÓÃÏÔÖø;¶øµ±Ç°Ö÷Á÷µÄMVC¿ò¼Ü¶ÔDAO²ãµÄ·â×°¶¼ÊÇÒ»ÌõSQLÓï¾ä·µ»ØÒ»¸ö¼Ç¼¼¯;µ±Êý¾ÝµÄ¹Øϵ±È½Ï¸´ÔÓ»òÊý¾ÝÁ¿±È½Ï´óµÄʱºò,ÓëÊý¾Ý¿âµÄ½»»¥¹ýÓÚƵ·±½«Ôö´óÄÚÍøµÄÊý¾Ý´«ÊäÁ¿ÓëÊý¾Ý¿âµÄIO.Õë¶Ô´ËÀิÔÓµÄÊý¾Ý´¦Àí,¿ÉÒÔ²ÉÓÃPL/SQL³ÌÐò°üÀ´Êµ ......
´´½¨×éºÏ²éѯ
¿ÉÓÃUNION²Ù×÷·ûÀ´×éºÏÊýÌõSQL²éѯ¡£ÀûÓÃUNION£¬¿É¸ø³ö¶àÌõSELECTÓï¾ä£¬½«ËüÃǵĽá¹û×éºÏ³Éµ¥¸ö½á¹û¼¯¡£
16.2.1 ʹÓÃUNION
UNIONµÄʹÓúܼòµ¥¡£ËùÐè×öµÄÖ»ÊǸø³öÿÌõSELECTÓï¾ä£¬ÔÚ¸÷ÌõÓï¾äÖ®¼ä·ÅÉϹؼü×ÖUNION¡£
¾ÙÒ»¸öÀý×Ó£¬¼ÙÈçÐèÒª¼Û¸ñСÓÚµÈÓÚ5µÄËùÓÐÎïÆ·µÄÒ»¸öÁÐ±í£¬¶øÇÒ»¹Ïë°üÀ¨¹©Ó¦ÉÌ1001ºÍ1002É ......
oracle³£ÓþµäSQL²éѯ
³£ÓÃSQL²éѯ£º
1¡¢²é¿´±í¿Õ¼äµÄÃû³Æ¼°´óС
select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size
from dba_tablespaces t, dba_data_files d
where t.tablespace_name = d.tablespace_name
group by t.tablespace_name;
2¡¢²é¿´±í¿Õ¼äÎïÀíÎļþµÄÃû³Æ ......
SQLÀïµÄEXISTSÓëin¡¢not existsÓënot in
ϵͳҪÇó½øÐÐSQLÓÅ»¯£¬¶ÔЧÂʱȽϵ͵ÄSQL½øÐÐÓÅ»¯£¬Ê¹ÆäÔËÐÐЧÂʸü¸ß£¬ÆäÖÐÒªÇó¶ÔSQLÖеIJ¿·Öin/not inÐÞ¸ÄΪexists/not exists
Ð޸ķ½·¨ÈçÏ£º
inµÄSQLÓï¾ä
SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime
from tab_oa_pub WHERE ......
¶ÔÓÚÿһ¸öÊý¾Ý¿âÀ´½²£¬¶¼ÐèÒªÖÁÉÙÒ»¸öÊÂÎñÈÕÖ¾Îļþ¡£ÊÂÎñÈÕÖ¾ÎļþÊÇÕû¸öÊý¾Ý¿âµÄѪҺ£¬Èç¹ûûÓÐÊÂÎñÈÕÖ¾µÄ»°£¬ÄÇô½«ÎÞ·¨½øÐÐÈκβÙ×÷¡£
ÊÂÎñÈÕÖ¾ÓÐʲô¶«Î÷£¿
ÊÂÎñÈÕÖ¾¼Ç¼×ÅÔÚÏà¹ØÊý¾Ý¿âÉϵIJÙ×÷£¬Í¬Ê±»¹´æ´¢Êý¾Ý¿â»Ö¸´(recovery)µÄÏà¹ØÐÅÏ¢¡£
ÊÂÎñÈÕÖ¾ÓëÊý¾Ý¿â»Ö ......