閱讀提示:本篇文章較長,建議從目錄上選取想看的內容。代碼上的話,我習慣用小寫,如果看不習慣建議跳過。有問題歡迎討論!!!
一、基礎概念
1.1數據庫的概念
????????數據庫(Database)是按照數據結構來組織、存儲和管理數據的倉庫,它能夠高效地存儲、檢索和管理大量數據。
1.2數據庫類型
- 關系型數據庫,基于關系模型,數據以表格形式存儲
- 非關系型數據庫,包括文檔型(MongoDB)、鍵值型(Redis)、列存儲型(Cassandra)、圖數據庫(Neo4j)等
二、關系模型
2.1關系型數據庫核心概念
- 模式:即邏輯模式,是數據庫中全體數據的邏輯和特征的描述。
- 表:數據以行和列的形式組織,每張表有一個唯一名稱。
- 屬性:表中的一列即一個屬性。
- 元組:表中的一行即一個元組。
- 字段:表中的列,表示數據的屬性,每個字段有特定的數據類型(如整數、字符串、日期等)。
- 域:一組具有相同數據類型的值的集合。
- 碼(鍵):表中某一個屬性或一組屬性,其值可以唯一確定一個元組。
- 主鍵(主碼):唯一標識表中每條記錄的字段或字段組,不能為NULL,且值必須唯一。
- 外鍵:一個表中的字段,引用另一個表的主鍵,用于建立表之間的關系。
- 索引:提高數據檢索速度的數據結構,類似于書籍的目錄。
2.2關系代數
2.2.1選擇
定義:從關系R中選擇滿足給定條件F的元組,記作σ_F(R)
特點:水平操作(按行篩選)
sql語句為:
select * --全體學生
from Student --從Student表
where Smajor='信息安全' ---條件
2.2.2投影
定義:從關系R中選擇若干屬性列組成新的關系,記作π_A(R),其中A是屬性列表
特點:垂直操作(按列篩選);會去除重復元組;結果關系的屬性是A中指定的屬性
sql語句
select distinct Sno,Smajor --distinct表示去除重復的元素
from Student
2.2.3連接
-
等值連接
定義:從關系R和S的笛卡爾積中選取滿足θ條件的元組,記作R?_{AθB}S.
????????????????????????/*笛卡爾積就不贅述*/
特點:θ為"="時稱為等值連接;結果包含R和S的所有屬性(可能有重復列)
sql語句:
select *
from R
join S on R.A=S.B
-
自然連接
- 定義:R和S在所有公共屬性上等值連接,并去除重復列
特點:自動識別同名屬性進行匹配;結果關系不包含重復屬性;如果無公共屬性則退化為笛卡爾積
sql語句:
select *
from table1
natural join table2
-
外連接
分類:
- 左外連接(R?S):保留左邊關系R的所有元組
- ?右外連接(R?S):保留右邊關系S的所有元組
- ?全外連接(R?S):保留兩邊關系的所有元組
sql語句:
-- 左外連接
select * from R left join S on R.A=S.B;
-- 右外連接
select * from R right join S on R.A=S.B;
-- 全外連接
select * from R full join S on R.A=S.B;
2.2.4除
定義:給定關系R(X,Y)和S(Y),其中X、Y是屬性組,R÷S得到一個新的關系,包含所有滿足"在R中存在與S中所有Y值對應的X值"的X值。
? ? ? ? /*定義相對有點抽象,需要用象集來定義除法,下面的字潦草點,請見諒*/
sql語句:
選擇那些X值,使得不存在任何S中的Y值與該X值沒有配對,即選擇與S中所有Y值都配對的X值
select distinct R.X from R R1
where not exists (select S.Y from Swhere not exist(select * from R R2where R2.X = R1.X and R2.Y = S.Y)
);
三、關系數據庫語言
3.1數據庫
3.1.1創建
create database?database_name
3.1.2刪除
drop database?database_name
代碼:
create database HE --創建數據庫
use HE --使用HE的數據庫
drop database HE --刪除數據庫
3.2數據庫模式
3.2.1創建
create schema uu --創建模式
create schema schema_name authorization owner_name;
--創建模式,并可指定所有者
3.2.2查看
SELECT name FROM sys.schemas;
--查看所有模式
--查看特定模式的詳細信息
SELECT *
FROM sys.schemas
WHERE name = 'dbo'; -- 替換為您想查看的模式名
/*創建、查看的方式有很多,這里就簡單介紹一下*/
3.2.3刪除
drop schema schema_name --刪除模式
3.3表
3.2.1創建
create table?table_name
create table 表名 (列名1 數據類型 [約束],列名2 數據類型 [約束],...[表級約束]
);
3.2.2刪除
drop table?table_name
3.2.3修改
alter table?database_name
例子:
alter table Student
add constraint c1 check(Ssex='男'OR Ssex='女')
--給表Student中Ssex添加約束,并將約束命名為c1
3.2.4約束
- 主鍵約束
create table employees (employee_id int primary key, --primary主碼約束name varchar(50)
);-- 命名主鍵約束
create table students (student_id int,name varchar(50),constraint pk_student primary key (student_id) --將主鍵命名為pk-studnet
);-- 復合主鍵
create table order_items (order_id int,product_id int,quantity int,constraint pk_order_items primary key (order_id, product_id)
);
- 外鍵約束
create table orders (order_id int primary key,customer_id int,constraint fk_customer foreign key (customer_id) references customers(customer_id)
);-- 帶級聯操作
create table order_details (detail_id int primary key,order_id int,constraint fk_order foreign key (order_id) references orders(order_id)
);
- 唯一約束
create table products (product_code varchar(20) unique, --unique唯一約束
);
- 檢查約束
create table employees (age int check (age >= 18 and age <= 65), Credit smallint not null check(Credit in('2','3','4')),Ssex char(2) check(Ssex in('男','女')) --check檢查約束
);
- 默認約束
create table orders (--默認約束 (default)status varchar(20) default 'pending'
);
- not null約束
create table customers (--非空約束 (not null)email varchar(100) not null
);
3.2.5插入
insert into 表名 (列1, 列2)
values (值1, 值2);
例子:
insert into Student values --沒有指定特定的列表示每列都要插入
('2018001','李勇','男','2000-03-08','信息安全'),
('2018002','劉晨','女','1999-09-01','計算機科學與技術'),
('2018003','王敏','女','2001-09-01','計算機科學與技術'),
('2018004','張立','男','2000-01-08','計算機科學與技術');
3.2.6查詢
1)單表查詢
- 基礎查詢
select * from Student --查看Student表中全部信息select Sno,Sname from Student --查看Student表中關于學號和姓名的信息select Sname, year(getdate()) - year(Sbirthdate)as age --計算并起別名為age
from Student --查看Student表中姓名和年齡
- 條件查詢
查詢條件 | 謂詞 |
比較 | =,>,? <,? ?<=,? ?>=,? ?!=,? ?<>(不等于),???!>(不大于,? !<(不小于);? ? NOT +上述符號 |
確定范圍 | between and,? ? ? ? not?between and |
字符匹配 | like ,? ? ? ? is not like |
空值 | is null,? ? ? ? is not null |
多重條件(邏輯) | and ,? ? ? ? or,? ? ? ? not |
?distinct消除重復列
select distinct Sno from SC --distinct消除重復列
?where中確定條件
--查詢專業為計算機科學與技術,性別為女的學生姓名
select Sname
from Student
where Smajor='計算機科學與技術'and Ssex='女';
?建立視圖,在視圖中使用between and
--查詢年齡在20和25之間的學生姓名、學號
--建立一個相關的視圖
create view Sage as
select Sname, Sno, year(getdate()) - year(Sbirthdate)as age
from Student--在視圖中查找
select Sname,Sno
from Sage
where age between 20 and 25;
?in 用來查找屬性值屬于集合的元組
--查找信息安全和計算機科學與技術學生姓名
--in 用來查找屬性值屬于集合的元組
select Sname
from Student
where Smajor in('信息安全','計算機科學與技術')
?字符匹配
--使用like進行匹配
select *
from Student
where Sname like '李勇'--%代表任意長度(長度可為0)的字符串
select *
from Student
where Sname like '劉%' --查找姓劉的人--_(下劃線)代表單個字符
select *
from Student
where Sno like '201800__'
排序
-- 單列排序
select * from 表名
order by 列名 asc; -- 升序(默認)-- 降序排序
select * from 表名
order by 列名 desc;-- 多列排序
select * from 表名
order by 列1 asc, 列2 desc;
聚集函數
select count(*) as 總數,sum(數值列) as 總和,avg(數值列) as 平均值,max(數值列) as 最大值,min(數值列) as 最小值
from 表名;
分組查詢
-- 基本分組
select 列名, count(*)
from 表名
group by 列名;-- 分組后篩選
select 列名, avg(數值列)
from 表名
group by 列名
having avg(數值列) > 100;
2)連接查詢
/*之前在關系代數也說過就不舉例子了*/
-- 內連接
select a.列, b.列
from 表1 a
inner join 表2 b on a.關聯列 = b.關聯列;-- 左連接
select a.列, b.列
from 表1 a
left join 表2 b on a.關聯列 = b.關聯列;-- 右連接
select a.列, b.列
from 表1 a
right join 表2 b on a.關聯列 = b.關聯列;
3)嵌套查詢
--查詢選修了"數據結構"課程的學生信息
select *
from Student
where Sno in (select Sno --找與課程號相對應的學號from SCwhere Cno = (select Cno --查找課程號from Coursewhere Cname = '數據結構')
);
4)集合查詢
并操作:union
交操作:intersect
差操作:except
--查詢選修了81001或81002課程的學生學號
select Sno
from SC
where Cno = '81001' --選修了81001的學號
union --并操作
select Sno
from SC
where Cno = '81002'; --選修了81002的學號
--查詢選修了81001但沒有81002課程的學生學號
select Sno
from SC
where Cno = '81001' --選修了81001的學號
except --差操作
select Sno
from SC
where Cno = '81002'; --選修了81002的學號
5)基于派生表的查詢
--查詢所有選修81001號課程的學生姓名
select Sname
from Student, (select Sno from SC where Cno='81001) as SCI
where Student.Sno=SCI.Sno
3.2.7更新
update 表名
set 列1 = 值1
where 條件;
3.4視圖
3.3.1創建
create view 視圖名 as
select 列1, 列2
from 表名
where 條件;
3.3.2刪除
drop view 視圖名
3.3.3查詢、更新
與表的查詢和更新差不多,只是把表名改為視圖名
3.5索引
3.4.1創建
create index 索引名
on 表名 (列名);
3.4.2刪除
drop index 索引名;
/*很久沒更了,一是準備比賽,二是github。不過我還是會在這上面更新一些基礎內容,項目這些的話代碼太多的就放在github上*/