1. 程式人生 > >SQLServer入門基礎(SQL語句)

SQLServer入門基礎(SQL語句)

資料型別:

char(固定長度)   varchar(長度隨輸入變化)
在字串長度固定的情況下,用char,因為char速度快

nchar和nvarchar是在前者的基礎上採用了Unicode編碼
Unicode編碼的特點是位元組和漢字佔用的空間一樣啊

bit(不用)   int	bigint	floa t	numeric(更加精準,好控制)

datetime	timestamp
getdate()	得到當前時間
create table aaa
(shijian datetime)
insert aaa values(getdate())  --插入系統時間
insert aaa values('2019/1/23')  --插入時間 按照預設的格式會將/變成-
select * from aaa
drop table aaa

圖片型別image和視訊型別binary一般都放在專門的伺服器上,因為太佔用頻寬
只有當需要安全時才直接放入資料庫,而且儘量小

基礎操作

–是註釋 格式 拼寫  
–新建資料庫  
create database shuguo  
–建表的同時建欄位  
create table renwu  
( bianhao int,  
xingming nvarchar(5),  
xingbie nchar(1),  
zhiwu nvarchar(10),  
nianling int,  
gongzi numeric(6,2) --一共佔6位,小數佔2位  
)  
–刪除表(包括欄位) drop只刪除表和資料庫  
drop table renwu

select * from renwu  
–別名  
select xingming 姓名,zhiwu 職務 from renwu  
–新增記錄  
insert into renwu values(1,‘劉備’,‘男’,‘主公’,40,9999)  
insert into renwu (bianhao,xingming,xingbie,zhiwu) values(2,‘甘夫人’,‘女’,‘夫人’)  
insert into renwu values(3,‘諸葛亮’,‘男’,‘軍師’,37,8000)  
insert into renwu values(4,‘關羽’,‘男’,‘將軍’,38,6000)  
insert into renwu values(5,‘張飛’,‘男’,‘將軍’,35,5000)  
–刪除所有記錄,不刪欄位  
delete from renwu  
–按條件刪除記錄  
delete from renwu where bianhao=2  
delete from renwu where xingming=‘甘夫人’  
–按條件查詢  
select * from renwu where gongzi>7000  
select xingming,xingbie,zhiwu,gongzi from renwu where gongzi>7000  
–把工資低於8000的加上2%  
update renwu set gongzi=gongzi*1.02 where gongzi<8000

–主鍵可以改,只要不重複,插入部分資料,主鍵必須得插入  
create table gsyg  
( bianhao int primary key,  
xingming nvarchar(5),  
nianling char(2)  
)  
insert gsyg values(1,‘唐僧’,‘78’)  
insert gsyg values(2,‘孫悟空’,‘50’)  
insert gsyg values(3,‘豬八戒’,‘57’)  
insert gsyg values(4,‘沙和尚’,‘23’)  
insert gsyg values(5,‘白龍馬’,‘19’)  
insert gsyg (bianhao,xingming) values(6,‘如來’)  
update gsyg set nianling=24,xingming=‘觀音菩薩’,bianhao=7 where bianhao=6  
update gsyg set nianling=13 where nianling is null --當資料為空時  
delete from gsyg where bianhao=7 and nianling=13  
delete from gsyg where bianhao=7 or nianling=24  
select * from gsyg

資料查詢

水滸例項:  
create database shuihu

create table yuangong  
( ygbianhao int primary key,  
xingming nvarchar(5),  
zhiwu nvarchar(8),  
shangji int,  
rzshijian datetime,  
gongzi numeric(6,1),  
buzhu numeric(5,1),  
bmbianhao int foreign key references bumen(bmbianhao)  
)

