SQL INSERT INTOµÄÓ÷¨
µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃÇѧµ½Á˽«ÈçºÎ°Ñ×ÊÁÏÓɱí¸ñÖÐÈ¡³ö¡£µ«ÊÇÕâЩ×ÊÁÏÊÇÈç¹û½øÈëÕâЩ±í¸ñµÄÄØ£¿ Õâ¾ÍÊÇÕâÒ»Ò³ (INSERT INTO) ºÍÏÂÒ»Ò³ (UPDATE) ÒªÌÖÂ۵ġ£
»ù±¾ÉÏ£¬ÎÒÃÇÓÐÁ½ÖÖ×÷·¨¿ÉÒÔ½«×ÊÁÏÊäÈë±í¸ñÖÐÄÚ¡£Ò»ÖÖÊÇÒ»´ÎÊäÈëÒ»±Ê£¬ÁíÒ»ÖÖÊÇÒ»´ÎÊäÈëºÃ¼¸±Ê¡£ ÎÒÃÇÏÈÀ´¿´Ò»´ÎÊäÈëÒ»±ÊµÄ·½Ê½¡£
ÒÀÕÕ¹ßÀý£¬ÎÒÃÇÏȽéÉÜÓï·¨¡£Ò»´ÎÊäÈëÒ»±Ê×ÊÁϵÄÓï·¨ÈçÏ£º
INSERT INTO "±í¸ñÃû" ("À¸Î»1", "À¸Î»2", ...)
VALUES ("Öµ1", "Öµ2", ...)
¼ÙÉèÎÒÃÇÓÐÒ»¸ö¼Ü¹¹Èçϵıí¸ñ£º
Store_Information ±í¸ñ
Column Name
Data Type
store_name
char(50)
Sales
float
Date
datetime
¶øÎÒÃÇÒª¼ÓÒÔϵÄÕâÒ»±Ê×ÊÁϽøÈ¥Õâ¸ö±í¸ñ£ºÔÚ January 10, 1999£¬Los Angeles µêÓÐ $900 µÄÓªÒµ¶î¡£ÎÒÃǾʹòÈëÒÔÏ嵀 SQL Óï¾ä£º
INSERT INTO Store_Information (store_name, Sales, Date)
VALUES ('Los Angeles', 900, 'Jan-10-1999')
µÚ¶þÖÖ INSERT INTO Äܹ»ÈÃÎÒÃÇÒ»´ÎÊäÈë¶à±ÊµÄ×ÊÁÏ¡£¸úÉÏÃæ¸ÕµÄÀý×Ó²»Í¬µÄÊÇ£¬ÏÖÔÚÎÒÃÇÒªÓà SELECT Ö¸ÁîÀ´Ö¸Ã÷ÒªÊäÈë±í¸ñµÄ×ÊÁÏ¡£Èç¹ûÄúÏë˵£¬ÕâÊDz»ÊÇ˵×ÊÁÏÊÇ´ÓÁíÒ»¸ö±í¸ñÀ´µÄ£¬ÄÇÄú¾ÍÏë¶ÔÁË¡£Ò»´ÎÊäÈë¶à±ÊµÄ×ÊÁϵÄÓï·¨ÊÇ£º
INSERT INTO "±í¸ñ1" ("À¸Î»1", "À¸Î»2", ...)
SELECT "À¸Î»3", "À¸Î»4", ...
from "±í¸ñ2"
ÒÔÉϵÄÓï·¨ÊÇ×î»ù±¾µÄ¡£ÕâÕû¾ä SQL Ò²¿ÉÒÔº¬ÓÐ WHERE¡¢ GROUP BY¡¢ ¼° HAVING µÈ×Ӿ䣬ÒÔ¼°±í¸ñÁ¬½Ó¼°±ðÃûµÈµÈ¡£
¾ÙÀýÀ´Ëµ£¬ÈôÎÒÃÇÏëÒª½« 1998 ÄêµÄÓªÒµ¶î×ÊÁÏ·ÅÈë Store_Information ±í¸ñ£¬¶øÎÒÃÇÖªµÀ×ÊÁϵÄÀ´Ô´ÊÇ¿ÉÒÔÓÉ Sales_Information ±í¸ñÈ¡µÃµÄ»°£¬ÄÇÎÒÃǾͿÉÒÔ´òÈëÒÔÏ嵀 SQL£º
INSERT INTO Store_Information (store_name, Sales, Date)
SELECT store_name, Sales, Date
from Sales_Information
WHERE Year(Date) = 1998
ÔÚÕâÀÎÒÓÃÁË SQL Server Öеĺ¯ÊýÀ´ÓÉÈÕÆÚÖÐÕÒ³öÄê¡£²»Í¬µÄÊý¾Ý¿â»áÓв»Í¬µÄÓï·¨¡£ ¾Ù¸öÀýÀ´Ëµ£¬ÔÚ Oracle ÉÏ£¬Äú½«»áʹÓà WHERE to_char(date,'yyyy')=1998¡£
Ïà¹ØÎĵµ£º
SQL ÖÐµÄ substring º¯ÊýÊÇÓÃÀ´×¥³öÒ»¸öÀ¸Î»×ÊÁÏÖеÄÆäÖÐÒ»²¿·Ö¡£Õâ¸öº¯ÊýµÄÃû³ÆÔÚ²»Í¬µÄ×ÊÁÏ¿âÖв»ÍêÈ«Ò»Ñù£º
MySQL: SUBSTR(), SUBSTRING()
Oracle: SUBSTR()
SQL Server: SUBSTRING()
×î³£Óõ½µÄ·½Ê½ÈçÏ (ÔÚÕâÀïÎÒÃÇÓÃSUBSTR()ΪÀý)£º
SUBSTR(str,pos): ÓÉ<str>ÖУ¬Ñ¡³öËùÓдӵÚ<pos>λÖÿªÊ¼µÄ× ......
SQL ÖÐµÄ TRIM º¯ÊýÊÇÓÃÀ´ÒƳýµôÒ»¸ö×Ö´®ÖеÄ×ÖÍ·»ò×Öβ¡£×î³£¼ûµÄÓÃ;ÊÇÒƳý×ÖÊ×»ò×ÖβµÄ¿Õ°×¡£Õâ¸öº¯ÊýÔÚ²»Í¬µÄ×ÊÁÏ¿âÖÐÓв»Í¬µÄÃû³Æ£º
MySQL: TRIM(), RTRIM(), LTRIM()
Oracle: RTRIM(), LTRIM()
SQL Server: RTRIM(), LTRIM()
¸÷ÖÖ trim º¯ÊýµÄÓï·¨ÈçÏ£º
TRIM([[λÖÃ] [ÒªÒƳýµÄ×Ö´®] from ] ×Ö´®): [λÖÃ] ......
ÓÐʱºòÎÒÃÇ»á¾ö¶¨ÎÒÃÇÐèÒª´ÓÊý¾Ý¿âÖÐÇå³ýÒ»¸ö±í¸ñ¡£ÊÂʵÉÏ£¬Èç¹ûÎÒÃDz»ÄÜÕâÑù×öµÄ»°£¬Äǽ«»áÊÇÒ»¸öºÜ´óµÄÎÊÌ⣬ÒòΪÊý¾Ý¿â¹ÜÀíʦ (Database Administrator -- DBA) ÊƱØÎÞ·¨¶ÔÊý¾Ý¿â×öÓÐЧÂʵĹÜÀí¡£»¹ºÃ£¬SQL ÓÐÌṩһ¸ö DROP TABLEµÄÓï·¨À´ÈÃÎÒÃÇÇå³ý±í¸ñ¡£ DROP TABLE µÄÓï·¨ÊÇ£º
DROP TABLE "±í¸ñÃû"
ÎÒÃÇÈç¹ûÒªÇå³ ......
ÓÐʱºòÎÒÃÇ»áÐèÒªÇå³ýÒ»¸ö±í¸ñÖеÄËùÓÐ×ÊÁÏ¡£Òª´ïµ½Õ߸öÄ¿µÄ£¬Ò»ÖÖ·½Ê½ÊÇÎÒÃÇÔÚ SQL DROP ÄÇÒ»Ò³ ¿´µ½µÄ DROP TABLE Ö¸Áî¡£²»¹ýÕâÑùÕû¸ö±í¸ñ¾ÍÏûʧ£¬¶øÎÞ·¨ÔÙ±»ÓÃÁË¡£ÁíÒ»ÖÖ·½Ê½¾ÍÊÇÔËÓà TRUNCATE TABLE µÄÖ¸Áî¡£ÔÚÕâ¸öÖ¸Áî֮ϣ¬±í¸ñÖеÄ×ÊÁÏ»áÍêÈ«Ïûʧ£¬¿ÉÊDZí¸ñ±¾Éí»á¼ÌÐø´æÔÚ¡£ TRUNCATE TABLE µÄÓ﷨ΪÏ£º
TRUNCATE ......