sql語句 不區分大小寫!sql
在 sql 語句裏面 全部的字符串 都用單引號 ''數據庫
create database Library安全
新建一個名字爲 Library 的表架構
建一個表 名字叫 Users 的表優化
(UserId int primary key, 是設置主鍵的意思)設計
create table Users日誌
(排序
UserId int primary key,索引
UserName nvarchar(20) not null,ip
UserPwd nvarchar(20) not null,
)
註釋 兩個橫線 --
select * from UserInfor
查詢 名字 爲UserInfor 我表裏面的全部內容
* 表示全部字段
select UserId,UserName,Pwd from UserInfor
只查詢部分的內容 好比 id name 密碼
where 查詢 查詢 name 爲 admin pwd 爲123456
select * from UserInfor where UserName='admin' and Pwd='123456'
select * from UserInfor where Sex='男'
查詢 UserInfor 表中 全部 Sex 爲男的
select * from UserInfor where Sex='男' and Origin='江西'
查詢 UserInfor 表中 全部 Sex 爲男的 Origin 爲江西的
select * from UserInfor where Sex='男' and Origin='江西' and Age>18
查詢 UserInfor 表中 全部 Sex 爲男的 Origin 爲江西的 年齡大於18的
or 是或者 或 的意思
select * from UserInfor where Sex='男' and (Origin='江西' or Origin='湖北' ) and Age>18
湖北 或 江西
select * from UserInfor where (age<30 or age>25)
select * from UserInfor where age<=30 and age>=25
select * from UserInfor where Age between 25 and 30
查詢年齡 小於 25大於30的
select * from UserInfor where Age between 25 and 30
是查區間
查記錄數
select count(*) from UserInfor where Origin='江西'
查詢江西的 個數 個數 返回個數
select count(*) as UserCount from UserInfor where Origin!='江西'
不爲江西 的個數
select count(*) as UserCount from UserInfor where Origin='江西'
返回的那個個數 多了個 列名 UserCount
select * from UserInfor order by Age
select * from UserInfor order by Age asc
排序查詢 經過 Age 升序 查詢
默認是升序 後面的 asc 能夠省略
select * from UserInfor order by Age desc
按照 Age 降序查詢 desc 表示降序
select * from UserInfor where UserId=5 or UserId=8 or UserId=10
select * from UserInfor where UserId in(5,8,10)
查詢 UserId 爲 5 8 10 的
select * from UserInfor where UserId not in(5,8,10)
除了 UserId 爲 8 5 10 的
select * from UserInfor where UserName like '謝%'
查詢查 UserName 爲 謝 開頭的
select * from UserInfor where UserName like '%帥'
查詢 UserName 爲 帥 結尾的
select * from UserInfor where UserName like '%帥%'
查詢 UserName 包含 帥 的
select min(Age) as MinAge from UserInfor
查詢 Age 最小的 列名 爲 MinAge
select max(Age) as MaxAge from UserInfor
查詢 Age 最大的 列名 爲 MaxAge
select avg(Age) as AvgAge from UserInfor
求平均數 列名 爲 AvgAge
select sum(Age) as SumAge from UserInfor
求和 列名 爲 SumAge
select * from UserInfor where Age>
(
select avg(Age) from UserInfor
)
查詢大於 平均值的
select top 3 * from UserInfor order by Age desc
查詢 年齡最高的三個
select count(*) as oruginCount,Origin from UserINfor group by Origin
分組查詢
select count(*) as oruginCount,Origin from UserINfor group by Origin having count(*)>2
查詢結果大於二的
select distinct UserName from UserInfor
查詢不重複的 distinct 表示不重複
left join
select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore
from UserInfor UI left join Score SC on
UI.UserId=SC.UserId
聯合查詢 UI.UserId=SC.UserId 關聯
right join
select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore
from UserInfor UI right join Score SC on
UI.UserId=SC.UserId
inner join
select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore
from UserInfor UI inner join Score SC on
UI.UserId=SC.UserId
select UISC.* ,PT.Father,PT.Mum from
(
select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore
from UserInfor UI inner join Score SC on
UI.UserId=SC.UserId
) UISC inner join Parent PT on
UISC.UserId=PT.UserId
三個表 聯合查詢 嵌套式
聯合查詢 很重要 必定要過
select row_number() over(order by UserId)as rownumber,* from UserInfor
分頁查詢
select top 5* from
(
select row_number() over(order by UserId)as rownumber,* from UserInfor
) A where rownumber>5
行號大於5
(之後用的很是多)
select UserId Age,
case
when Age<20 then '大一'
when Age>20 and Age<=25 then '大二'
when Age>25 and Age<=30 then '大三'
else '大四'
end as geade from UserInfor
對某個字段進行判斷
獲取時間
select year(getdate())
獲取當前系統年
select month(getdate())
獲取當前系統月
select day(getdate())
獲取當前系統日
select dateadd(yy,100,getdate())
當前系統時間再加100年 yy是年
select dateadd(mm,100,getdate())
當前系統時間再加 100個月 mm是月
select dateadd(dd,100,getdate())
當前系統時間再加 100個天 dd是天
能夠用在VIP到期時間 添加臨時VIP
間隔時間
select datediff(yy,getdate(),'2108/12/31')
當前時間 距離 2108/12/31 多少年
select datediff(mm,getdate(),'2108/12/31')
select datediff(dd,getdate(),'2108/12/31')
間隔多少月 間隔多少天
下面是增長 增
查詢到這了告一段落
--添加 下面這種方式 表裏面的每個字段都要寫
insert into UserInfor values ('林東羣','dongdong520',20,'1997-7-12 00:00','女','廣東')
若是不想 寫值 ‘’ 不能不填
--下面這種方式能夠添加表單中的指定項
insert into UserInfor(UserName,Pwd,Age) values('劉嚴','liuyan',22)
下面學 刪除 刪
delete UserInfor
刪除這個 叫 UserInfor 的表
--下面是刪除
delete UserInfor where UserId=1004
刪除 UserId=1004 的
下面是該 修改
update UserInfor set UserName='劉翔',Age=22,Pwd='qwe1234' where UserId=11
修改 UserId 爲11的 UserNam Age Pwd
--約束
找到對應的表 設計 選擇對應的項 CHECK 約束 而後寫表達式
也能夠在JS裏面約束 好比年齡 不能小於1 不能大於100
索引 數據庫優化 要用的技術
彙集索引
非彙集索引
一個表裏面只有一個彙集索引
設置了主鍵 那麼就被默認設置了是彙集索引
提升查詢效率
(彙集索引 訪問量不是特別大就不要加 訪問量特別大就要就 好比和百度大那樣的就要加)
------------------------------------
視圖 原理 就是聯合查詢
聯合查詢 是寫在程序裏面 寫在C# 裏面
視圖 把它當作單獨個體存在數據庫裏面 能夠單一個單獨的表使用
視圖(佔用數據庫空間 用的比較少)
存儲過程
相對安全 相對高效
--觸發器
利用觸發器 把刪除的東西 顯示出來
delete UserInfor where UserId=1005
create trigger trigDeIuser
ON UserInfor
after delete /* 有三種INSERT,DELETE,UPDATE*/
AS
begin
select * from deleted /*在刪除後同時查出刪除後的內容*/
end
數據庫的還原 備份
備份方式 三種
方法1. 選中要備份的數據庫----任務-----備份 找到備份文件
還原 選中數據庫---右鍵----還原數據庫---設備--添加----覆蓋現有數據庫---確實
若是向下兼容?
修改兼容級別
選中數據庫---右鍵----屬性----選項----下降至本身須要的級別
方法2.經過日誌文件還原
C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA
下的.mdf 文件 和.ldf
要先保存起來 否則刪了庫 這個兩個文件也會被刪除
而後加進去
還原 選中數據庫---右鍵---附加---選中---肯定
方法3.導出腳本
選擇數據庫----右鍵---任務----生成腳本----下一步----下一步----高級---倒數第二個
要修改的腳本類型:架構和數據。---而後一直按肯定
存入進去
找到腳本 拉倒軟件裏面 執行就能夠了 也能夠複製代碼執行
修改VIP到期
--update Users set Isvip='false' where Isvip='true' and datediff(dd,getdate(),Expire)<0
--到期了 結果是負數 沒到期 結果是正數 剛恰好是0