insert into yuangong (ygbianhao,xingming,zhiwu,rzshijian,gongzi,buzhu,bmbianhao) values (101,‘宋江’,‘寨主’,‘2002/3/15’,20000,3000,1)  
insert into yuangong values(102,‘盧俊義’,‘寨主’,101,‘2002/6/16’,18000,3000,1)  
insert into yuangong values(103,‘吳用’,‘軍師’,101,‘2002/8/19’,17000,2800,2)  
insert into yuangong values(104,‘公孫勝’,‘軍師’,103,‘2002/9/22’,16000,2600,2)  
insert into yuangong values(105,‘關勝’,‘大將’,102,‘2003/2/15’,15000,2300,3)  
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(106,‘徐寧’,‘小將’,105,‘2005/7/9’,14000,3)  
insert into yuangong values(107,‘魯智深’,‘大將’,102,‘2003/4/22’,13000,2000,4)  
insert into yuangong values(108,‘武松’,‘大將’,107,‘2003/9/8’,12000,1800,4)  
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(109,‘李逵’,‘小將’,107,‘2005/4/11’,10000,4)  
insert into yuangong values(110,‘李俊’,‘大將’,102,‘2005/1/6’,13000,3000,5)  
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(111,‘阮小二’,‘小將’,110,‘2005/6/24’,13000,5)  
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(112,‘阮小七’,‘小將’,110,‘2005/6/15’,12000,5)  
insert into yuangong values(113,‘柴進’,‘總管’,101,‘2004/3/5’,11000,1000,6)  
insert into yuangong values(114,‘李英’,‘總管’,113,‘2006/3/10’,10000,600,6)  
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(115,‘時遷’,‘小頭領’,113,‘2007/11/19’,8000,6)
create table bumen
(
	bmbianhao int primary key,
	mingcheng nvarchar(8),
	didian nvarchar(8)
)
insert into bumen values(1,‘總頭領’,‘聚義廳’)  
insert into bumen values(2,‘文職’,‘前山’)  
insert into bumen values(3,‘馬軍’,‘左山’)  
insert into bumen values(4,‘步軍’,‘右山’)  
insert into bumen values(5,‘水軍’,‘山下’)  
insert into bumen values(6,‘後勤’,‘後山’)

–查詢魯智深的工資,職務,所在部門的編號  
select xingming,gongzi,zhiwu,bmbianhao from yuangong where xingming=‘魯智深’  
–查詢一共有幾個部門(去重)  
select distinct bmbianhao from yuangong  
–顯示每個員工的年薪,以中文顯示欄位  
select xingming 姓名,gongzi_12 年薪 from yuangong  
–計算年總收入和 空值 做四則運算  
select xingming 姓名,gongzi\_12+isnull(buzhu,0)\_12 年總收入 from yuangong  
–查詢2004年以後入職的員工  
select xingming,rzshijian from yuangong where rzshijian>‘2004-1-1’  
–查詢工資在13000到1800之間的員工  
select xingming 姓名,gongzi 工資 from yuangong where gongzi>13000 and gongzi<=18000  
–查詢姓李的員工  
select xingming 姓名 from yuangong where xingming like ‘李%’  
–查詢第二個字是進的員工  
select xingming 姓名 from yuangong where xingming like ‘_進%’  
–批量查詢  
select xingming 姓名,ygbianhao 員工編號 from yuangong where ygbianhao in (101,105,108,114)  
–顯示一把手的姓名和職務  
select xingming 姓名,zhiwu 職務 from yuangong where shangji is null  
–排序 預設升序 desc降序 中文按照拼音順序和音調  
select xingming 姓名,zhiwu 職務,gongzi 工資 from yuangong order by gongzi desc  
select xingming 姓名 from yuangong order by xingming  
–查詢總工資和平均工資  
select sum(gongzi) 總工資,avg(gongzi) 平均工資 from yuangong  
–查詢最高工資和最低工資  
select max(gongzi) 最高工資,min(gongzi) 最低工資 from yuangong  
–查詢最高工資所有人的姓名和工資  
select xingming 姓名,gongzi 工資 from yuangong where gongzi=(select max(gongzi) from yuangong)  
–查詢高於平均工資的員工的姓名和工資  
select xingming 姓名,gongzi 工資 from yuangong where gongzi>(select avg(gongzi) from yuangong)  
select xingming 姓名,gongzi 工資,(select avg(gongzi) from yuangong) 平均工資 from yuangong where gongzi>(select avg(gongzi) from yuangong)  
–按照部門編號升序,工資降序  
select xingming 姓名,gongzi 工資,bmbianhao from yuangong order by bmbianhao,gongzi desc  
–用起別名的方法算年總收入並降序排列  
select xingming 姓名,gongzi\_12+isnull(buzhu,0) 年總收入 from yuangong order by gongzi\_12+isnull(buzhu,0) desc  
select xingming 姓名,gongzi_12+isnull(buzhu,0) 年總收入 from yuangong order by 年總收入 desc  
–統計有多少條記錄  
select count(*) from yuangong  
–統計每個部門的平均工資和總工資 group by 後面的欄位必須出現在顯示欄位中  
select bmbianhao,sum(gongzi) 總工資,avg(gongzi)平均工資 from yuangong group by bmbianhao  
–查詢每個部門每個職務的平均工資和最低工資 降序排列  
select bmbianhao,avg(gongzi) 平均工資,min(gongzi)最低工資,zhiwu 職務 from yuangong group by bmbianhao,zhiwu order by bmbianhao  
–顯示平均工資低於15000的部門編號和平均工資  
select bmbianhao 部門編號,avg(gongzi) 平均工資 from yuangong group by bmbianhao having avg(gongzi) <15000  
–笛卡爾集現象(主外來鍵沒有對應)  
select * from yuangong,bumen  
select * from yuangong,bumen where bumen.mingcheng=‘水軍’ and yuangong.bmbianhao=bumen.bmbianhao  
select * from yuangong,bumen where yuangong.bmbianhao=bumen.bmbianhao  
–查詢姓名和所在部門以及編號  
select xingming 姓名,mingcheng 部門,yuangong.bmbianhao 部門編號 from yuangong,bumen where yuangong.bmbianhao=bumen.bmbianhao  
–查詢部門號為4的部門名稱,職員姓名和工資  
select mingcheng 部門,xingming 姓名,gongzi 工資 from yuangong,bumen where yuangong.bmbianhao=bumen.bmbianhao and bumen.bmbianhao=4  
–查詢員工名,部門名和工資,按照部門排序  
select xingming,mingcheng,gongzi from yuangong,bumen where bumen.bmbianhao=yuangong.bmbianhao order by bumen.bmbianhao desc  
–查詢李逵的上級  
select xingming from yuangong where ygbianhao=(select shangji from yuangong where xingming=‘李逵’)  
–查詢所有員工的姓名和其上級(自連線)  
select a.xingming 員工,b.xingming 領導 from yuangong a,yuangong b where a.shangji=b.ygbianhao  
–子查詢(巢狀查詢)單行子查詢:返回結果為單行的子查詢稱為單行子查詢  
–顯示與魯智深同部門的員工  
select xingming from yuangong where bmbianhao=(select bmbianhao from yuangong where xingming=‘魯智深’)  
–多行子查詢:返回結果為多行的子查詢稱為多行子查詢  
–多行子查詢如果不能一次寫對,就寫兩行,先寫子查詢,再寫主查詢  
–查詢和5號部門相同職務的員工姓名,職務,工資和部門編號  
select xingming 姓名,zhiwu 職務,gongzi 工資,bmbianhao 部門編號 from yuangong where zhiwu in  
(select distinct zhiwu from yuangong where bmbianhao=5) and bmbianhao!=5  
–查詢高於部門平均工資的員工姓名和工資,部門編號以及部門的平均工資  
–首先得到每個部門的平均工資  
select avg(gongzi) from yuangong group by bmbianhao  
select xingming 姓名,gongzi 工資,yuangong.bmbianhao 部門編號,pjgz 平均工資 from yuangong,(select avg(gongzi) pjgz,bmbianhao from yuangong group by bmbianhao) gzb where gongzi>pjgz and gzb.bmbianhao=yuangong.bmbianhao  
–分頁查詢  
–查詢第一個到第五個入職的員工  
select top 5 xingming 姓名,zhiwu 職務,rzshijian 入職時間 from yuangong order by rzshijian  
–查詢第6個到第13個入職的員工  
select top (13-6) xingming 姓名,zhiwu 職務,rzshijian 入職時間 from yuangong where ygbianhao not in (select top 5 ygbianhao from yuangong order by rzshijian) order by rzshijian  
–內連線  
select a.xingming 員工,b.xingming 領導 from yuangong a,yuangong b where a.shangji=b.ygbianhao  
–左外連線  
–左邊表中的資料全部顯示,右邊的有則顯示,沒有就顯示空  
select a.xingming 員工,b.xingming 領導 from yuangong a left join yuangong b on a.shangji=b.ygbianhao

