SQL一些基礎(chǔ)的命令
Insert into Persion1(Name,Age) Values(‘zhanghui’,21);
用newid自動生成序號。
Insert into Persion2(Id,Name,Age) Values(newid(),’hui’,22);
1: UPDATE T_Person1 set Age=30;
2: Update Person set Name=’xiaohui’
Where Age>=20;
** 在數(shù)據(jù)庫中等于號為一個等號。
** “<>”:表示不等于;
** 或者:為or;
刪除表全部
Delete from Person1
刪除表的數(shù)據(jù),表還在。
Delete from Person1 where Age>21
Select * from <*:表示表名> 后面也可以加where 來區(qū)劃大小
其啟別名
Select FName as 姓名,FAge as 年齡, FSalary as 月薪 from Student
Select count(*) from Student; <總和>
Select max(*) from Student;
Select sum(*) from Student; <和>
Select * from Student order by Age ASC/DESC <按年齡排序>
<ASC:從小到大;DESC:從大到小>
*單字符通配符以‘_’,它匹配單個出現(xiàn)的字符。
以任意字符開頭,剩余部分為‘erry’:
Select * from Student where FName like ‘_erry’
*多字符匹配以‘%’,它匹配任意此數(shù)出現(xiàn)的字符。“K%”匹配以 K 開頭的字符串。
Select * from Student where FNAme like ‘%n%’
** 在數(shù)據(jù)庫中的NULL為‘不知道’和C#中的解釋不一樣。不是沒有值。

按照年齡進行分組統(tǒng)計各個年齡斷的人數(shù):
select FAge ,Count(*) from Student
group by FAge
** group by 子句必須放在where語句之后。
** 沒有出現(xiàn)group by子句中的列是不能放到select語句后的列名列表中的(聚合函數(shù)除外)


在where中不能使用聚合函數(shù),必須使用Having,Having必須位于Group by 之后。
eg:
select Name from ,count(*) as 人數(shù) from Student
group by Name
Having count(*)>1 //Having 不能代替where,Having是對組進行過濾的。

eg: < 只取前3行>
select top 3 * from Student
order by Name DESC

eg: <只取去除5行的前3個>
select top 3 * from Student
where Name not in(select top 2* from Student order by Age DESC)
order by Age DESC
<.為什么出現(xiàn)錯誤???>
eg:
select Gender from Student

select Gender, chinese from Student

eg: <加入distinct之后,去除重復(fù)>
select distinct Gender,chinese from Student
<distinct是對整個結(jié)果集進行數(shù)據(jù)重復(fù)處理的而不是針對每一列。>
select distinct Gender from Student

數(shù)據(jù)庫的命令在不同的數(shù)據(jù)庫中是相同的,但只有掌握了最基礎(chǔ)的sql數(shù)據(jù)庫才能應(yīng)用于oclace等數(shù)據(jù)庫。雖然現(xiàn)在只是學(xué)習(xí)數(shù)據(jù)庫,但是我覺的還是挺有意思的,自己挺喜歡數(shù)據(jù)庫的。加油。
新聞熱點
疑難解答