SQl 資料庫 使用介紹
阿新 • • 發佈:2020-12-02
什麼是資料庫?
資料庫(Database)是按照資料結構來組織、儲存和管理資料的倉庫。
每個資料庫都有一個或多個不同的 API 用於建立,訪問,管理,搜尋和複製所儲存的資料。
我們也可以將資料儲存在檔案中,但是在檔案中讀寫資料速度相對較慢。
所以,現在我們使用關係型資料庫管理系統(RDBMS)來儲存和管理大資料量。所謂的關係型資料庫,是建立在關係模型基礎上的資料庫,藉助於集合代數等數學概念和方法來處理資料庫中的資料。
RDBMS 即關係資料庫管理系統(Relational Database Management System)的特點:
- 1.資料以表格的形式出現
- 2.每行為各種記錄名稱
- 3.每列為記錄名稱所對應的資料域
- 4.許多的行和列組成一張表單
- 5.若干的表單組成database
需要的軟體有:
MySQL服務
navicat
RDBMS 術語
- 資料庫:資料庫是一些關聯表的集合。
- 資料表:表是資料的矩陣。在一個數據庫中的表看起來像一個簡單的電子表格。
- 列:一列(資料元素) 包含了相同型別的資料, 例如郵政編碼的資料。
- 行:一行(=元組,或記錄)是一組相關的資料,例如一條使用者訂閱的資料。
- 冗餘:儲存兩倍資料,冗餘降低了效能,但提高了資料的安全性。
- 主鍵:主鍵是唯一的。一個數據表中只能包含一個主鍵。你可以使用主鍵來查詢資料。
- 外來鍵:外來鍵用於關聯兩個表。
- 複合鍵:複合鍵(組合鍵)將多個列作為一個索引鍵,一般用於複合索引。
- 索引:使用索引可快速訪問資料庫表中的特定資訊。索引是對資料庫表中一列或多列的值進行排序的一種結構。類似於書籍的目錄。
- 參照完整性:參照的完整性要求關係中不允許引用不存在的實體。與實體完整性是關係模型必須滿足的完整性約束條件,目的是保證資料的一致性。
- 表頭(header): 每一列的名稱;
- 列(col): 具有相同資料型別的資料的集合;
- 行(row): 每一行用來描述某條記錄的具體資訊;
- 值(value): 行的具體資訊, 每個值必須與該列的資料型別相同;
- 鍵(key): 鍵的值在當前列中具有唯一性。
-
開啟和關閉資料庫連線
-
建立使用者、設定使用者許可權
-
新建資料庫、新建表、設計表(設定欄位型別)、設定Id自增、日期自動插入
- 示例:
-
新建
users
使用者表,表字段為:id、name、age、gender、address、isdel -
Id自增:設定為主鍵,勾選
自動遞增
-
主鍵:是用來標識資料表中每一行資料的唯一性的,確保獨一無二性。
1 -- 查詢的SQL語句 查詢出來的資料都是結果集 2 -- 語法: select * from 表名 where 條件 3 -- && 用 and 代替 || 用 or 代替 ! 用 not 代替 4 select * from stuInfo; -- 表示查詢所有的資料資訊 5 select id,name,gender from stuinfo; -- 根據指定的欄位來查詢 6 select * from stuinfo where id = 2; -- 查詢id為2的那一條資料 7 select name,age from stuinfo where id = 3; -- 查詢id為3的那條資料中的name age id 8 select * from stuinfo where id =3 or id= 5; -- 查詢id為3 和5 的那兩條資料 9 select * from stuinfo where id in (2,3,4,5,7); -- 查詢id為 2 3 4 5 7 的那些資料
1 -- 增加語句 2 -- 語法: insert [into] 表名 (欄位1,欄位2...) values (值1,值2...); 3 insert into stuinfo (id,name,gender,age,phone) values (NULL,'張三','男',20,'1302012345'); 4 insert into stuinfo (name,age,gender) values ('李四',21,'男'); 5 insert into stuinfo values (null,"王五","男",21); -- 如果直接寫值的話,必須和欄位匹配起來,這樣寫會報錯的 6 insert into stuinfo values (null,"王五","男",21,"1234666"); -- 沒有欄位的時候,如果要是直接新增值的話,必須和欄位對應,不能缺少
修改 :
-- 修改語句 -- 語法: UPDATE 表名 set 欄位1= 值1,欄位2 = 值2... where 條件 UPDATE stuinfo set gender = '男'; -- 如果後面沒有跟條件的話,則表示將表中所有的資料中的gender都改成了男 UPDATE stuinfo set gender = '女' WHERE id = 3; -- 將id為3的那條資料中的gender修改為“女” UPDATE stuinfo set gender = '女' WHERE age = 21 and name = '李四'; -- 將資料表中名字叫"李四"
刪除 :
1 -- 刪除語句 2 -- 語法: DELETE FROM 表名 where 條件 3 DELETE FROM stuinfo where id = 7; -- 刪除id=7的資料 4 DELETE FROM stuinfo where id = 2 or id = 8; --刪除id=2 和 id=8 的資料 5 DELETE FROM stuinfo where id in (4,5,10); -- 刪除id為 4 5 10 的資料 6 DELETE FROM stuinfo WHERE gender ='男' AND age = 20; -- 通過多條件來刪除資料 and 在這裡表示並且的關係 7 DELETE FROM stuinfo WHERE id > 10; -- 還可以根據id的特性 刪除id大於10的資料 8 DELETE FROM stuinfo -- 不寫條件 則會刪除庫中所有的資料
mySQL 常用函式:
COUNT()函式
SELECT count(*) FROM stuinfo; -- count用來統計所有的資料條數 會根據裡面的引數來進行統計 SELECT count(id) FROM stuinfo; -- 根據id來統計具體的資料條數 SELECT count(phone) FROM stuinfo; -- 根據phone欄位來統計具體的條數,如果資料為空則會忽略
MAX, MIN, AVG 函式
SELECT MAX(age) FROM stuinfo; -- 查詢年齡中的最大值 SELECT MIN(age) FROM stuinfo; -- 查詢年齡中的最小值 SELECT avg(age) FROM stuinfo; -- 查詢年齡中的平均值 average 平均值
1 SELECT * FROM stuinfo order by id; -- order by是用來排序的,預設是以升序進行排序 asc就是升序排序 2 SELECT * FROM stuinfo ORDER BY id desc; 3 SELECT * FROM stuinfo ORDER BY age desc; -- desc是降序排列
1 SELECT * FROM stuinfo ORDER BY id; 2 select * from stuinfo limit 3; -- 取結果集中的前3條 3 SELECT * FROM stuinfo ORDER BY id limit 3; 4 5 -- limit n (索引) m (數量) 資料表中資料的索引預設也是從0開始的,索引只會對剩下的資料進行排序 6 SELECT * FROM stuinfo ORDER BY id LIMIT 3, 3; 7 8 -- 還可以這樣來實現一個分頁 limit count(數量) offset index(索引); 9 SELECT * FROM stuinfo ORDER BY id limit 3 offset 0; -- 從索引為0的位置向下查詢3條 10 SELECT * FROM stuinfo ORDER BY id limit 3 offset 3; -- 從索引為3的位置向下查詢3條 11 SELECT * FROM stuinfo ORDER BY id limit 3 offset 6; -- 從索引為6的位置向下查詢3條 12 13 -- 經過上面的規律可以總結如下: 14 int pageCount = 3 ; -- 每頁顯示的條數 15 int pageSize = 1 ; -- 當前的頁碼 1 第1頁 2 第2頁 3 第3頁 16 17 SELECT * FROM stuinfo ORDER BY id LIMIT pageCount offset (pageSize - 1)* pageCount;
Node.js 連線 MySQL
安裝驅動
在專案跟目錄下安裝 :$ cnpm install mysql
連線資料庫
let mysql = require('mysql'); let connection = mysql.createPool({ host : 'localhost', // user : 'root', password : '123456', database : 'test' }); connection.connect(); connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results[0].solution); });
資料庫連線引數說明:
資料庫操作( CURD )
查詢資料
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', port: '3306', database: 'test' }); connection.connect(); var sql = 'SELECT * FROM websites'; //查 connection.query(sql,function (err, result) { if(err){ console.log('[SELECT ERROR] - ',err.message); return; } console.log('--------------------------SELECT----------------------------'); console.log(result); console.log('------------------------------------------------------------\n\n'); }); connection.end();
插入資料
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', port: '3306', database: 'test' }); connection.connect(); var addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(0,?,?,?,?)'; var addSqlParams = ['菜鳥工具', 'https://c.runoob.com','23453', 'CN']; //增 connection.query(addSql,addSqlParams,function (err, result) { if(err){ console.log('[INSERT ERROR] - ',err.message); return; } console.log('--------------------------INSERT----------------------------'); //console.log('INSERT ID:',result.insertId); console.log('INSERT ID:',result); console.log('-----------------------------------------------------------------\n\n'); }); connection.end();
更新資料
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', port: '3306', database: 'test' }); connection.connect(); var modSql = 'UPDATE websites SET name = ?,url = ? WHERE Id = ?'; var modSqlParams = ['菜鳥移動站', 'https://m.runoob.com',6]; //改 connection.query(modSql,modSqlParams,function (err, result) { if(err){ console.log('[UPDATE ERROR] - ',err.message); return; } console.log('--------------------------UPDATE----------------------------'); console.log('UPDATE affectedRows',result.affectedRows); console.log('-----------------------------------------------------------------\n\n'); }); connection.end();
刪除資料
ar mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', port: '3306', database: 'test' }); connection.connect(); var delSql = 'DELETE FROM websites where id=6'; //刪 connection.query(delSql,function (err, result) { if(err){ console.log('[DELETE ERROR] - ',err.message); return; } console.log('--------------------------DELETE----------------------------'); console.log('DELETE affectedRows',result.affectedRows); console.log('-----------------------------------------------------------------\n\n'); }); connection.end();