數據庫入門
一、數據庫介紹
-
介紹
- 數據庫就是一個存儲數據的倉庫,能夠長期存儲數據且能夠對數據進行增刪改查
-
常見的數據庫
-
關系型數據庫:
- mysql:開源免費,常用于中小型項目
- sql server:微軟開發的數據庫,適用于微軟項目
- oreale:適用大型項目,比如銀行
- sqlite:主要用于移動應用
-
非關系型數據庫
- redis:鍵值對的形式存儲數據
- mangodb:文檔的形式存儲數據
- Hbase:結構化數據分布式存儲系統
-
兩者之間的差別:關系型數據是以二維表的形式儲存數據,二維表即excel中的表格
-
-
數據庫核心要素
- 數據庫:數據表的集合
- 數據表:數據的集合
- 字段:數據的列名
- 數據行:一行數據
-
SQL介紹
- SQL是一種結構化查詢語言,可以通過SQL語言可以對數據庫進行操作
- sql語言分類
- DQL:數據查詢語言
- DML:數據操作語言
- TPL:事務處理語言
- DCL:數據控制語言
- DDL:數據定義語言
- CCL:指針控制語言
我們主要學習MySQL數據庫,其他的數據庫了解即可,主要用到的sql語言有DQL、DML、DDL三種
二、MySQL介紹
-
介紹
- 由瑞典MySQL AB格式公司開發,后被Sun公司收購,接著Sun公司被Oreale公司收購,即MySQL現在數據Oreale的產品
-
特點(主要)
- 免費、開源
- 可移植性好
- 支持多操作系統
- 支持多種編程語言
- .....
-
組成
- MySQL服務器:存儲數據并解析編譯后的sql語句,將執行結果返回到客戶端
- MySQL客戶端:下發用戶要執行的sql語句,并顯示服務器返回的執行結果
-
連接mysql數據庫方式
-
命令行模式
- 在控制臺輸入mysql -h ip地址 -p 端口號 -u 用戶名 -p 回車后,輸入密碼即可進入到數據庫
-
工具連接
- DBerver
- Navicat
-
三、數據庫基礎理論知識
-
數據類型以及約束
-
數據類型
-
int:整數類型,有符號整數范圍(-2147483648~2147483647)
- unsigned:無符號整數范圍(0~4294967295)
-
varchar:字符串類型,范圍在 0~65533
-
decimal(5,2):小數,5為總位數,2為有兩位小數
-
datetime:時間
-
-
約束
- 默認值:即不填數據時,內容默認填寫的值
- 非空:該字段不能不輸入數據
- 主鍵:提高查詢的效率
- 唯一性:主鍵的值不能重復
- 非空性:主鍵值不能為空
- 單一性:一張表只能由一個主鍵
- 不變性:主鍵的值不能隨意更改
- 外鍵:維護兩個表之間的依賴關系
- 唯一性:定義字段的值不允許重復
-
四、SQL語句
-
創建數據庫語句
create database 數據庫名 charset=utf8; -
刪除數據庫
drop database 數據庫名; #在不知數據庫是否存在的情況可以使用is exists drop database is exists 數據庫名; -
創建數據表
create table 表名( 字段1 類型, 字段2 類型, 字段3 類型,.... ); -
刪除數據表
-
drop 刪除數據表
drop from 表名; -
truncate 清空數據表結構
truncate table 表名; #自增長字段會初始化 -
delete 刪除數據但不刪除表結構
delete from 表名稱 where 字段名=值; #該命令不會清空表結構,比如id是主鍵自增長,刪除其中一條數據時,id不會發生變化 #若是沒有條件則會直接刪除表中所有的數據,但不會刪除表結構,id自增長不會初始化
-
-
插入數據
-
insert into
#單行全字段插入 注意:存在主鍵自增長則賦值0 or null,值和字段位置要一一對應 insert into 表名 values(值1,值2,值3,...); #單行選擇字段插入 insert into 表名(字段1,字段2) values(值1,值2); #多行全字段插入 一個括號代表一個數據 insert into 表名 values(值1,值2,值3,...),(值1,值2,值3,...),(值1,值2,值3,...),....; #多行選擇字段插入 insert into 表名(字段1,字段2) values(值1,值2),(值1,值2),(值1,值2),....;
-
-
修改數據/更新數據
-
update
update 表名 set 字段1=值1,字段2=值2,字段3=值3,... where 字段=值;
-
-
查詢數據
-
基本語法
select * from tableName;#查詢所有信息 select 字段 from tableName;#查詢指定字段 -
條件查詢
-
比較運算符
#查詢學生表中學號為1的學生信息 select * from student where studentNo = 1; #查詢學生表中成績大于等于60的學生信息 select * from student where score >= 60; #查詢學生表中成績小于等于60的學生信息 select * from student where studentNo <= 60; #查詢學生表中成績不等于60的學生信息 select * from student where studentNo != 60; -
邏輯運算符
-
and
#查詢學生表中所有高于60分的男生信息 select * from student where score > 60 and sex = '男'; -
or
#查詢學生表中所有高于60分或班級為1班的學生信息 select * from student where score > 60 or class = '1班'; -
not
#查詢學生表中班級除1班以外班級的學生信息 select * from student where not class = '1班';
-
-
去重
-
distinct
#查詢學生表中,學生都來自于哪些省份 select distinct(hometown) from students;
-
-
模糊查詢
-
like
#查詢學生表中姓孫的學生 select * from students where name like '孫%'; #查詢學生表中名字最后一個字為白的學生 select * from students where name like '%白'; #查詢學生表中姓孫名字兩個字的學生 select * from students where name like '孫_'; #查詢學生表中姓名兩個字的學生 select * from students where name like '__'; #查詢學生表中名字兩個字最后一個字為白的學生 select * from students where name like '_白';
-
-
范圍查詢
-
in
#查找學生表中姓名為張三、李四、王五的同學的信息 select * from students where name in ('張三','李四','王五'); -
between ... and ...
#查找學生表中年齡在18到20的所有學生信息 select * from students where age between 18 and 20;
-
-
空判斷
-
is null
#查找身份證號為空的學生信息 select * from students where card is null; -
is not null
#查找身份證號不為空的學生信息 select * from students where card is not null;
-
-
-
排序
-
order by
#默認升序 select * from students order by 字段; select * from students order by 字段 asc; #降序 select * from students order by 字段 desc;
-
-
聚合函數
#最大值 select max(字段) from students; #最小值 select min(字段) from students; #平均值 select avg(字段) from students; #統計 select count(字段) from students; #求和 select sum(字段) from students;- 一個sql語句可以使用多個聚合函數,使用 , 分割即可
- where 條件中不能使用聚合函數
-
分組查詢
-
group by
select 分組字段,... from studnets group by 字段; -
having
select 分組字段,... from studnets group by 字段 having 字段=值;- having可以使用聚合函數
-
-
分頁
-
limit
select * from students limit 開始的行數,顯示的條數;
-
-
聯表查詢
-
內連接
-
內連接是取兩個表共有的數據
select * from 表1 s1 inner join 表2 s2 on s1.關聯字段=s2.關聯字段;
-
-
外連接
-
左連接:共有數據+左邊特有數據+右邊以空(null)填充
select * from 表1 s1 left join 表2 s2 on s1.關聯字段=s2.關聯字段; -
右鏈接:共有數據+右邊特有數據+左邊以空(null)填充
select * from 表1 s1 right join 表2 on s1.關聯字段=s2.關聯字段;
-
-
-
自關聯
- 使用場景:當表存在等級關系時,查詢其中數據需要使用自關聯
- 比如:省,市,縣
- 比如:總經理,部門主管,組長,組員
- 自關聯思路:
- 把一張表當成多張表來使用
- 說明:將表定義為不同的別名
- A表的字段與B表的字段進行關聯
- 強調:一定是表中不同的字段
- 把一張表當成多張表來使用
select * from 表1 s1 inner join 表1 s2 on s1.關聯字段=s2.關聯但不相同字段; - 使用場景:當表存在等級關系時,查詢其中數據需要使用自關聯
-
子查詢
-
前置介紹:涉及到了表之間的關聯,可以使用表連接,也可以使用子查詢
- 強調:如果能用表連接,絕對不用子查詢。因為子查詢SQL語句執行效率很低
-
子查詢,充當條件分類
- 標量子查詢:一行一列
- 最簡單的子查詢
- 列子查詢:一列多行
- 行子查詢:一行,多列
#標量子查詢:即返回一個數據 select * from students where age = (select age from students where id = 1); #列子查詢:返回多個相同字段的值 select * from students where age in (select age from students); #行子查詢:返回一行多個字段值 select * from students where (age,class) = (select age,class from students where id = 1); - 標量子查詢:一行一列
-
充當數據源
select * from (select * from students);
-
-
浙公網安備 33010602011771號