6-14 数据库高级
生活随笔
收集整理的這篇文章主要介紹了
6-14 数据库高级
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
--================先通過設計器手動添加,然后通過代碼來添加==== --============手動增加約束========== --手動刪除一列(刪除EmpAddress列)
alter table Employees drop column EmpAddress
go--手動增加一列(增加一列EmpAddr varchar(1000))
alter table Employees add EmpAddr varchar(1000)--手動修改一下EmpEmail的數據類型(varchar(200))
alter table Employees alter column EmpAddr varchar(200)
go--為EmpId增加一個主鍵約束
alter table Employees
add constraint PK_Employees_EmpId primary key(EmpId)--非空約束,為EmpName增加一個非空約束,修改列為not null
--增加一個非空約束其實就是修改列
alter table Employees
alter column EmpName varchar(50) not null
go--為EmpName增加一個唯一約束
alter table Employees add constraint
UQ_Employees_EmpName unique(EmpName)
Go--為性別增加一個默認約束,默認為'男'
alter table Employees add constraint
DF_Employees_EmpGender default('男') for EmpGender go--為年齡增加一個檢查約束:年齡必須在0-120歲之間,含0歲與120歲。
alter table Employees add constraint
CK_Emplpoyees_EmpAge check(empage>=0 and empage<=120)
go--增加外鍵約束,表Employee中有一列EmpDeptId引用TblDepartment表中的DeptIdalter table Employees add DeptId int not null
alter table Department add constraint
PK_Department_DeptId primary key(DepId)alter table Employees add constraint
FK_Employees_Department foreign key(DeptId)
references Department(DepId) on delete cascade--先刪除原來的外鍵
alter table Employees drop constraint FK_Employees_Department--刪除某個名字的約束--一條語句刪除多個約束,約束名用 逗號 隔開
alter table Employees drop constraintFK_Employees_Department,CK_Emplpoyees_EmpAge,
UQ_Employees_EmpName--用一條語句為表增加多個約束。
alter table Employees add
constraint UQ_Employees_EmpName unique(EmpName),
constraint CK_Emplpoyees_EmpAge check(EmpAge>=0 and EmpAge<=120)
?
--===============給列起名的三種方式 select Fid as 學號, fname as 姓名, fage 年齡, 性別=fgender, 數學成績=fmath, 英語成績=fenglish from MyStudent--獲得系統時間 select getdate()--top 數字后加percent表示百分比,如果百分比最后計算出來的條數中有小數,則直接進位。 select top 30 percent * from MyStudent order by fage desc?
--===================distinct去除重復數據======= select distinct uname,uage from Test2 --==============數據庫中的自帶函數 --查詢數學成績中,最高分是多少分 select max(fMath) as 數學成績最高分 from MyStudentselect min(fMath) as 數學成績最低分 from MyStudent--求總分,求和 select sum(fmath) as 總分 from MyStudent--求班級中的總的記錄條數(總人數) select count(*) as 班級總人數 from MyStudent --計算平均分的時候對空值不處理 select avg(uscore) from Test3 --count計算的時候也不考慮空值 select count(uscore) from Test3 --求平均分 select avg(fmath) as 平均分 from MyStudent --====================sql 中的語法 --推薦使用between ... and ... --between 18 and 24 and也相當于是>=18 and <=24--在1,2,3中選擇任意一個班級都返回true --可以用in()語法替換多個or select * from TblStudent where tsclassid in (1,2,3)?
--------------------------模糊查詢================================= --通配符%表示:任意多個任意字符 select * from Mystudent where fname like '趙%'--通配符 _ :表示任意的單個字符。 select * from Mystudent--表示x與y之間只要不是'磊'或'偉'的任意單個字符都可以。 --[]通配符,表示中括號中的任意個字符,只選一個匹配。 --[^]表示除了中括號中的任意單個字符。where fname like '趙__'select * from MyStudent where fname like '%[磊偉]%'select * from MyStudent where fname like 'x[^磊偉]y'--查詢出姓名中包含百分號的 --用[]括起來,就表示轉義了。 select * from MyStudent where fname like '%[%]%'select * from MyStudent where fname like '%[_]%'--查詢出所有不姓趙的同學 select * from Mystudent where fname not like '趙%' --1.請查詢出學生表中所有數學成績為null的人的信息 --null在數據庫中表示unkonw(不知道),判斷一個值是否為null,也就不能用=或者<>來判斷--查詢所有fmath為null的值。 select * from MyStudent where fmath is null--查詢所有fmath為非null的值。 select * from MyStudent where fmath is not null--null值與任何數據運算后得到的還是null值。 --就像不知道與1相加結果還是不知道。--注意:同一列上的數據,數據類型必須一致,如果不一致就會報錯。所以要求自己定義查詢的時候,注意同一列數據類型一致。 --這里的'缺考',只存在與查詢出的結果集中,表中的數據沒有變化 數學成績=isnull(cast(fmath as varchar(50)),'缺考')?
--=================sql中的執行順序=================== select --distinct / top 之類的關鍵字(這些都是一些現實的選項) fgender as 性別, --5>選擇列 count(*) as 人數 from MyStudent --1>先從MyStudent表中拿到數據(全部數據的一個結果集) where fage>30 --2>從MyStudent的數據中篩選出所有年齡大于30歲的人的信息(新結果集,都是年齡大于30的) group by fgender --3>按照性別分組,分完組以后又得到一個新結果集(分組后的結果) having count(*)>355 --4>基于分組以后的結果集,然后在篩選,篩選出那些組中記錄大于500的組。 order by 人數 desc--6>最后把顯示出來的結果排序?
--================================================= select --tsname as 姓名, tsclassid as 班級Id, count(*) as 班級人數 from TblStudent where tsgender='男' group by TSClassId --一般分組語句group by 都要與聚合函數配合使用,如果沒有聚合函數,分組的意義不大。--當在select查詢語句中出現聚合函數時,這時不能在select查詢中再出現其他列,除非:該列也在group子句中出現或者也在聚合函數中出現。?
--========================類型轉換============================、 --select 100+'hello' --select 'hello'+100select 100+'100'select cast(100 as varchar(10))+'hello' select convert(varchar(10),100)+'hello'select convert(varchar(50),getdate())select convert(varchar(50),getdate()) select convert(varchar(50),getdate(),101) select convert(varchar(50),getdate(),100)?
------------------------聯合--------------------------------------- --union聯合的作用就是將多個結果集并到了一起 select fname,fage from mystudent where fid>999 union all select tsname,tsage from itcastcn..tblstudent where tsid>12?
--=================================================== --當聯合的時候注意: --1.多個結果集中的列的數據類型必須一一對應 --2.列的個數必須一樣 --聯合的時候如果只寫union,則會去除重復數據,如果寫union all則不會去除重復數據。由于一般情況下我們都沒有重復數據,所以也不需要去除,所以我們一般建議使用union all。 --================字符串函數================= select len('哈哈hello') --返回字符的個數 select datalength('哈哈hello') --返回是字節個數select lower('AaBb')select UPPeR('AaBb')select '==========='+rtrim(ltrim(' aaa '))+'==============='--從左邊數,截取10個字符 select left('hello welcome to China.',10)--從右邊數,截取10個字符 select right('hello welcome to China.',10)--從索引為2的開始,截取5個字符。 --注意:1.索引從1開始數。2.含當前索引。 select substring('abcdefghijklmn',2,5)select replace('fjdsalfafdjaslkfjdsakjflksafjdsfjdslkfjdsljf','f','★') select replace(username,'趙','李')?
=================日期函數=============== select get date() print convert(varchar(50),)GETDATE() :取得當前日期時間 DATEADD (datepart , number, date ),計算增加以后的日期。參數date為待計算的日期;參數number為增量;參數datepart為計量單位,可選值見備注。DATEADD(DAY, 3,date)為計算日期date的3天后的日期,而DATEADD(MONTH ,-8,date)為計算日期date的8個月之前的日期 。(入職一年以上的員工發1000$) DATEDIFF ( datepart , startdate , enddate ) :計算兩個日期之間的差額。 datepart 為計量單位,可取值參考DateAdd。 統計不同入學年數的學生個數: select DateDiff(year,sInDate,getdate()),count(*) from student Group by DateDiff(year,sInDate,getdate()) DATEPART (datepart,date):返回一個日期的特定部分 year(date) 計算日期返回的年份 Datepart可選值 取值別名說明 yearyy,yyyy年份 quarterqq,q季度 monthmm,m月份 dayofyeardy,y當年度的第幾天 daydd,d日 weekwk,ww當年度的第幾周 weekdaydw,w星期幾 hourhh小時 minutemi,n分 secondss,s秒 millisecondms毫秒?
轉載于:https://www.cnblogs.com/cheshui/archive/2012/07/26/2610055.html
總結
以上是生活随笔為你收集整理的6-14 数据库高级的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不同技术团队的配合问题及DevOps
- 下一篇: 两个指针变量可以相减