1. 程式人生 > 實用技巧 >資料庫知識

資料庫知識

資料庫的基本操作:

一、建立

1.1 建立資料庫
create database 庫名
1.2 切換資料庫
use 庫名
1.3 建立表
create table 表名(
		
		)

二、關於資料的型別

資料型別:int、float、numeric\decimal、char(n)/varchar(n)
money、date、datetime、binary(n)/varbinary(n)
bit ......

1.4 約束
主鍵:primary key   (不能重複,不能出現空值)
自增:identity(m,n) 整型列才可以用。 m代表自增開始的數 , n代表每次自增的數量
非空:not null
唯一:unique   不能重複,允許出現一個空值
預設值:default '值'
檢查:check(條件表示式)
外來鍵:foreign key references 表名(列名)

三、新增資料

		--自增列不需要指定資料;
		--非空列一定要指定資料;
		--在insert語句中使用default表示預設值
- - 1)最常用的:
insert into 表名 values(...),(...)
1.5 例項
insert into Department values('外聯部')
insert into Department values('資訊部'),('外交部')
- - 2)變化寫法:可以指定列名
insert into Worker(wid,wname)
values('1004','趙六')
go
- -3) 用insert ....select語句新增資料(不能用default)
insert into Worker
select '1005','王小五','女','555555','119','2019-1-1',3  union
select '1006','王小六','女','444444','119','2019-10-15',3  union
select '1007','王小七','女','666666','119','2019-10-15',3
go
- - 4) --4)select into語句:常用於備份
select *
into worker_new
from worker

四、查詢資料

- - 1)
select * from product  --*表示所有列

--查部分列
select pid,pname,price from product

--查詢給列取別名
select pid as 商品編號,pname as 商品名稱,price as 價格
from product   --as可以省略

select 商品編號=pid,商品名稱=pname,價格=price 
from product   -- =取別名時,別名在前
- - 2)distinct修飾列:去除重複資料
select distinct pstime from product
- - 3) top n 修飾列:查詢前n條資料
select top 2 * from product

select top 30 percent * from product  --percent表示百分比

五、修改資料

		--update 表名 set 列名1=值1,列名2=值2
		--where 條件
- - 1) 例項
update Worker
set wsex='女',did=3
go
- - 2) 例項二:加入where條件
update Worker
set wsex='女',did=3
where age = 1000
go

六、刪除資料

- - 1)
delete from Worker
where did=1  --刪除編號為1的員工
go
- - 2) 關於刪除主鍵表的資料
update Worker
set did=null
where did=2  --將部門編號為2的員工的部門清空
go

delete from Department
where did=2
go

思路:先將外來鍵修改成null, 再刪除主鍵表資料
- - 3) 附加知識:清空表 (不能加where)
truncate table Worker
go

七、group by 子句: 分組

   -- 常與聚合函式配合使用
   --就是分組後要進行統計
   	--如果有分組語句,select後只可以寫兩種列:
		--	1)用於分組的列
		--	2 ) 聚合函式的列
- - 1) AVG() :統計均價
--1)按照商品型別編號ptid進行分組,統計均價
select ptid 型別,AVG(price) 均價 from product
group by ptid
- - 2) COUNT(): 統計數量
select adid 產地編號,COUNT(*) 數量  from product
group by adid
go
- - 3) having子句:分組後的再次篩選,緊跟group by
select adid 產地編號,COUNT(*) 數量  from product
group by adid
having COUNT(*)>1  --數量大於1的組
go

注:having跟where不同,where用於分組前的篩選
- - 4) 例項演示
select ptid 型別,AVG(price) 均價 from product
where pid>3
group by ptid
having AVG(price)>5  --不能用別名
order by 均價 desc  --排序可以用別名
go

八、order by子句:排序,一定要寫在最後面

- - 1) 預設
select * from product
order by price   --預設升序
go
-------------------------------
--也可以根據多個列進行排序
select * from product
order by price desc,pname desc
go

- - 2) 例項
select * from product
order by price desc  --desc表示降序,asc表示升序
go

九、where子句:篩選資料

例項
select * from product
where pname like '%旺%'  -- 包含'旺'關鍵字的商品
go

select * from product
where pid not in(1,3,5)  --不在(1,3,5)裡面
go

十、聚合函式:用於統計的函式

- - 1)
計數函式:COUNT(列名) 
求和函式:SUM(列名)
平均值函式:AVG(列名)
最大值函式:MAX(列名)
最小值:MIN(列名)    
   -- 注:不分大小寫!

十一、運算子

賦值運算子:
--關係運算符
 	= 		  等於
	!=或者<>  都表示不等於
--邏輯運算子:and 、or、not   
	  check(age>=18 and age<=30)     and:與,並且
      check(gender='男' or gender='女')    or:或者,
      not null 非空        				not : 非,反的
      	like :像...一樣
萬用字元:
   % :匹配0個或者多個任意字元
   		where name like '%小%'  --小明、黃小明、小時候、小
   _ :下劃線,匹配一個任意字元
   		where username like '_abc'   --xabc、yabc  
   []:匹配[]裡面出現的某一個字元
   		where username  like 'aa[xyz]'  -- aax、aay、aaz
		where username  like 'aa[^xyz]'  --aaa、aab、.. (匹配xyz以外的任意一個字元)
		where username like 'aa[0-9]'  --aa0 、aa1、aa2..

十二、多表連線

- - 1)內連線:innser join .... on
--內連線:inner join ... on
--返回兩張表匹配的資料
select A.StuCode,StuName, SubjectName,Score
from StudentInfo A
inner join ScoreInfo B
on A.StuCode=B.StuCode 
go

select A.列名,列名,
from 表名 A
innser join 表名 B
on A.列名 = B.列名    -- 一般是相同的資料

​ 十五

​ 初稿於2020/8/27寫