µÚ2 ÕÂ SQL ÎÊÌâ
2.1 ±¾ÕÂÄ¿µÄ
ÔÚ±¾ÕÂÖоÍÔÚ HSQLDB Ö÷Ò³ÂÛ̳»òÓʼþÁбíÖжà´ÎÌá³öµÄÎÊÌâ½øÐнâ´ð£¬Èç¹ûÄã´òËãÔÚÓ¦
ÓóÌÐòÖÐʹÓÃHSQLDB µÄ»°£¬ÄãÓ¦¸ÃÔĶÁһϱ¾Õ¡£
2.2 ¶ÔSQL ±ê×¼µÄÖ§³Ö
1.8.0 °æ±¾µÄHSQLDB Ö§³ÖSQL92¡¢99 ºÍ2003 ±ê×¼¹æ¶¨µÄSQL ·½ÑÔ¡£ÕâÒâζ×ÅHSQLDB
ÖÐÖ§³ÖµÄ±ê×¼ÌØÐÔ£¨ÀýÈç×óÍâÁ¬½Ó£©µÄÓï·¨ÊÇÓɱê×¼Îı¾¹æ¶¨µÄ¡£Ðí¶àSQL92¡¢99 ÉõÖÁ¸ü
¸ß¼¶µÄÌØÕ÷ÔÚHSQLDB Öеõ½ÁËÖ§³Ö£¬²¢ÇÒ¶ÔSQL2003 ±ê×¼µÄ´ó¶àÊýÒÔ¼°Ò»Ð©¿ÉÑ¡µÄÌØÐÔ
½øÐÐÖ§³Ö¡£È»¶ø£¬¶ÔÓÚijЩ±ê×¼µÄÌØÐÔûÓÐÖ§³Ö£¬ËùÒÔHSQLDB ¾ÍûÓÐ×ö³öÖ§³Ö¸÷¸ö¼¶±ð
ËùÓеıê×¼ÌØÐÔµÄÉùÃ÷¡£
“SQL Óï·¨”Ò»ÕÂÁгöÁËHSQLDB ËùÖ§³ÖµÄËùÓеĹؼü×ÖºÍÓï·¨¡£µ±ÊéдÓйØHSQLDB »òÕß
ת»»ÏÖÓеÄÓйØHSQLDB µÄSQL DDL£¨Êý¾Ý¶¨ÒåÓïÑÔ£©ºÍDML£¨Êý¾Ý²Ù×÷ÓïÑÔ£©Óï¾äµÄ
ʱºò£¬ÄãÓ¦¸Ã²éÔÄÒ»ÏÂHSQLDB ËùÖ§³ÖµÄÓï·¨£¬²¢¶ÔSQL Óï¾ä×÷³öÏàÓ¦µÄÐ޸ġ£
SQL ±ê×¼Öб£ÁôµÄ¹Ø¼ü×ÖÊDz»ÄÜ×÷Ϊ±íÃ÷»ò×Ö¶ÎÃûʹÓõġ£ÀýÈ磬“POSITION”±»×÷ΪÓëJava
ÖеÄString.indexOf()×÷ÓÃÀàËƵĺ¯Êý¼ÓÒÔ±£Áô¡£HSQLDB Ä¿Ç°²¢²»ÏÞÖÆʹÓÃËü²»Ö§³ÖÆäÓÃ
·¨µÄ¹Ø¼ü×Ö»òÓû§Äܹ»Çø·ÖÇå³þµÄ¹Ø¼ü×Ö¡£ÀýÈ磬“BEGIN”ÊÇHSQLDB ĿǰûÓÐÖ§³ÖµÄ¹Ø
¼ü×Ö£¬ËùÒÔÄãÒ²¿ÉʹÓÃËü×÷Ϊ±í»òÕßÁеÄÃû³Æ¡£²»¹ýÄãÓ¦¸Ã±ÜÃâʹÓÃÕâЩ±£Áô×Ö£¬ÒòΪÔÚ
HSQLDB ÒÔºóµÄ°æ±¾ÖÐÓпÉÄÜÖ§³ÖÕâЩ±£Áô×Ö£¬·ñÔò½«¾Ü¾øº¬ÓÐÕâЩ±£Áô×Ö±í¶¨Òå»ò²éѯ
Óï¾ä¡£È«²¿SQL ±£Áô×ÖÁбíÇë²Î¿´org.hsqldb.Token Àà¡£
HSQLDB Ò²Ö§³ÖһЩSQL ±ê×¼Ö®ÍâµÄ¹Ø¼ü×ֺͱí´ïʽ×÷ΪÐÔÄܵÄÔöÇ¿¡£ÏñSELECT TOP 5
from .., SELECT LIMIT 0 10 from ... »òÕß DROP TABLE mytable IF EXISTS ÕâÑùµÄ±í´ï
ʽ¶¼ÊÇHSQLDB ÔöÇ¿ÐÔÄÜËùÖ§³Ö¡£
ËùÓб»Ë«ÒýºÅ±ê×¢µÄ¹Ø¼ü×Ö¿ÉÒÔ±»ÓÃ×öÊý¾Ý¿â¶ÔÏó¡£
2.3 Ô¼ÊøºÍË÷Òý
2.3.1 Ö÷¼üÔ¼Êø
ÔÚ 1.7.0 °æ±¾Ö®Ç°£¬Ò»¸öCONSTRAINT <name> PRIMARY KEY£¨ÃûΪname µÄÖ÷¼üÔ¼Êø£©
±»ÔÚÄÚ²¿·Òë³ÉÒ»¸öΨһµÄË÷Òý£¬ÁíÍ⣬һ¸öÒþ²ØÁб»Ìí¼Óµ½¾ßÓжîÍâΨһË÷ÒýµÄ±íÉÏ¡£´Ó
1.7.0 ¿ªÊ¼£¬µ¥Ò»ÁÐÖ÷¼üºÍ¶àÁÐÖ÷¼ü(single-column and multi-column PRIMARY KEY)Ô¼Êø¶¼µÃ
µ½Ö§³Ö¡£ËüÃÇÓÉÖ÷¼üÁÐÖ¸¶¨µÄΨһË÷ÒýÖ§³Ö£¬¶øûÓжîÍâµÄÒþ²ØÁÐÀ´Î¬»¤ËüÃǵÄË÷Òý¡£
2.3.2 ΨһÐÔÔ¼Êø
¸ù¾Ý SQL ±ê×¼£¬Ò»¸öµ¥Ò»ÁÐÉϵÄΨһÐÔÔ¼Êø±íʾ²»ÔÊÐí´æÔÚÁ½¸öÏàͬµÄÖµ£¨¿ÕÖµ³öÍ⣩£¬Ò²
¾ÍÊÇ˵ÕâÑùµÄÁÐÖпÉÒÔÒ»¸ö»ò¸ü¶àΪ¿ÕÖµ£¨NULL£©µÄÐжø²»Î¥·´Î¨Ò»ÐÔÔ¼Êø¡£
¶à¸öÁÐ(c1, c2, c
Ïà¹ØÎĵµ£º
ÔÚSQL Server ÖвåÈëÒ»ÌõÊý¾ÝʹÓÃInsertÓï¾ä£¬µ«ÊÇÈç¹ûÏëÒªÅúÁ¿²åÈëÒ»¶ÑÊý¾ÝµÄ»°£¬Ñ»·Ê¹ÓÃInsert²»½öЧÂʵͣ¬¶øÇһᵼÖÂSQLһϵͳÐÔÄÜÎÊÌâ¡£ÏÂÃæ½éÉÜSQL ServerÖ§³ÖµÄÁ½ÖÖÅúÁ¿Êý¾Ý²åÈë·½·¨£ºBulkºÍ±íÖµ²ÎÊý(Table-Valued Parameters)¡£
ÔËÐÐÏÂÃæµÄ½Å±¾£¬½¨Á¢²âÊÔÊý¾Ý¿âºÍ±íÖµ²ÎÊý¡£
´úÂëÈçÏÂ:
--Create D ......
ͨÓñí±í´ïʽ Common Table Expressions
ͨÓñí±í´ïʽ£¨CTE£©ÊÇÒ»¸ö¿ÉÒÔÓɶ¨ÒåÓï¾äÒýÓõÄÁÙʱ±íÃüÃûµÄ½á¹û¼¯¡£ÔÚËûÃǵļòµ¥ÐÎʽÖУ¬Äú¿ÉÒÔ½«CTEÊÓΪÀàËÆÓÚÊÓͼºÍÅÉÉú±í»ìºÏ¹¦ÄܵĸĽø°æ±¾¡£ÔÚ²éѯµÄfrom×Ó¾äÖÐÒýÓÃCTEµÄ·½Ê½ÀàËÆÓÚÒýÓÃÅÉÉú±íºÍÊÓͼµÄ·½Ê½¡£Ö»Ð붨ÒåCTEÒ»´Î£¬¼´¿ÉÔÚ²éѯÖжà´ÎÒýÓÃËü¡£ÔÚCTEµÄ¶¨ÒåÖУ¬¿ÉÒÔÒ ......
1¡£select * from a where a.rowid=(select min(b.rowid) from b where a.id=b.id);
create test1(
nflowid number primary key,
ndocid number,
drecvdate date);
insert into test1 values (1, 12301, sysdate) ;
insert into test1 values (2, 12301, sysdate);
select * from test1 order by drecvdate:
......
Ê×Òª²½Öè(Ò»°ã¿ÉÊ¡ÂÔ):
¿ªÆômysql·þÎñ(ĬÈÏÊÇ¿ª»úʱ¾Í×Ô¶¯ÔËÐÐÁË):
¿ØÖÆÃæ°å
-->¹ÜÀí¹¤¾ß-->·þÎñ,ÔÚÀïÃæÕÒµ½Ãû³ÆΪmysqlµÄÏî,Ë«»÷¾Í¿É¿´ËüµÄ·þÎñ״̬ÊÇÆô¶¯»¹ÊÇÍ£Ö¹,°ÑËüÉèΪÆô¶¯
Á¬½Ómysql:
ÔÚÃüÁîÐÐÏÂÊäÈë mysql -h
localhost -u root -p»Ø³µ,È»ºóÊäÈëÃÜÂë¼´¿É;»òÖ±½ÓÔËÐÐmysql×Ô´øµÄÁ¬½Ó¹¤¾ß,È»ºóÊäÈëÃÜÂë¼ ......