刪除重複資料

–刪除重複資料  
create table xiaobaio  
(bh int,  
mc nvarchar(5))  
insert into xiaobaio values (1,‘姓名1’)  
insert into xiaobaio values (2,‘姓名2’)  
select * from xiaobaio

select distinct * into lsb from xiaobaio  
delete from xiaobaio  
insert into xiaobaio select * from lsb  
drop table lsb  
select * from xiaobaio

約束

–約束 保證資料滿足應有的條件
–約束分為 not null(非空) unique(唯一的) primary key foreign key check(自定義) default(預設)
create table biao
( dbbh int primary key,
dbxm varchar(20) unique,
dbmm varchar(20) not null,
nianling int check(nianling>=20 and nianling<=30) default 18,
bmbianhao int foreign key references bumen(bmbianhao) )

資料庫分離

將資料庫隱藏,防止誤操作:
資料庫分離:右鍵想要分離的資料庫–任務–分離–直接點確定–開啟資料庫儲存全路徑–拷貝資料庫(.mdf)和它的日誌檔案(_log)
資料庫恢復:將資料可拷貝到它的儲存全路徑–右鍵資料庫–附加–新增–確定
資料庫備份:右鍵想要分離的資料庫–任務–備份
資料庫還原:右鍵資料庫–還原資料庫–找到備份路徑(備份檔案.bak)

資料庫備份:backup database sss to disk=‘E:/sss.bak’
資料庫還原:restore database sss from disk=‘E:/sss.bak’
資料庫的儲存路徑:
C:\Program Files\Microsoft SQL Server\MSSQL14.