1. 程式人生 > 其它 >MySQL資料庫知識點

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個位元組

decimal 字串形式的浮點數 金融計算的時候,一般用decimal

  (2)字串

  char 字串固定大小的 0-255

  varchar 可變字串  0-65535  常用的變數 String

  tinytext 微型文字 2^8-1

  text 文字串 2^16-1  儲存大文字

  (3)時間日期

  java.util.Date

  date YYY-MM-DD 日期格式

  time HH:mm:ss 時間格式

  datetime YYY-MM-DD HH:mm:ss 最常用的時間格式

  timestamp 時間戳 1970.1.1到現在的毫秒數 

  year年份的表示

  (4)null

  沒有值,未知

  注意:不能使用null進行運算,結果為null

3.資料庫的欄位屬性

  (1)Unsigned

  無符號的整數

  聲明瞭該列不能宣告為負數

  (2)zerofill

  0填充

  不足的位數,使用0來填充,int(3), 5 ----005

  (3)自增

  通常理解為自增,自動在上一條記錄的基礎上+1(預設)

  通常用來設計唯一的主鍵--index,必須是整數型別

  可以自定義設計主鍵自增的起始值和步長

  (4)非空:NUll not null

  假設設定為not null,如果不給它賦值,就會報錯

  NUll,如果不填寫值,預設就是null

  (5)預設

  設定預設的值

  sex,預設值為男

4.資料表的型別

  關於資料庫引擎

  INNODB 預設使用

  MYISAM 早些年使用

MYISAMINNODB
事務支援 不支援 支援
資料行鎖定 不支援 支援
外來鍵約束 不支援 支援
全文索引 支援 不支援
表空間的大小 較小 較大,約為2倍

  常規使用操作

  MYISAM 節約空間 速度較快

  INNODB 安全性高,事務的處理,多表多使用者操作

  在物理空間存在的位置

  所有的資料庫檔案都存在data目錄下,一個資料夾就對應一個數據庫

  本質還是檔案的儲存

5.刪除修改表

  (1)修改

  修改表名 :alter table 舊錶名 rename as 新表名

  例子:alter table teacher rename as teacher1

  增加表的欄位:alter table 表名 add 欄位名 列屬性

  例子:alter table teacher1 add age int(11)

  修改表的欄位:

  1)修改約束:alter table 表名 modify 欄位名 列屬性[]

  例子:alter table teacher1 modify age varchar(11)

  2)欄位重新命名:alter table 表名 change 舊名字 新名字 列屬性[]

  例子:alter table teacher1 change age age1 int(1)

  刪除表的欄位:alter table 表名 drop 欄位名

  例子:alter table teacher1 drop age1

  (2)刪除

  刪除表(如果表存在再刪除):drop table if exists 表名

  例子:drop table if exists teacher1

6.DML語言

  資料庫操作語言

  insert、updata、delete

  (1)insert(新增)

--插入語句
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,....) 假設a在a1,或者a2...其中的一個值中,結果為真
 --模糊查詢
 --查詢姓劉的同學
 --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

  (5)排序

--排序:升序 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)];
    --指定查詢的記錄從哪條至哪條