主頁 > 知識庫 > 三十分鐘MySQL快速入門(圖解)

三十分鐘MySQL快速入門(圖解)

熱門標簽:Linux服務器 網(wǎng)站排名優(yōu)化 呼叫中心市場需求 AI電銷 鐵路電話系統(tǒng) 百度競價排名 地方門戶網(wǎng)站 服務外包

一、MySQL安裝

MySQL的下載

http://dev.mysql.com/downloads/mysql/

MySQL版本選擇

MySQL功能自定義選擇安裝

功能自定義選擇

路徑自定義選擇

設置root用戶密碼

安裝完成,點擊MySQL Workbench 6.3 CE進入MySQL客戶端

二、SQL基礎

SQL語句分類

1.DDL(Data Definition Languages)語句:數(shù)據(jù)定義語言,這些語句定義了不同的數(shù)據(jù)段、數(shù)據(jù)庫、表、列、索引等數(shù)據(jù)庫對象。常用的語句關鍵字主要包括create/drop/alter

2.DML(Data Manipulation Language)語句:數(shù)據(jù)操縱語句,用于添加、刪除、更新和查詢數(shù)據(jù)庫記錄,并檢查數(shù)據(jù)完整性。常用的語句關鍵字主要包括 insert/delete/update/select等

3.DCL(Data Control Language)語句:數(shù)據(jù)控制語句,用于控制不同數(shù)據(jù)段直接的許可和訪問級別的語句。這些語句定義了數(shù)據(jù)庫、表、字段、用戶的訪問權限和安全級別。主要的語句關鍵字包括grant/revoke等

DDL語句(涉及表的定義、結構的修改)

一、create語句

Query Ok代表語句執(zhí)行成功

1 row affected代表數(shù)據(jù)庫一行收到影響

0.01 sec代表操作執(zhí)行的時間

create table student(
SID int not null auto_increment,
sNo int ,
sName varchar(50) not null,
primary key(SID)
);

1.查看系統(tǒng)中都存在哪些數(shù)據(jù)庫(show databases;)

2.在查看系統(tǒng)中已有的數(shù)據(jù)庫后,可以用(use dbname)選擇對應的數(shù)據(jù)庫

3.在選擇對應的數(shù)據(jù)庫后,查詢該數(shù)據(jù)庫下面的所有的表(show tables)

二、刪除數(shù)據(jù)庫

刪除數(shù)據(jù)庫的語法:drop databse dbname;

三、創(chuàng)建表

語法:create table tablename(column_name_1 column_type_1 constraints,column_name_2 column_type2 constrationts)

mysql的表名是以目錄形式存儲在磁盤上,表名的字符可以是任何目錄名允許的字符。

column_name是列名

column_type是列的數(shù)據(jù)類型

constrationts是列的約束條件

1.查看表定義:desc tablename

2.查看創(chuàng)建表的SQL語句:show create table tablename

四、刪除表

刪除表的語法:drop table tablename;

五、修改表

aleter 語法 | 說明

---|---
alter table tablename modify columnname newColumnType | 修改表字段的類型(==modify 不能更改字段名稱==)
alter table tablename add newColumnname newColumnType| 增加表字段
alter table tablename drop oldCloumnname|刪除表字段
alter table tablename change oldColumname newColumnname newColumntype|修改字段的名稱及類型
alter table tablename rename (to) newtablename|修改表名稱

修改字段的排列順序

在alter的語法后面都有[first\after columnname]可選項

alter table user add address varchar(20) first ;
alter table user add age int after name ; 

DML(對數(shù)據(jù)庫表記錄進行操作,增(insert)刪(delete)改(update)查(select))

1.insert語句

語法:

插入一條:insert into tablename(columnname1,columnname2...)values(val1,val2...);

插入多條:insert into tablename(columnname1,columnname2...)values(val1,val2...),(val1,val2...);

2.update語句

語法:update tablename set columnname=value [where condition]

