SQL³£ÓÃ×Ö·û´®º¯Êý
Ò»¡¢×Ö·ûת»»º¯Êý
1¡¢ASCII()
·µ»Ø×Ö·û±í´ïʽ×î×ó¶Ë×Ö·ûµÄASCII ÂëÖµ¡£ÔÚASCII£¨£©º¯ÊýÖУ¬´¿Êý×ÖµÄ×Ö·û´®¿É²»ÓÑ’À¨ÆðÀ´£¬µ«º¬ÆäËü×Ö·ûµÄ×Ö·û´®±ØÐëÓÑ’À¨ÆðÀ´Ê¹Ó㬷ñÔò»á³ö´í¡£
2¡¢CHAR()
½«ASCII Âëת»»Îª×Ö·û¡£Èç¹ûûÓÐÊäÈë0 ~ 255 Ö®¼äµÄASCII ÂëÖµ£¬CHAR£¨£© ·µ»ØNULL ¡£
3¡¢LOWER()ºÍUPPER()
LOWER()½«×Ö·û´®È«²¿×ªÎªÐ¡Ð´£»UPPER()½«×Ö·û´®È«²¿×ªÎª´óд¡£
4¡¢STR()
°ÑÊýÖµÐÍÊý¾Ýת»»Îª×Ö·ûÐÍÊý¾Ý¡£
STR (<float_expression>[£¬length[£¬ <decimal>]])
length Ö¸¶¨·µ»ØµÄ×Ö·û´®µÄ³¤¶È£¬decimal Ö¸¶¨·µ»ØµÄСÊýλÊý¡£Èç¹ûûÓÐÖ¸¶¨³¤¶È£¬È±Ê¡µÄlength ֵΪ10£¬ decimal ȱʡֵΪ0¡£
µ±length »òÕßdecimal Ϊ¸ºÖµÊ±£¬·µ»ØNULL£»
µ±length СÓÚСÊýµã×ó±ß£¨°üÀ¨·ûºÅ룩µÄλÊýʱ£¬·µ»Ølength ¸ö*£»
ÏÈ·þ´Ólength £¬ÔÙÈ¡decimal £»
µ±·µ»ØµÄ×Ö·û´®Î»ÊýСÓÚlength £¬×ó±ß²¹×ã¿Õ¸ñ¡£
¶þ¡¢È¥¿Õ¸ñº¯Êý
1¡¢LTRIM() °Ñ×Ö·û´®Í·²¿µÄ¿Õ¸ñÈ¥µô¡£
2¡¢RTRIM() ° ......
SQL ³£ÓÃÓï¾äÒÔ¼°º¯ÊýÖ®Ò»
SELECT --´ÓÊý¾Ý¿â±íÖмìË÷Êý¾ÝÐкÍÁÐ
¡¡¡¡¡¡¡¡¡¡¡¡INSERT --ÏòÊý¾Ý¿â±íÌí¼ÓÐÂÊý¾ÝÐÐ
¡¡¡¡¡¡¡¡¡¡¡¡DELETE --´ÓÊý¾Ý¿â±íÖÐɾ³ýÊý¾ÝÐÐ
¡¡¡¡¡¡¡¡¡¡¡¡UPDATE --¸üÐÂÊý¾Ý¿â±íÖеÄÊý¾Ý
¡¡¡¡--Êý¾Ý¶¨Òå
¡¡¡¡ CREATE TABLE --´´½¨Ò»¸öÊý¾Ý¿â±í
¡¡¡¡¡¡¡¡¡¡¡¡DROP TABLE --´ÓÊý¾Ý¿âÖÐɾ³ý±í
¡¡¡¡¡¡¡¡¡¡¡¡ALTER TABLE --ÐÞ¸ÄÊý¾Ý¿â±í½á¹¹
¡¡¡¡¡¡¡¡¡¡¡¡CREATE VIEW --´´½¨Ò»¸öÊÓͼ
¡¡¡¡¡¡¡¡¡¡¡¡DROP VIEW --´ÓÊý¾Ý¿âÖÐɾ³ýÊÓͼ
¡¡¡¡¡¡¡¡¡¡¡¡CREATE INDEX --ΪÊý¾Ý¿â±í´´½¨Ò»¸öË÷Òý
¡¡¡¡¡¡¡¡¡¡¡¡DROP INDEX --´ÓÊý¾Ý¿âÖÐɾ³ýË÷Òý
¡¡¡¡¡¡¡¡¡¡¡¡CREATE PROCEDURE --´´½¨Ò»¸ö´æ´¢¹ý³Ì
¡¡¡¡¡¡¡¡¡¡¡¡DROP PROCEDURE --´ÓÊý¾Ý¿âÖÐɾ³ý´æ´¢¹ý³Ì
¡¡¡¡¡¡¡¡¡¡¡¡CREATE TRIGGER --´´½¨Ò»¸ö´¥·¢Æ÷
¡¡¡¡¡¡¡¡¡¡¡¡DROP TRIGGER --´ÓÊý¾Ý¿âÖÐɾ³ý´¥·¢Æ÷
¡¡¡¡¡¡¡¡¡¡¡¡CREATE SCHEMA --ÏòÊý¾Ý¿âÌí¼ÓÒ»¸öÐÂģʽ
¡¡¡¡¡¡¡¡¡¡¡¡DROP SCHEMA --´ÓÊý¾Ý¿âÖÐɾ³ýÒ»¸öģʽ
¡¡¡¡¡¡¡¡¡¡¡¡CREATE DOMAIN --´´½¨Ò»¸öÊý¾ÝÖµÓò
¡¡¡¡¡¡¡¡¡¡¡¡ALTER DOMAIN --¸Ä±äÓò¶¨Òå
¡¡¡¡¡¡¡¡¡¡¡¡DROP DOMAIN --´ÓÊý¾Ý¿âÖÐɾ³ýÒ»¸öÓò
¡¡¡¡--Êý¾Ý¿ØÖÆ
¡¡¡¡ GRANT --ÊÚÓèÓû§·ÃÎÊȨÏÞ ......
ÔÚsqlÓï¾äÖÐÌí¼Ó±äÁ¿¡£
declare @local_variable data_type
ÉùÃ÷ʱÐèÒªÖ¸¶¨±äÁ¿µÄÀàÐÍ£¬
¿ÉÒÔʹÓÃsetºÍselect¶Ô±äÁ¿½øÐи³Öµ£¬
ÔÚsqlÓï¾äÖоͿÉÒÔʹÓÃ@local_variableÀ´µ÷ÓñäÁ¿
ÉùÃ÷ÖпÉÒÔÌṩֵ£¬·ñÔòÉùÃ÷Ö®ºóËùÓбäÁ¿½«³õʼ»¯ÎªNULL¡£
ÀýÈ磺declare @id int
declare @id int = 3
set @id=2
select id
select @id = column_id from table where column_user = 'ABC' ......
--²âÊÔÊý¾Ý
if OBJECT_ID('tb') is not null
drop table tb
go
CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
INSERT tb SELECT '001',NULL ,'ɽ¶«Ê¡'
UNION ALL SELECT '002','001','ÑĮ̀ÊÐ'
UNION ALL SELECT '004','002','ÕÐÔ¶ÊÐ'
UNION ALL SELECT '003','001','ÇൺÊÐ'
UNION ALL SELECT '005',NULL ,'ËÄ»áÊÐ'
UNION ALL SELECT '006','005','ÇåÔ¶ÊÐ'
UNION ALL SELECT '007','006','С·ÖÊÐ'
GO
--2000µÄ·½·¨
--²éѯָ¶¨½Úµã¼°ÆäËùÓÐ×Ó½ÚµãµÄº¯Êý
CREATE FUNCTION f_Cid(@ID char(3))
RETURNS @t_Level TABLE(ID char(3),Level int)
AS
BEGIN
declare @Level int
set @level=1
insert @t_level select @id,@level
while @@rowcount>0
begin
set @level=@level+1
insert @t_Level select tb.id,@level
from tb join @t_level t on tb.pid=t.id
where t.level+1=@level
end
return
end
select tb.*
from tb join dbo.f_cid('002') b
on tb.ID=b.id
/*
ID PID Name
---- - ......
´´½¨º¯Êý
CREATE OR REPLACE FUNCTION ntfuc(inp IN NUMBER)
RETURN NUMBER
IS
ntmp NUMBER;
BEGIN
ntmp := inp;
RETURN ntmp;
END ntfuc;
/
Ö´Ðиú¯Êýʱ
DECLARE
rcn NUMBER;
BEGIN
rcn := ntfunc(1);
END;
/ ......
1¡¢¶¨Òå»ù±¾±í
SQLÓïÑÔʹÓö¯´ÊCREATE¶¨Òå»ù±¾±í£¬Æä¾ßÌåÓï·¨¸ñʽÈçÏ£º
CREATE TABLE <±íÃû>
(<ÁÐÃû><Êý¾ÝÀàÐÍ>[Áм¶ÍêÕûÐÔÔ¼ÊøÌõ¼þ]...[£¬<ÁÐÃû><Êý¾ÝÀàÐÍ>[Áм¶ÍêÕûÐÔÔ¼ÊøÌõ¼þ]][,<±í¼¶ÍêÕûÐÔÔ¼ÊøÌõ¼þ>])£»
ÀýÈ磺½¨Á¢Ò»¸öѧÉú±íStudent£¬ËüÓÉѧºÅSno£¬ÐÕÃûSname£¬ÐÔ±ðSsex£¬ÄêÁäSage£¬ËùÔÚϵSdeptÕâ5¸öÊôÐÔ×é³É¡£ÆäÖÐѧºÅ²»ÄÜΪ¿Õ£¬ÖµÊÇΨһµÄ£¬²¢ÇÒÐÕÃûȡֵҲΨһ¡£SQLÃüÁîÈçÏ£º
CREATE TABLE Student
(Sno CHAR(5) NOT NULL UNIQUE,
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage INT,
Sdept CHAR(15));
¶¨Òå±íµÄ¸÷¸öÊôÐÔʱÐèÒªÖ¸Ã÷ÆäÊý¾ÝÀàÐͼ°³¤¶È¡£
SMALLINT °ë×Ö³¤¶þ½øÖÆÕûÊý¡£
INTEGER»òINT È«×Ö³¤¶þ½øÖÆÕûÊý¡£
DECIMAL(p[,q])»òDEC(p[,q]) ѹËõÊ®½øÖÆÊý£¬¹²P룬ÆäÖÐСÊýλÓÐqλ¡£0<=q<=p<=15,q=0ʱ¿ÉÒÔÊ¡ÂÔ²»Ð´¡£
FLOAT Ë«×Ö³¤¸¡µãÊý¡£
CHARTER(n)»òCHAR(n) ³¤¶ÈΪnµÄ¶¨³¤×Ö·û´®¡£
VARCHAR(n) ×î´ó³¤¶ÈΪnµÄ±ä³¤×Ö·û´®¡£
GRAPHIC(n) ³¤¶ÈΪnµÄ¶¨³¤Í¼ÐÎ×Ö·û´®¡£
VARGRAPHIC(n) ×î´ó³¤¶ÈΪnµÄ±ä³¤Í¼ÐÎ×Ö·û´®¡£
DATE ÈÕÆÚÐÍ£¬¸ñʽΪYYYY-MM-DD¡£
TIME ʱ¼äÐÍ£¬¸ñÊ ......