一哥谷、 建表Tables
column: 列名稱
dateType:這一列的類型(int整數(shù)類型、varchar字符串類型 varchar(20)值的最大長度是20瓶堕、longtext可以存儲很大數(shù)據(jù)隘道,最多可存儲4G大小、bigint用來存儲更大數(shù)據(jù)的整數(shù)類型)
pk主鍵:這一列的值不能重復(fù)(主鍵的意思就是保證不能重復(fù))如何保證不能重復(fù)呢郎笆?
AI自動增加:設(shè)置為Y
nn不為空:這一列的值是不是允許為空(Y是不允許為空)
Default: 默認值
修改表:圖形界面表名右鍵選擇Alter Table
二谭梗、操作表(增刪改查)
1.想要操作數(shù)據(jù)庫,首先先use這個數(shù)據(jù)庫宛蚓,然后執(zhí)行這個語句
use 數(shù)據(jù)庫名;
2.顯示所有數(shù)據(jù)庫的表
show tables;
注釋的寫法 --
-- show tables;
3.給users表增加數(shù)據(jù) insert into 表名 (列名) values (數(shù)據(jù));
insert into users (username, `password`, realname) values ('zhangsan','123','張三');
- 批量插入:
insert into test_table (name, age, sex) values ('張三', 18, '男'), ('李四',16,'女'), ('王五',19,'女')
- 查詢
- 查詢?nèi)?/li>
select * from users;
出于性能考慮激捏,一般情況下,避免使用*
- 查詢部分
select 列名 from 表
select id, username from users;
- 加查詢條件 (后面加
where
條件)
select * from users where username='zhangsan';
- 多個查詢條件(
where
條件1and
條件2)
(where
條件1or
條件2)
select * from users where username='zhangsan' and `password`='123';
- 模糊查詢
like
select * from users where username like '%zhang%';
- 排序
order by
默認正序凄吏,加desc
后是倒序
select * from users where password like '%1%' order by id desc;
- 更新
update
表名set
值where
條件
update users set realname='李四' where username='lisi';
若執(zhí)行update報錯远舅,則需執(zhí)行下面的語句闰蛔,執(zhí)行完刪除,重新執(zhí)行剛才的update語句
SET SQL_SAFE_UPDATES = 0;
6.刪除 delete from 表名 where 條件
一般刪除是軟刪除图柏,不會直接使用delete
序六,而是使用update
- 比如加一列state狀態(tài),是1的話存在爆办,0的話刪除
update users set state='0' where username='lisi';
select * from users where state='1';
- 如果想要查詢state不等于0难咕,使用
<>
select * from users where state <> '0';
三课梳、nodejs連接數(shù)據(jù)庫
- 下載mysql包
npm i mysql -S
- 引用
const mysql = require('mysql')
3.配置并連接數(shù)據(jù)庫
// 創(chuàng)建鏈接對象
const con = mysql.createConnection({
// 配置
})
// 開始連接
con.connect()
4.執(zhí)行sql
const sql = `select * from users`;
con.query(sql, (err, result) => {
if (err) {
return
}
console.log(result)
})
四距辆、工作中遇到的一些sql 關(guān)鍵字的意思:
as
作為(列或表的)別名(可省略)
Coalesce(A,B)
相當(dāng)于三目表達式,如果A不為null返回A暮刃,否則如果B不為null返回B跨算,否則返回null
distinct
去重
in
查詢滿足指定范圍內(nèi)的數(shù)據(jù)
select column1, column2 from table where column1 in (value1, value2, value3)
between .... and ...
:查詢某個范圍內(nèi)的值
select * from table where column between value1 and value2
to_char(A, B)
把A轉(zhuǎn)成B格式的字符串
inner join ...on...
根據(jù)on的條件將兩個表鏈接起來,當(dāng)兩表同時滿足鏈接的條件椭懊,滿足的部分才會列出
left join
返回左表的全部行和右表滿足on條件的行
// Table A是左邊的表诸蚕,Table B是右邊的表
select * from Table A left join Table B
on Table A.id=Table B.id
right join
返回右表的全部行和左表滿足on條件的行
// Table A是左邊的表,Table B是右邊的表
select * from Table A right join Table B
on Table A.id=Table B.id
不等于 <> 或者!=
like +
通配符字符串:模糊查詢
mysql通配符有兩個:
%
:表示0個或多個字符(任意個字符)
_
: 表示一個字符
order by
子句進行排序 氧猬,默認是升序
order by 字段名 asc
(升序
order by 字段名 desc
(降序
組合排序:就是先按第一個字段進行排序背犯,如果第一個字段相同,才按第二個字段進行排序盅抚,依次類推
select * from 表名 where 字段= 值 order by 字段名1 asc漠魏,字段名2 desc;
聚合函數(shù)
:
之前所做的查詢都是橫向查詢妄均,它們都是根據(jù)條件一行一行進行判斷柱锹。而使用聚合函數(shù)查詢是縱向查詢,它是對一列的值進行計算丰包,然后返回一個結(jié)果值禁熏。另外聚合函數(shù)會忽略空值
五個聚合函數(shù):count sum max min avg
聚合函數(shù)寫在select后字段名的地方:
Select 字段名 from 表名
Select count(age) from 表名;
// 統(tǒng)計所有數(shù)據(jù)個數(shù):
select count(*) from 表名邑彪;
分組查詢
:
使用group by
語句對查詢信息進行分組瞧毙,將分組字段結(jié)果中相同內(nèi)容作為一組;
Select 字段1寄症,字段2 from 表名 group by 分組字段 having 條件升筏;
Select * from 表名 group by sex;
這句話會將sex相同的數(shù)據(jù)作為一組,但是會返回每組的第一條瘸爽,沒有任何意義您访;
分組的目的就是為了統(tǒng)計,一般分組查詢會跟聚合函數(shù)一起搭配使用剪决;
分組后聚合函數(shù)不是操作所有數(shù)據(jù)灵汪,而是操作一組數(shù)據(jù)檀训。
select sum(score), sex from 表名 group by sex;
注意:當(dāng)我們使用某個字段分組,在查詢的時候也需要將這個字段查詢出來享言,否則看不到數(shù)據(jù)屬于哪個組的
where是分組前的條件峻凫,分組后的過濾條件使用having;
Select count(*), sex from hero where age < 30 group by sex having count(*) > 2;
having和where的區(qū)別:
- having在分組后對數(shù)據(jù)進行過濾
- where是在分組前對數(shù)據(jù)進行過濾
- having后面可以使用聚合函數(shù)
- where后面不可以使用聚合函數(shù)
limit
:限制查詢記錄的條數(shù) limit offset, length 或者 limit length(只適用第一頁);
offset
指的是偏移量,可以認為是跳過的記錄數(shù)量览露,默認為0
length
是指需要顯示的總記錄數(shù)
select 字段列表 [as 別名] from 表名 [where子句] [group by子句] [having子句] [order by子句] [limit子句];
case when ... then ... else ... End
case when
條件A then
A when
條件B then
B when
條件C then
C else
D end
相當(dāng)于if else語句
select name, (case when score < 60 then 'fail' when score >=60 and score < 80 then 'pass' else 'fine' end) as grade from table
sql中除了數(shù)值類型荧琼,其他字段類型的值必須使用引號引起,建議單引號
desc
表名:查看表結(jié)構(gòu)
子查詢
就是在主select 里面套select from, 相當(dāng)于從一個結(jié)果集里面去查詢差牛。子查詢一般都寫在括號里面命锄,然后在括號外面用as接收這個子查詢出來的值