如果使用MySQL Workbench,update語句不加where條件的會執(zhí)行錯誤,需要如下圖設置取消設置:

-3.delete語句

語法:delete from tablename where condition

-4.select語句

語法:select * from tablename [where condition]

5.表連接

1.內(nèi)連接(僅選出兩張表中互相匹配的數(shù)據(jù))

select cno,cname,sname from student inner join course on cno=sno;
select cno,cname,sname from student,course where cno=sno;

2.外連接

外連接又區(qū)分:

1.左連接(left join):包含左邊表的所有記錄,右邊沒有的為Null

2.右連接(right join):包含右邊表的所有記錄,左邊沒有的為null

6.子查詢

-7.記錄聯(lián)合

語法:

select * from t1 union all select * from t2;
select * from t1 union select * from t2;

union all與union的區(qū)別:

union all是把結果集直接合并在一起,而union是將union all后的結果進行一次distinct,去除重復后的結果

DCL語句(DCL語句主要是dba用來管理系統(tǒng)中的對象權限)

grant與revoke

三、MySQL支持的數(shù)據(jù)類型

數(shù)值類型

MySQL支持類型后面的小括號指定顯示寬度,例如:int(5)表示當數(shù)值寬度小于5的時候在數(shù)字前面填滿寬度,如果不顯示指定寬度則默認為int(11)。如果插入的數(shù)據(jù)大于這個數(shù)值寬度,對實際的插入值是沒有影響的,是按照int類型的實際大小進行的。

create table valuetype(
age int,
age1 int
)
insert into valuetype(age,age1)values(1,2);//這時候數(shù)據(jù)庫就顯示1
alter table valuetype modify age int zerofill;//這時候數(shù)據(jù)庫就顯示'0000000001'

數(shù)據(jù)插入bit類型字段時,首先轉(zhuǎn)換為二進制,如果位數(shù)允許,將插入成功,如果位數(shù)小于實際的位置,則插入失敗。

日期時間類型

mysql里面獲取當前時間為now().mssql獲取當前時間為getdate()

timestamp,支持的范圍非常小,從1970-2038年,timestamp受時區(qū)的影響

create table timestamptest(
tp timestamp)

系統(tǒng)會自動給tp賦予默認值current_timestamp(系統(tǒng)日期),但是mysql只給第一個timestamp設置默認值,如果有第二個timestamp類型,則默認值設置為0

字符串類型

1.char與varchar類型的區(qū)別:

char列最后的空格已經(jīng)刪除,而varchar保留空格

四、MySQL中運算符

算術運算符

比較運算符,滿足返回1,否則返回0

邏輯運算符(布爾運算符)

位運算符

運算符優(yōu)先級,大多情況下使用()進行操作

五、常用函數(shù)

字符串函數(shù)

數(shù)值函數(shù)

日期和時間函數(shù)

流程函數(shù)

其他函數(shù)

六、選擇合適的數(shù)據(jù)類型

char與varchar

在Innodb存儲引擎中,建議使用varchar類型。對于Innodb數(shù)據(jù)表,內(nèi)部的行存儲格式?jīng)]有區(qū)分固定長度和可變長度列,因此固定長度列的性能不一定比不可變長度的性能好。

Text與blob

一般在保存少量字符串的時候,我們會選擇char或者varchar,而在保存較大文本的時候,通常會選擇使用text或者blob。兩者的區(qū)別:text只能保存字符數(shù)據(jù),比如日志。blob能保存二進制數(shù)據(jù),比如照片。

浮點數(shù)與定點數(shù)

在MySQL中,decimal或者(numberic)用來表示定點數(shù)

日期類型的選擇

date/time/datetime/timestamp

七、索引的設計和使用

索引概述

索引是數(shù)據(jù)庫中用來提高性能的最常用工具。在MySQL中,MyISAM與Innodb存儲引擎的表默認創(chuàng)建的都是Btree索引。

1.索引的創(chuàng)建

