數據庫技術的基礎
什么是數據庫技術
數據庫技術就是數據管理的技術,是計算機科學與計算的重要分支,是信息系統的核心和基礎;
基本概念的認識
理清概念,才能清晰的前行;
- 數據
存儲在存儲介質上的物理符號,包括數字、字符、圖形、圖像、audio和vedio等數據類型;
- 信息
經過加工處理后的數據;
單純的數據是無價值的,經過處理后符合特定目的的數據,即信息才是有價值的;
數據承載信息,信息表現數據內涵;
- 數據處理
把數據變成信息的過程,即
對原始數據進行收集、整理、加工、存儲、傳輸和檢索等一系列操作過程;
- 數據庫
數據庫是存儲在計算機的有組織、可共享的數據集合;
數據庫按一定的數據模型組織、描述和存儲;
- 數據庫管理系統
對數據庫進行管理的系統軟件,提供如下主要功能:
- 數據定義功能
DBMS提供數據定義語言DDL,對數據庫中相關內容進行定義;
- 數據操縱功能
DBMS提供數據操作語言DML,對象數據庫進行基本操作,即數據的增刪改查;
- 數據庫運行控制功能
是DBMS的核心功能,包括:
- 并發控制,多個用戶同時使用某些數據;
- 安全性檢查
- 數據完整性條件檢查
- 數據庫內部維護
- 數據庫的建立和維護功能
- 數據庫的建立,是指數據的載入、轉儲、重組織和數據庫的恢復功能;
- 數據庫的維護,是指數據庫結構的修改、變更集擴充功能;
【注】
作為一個前端攻城獅,前期主要在數據庫的建立與維護、數據的定義、數據操縱等功能理解;
-
數據庫系統
擁有數據庫并利用數據庫技術進行數據管理的計算機系統;
數據庫系統的體系包括:
- 計算機硬件設備 - 數據庫及相關軟件系統 - 開發及管理人員
數據管理的發展過程
數據管理發展大致經歷人工管理階段、文件系統管理階段和數據庫系統管理階段;
- 人工管理階段
20世紀50年代:
硬件方面存儲介質只有磁帶、紙帶等,沒有磁盤,軟件方面沒有操作系統和數據管理軟件;
此時,數據和程序集合在一起,每個程序有自己的數據集,彼此獨立,數據無法共享;
- 文件系統管理階段
20世紀60年代:
硬件方面出現了磁帶、磁盤等大容量存儲介質,軟件方面有操作系統和用文件系統處理數據的技術;
文件系統是專門處理數據的系統軟件,系統中數據以文件形式存儲在外存,應用程序通過文件系統對文件數據進行存取,文件系統是應用程序和數據之間的一個接口,實現對數據的增刪改查功能;
這段期間,數據管理有如下特點:
- 數據以文件形式長期存儲在外存,文件系統提供接口對文件進行操作;
- 程序和數據之間分開,數據的物理結構和邏輯結構有所區別;
- 但文件結構的設計仍基于特定應用,程序與數據結構之間的依賴關系未根本改變;
- 存在數據冗余,同樣的數據可能存在多個文件中;
- 數據庫系統管理階段
硬件方面出現大容量磁盤;
軟件方面出現為多用戶、多應用程序共享數據的數據庫管理技術;
此時的數據管理有如下特點:
- 數據不在針對特定應用,是面向全組織;
- 具有整體的結構性、共享性高和冗余度小
- 實現程序與數據的解耦和對數據的統一控制;
概念模型和數據模型
概念模型: 對信息世界的管理對象、屬性和聯系等信息的抽象和形式化描述,概念模型不依賴與DBMS;
數據模型:
按計算機系統的觀點對數據建模,是數據庫管理系統的實現;
- 概念模型包括以下概念:
-
實體和實體集
實體是客觀存在并且相互區別的事物,包括具體的事物和事物之間的聯系;
如,每一個學生以及班級中學生與學生的關系是一個實體實體集是具有相同屬性的實體的集合,如若干學生組成的學生實體集;
-
屬性和屬性值
屬性是描述實體的特性,如學生的名字和學號;
屬性值是屬性的具體指,如學生的學號為1;
-
實體集間的聯系
實體級之間的對應關系成為聯系,根據一個實體集中每個實體與另一個實體集中的實體可能出現的數目對應關系,兩個實體集之間的關系可分為3個類型:1對1聯系
實體集A與實體集B的1對1聯系,記做1:1,實體集A中每個實體在實體集B中至多有一個實體與之聯系;
如,班級實體集和班長實體集,班級實體集中的每一班在班長實體集中至多有一個班長實體與之聯系;1對n聯系
實體集A中的每一個實體在實體集B中與之對應的實體至少1個,記做1:n;
如,學校實體集中在學生實體集中的學生實體不止一個,但每一個學生只能屬于一個學校;n對n聯系
實體集A中每個實體在實體集B中對于對象的實體至少1個,反之亦然,記做n:n;
如,教師實體集中每個教師對應課程實體集中的多門課程實體,課程實體集中的每個課程實體也可對應教師實體集中的多個教師;-
E-R模型
E-R模型是用來描述現實世界的概念模型- 實體用矩形表示
- 屬性用橢圓表示
- 聯系用菱形表示實體集之間的聯系
- 連線用線段表示實體及其屬性之間的聯系
-
1對1聯系可看做特殊的1對n聯系,1對n聯系可以看做特殊的n對n聯系
-
數據模型
數據模型由模型結構、數據操作和完整性約束組成,下面主要講解模型結構和數據操作;
-
模型結構
模型結構經歷層次模型、網狀模型、關系模型和面向對象模型,這里重點講解關系模型,稍微提及面向對象模型;關系模型是數據模型中最紅要的額模型結構,應用最為廣泛,常見的數據庫管理系統MySQL、Oracle、SQL Server都是采用關系模型對數據進行組織;
關系模型包括以下概念:
關系
就是一個二維表代表的數據集合,講一個實體集放入一個二維表;
理解:關系型數據庫是以“關系”作為數據的基本單元,所以一個關系就是一個數據單元;關系結構
關系結構描述關系中數據的意義,如student(學號,姓名,入學成績)記錄
二維表中的一行成為一條記錄或元組屬性
二維表中的每一列是的名稱,每一列還有許多屬性值,組成屬性值集關鍵字
唯一表示記錄的屬性,成為關鍵字,關鍵字包含一個以上時要選定主鍵;
【面向對象模型】
面向對象模型將現實世界的數據抽象為對象和類,代表的NoSQL數據庫有MongoDB
- 對象
現實世界中實體的模型化,每個對象有唯一標識符、狀態和行為;
- 狀態是對象屬性值得集合
- 行為是對對象狀態的操作方法集- 類 共享同一屬性集合方法集的所有對象構成一個類
數據操作
數據操作至少支持選擇、投影和聯接三種基本關系運算- 選擇 在關系中選擇滿足條件的元組,相當于查找一行的記錄 - 投影 在關系中選擇滿足條件的屬性列,相當于查找一列的屬性 - 聯接 將兩個關系的屬性拼接成一個新的關系
-
SQL基本操作
SQL,指結構化查詢語言,全稱是 Structured Query Language是用于處理數據庫的標準計算機語言;
這里使用SQL去訪問處理MySQL數據庫,需要事先安裝MySQL
- MySQL的安裝
具體教程網上有,這里小羊就沒贅述了;
安裝完后,輸入:
$ mysql -V
mysql Ver 14.14 Distrib 5.7.17, for osx10.12 (x86_64) using EditLine wrapper
查看是否安裝成功;
- 開啟數據庫服務
$ mysql.server start
- 進入數據庫
$ mysql -u root -p
# 輸入密碼
Enter password:
- 退出mysql
mysql> exit
Bye
- 顯示數據庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| test |
+--------------------+
1 rows in set (0.00 sec)
- 創建數據庫
mysql> create database demo;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| demo |
| test |
+--------------------+
2 rows in set (0.00 sec)
- 選擇數據庫
mysql> use demo;
Database changed
- 創建數據庫的表
peole(...)代表前面的關系模型中的關系結構,定義一張表的結構;
括號的參數表示屬性名,屬性名后面是屬性值得類型描述;
mysql> create table people(id int, lastname varchar(20), firstname varchar(20), age int);
Query OK, 0 rows affected (0.03 sec)
- 顯示所有數據表
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| people |
+----------------+
1 row in set (0.00 sec)
- 插入記錄(元祖)
mysql> insert into people values (1, 'teren', 'yeung', 18);
Query OK, 1 row affected (0.01 sec)
- 查詢數據表
查詢所有記錄
mysql> select * from people;
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 1 | teren | yeung | 18 |
+------+----------+-----------+------+
1 row in set (0.00 sec)
查詢特定屬性的記錄
mysql> select lastname, age from people;
+----------+------+
| lastname | age |
+----------+------+
| teren | 18 |
+----------+------+
1 row in set (0.00 sec)
查詢唯一記錄
mysql> select * from people;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 1 | teren | yeung | 18 |
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
+------+-------------+-----------+------+
4 rows in set (0.00 sec)
mysql> select distinct firstname from people;
+-----------+
| firstname |
+-----------+
| yeung |
| cat |
| dog |
+-----------+
3 rows in set (0.00 sec)
條件查詢
mysql> insert into people values (3,"kobe", "bryant", 18);
Query OK, 1 row affected (0.00 sec)
mysql> select * from people where lastname="kobe";
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 3 | kobe | bryant | 18 |
+------+----------+-----------+------+
1 row in set (0.00 sec)
mysql> select * from people where age<18;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
+------+-------------+-----------+------+
3 rows in set (0.00 sec)
mysql> select * from people;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 1 | teren | yeung | 18 |
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
| 3 | kobe | bryant | 18 |
+------+-------------+-----------+------+
5 rows in set (0.00 sec)
mysql> select * from people where age between 2 and 4;
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 2 | totora | cat | 2 |
+------+----------+-----------+------+
1 row in set (0.00 sec)
LIKE用于匹配符合特定模式的數據;
mysql> select * from people where lastname like 'teren';
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 1 | teren | yeung | 18 |
+------+----------+-----------+------+
1 row in set (0.00 sec)
mysql> select * from people where lastname not like 'teren';
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
| 3 | kobe | bryant | 18 |
+------+-------------+-----------+------+
4 rows in set (0.00 sec)
mysql> select * from people where lastname like '%ren';
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 1 | teren | yeung | 18 |
+------+----------+-----------+------+
1 row in set (0.00 sec)
IN用于符合查詢符合IN集合后的數據
mysql> select * from people where lastname in ('hitachi', 'totora');
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
+------+----------+-----------+------+
2 rows in set (0.01 sec)
通配符
通配符 | 描述 |
---|---|
% | 替代0個或多個字符 |
- | 替代一個字符 |
[charlist] | 字符列表中的任一字符 |
[^charlist] | 不屬于字符列表的任一字符 |
mysql> select * from people where lastname like '%to%';
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 2 | totora | cat | 2 |
+------+----------+-----------+------+
1 row in set (0.00 sec)
mysql> select * from people where lastname like '_o%';
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 2 | totora | cat | 2 |
| 3 | kobe | bryant | 18 |
+------+----------+-----------+------+
2 rows in set (0.00 sec)
mysql> select * from people where lastname regexp '^[t]';
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 1 | teren | yeung | 18 |
| 2 | totora | cat | 2 |
+------+----------+-----------+------+
2 rows in set (0.00 sec)
AND && OR 運算符
mysql> select * from people where id=2 and age !=2;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
+------+-------------+-----------+------+
2 rows in set (0.00 sec)
mysql> select * from people where id=1 or age =2;
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 1 | teren | yeung | 18 |
| 2 | totora | cat | 2 |
+------+----------+-----------+------+
2 rows in set (0.00 sec)
ODBER BY關鍵字
對結果進行排序
mysql> select * from people order by lastname;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 2 | hello_kitty | cat | 5 |
| 2 | hitachi | dog | 5 |
| 3 | kobe | bryant | 18 |
| 1 | teren | yeung | 18 |
| 2 | totora | cat | 2 |
+------+-------------+-----------+------+
5 rows in set (0.00 sec)
mysql> select * from people order by age desc, firstname;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 3 | kobe | bryant | 18 |
| 1 | teren | yeung | 18 |
| 2 | hello_kitty | cat | 5 |
| 2 | hitachi | dog | 5 |
| 2 | totora | cat | 2 |
+------+-------------+-----------+------+
5 rows in set (0.00 sec)
SELEC TOP
返回指定數量記錄
mysql> select * from people limit 3;
+------+----------+-----------+------+
| id | lastname | firstname | age |
+------+----------+-----------+------+
| 1 | teren | yeung | 20 |
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
+------+----------+-----------+------+
3 rows in set (0.00 sec)
別名查詢
為查詢結構的屬性名或是表指定別名
mysql> select id as object_id, lastname as lname from people;
+-----------+-------------+
| object_id | lname |
+-----------+-------------+
| 1 | teren |
| 2 | totora |
| 2 | hitachi |
| 2 | hello_kitty |
+-----------+-------------+
4 rows in set (0.00 sec)
mysql> select * from people as c where c.id=2;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
+------+-------------+-----------+------+
3 rows in set (0.00 sec)
別名的使用場景,在設計復雜的查詢時,可以簡化查詢語句;
數據更新
mysql> select * from people;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 1 | teren | yeung | 18 |
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
| 3 | kobe | bryant | 18 |
+------+-------------+-----------+------+
5 rows in set (0.00 sec)
mysql> update people set age=20 where lastname='teren';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from people;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 1 | teren | yeung | 20 |
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
| 3 | kobe | bryant | 18 |
+------+-------------+-----------+------+
5 rows in set (0.00 sec)
添加、刪除或修改列
- alter table table_name add column_name type
mysql> alter table book add date date;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from book;
+----+------------+------------------+-------+------+
| id | bookname | author | price | date |
+----+------------+------------------+-------+------+
| 1 | MySQL | Monty Widenius | 20 | NULL |
| 2 | Python | Guido van Rossum | 30 | NULL |
| 3 | JavaScript | Brendan Eich | 30 | NULL |
+----+------------+------------------+-------+------+
3 rows in set (0.00 sec)
mysql> alter table book add remark varchar(20);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from book;
+----+------------+------------------+-------+------+--------+
| id | bookname | author | price | date | remark |
+----+------------+------------------+-------+------+--------+
| 1 | MySQL | Monty Widenius | 20 | NULL | NULL |
| 2 | Python | Guido van Rossum | 30 | NULL | NULL |
| 3 | JavaScript | Brendan Eich | 30 | NULL | NULL |
+----+------------+------------------+-------+------+--------+
3 rows in set (0.00 sec)
- alter table table_name drop column column_name;
mysql> alter table book drop column remark;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from book;
+----+------------+------------------+-------+------+
| id | bookname | author | price | date |
+----+------------+------------------+-------+------+
| 1 | MySQL | Monty Widenius | 20 | NULL |
| 2 | Python | Guido van Rossum | 30 | NULL |
| 3 | JavaScript | Brendan Eich | 30 | NULL |
+----+------------+------------------+-------+------+
3 rows in set (0.00 sec)
- 修改字段的數據類型
mysql> alter table book modify column date int;
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
刪除特定記錄
mysql> delete from people where lastname='kobe';
Query OK, 1 row affected (0.01 sec)
mysql> select * from people;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 1 | teren | yeung | 20 |
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
+------+-------------+-----------+------+
4 rows in set (0.00 sec)
復制數據表
mysql> create table animals select * from people;
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| animals |
| people |
+----------------+
2 rows in set (0.00 sec)
mysql> select * from animals;
+------+-------------+-----------+------+
| id | lastname | firstname | age |
+------+-------------+-----------+------+
| 1 | teren | yeung | 20 |
| 2 | totora | cat | 2 |
| 2 | hitachi | dog | 5 |
| 2 | hello_kitty | cat | 5 |
+------+-------------+-----------+------+
4 rows in set (0.00 sec)
SQL約束用于定義特定字段(屬性)的規則
- PRIMARY KEY
唯一標識數據庫表中的每條記錄;
具有NOT NULL和UNIQU的約束規則;
每個表都有且只有一個主鍵
mysql> create table book(id int primary key, bookname varchar(30) not null, author varchar(20) unique, price int);
mysql> insert into book values(1, 'MySQL','teren', 20);
Query OK, 1 row affected (0.01 sec)
mysql> insert into book values(1, 'Python','Guido van Rossum', 30);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into book values(2, 'Python','Guido van Rossum', 30);
Query OK, 1 row affected (0.00 sec)
mysql> select * from book;
+----+----------+------------------+-------+
| id | bookname | author | price |
+----+----------+------------------+-------+
| 1 | MySQL | Monty Widenius | 20 |
| 2 | Python | Guido van Rossum | 30 |
+----+----------+------------------+-------+
2 rows in set (0.00 sec)
當表已經創建,需要后續進行字段約束,可使用 ALTER ADD || ALTER DROP INDEX
mysql> select * from book;
+----+----------+------------------+-------+
| id | bookname | author | price |
+----+----------+------------------+-------+
| 1 | MySQL | Monty Widenius | 20 |
| 2 | Python | Guido van Rossum | 30 |
+----+----------+------------------+-------+
2 rows in set (0.00 sec)
mysql> alter table book add unique(price);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into book values(3, 'JavaScript', 'Brendan Eich', 30);
ERROR 1062 (23000): Duplicate entry '30' for key 'price'
mysql> alter table book drop index price;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into book values(3, 'JavaScript', 'Brendan Eich', 30);
Query OK, 1 row affected (0.00 sec)
mysql> select * from book;
+----+------------+------------------+-------+
| id | bookname | author | price |
+----+------------+------------------+-------+
| 1 | MySQL | Monty Widenius | 20 |
| 2 | Python | Guido van Rossum | 30 |
| 3 | JavaScript | Brendan Eich | 30 |
+----+------------+------------------+-------+
3 rows in set (0.00 sec)
- FOREIGN KEY
一個表中的FOREIGN KEY指向另一個表中的PRIMARY KEY
FOREIGN KEY技能表示表之間的聯系,又能防止這種連接被破壞;
mysql> create table book_detail(id int,book_id int, field varchar(20),primary key(id), foreign key(book_id) references book(id));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into book_detail values (1,1, 'DBMS');
Query OK, 1 row affected (0.00 sec)
mysql> select * from book_detail;
+----+---------+-------+
| id | book_id | field |
+----+---------+-------+
| 1 | 1 | DBMS |
+----+---------+-------+
1 row in set (0.00 sec)
刪除表
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| animals |
| book |
| book_detail |
| people |
+----------------+
4 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| book |
| book_detail |
| people |
+----------------+
3 rows in set (0.00 sec)
清空數據庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| demo |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test;
+------+----------+-----------+---------+
| id | lastname | firstname | address |
+------+----------+-----------+---------+
| 1 | teren | yeung | canton |
| 2 | kobe | bryant | America |
+------+----------+-----------+---------+
2 rows in set (0.00 sec)
mysql> truncate table test;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test;
Empty set (0.00 sec)
刪除數據庫
mysql> drop database test;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| demo |
+--------------------+
1 rows in set (0.00 sec)
SQL函數
SQL還有許多內建函數,用于查詢結果的簡單計算,如有需要可以查找相關資料,這里不詳細講解;
一般前端工程師只要做到認識數據庫到以上基本操作就行了,SQL那些查找的數據操作一般不用內建函數,而是采用特定高級語言的模塊進行處理;