sql²»³£¼ûÓï¾ä»ã×Ü
1¡¢join
A±íµÄÖ÷¼üÊÇ×÷ΪB±íµÄÍâ¼ü¡£ÔÚ²éѯµÄʱºò£¬¿ÉÒÔͨ¹ý²»Í¬µÄjoin½«AºÍB±íÁ´½ÓÆðÀ´£¬´Ó¶øµÃµ½²»Í¬µÄ²éѯ½á¹û¡£
* JOIN: Èç¹û±íÖÐÓÐÖÁÉÙÒ»¸öÆ¥Å䣬Ôò·µ»ØÐÐ
* INNER JOIN: Èç¹ûÁ½¸ö±íÖÐÓÐÆ¥ÅäµÄ£¬Ôò·µ»ØÐÐ
* LEFT JOIN: ¼´Ê¹ÓÒ±íÖÐûÓÐÆ¥Å䣬Ҳ´Ó×ó±í·µ»ØËùÓеÄÐÐ
* RIGHT JOIN: ¼´Ê¹×ó±íÖÐûÓÐÆ¥Å䣬Ҳ´ÓÓÒ±í·µ»ØËùÓеÄÐÐ
* FULL JOIN: Ö»ÒªÆäÖÐÒ»¸ö±íÖдæÔÚÆ¥Å䣬¾Í·µ»ØÐÐ
²»²ÉÓÃjoin
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
from Persons, Orders
WHERE Persons.Id_P = Orders.Id_P
²ÉÓÃjoin
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
from Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName
2¡¢top
TOP ×Ó¾äÓÃÓڹ涨Ҫ·µ»ØµÄ¼Ç¼µÄÊýÄ¿¡£
SELECT * from Persons LIMIT 5;
SELECT TOP 5 £ª from Persons;
SELECT TOP 50 PERCENT * from Persons;
3¡¢UNION
UNION ²Ù×÷·ûÓÃÓںϲ¢Á½¸ö»ò¶à¸ö SELECT Óï¾äµÄ½á¹û¼¯¡£
Çë×¢Ò⣬UNION ÄÚ²¿µÄ SELECT Óï¾ä±ØÐëÓµÓÐÏàͬÊýÁ¿µÄÁС£ÁÐÒ²±ØÐëÓµÓÐÏàËƵÄÊý¾ÝÀàÐÍ¡£Í¬Ê±£¬Ã¿Ìõ SELECT Óï¾äÖеÄÁеÄ˳Ðò±ØÐëÏàͬ¡£
SELECT E_Name from Employees_China
UNION
SELECT E_Name from Employees_USA£»
4¡¢SELECT INTO
SELECT INTO Óï¾ä´ÓÒ»¸ö±íÖÐÑ¡È¡Êý¾Ý£¬È»ºó°ÑÊý¾Ý²åÈëÁíÒ»¸ö±íÖС£
SELECT INTO Óï¾ä³£ÓÃÓÚ´´½¨±íµÄ±¸·Ý¸´¼þ»òÕßÓÃÓڶԼǼ½øÐд浵¡£
SELECT¡£¡£¡£ INTO¡£¡£¡£from¡£¡£¡£WHERE¡£¡£¡£
5¡¢ConstraintsÁù´óÔ¼Êø
Ô¼ÊøÓÃÓÚÏÞÖƼÓÈë±íµÄÊý¾ÝµÄÀàÐÍ¡£
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
UNIQUE (Id_P)
PRIMARY KEY (Id_P)
)
UNIQUE Ô¼ÊøΨһ±êʶÊý¾Ý¿â±íÖеÄÿÌõ¼Ç¼¡£
Çë×¢Ò⣬ÿ¸ö±í¿ÉÒÔÓжà¸ö UNIQUE Ô¼Êø£¬µ«ÊÇÿ¸ö±íÖ»ÄÜÓÐÒ»¸ö PRIMARY KEY Ô¼Êø¡£
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
PRIMARY KEY (O_Id),
FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)
)
CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Ïà¹ØÎĵµ£º
SQL Server 2005 ºÍ 2008 Óм¸¸ö¹ØÓڸ߿ÉÓÃÐÔµÄÑ¡ÏÈçÈÕÖ¾´«Êä¡¢¸±±¾ºÍÊý¾Ý¿â¾µÏñ¡£ËùÓÐÕâЩ¼¼Êõ¶¼Äܹ»×÷Ϊά»¤Ò»¸ö±¸Ó÷þÎñÆ÷µÄÊֶΣ¬Í¬Ê±Õâ¸öÊý¾Ý¿â¿ÉÒÔÔÚÄãÔÏȵÄÖ÷Êý¾Ý¿â³öÎÊÌâʱÉÏÏß²¢×÷ΪеÄÖ÷·þÎñÆ÷¡£È»¶ø£¬Äã±ØÐë¼ÇסµÄÊǽ«±¸Ó÷þÎñÆ÷Ìæ»»ÉÏÏßÖ»ÊÇÍê³ÉÁËÒ»°ëµÄ¹ÊÕÏÐÞ¸´¹¤×÷¡£
Òª±£Ö¤ÄãµÄÓ¦ÓÃÕý³£¹¤×÷£¬ÔÚÊý¾Ý¿â ......
Ò»¡¢ ÏȽéÉÜÒ»ÏÂoracleµÄSGA£ºÊý¾Ý¿âµÄϵͳȫ¾ÖÇø£¬SGAÖ÷ÒªÓÉÈý²¿·Ö¹¹³É£º¹²Ïí³Ø¡¢Êý¾Ý»º³åÇø¡¢ÈÕÖ¾»º³åÇø¡£ 1¡¢¹²Ïí³ØÓÖÓÉÁ½²¿·Ö¹¹³É£º¹²ÏíSQLÇøºÍÊý¾Ý×ֵ仺³åÇø¡£¹²ÏíSQLÇøרÃÅ´æ·ÅÓû§SQLÃüÁoracleʹÓÃ×î½ü×îÉÙʹÓõÈÓÅÏȼ¶Ëã·¨À´¸üи²¸Ç£»Êý¾Ý×ֵ仺³åÇø£¨library cache£©´æ·ÅÊý¾Ý¿âÔËÐеĶ¯Ì¬ÐÅÏ¢¡£Êý¾Ý¿âÔËÐÐÒ» ......
×òÌìÔÚ½«Ò»¸ö SQL Server 2000 Êý¾Ý¿â¸½¼Óµ½ SQL Server 2008 ʱ³öÏÖÈçϵĴíÎó£º
¸½¼ÓÊý¾Ý¿â ¶ÔÓÚ ·þÎñÆ÷“servername”ʧ°Ü¡£ (Microsoft.SqlServer.Smo)
ÎÞ·¨Éý¼¶Êý¾Ý¿â ‘dbname’£¬ÒòΪËüÊÇÖ»¶ÁµÄ£¬»òÕß¾ßÓÐÖ»¶ÁÎļþ¡£Ç뽫Êý¾Ý¿â»òÎļþÉèΪ¿Éд£¬È»ºóÖØÐÂÔËÐлָ´²Ù×÷¡£ (Microsoft SQL S ......
ÊÓͼÊÇ¿ÉÊÓ»¯µÄ±í¡£
±¾Õ½²½âÈçºÎ´´½¨¡¢¸üкÍɾ³ýÊÓͼ¡£
SQL CREATE VIEW Óï¾ä
ʲôÊÇÊÓͼ£¿
ÔÚ SQL ÖУ¬ÊÓͼÊÇ»ùÓÚ SQL Óï¾äµÄ½á¹û¼¯µÄ¿ÉÊÓ»¯µÄ±í¡£
ÊÓͼ°üº¬ÐкÍÁУ¬¾ÍÏñÒ»¸öÕæʵµÄ±í¡£ÊÓͼÖеÄ×ֶξÍÊÇÀ´×ÔÒ»¸ö»ò¶à¸öÊý¾Ý¿âÖеÄÕæʵµÄ±íÖеÄ×ֶΡ£ÎÒÃÇ¿ÉÒÔÏòÊÓͼÌí¼Ó SQL º¯Êý¡¢WHERE ÒÔ¼° JOIN Óï¾ä£¬ÎÒÃÇÒ²¿ÉÒ ......
http://www.cnblogs.com/dwjaissk/archive/2006/07/25/459476.aspx
Ôö¼Ó×Ö¶Î
alter table docdsp add dspcode char(200)
ɾ³ý×Ö¶Î
ALTER TABLE
table_NAME DROP COLUMN column_NAME
ÐÞ¸Ä×Ö¶ÎÀàÐÍ
ALTER TABLE table_name
ALTER COLUMN column_name new_data_type
¸ÄÃû
sp_rename
¸ü¸Äµ±Ç°Êý¾Ý¿âÖÐ ......