create table indexTest(
id int not null auto_increment,
memberid int not null,
createtime datetime not null default current_timestamp,
primary key (id)
)
alter table indextest add orderserial varchar(50) not null;
create unique index IX_orderserial on indexTest(orderserial);

insert into indextest (memberid,createtime,orderserial)values(112123,'2016-08-14','sz121213')
說明:上面創(chuàng)建一個表,其中定義orderserial為唯一索引。

語法:create [unique\fulltext\spatial] index index_name on tablename(columname)

2.設計索引的原則

1.最合適的索引列是出現(xiàn)在where子句中列,或連接子句中指定的列,而不是出現(xiàn)在select關鍵字后面的選擇列表的列

2.使用唯一索引,需要考慮列中某個值得分布,如果索引列種的基數(shù)越大,則索引的效果越好。舉個例子:訂單號就可以設置唯一索引,因為訂單號的不一樣。而對于rowstatus就無須了,因為rowstatus要么是有效要么是無效。這樣的篩選出的范圍還是很多,沒有意義

3.不要過度索引。因為所有也要占用額外的磁盤空間,如果一個索引很少使用,那么會不必要的減緩表的修改速度
顯示MySQL的執(zhí)行計劃:explain 后面加mysql語句

八、視圖

視圖(View)

定義:視圖是一種虛擬存在的表,對于使用視圖的用戶來說基本上是透明的,視圖并不是在數(shù)據(jù)庫中實際存在。

優(yōu)勢:

1.簡單,用戶完全不需要關心后面對應的表的結構/關聯(lián)條件和篩選條件。對用戶來說已經(jīng)是過濾好的符合條件的結果集

2.安全,使用視圖的用戶只能訪問他們被允許查詢的結果集

3.數(shù)據(jù)獨立,一旦視圖的結構確定了,可以屏蔽表結構變化對用戶的影響,源表增加列對視圖沒有影響。

語法:

create or replace view index_view as
select * from indextest

1.創(chuàng)建create [or replace] view viewName as select ...

2.查詢 select * from 視圖名稱

3.展示視圖 show tables;

4.刪除視圖 drop view viewname

九、存儲過程和函數(shù)

一、存儲過程(store procedure)和函數(shù)

存儲過程和函數(shù)是事先經(jīng)過編譯并存在數(shù)據(jù)庫中的一段SQL語句的集合,調(diào)用存儲過程和函數(shù)可以簡化應用開發(fā)人員的很多工作,減少數(shù)據(jù)在數(shù)據(jù)庫和應用服務器之間的傳輸,對于提高數(shù)據(jù)處理的效率是有好處的

語法:

create database finance;//創(chuàng)建finance數(shù)據(jù)庫
use finance;
create table orders(
orderId bigint not null auto_increment,
memberId int not null default 0,
serialNumber varchar(50) not null default '',
amount decimal(18,2) not null default 0,
createTime datetime not null default current_timestamp,
primary key (orderid)
)//創(chuàng)建orders訂單表
insert into orders (memberId,serialNumber,amount) values(6561121,'sz12234222',5),(233444,'ys1652233',10)//插入測試數(shù)據(jù)
delimiter 
create procedure orders_serial(in serial varchar(50))
reads sql data
begin
select * from orders
where serialNumber=serial;
end 

注釋:delimiter $$命令就是將語句的結束符從分號;修改成其他符號,這里指的是$$為結尾。這樣在number后面的分號就不會認為結束。

1.調(diào)用存儲過程

call orders_serial('sz12234222')

2.存儲過程的好處

邏輯封裝在數(shù)據(jù)庫端,調(diào)用者不需要了解中間的處理邏輯,一旦調(diào)用邏輯發(fā)生變化,只需要修改存儲過程即可,而對調(diào)用者的程序完全沒有影響。

3.刪除存儲過程

drop procedure if exists orders_serial
//if exists可選

4.查看存儲過程差狀態(tài)

show procedure status like 'orders_serial'

5.查詢存儲過程的定義

show create procedure orders_serial

