MySQL資料庫知識點
1.MySQL資料庫的常用命令
(1)啟動MySQL服務
net start mysql
(2)連線資料庫
mysql -u root -p123456
(3)建立資料庫
create database 資料庫名
(4)檢視所有的資料庫
show databases
(5)切換資料庫
use 資料庫名
(6)刪除資料庫
drop database [if exists] 資料庫名
(7)建立資料庫表
CREATE TABLE [IF NOT EXISTS] '表名' ( '欄位名' 列型別 [屬性][索引][註釋], '欄位名' 列型別 [屬性][索引][註釋], ... '欄位名' 列型別 [屬性][索引][註釋], [PRIMARY KEY ('欄位名')] )[表型別][字串設定][註釋]
其中[表型別]ENGINE=INNODB [字串設定]DEFAULT CHARSET=utf8
[]表示可選操作
(8)檢視資料庫表
show tables
(9)退出連線
exit
(10)終止MySQL服務
net stop mysql
2.資料庫的列型別
(1)數值
tinyint 十分小的資料 1個位元組
smallint 較小的資料 2個位元組
mediumint 中等大小的資料 3個位元組
int 標準的整數 4個位元組 常用的
bigint 較大的資料 8個位元組
float 浮點數 4個位元組
double 浮點數 8個位元組
varchar 可變字串 0-65535 常用的變數 String
tinytext 微型文字 2^8-1
text
datetime YYY-MM-DD HH:mm:ss 最常用的時間格式
timestamp 時間戳 1970.1.1到現在的毫秒數
通常用來設計唯一的主鍵--index,必須是整數型別
假設設定為not null,如果不給它賦值,就會報錯
INNODB 預設使用
MYISAM 早些年使用
MYISAM | INNODB | |
---|---|---|
事務支援 | 不支援 | 支援 |
不支援 | 支援 | |
外來鍵約束 | 不支援 | 支援 |
全文索引 | 支援 | 不支援 |
表空間的大小 | 較小 |
常規使用操作
MYISAM 節約空間 速度較快
INNODB 安全性高,事務的處理,多表多使用者操作
在物理空間存在的位置
例子:
1)修改約束:alter table 表名 modify 欄位名 列屬性[]
例子:alter table teacher1 modify age varchar(11)
2)欄位重新命名:alter table 表名 change 舊名字 新名字 列屬性[]
例子:
例子:
例子:
--插入語句 insert into ‘表名’(‘欄位名1’,‘欄位名2’,...) values(‘值1’),(‘值2’),... --例:insert into ‘grade’ (‘gradename’) values('大四')
--一般寫插入語句,一定要資料和欄位一一對應
--插入多個欄位
insert into 'grade'('gradename')
values('大二'),('大三')
insert into 'student'('name','pwd','sex')
values('張三','123456','男'),('李四','aaaa','男')
(2)updata(修改)
--修改學員的名字,帶了條件 updata 'student' set 'name'='張三' where id=1; --不指定條件的情況下,會改動所有表 updata 'student' set 'name'='狂神'; --語法 updata '表名' set '屬性'=‘new_value’,... [where 條件] --修改多個屬性,用,隔開 updata 'student' set 'name'='狂神',‘email’='[email protected]' where id=1;
條件:where 子句 運算子 id等於某個值,大於某個值,在某個區間內修改
含義 | 範圍 | 結果 | |
---|---|---|---|
= | 等於 | ||
<>或!= | 不等於 | ||
> | |||
< | |||
<= | |||
>= | |||
between...and... | 在某個範圍內 | [2,5] | |
and | &&與 | ||
or | ||或 |
--通過多個條件定位資料 updata 'student' set 'name'='長江七號' where 'name'='狂神' and 'sex'=‘女’; --修改的新值,可以是一個具體的值,也可以是一個變數 updata 'student' set 'birthday'=current_time where 'name'='長江七號'
and 'sex'=‘女’;
(3)delete(刪除)
語法:
delete from '表名' [where 條件]
--刪除資料,沒有條件,會全部刪除 delete from 'student' --刪除指定資料 delete from 'student' where id=1;
truncate命令
作用:完全清空一個數據庫表,表的結構和索引約束不會變
語法:truncate table ‘表名’
delete和truncate的區別
相同點:都能刪除資料,都不會刪除表的結構
不同點:truncate重新設定自增列,計數器會歸零
truncate不會影響事務
delete不會影響自增
7.DQL語言
資料查詢語言
所有的查詢操作都用它 select
簡單的查詢,複雜的查詢都能做
資料庫中最核心的語言,最重要的語句
(1)指定查詢欄位
--查詢全部的學生 select 欄位 from 表 select * from student --查詢指定欄位 select 'StudentNo','StudentName' from student --別名, 給結果取一個名字 as 可以給欄位起別名,也可以給表起別名 select 'StudentNo' as 學號,'StudentName' as 學生姓名 from student as s --函式 concat(a,b)(連線字串) select concat('姓名:',StudentName) as 新名字 from student
語法:
select 欄位,... from 表名
有的時候,列名字不是那麼見名知意,我們會起別名,欄位名 as 新名字或表名 as 新名字
(2)distinct(去重)
作用:去除查詢出來的結果中重要的資料,只顯示一條
--查詢一下有哪些學生參加了考試,成績 select * from result --查詢全部的考試成績 select 'StudentNo' from result --查詢有哪些學生參加了考試 select distinct 'StudentNo' from result --發現重複資料,去重
select version() --查詢系統版本(函式) select 100*3-1 as 計算結果 --用來計算表示式(表示式) select @@auto_increment --查詢自增的步長(變數) --學員考試成績+1分檢視 select 'StudentNo','StudentResult'+1 as '提分後' from result 資料庫中的表示式:文字值,列,null, 函式,計算表示式,系統變數... select '表示式' from 表名
(3)where條件子句
作用:檢索資料中符合條件的值
搜尋的條件由一個或者多個表示式組成!結果為布林值
邏輯運算子
語法 | 描述 | |
---|---|---|
and && | a and b a&&b | 邏輯與 |
or || | a or b a||b | 邏輯或 |
not ! | not a !a | 邏輯非 |
--where select 'StudentNo','StudentResult' from result --查詢考試成績在95-100分之間的 select 'StudentNo','StudentResult' from result where StudentResult>=95 and studentResult<=100 --and && select 'studentNo','studentresult' from result where studentResult>=95 && studentResult<=100 --模糊查詢 select 'studentNo','studentResult' from result where studentResult between 95 and 100 --除了1000號學生以外的同學的成績 select 'studentNo','studentName' from result where studentNo!=1000 select 'studentNo','studentName' from result where not studentNo=1000
模糊查詢:比較運算子
語法 | 描述 | |
---|---|---|
is null | a is null | |
is not null | a is not null | |
between | a between b and c | 若a在b和c之間,結果為真 |
like | a like b | SQL匹配,如果a 匹配b,則結果為真 |
in | a in(a1,a2,a3,....) |
--模糊查詢 --查詢姓劉的同學 --like 結合 %(代表0到任意個字元) _(一個字元) select 'studentNo','studentName' from student where studentName like '劉%' --查詢姓劉的同學,名字後面只有一個字元 select 'studentNo','studentName' from student where studentName like '劉_' --查詢名字中間有嘉字的同學 %嘉% select 'studentNo','studentName' from student where studentName like '%嘉%' --in(具體的一個或者多個值) --查詢1001,1002,1003號學員 select 'studentNo','studentName' from student where studentNo in(1001,1002,1003) --查詢學生 select 'studentNo','studentName' from student where address in ('安徽','河南洛陽'); --null not null --查詢地址為空的學生 為空 select 'studentNo','studentName' from student where address='' or address is null --查詢有出生日期的同學 不為空 select 'studentNo','studentName' from student where BornDate is not null --查詢沒有出生日期的同學,為空 select 'studentNo','studentName' from student where BornDate is null
(4)聯表查詢
--連結串列查詢 join --查詢參加了考試的同學(學號,姓名,科目編號,分數) select * from student select * from result /*思路 1.分析需求,分析查詢的欄位來自那些表,(多個表——連線查詢) 2.確定使用那種連線查詢 7種 確定交叉點(兩個表中那個資料是相同的) 判斷條件:學生表中的studentNo=成績表中的studentNo */ --內連線 select s.studentNo,studentName,subjectNO,studentResult from student as s inner join result as r on s.studentNo=r.studentNo --right join右連線 select s.studentNo,studentName,subjectNO,studentResult from student as s right join result as r on s.studentNo=r.studentNo --left join 左連線 select s.studentNo,studentName,subjectNO,studentResult from student as s left join result as r on s.studentNo=r.studentNo --查詢缺考的同學 select s.studentNo,studentName,subjectNO,studentResult from student as s left join result as r on s.studentNo=r.studentNo where studentResult is null --思考題(查詢參加考試的同學資訊,學號,學生姓名,科目名,分數) /*思路 1.分析需求,分析查詢的欄位來自那些表,student, result , subject(連線查詢) 2.確定使用那種連線查詢 確定交叉點 判斷條件 */ select s.studentNo,studentName,subjectName,studentResult from student as s right join result as r on r.studentNo=s.studentNo inner join subject as sub on r.subjectNo=sub.subjectNo
--排序:升序 asc, 降序 desc --order by 通過那個欄位排序 --查詢的結果根據成績降序排序 select s.studentNo,studentName,subjectName,studentResult from student as s inner join result as r on r.studentNo=s.studentNo inner join subject as sub on r.subjectNo=sub.subjectNo where subjectName='資料庫結構-1' order by studentResult desc
select完整的語法
select [all | distinct] (*| table.* | [table.field1[as alias1][.table.field2][as alias2]][...]) from table_name[as table_alias] [left|right|inner join table_name] --聯表查詢 [where....] --指定結果需滿足的條件 [group by...] --指定結果按照哪幾個欄位來分組 [having] --過濾分組的記錄必須滿足的次要條件 [order by...] --指定查詢記錄按什麼排序 [limit ([offset.]row_count | row_countOFFSET offset)]; --指定查詢的記錄從哪條至哪條