mySql 資料庫
SQL 是用于訪問和處理資料庫的標準的計算機語言,提供了安全機制、便于通過Sql陳述句對資料直接進行操作
MySQL 是最流行的關系型資料庫管理系統,在 WEB 應用方面 MySQL 是最好的 RDBMS(Relational Database Management System:關系資料庫管理系統)應用軟體之一,
什么是資料庫?
資料庫(Database)是按照資料結構來組織、存盤和管理資料的倉庫,
每個資料庫都有一個或多個不同的 API 用于創建,訪問,管理,搜索和復制所保存的資料,
我們也可以將資料存盤在檔案中,但是在檔案中讀寫資料速度相對較慢,
所以,現在我們使用關系型資料庫管理系統(RDBMS)來存盤和管理大資料量,所謂的關系型資料庫,是建立在關系模型基礎上的資料庫,借助于集合代數等數學概念和方法來處理資料庫中的資料,
RDBMS 即關系資料庫管理系統(Relational Database Management System)的特點:
- 1.資料以表格的形式出現
- 2.每行為各種記錄名稱
- 3.每列為記錄名稱所對應的資料域
- 4.許多的行和列組成一張表單
- 5.若干的表單組成database
需要的軟體有:
MySQL服務
navicat
RDBMS 術語
- 資料庫: 資料庫是一些關聯表的集合,
- 資料表: 表是資料的矩陣,在一個資料庫中的表看起來像一個簡單的電子表格,
- 列: 一列(資料元素) 包含了相同型別的資料, 例如郵政編碼的資料,
- 行:一行(=元組,或記錄)是一組相關的資料,例如一條用戶訂閱的資料,
- 冗余:存盤兩倍資料,冗余降低了性能,但提高了資料的安全性,
- 主鍵:主鍵是唯一的,一個資料表中只能包含一個主鍵,你可以使用主鍵來查詢資料,
- 外鍵:外鍵用于關聯兩個表,
- 復合鍵:復合鍵(組合鍵)將多個列作為一個索引鍵,一般用于復合索引,
- 索引:使用索引可快速訪問資料庫表中的特定資訊,索引是對資料庫表中一列或多列的值進行排序的一種結構,類似于書籍的目錄,
- 參照完整性: 參照的完整性要求關系中不允許參考不存在的物體,與物體完整性是關系模型必須滿足的完整性約束條件,目的是保證資料的一致性,

- 表頭(header): 每一列的名稱;
- 列(col): 具有相同資料型別的資料的集合;
- 行(row): 每一行用來描述某條記錄的具體資訊;
- 值(value): 行的具體資訊, 每個值必須與該列的資料型別相同;
- 鍵(key): 鍵的值在當前列中具有唯一性,
資料庫可視化工具navicat的基本使用
-
打開和關閉資料庫連接
-
創建用戶、設定用戶權限
-
新建資料庫、新建表、設計表(設定欄位型別)、設定Id自增、日期自動插入
- 示例:
-
新建
users用戶表,表欄位為:id、name、age、gender、address、isdel -
Id自增:設定為主鍵,勾選
自動遞增 -
主鍵:是用來標識資料表中每一行資料的唯一性的,確保獨一無二性,
使用Sql陳述句對資料庫執行基本操作
查詢 :
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 平均值
order by 函式
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是降序排列
limit 函式 : 主要用于分頁場合
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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/228965.html
標籤:MySQL
上一篇:Mysql詳解--知識整理
下一篇:資料庫