二、存儲過程變量的使用

存儲過程可以使用變量,并且在MySQL5.1版本后,不區(qū)分大小寫

1.變量的定義

變量的作用域只能在begin...end塊中,可以嵌套在塊中

declare currentTime date;

2.變量的賦值

set currentTime=now();//直接賦值
select XX into currentTime from XX;//也可以通過sql語句進行賦值

3.定義條件和處理

declare handler_type handler for contidtion_value;

handler_type:

1.continue;
2.exit;
3.undo;

condition_value:

1.sqlstate
2.sqlwarning
3.not found
4.sqlexception

eg: declare continue handler for sqlstate '2' set @x=1;

三、光標的使用

在存儲過程和函數(shù)中,可以使用光標對結果集進行循環(huán)處理,光標的使用包含光標的聲明: open、fetch、close

定義:

declare cur_id cursor for select * from orders;
open cur_id;
fetch cur_id;
close cur_id;

四、事件調(diào)度器

事件調(diào)度器是MySQL5.1后面新增的功能,可以將數(shù)據(jù)庫按照自定義時間周期觸發(fā)某種操作。數(shù)據(jù)庫默認操作是關閉的。需要打開

create event x
on schedule
every 5 second
do
insert into orders (memberId,serialNumber,amount) values(6561121,'222',5)
set global event_scheduler =1//打開調(diào)度器
alter event x disable;//禁用事件調(diào)度器
drop event x;//刪除事件調(diào)度器

十、 觸發(fā)器

觸發(fā)器

觸發(fā)器是在5.02版本后支持的,觸發(fā)器是與表有關的數(shù)據(jù)庫對象,在滿足條件時觸發(fā),并執(zhí)行觸發(fā)器中定義的語句集合??梢詤f(xié)助應用在數(shù)據(jù)庫端確保數(shù)據(jù)的完整性

drop trigger orderlog
delimiter $
create trigger orderlog 
after insert on orders for each row
begin
insert into orderslog (content) values(new.serialNumber);
end 
insert into orders (memberId,serialNumber,amount) values(6561121,'sz12234222',5)

解釋:上面描述的是創(chuàng)建一個觸發(fā)器,當往訂單表中插入數(shù)據(jù)之后,在訂單日志表插入一條記錄。使用old和new來引用觸發(fā)器發(fā)生變化的記錄內(nèi)容,目前只支出行級觸發(fā),不支持語句級觸發(fā)

觸發(fā)器執(zhí)行的順序

before insert\before update\after update

十一、 事務控制和鎖定語句

MySQL存儲引擎的事務說明

1.Lock Table與Unlock Table

語法:

use finance;
lock table orders read;
unlock table;

如果某個進程(session1)lock定了表,那么其他的進程(session2)可以查詢,但是不能進行更新操作,直到第一個進程釋放了鎖

2.事務控制

十二、總結

很高興您能閱讀到這里,可能在三十分鐘很難吸收這么多的知識,這篇文章也是我之前學習MySQL筆記整合的。這篇文章也是理論偏多,對于其中比較比較難理解知識點寫些Demo,權當個人理解,如有不足的地方,請您指出。如果對您有所幫助,請點個贊!

您可能感興趣的文章:
  • Mysql入門基礎 數(shù)據(jù)庫創(chuàng)建篇
  • mysql入門之1小時學會MySQL基礎
  • Mysql基礎入門 輕松學習Mysql命令
  • 20分鐘MySQL基礎入門
  • MySQL新手入門指南--快速參考
  • 21分鐘 MySQL 入門教程
  • 快速學習MySQL索引的入門超級教程
  • Mysql基礎知識點匯總
  • MySQL 視圖的基礎操作(五)
  • MySQL基礎快速入門知識總結(附思維導圖)

標簽:銅川 湘潭 湖南 黃山 仙桃 衡水 蘭州 崇左

巨人網(wǎng)絡通訊聲明:本文標題《三十分鐘MySQL快速入門(圖解)》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266