<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          MYSQL 入門全套

          共 9660字,需瀏覽 20分鐘

           ·

          2020-09-21 08:32



          點(diǎn)擊「閱讀原文」查看良許原創(chuàng)精品視頻。


          MySQL簡介


          1、什么是數(shù)據(jù)庫 ?


          數(shù)據(jù)庫(Database)是按照數(shù)據(jù)結(jié)構(gòu)來組織、存儲(chǔ)和管理數(shù)據(jù)的倉庫,它產(chǎn)生于距今六十多年前,隨著信息技術(shù)和市場的發(fā)展,特別是二十世紀(jì)九十年代以后,數(shù)據(jù)管理不再僅僅是存儲(chǔ)和管理數(shù)據(jù),而轉(zhuǎn)變成用戶所需要的各種數(shù)據(jù)管理的方式。數(shù)據(jù)庫有很多種類型,從最簡單的存儲(chǔ)有各種數(shù)據(jù)的表格到能夠進(jìn)行海量數(shù)據(jù)存儲(chǔ)的大型數(shù)據(jù)庫系統(tǒng)都在各個(gè)方面得到了廣泛的應(yīng)用。


          主流的數(shù)據(jù)庫有:sqlserver,mysql,Oracle、SQLite、Access、MS SQL Server等,本文主要講述的是mysql


          2、數(shù)據(jù)庫管理是干什么用的?


          • a. 將數(shù)據(jù)保存到文件或內(nèi)存


          • b. 接收特定的命令,然后對(duì)文件進(jìn)行相應(yīng)的操作


          PS:如果有了以上管理系統(tǒng),無須自己再去創(chuàng)建文件和文件夾,而是直接傳遞 命令 給上述軟件,讓其來進(jìn)行文件操作,他們統(tǒng)稱為數(shù)據(jù)庫管理系統(tǒng)(DBMS,Database Management System)


          MySQL安裝


          MySQL是一種開放源代碼的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),MySQL數(shù)據(jù)庫系統(tǒng)使用最常用的數(shù)據(jù)庫管理語言–結(jié)構(gòu)化查詢語言(SQL)進(jìn)行數(shù)據(jù)庫管理。在 WEB 應(yīng)用方面MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫管理系統(tǒng)) 應(yīng)用軟件之一。


          使用mysql必須具備一下條件


          • a. 安裝MySQL服務(wù)端


          • b. 安裝MySQL客戶端


          • c. 【客戶端】連接【服務(wù)端】


          • d. 【客戶端】發(fā)送命令給【服務(wù)端MySQL】服務(wù)的接受命令并執(zhí)行相應(yīng)操作(增刪改查等)


          1、下載地址:http://dev.mysql.com/downloads/mysql/


          2、安裝


          • windows安裝請(qǐng)參考:

            http://www.cnblogs.com/lonelywolfmoutain/p/4547115.html

          • linux下安裝:http://www.cnblogs.com/chenjunbiao/archive/2011/01/24/1940256.html


          注:以上兩個(gè)鏈接有完整的安裝方式,擼主也是參考他的安裝的,安裝完以后mysql.server start啟動(dòng)mysql服務(wù)


          MySQL操作


          一、連接數(shù)據(jù)庫

          mysql ?-u user -p ? ? ? ? ? ? ? ? ? 
          例:mysql -u root -p

          常見錯(cuò)誤如下:


          ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), it means that the MySQL server daemon (Unix) or service (Windows) is not running.


          退出連接:

          QUIT 或者 Ctrl+D

          二、查看數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫,使用數(shù)據(jù)庫查看數(shù)據(jù)庫:?

          show databases;

          默認(rèn)數(shù)據(jù)庫:

          mysql - 用戶權(quán)限相關(guān)數(shù)據(jù)
          test - 用于用戶測試數(shù)據(jù)
          information_schema - MySQL本身架構(gòu)相關(guān)數(shù)據(jù)

          創(chuàng)建數(shù)據(jù)庫: ? ?

          create database db1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; # utf8編碼

          create database db1 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; # gbk編碼

          使用數(shù)據(jù)庫:

           use db1;

          顯示當(dāng)前使用的數(shù)據(jù)庫中所有表:

          SHOW TABLES;

          三、用戶管理


          創(chuàng)建用戶

          create user '用戶名'@'IP地址' identified by '密碼';

          刪除用戶

          drop user '用戶名'@'IP地址';

          修改用戶

          rename user '用戶名'@'IP地址'; to '新用戶名'@'IP地址';

          修改密碼

          set password for '用戶名'@'IP地址' = Password('新密碼');

          注:用戶權(quán)限相關(guān)數(shù)據(jù)保存在mysql數(shù)據(jù)庫的user表中,所以也可以直接對(duì)其進(jìn)行操作(不建議)


          四、權(quán)限管理


          mysql對(duì)于權(quán)限這塊有以下限制:

          all privileges除grant外的所有權(quán)限
          select:僅查權(quán)限
          select,insert查和插入權(quán)限
          ...
          usage
          無訪問權(quán)限
          alter使用alter table
          alter routine使用alter procedure和drop procedure
          create使用create table
          create routine使用create procedure
          create temporary tables使用create temporary tables
          create user使用create user、drop user、rename user和revoke ?all privileges
          create view使用create view
          delete使用delete
          drop使用drop table
          execute使用call和存儲(chǔ)過程
          file使用select into outfile 和 load data infile
          grant option使用grant 和 revoke
          index使用index
          insert使用insert
          lock tables使用lock table
          process使用show full processlist
          select使用select
          show databases使用show databases
          show view使用show view
          update使用update
          reload使用flush
          shutdown使用mysqladmin shutdown(關(guān)閉MySQL)
          super使用change master、kill、logs、purge、master和set global。還允許mysqladmin調(diào)試登陸
          replication client服務(wù)器位置的訪問
          replication slave由復(fù)制從屬使用

          對(duì)于數(shù)據(jù)庫及內(nèi)部其他權(quán)限如下:

          數(shù)據(jù)庫名.* ? ? ? ? ? ?數(shù)據(jù)庫中的所有
          數(shù)據(jù)庫名.表 ? ? ? ? ? 指定數(shù)據(jù)庫中的某張表
          數(shù)據(jù)庫名.存儲(chǔ)過程 ? ? ?指定數(shù)據(jù)庫中的存儲(chǔ)過程
          *.* ? ? ? ? ? ? ? ? ? 所有數(shù)據(jù)庫

          對(duì)于用戶和IP的權(quán)限如下:

          用戶名@IP地址 ? ? ? ?用戶只能在改IP下才能訪問
          用戶名@192.168.1.% ? 用戶只能在改IP段下才能訪問(通配符%表示任意)
          用戶名@% ? ? ? ? ? ? 用戶可以再任意IP下訪問(默認(rèn)IP地址為%)

          1、查看權(quán)限:

          show grants for '用戶'@'IP地址'

          2、授權(quán)

          grant ?權(quán)限 on 數(shù)據(jù)庫.表 to ? '用戶'@'IP地址'

          3、取消授權(quán)

          revoke 權(quán)限 on 數(shù)據(jù)庫.表 from '用戶名'@'IP地址'

          授權(quán)實(shí)例如下:

          grant all privileges on db1.tb1 TO '用戶名'@'IP'

          grant select on db1.* TO '用戶名'@'IP'

          grant select,insert on *.* TO '用戶名'@'IP'

          revoke select on db1.tb1 from '用戶名'@'IP'

          MySQL表操作


          1、查看表

          show tables; # 查看數(shù)據(jù)庫全部表

          select * from 表名; # 查看表所有內(nèi)容

          2、創(chuàng)建表

          create table 表名(
          ? ?列名 ?類型 ?是否可以為空,
          ? ?列名 ?類型 ?是否可以為空
          )ENGINE=InnoDB DEFAULT CHARSET=utf8

          來一個(gè)實(shí)例好詳解

          CREATE TABLE `tab1` (
          ?`nid` int(11) NOT NULL auto_increment,
          ?`name` varchar(255) DEFAULT zhangyanlin,
          ?`email` varchar(255),
          ?PRIMARY KEY (`nid`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

          注:


          • 默認(rèn)值,創(chuàng)建列時(shí)可以指定默認(rèn)值,當(dāng)插入數(shù)據(jù)時(shí)如果未主動(dòng)設(shè)置,則自動(dòng)添加默認(rèn)值


          • 自增,如果為某列設(shè)置自增列,插入數(shù)據(jù)時(shí)無需設(shè)置此列,默認(rèn)將自增(表中只能有一個(gè)自增列)注意:1、對(duì)于自增列,必須是索引(含主鍵)2、對(duì)于自增可以設(shè)置步長和起始值


          • 主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個(gè)列,則它的值必須唯一,如果是多列,則其組合必須唯一。


          3、刪除表

          drop table 表名

          4、清空表內(nèi)容

          delete from 表名
          truncate table 表名

          5、修改表

          添加列: ? 

          alter table 表名 add 列名 類型

          刪除列: ?

          alter table 表名 drop column 列名

          修改列:
          ? ? ? ? ?
          alter table 表名 modify column 列名 類型; ?-- 類型
          alter table 表名 change 原列名 新列名 類型; -- 列名,類型

          添加主鍵:
          ? ? ? ? ?
          alter table 表名 add primary key(列名);

          刪除主鍵:
          ? ? ? ? ?
          alter table 表名 drop primary key;
          alter table 表名 ?modify ?列名 int, drop primary key;

          添加外鍵:

          alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段);

          刪除外鍵:

          alter table 表名 drop foreign key 外鍵名稱

          修改默認(rèn)值:

          ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;

          刪除默認(rèn)值:

          ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

          對(duì)于上述這些操作是不是看起來很麻煩,很浪費(fèi)時(shí)間,別慌!有專門的軟件能提供這些功能,操作起來非常簡單,這個(gè)軟件名字叫Navicat Premium ,大家自行在網(wǎng)上下載,練練手,但是下面的即將講到表內(nèi)容操作還是建議自己寫命令來進(jìn)行


          6、基本數(shù)據(jù)類型


          MySQL的數(shù)據(jù)類型大致分為:數(shù)值、時(shí)間和字符串

          bit[(M)]
          ? ? ? ? ? ?二進(jìn)制位(101001),m表示二進(jìn)制位的長度(1-64),默認(rèn)m=1

          tinyint[(m)] [unsigned] [zerofill]

          ? ? ? ? ? ?小整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:
          ? ? ? ? ? ?有符號(hào):
          ? ? ? ? ? ? ? ?-128 ~ 127.
          ? ? ? ? ? ?無符號(hào):
          ? ? ? ? ? ? ? ?0 ~ 255

          ? ? ? ? ? ?特別的: MySQL中無布爾值,使用tinyint(1)構(gòu)造。

          int[(m)][unsigned][zerofill]

          ? ? ? ? ? ?整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:
          ? ? ? ? ? ? ? ?有符號(hào):
          ? ? ? ? ? ? ? ? ? ?-2147483648 ~ 2147483647
          ? ? ? ? ? ? ? ?無符號(hào):
          ? ? ? ? ? ? ? ? ? ?0 ~ 4294967295

          ? ? ? ? ? ?特別的:整數(shù)類型中的m僅用于顯示,對(duì)存儲(chǔ)范圍無限制。例如: int(5),當(dāng)插入數(shù)據(jù)2時(shí),select 時(shí)數(shù)據(jù)顯示為:00002

          bigint[(m)][unsigned][zerofill]

          ? ? ? ? ? ?大整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:
          ? ? ? ? ? ? ? ?有符號(hào):
          ? ? ? ? ? ? ? ? ? ?-9223372036854775808 ~ 9223372036854775807
          ? ? ? ? ? ? ? ?無符號(hào):
          ? ? ? ? ? ? ? ? ? ?0 ?~ ?18446744073709551615


          decimal[(m[,d])] [unsigned] [zerofill]

          ? ? ? ? ? ?準(zhǔn)確的小數(shù)值,m是數(shù)字總個(gè)數(shù)(負(fù)號(hào)不算),d是小數(shù)點(diǎn)后個(gè)數(shù)。 m最大值為65,d最大值為30。

          ? ? ? ? ? ?特別的:對(duì)于精確數(shù)值計(jì)算時(shí)需要用此類型
          ? ? ? ? ? ? ? ? ? decaimal能夠存儲(chǔ)精確值的原因在于其內(nèi)部按照字符串存儲(chǔ)。

          FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
          ? ? ? ? ? ?
          ? ? ? ? ? ?單精度浮點(diǎn)數(shù)(非準(zhǔn)確小數(shù)值),m是數(shù)字總個(gè)數(shù),d是小數(shù)點(diǎn)后個(gè)數(shù)。
          ? ? ? ? ? ? ? ?無符號(hào):
          ? ? ? ? ? ? ? ? ? ?-3.402823466E+38 to -1.175494351E-38,
          ? ? ? ? ? ? ? ? ? ?0
          ? ? ? ? ? ? ? ? ? ?1.175494351E-38 to 3.402823466E+38
          ? ? ? ? ? ? ? ?有符號(hào):
          ? ? ? ? ? ? ? ? ? ?0
          ? ? ? ? ? ? ? ? ? ?1.175494351E-38 to 3.402823466E+38


          ? ? ? ? ? ?**** 數(shù)值越大,越不準(zhǔn)確 ****

          DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]

          ? ? ? ? ? ?雙精度浮點(diǎn)數(shù)(非準(zhǔn)確小數(shù)值),m是數(shù)字總個(gè)數(shù),d是小數(shù)點(diǎn)后個(gè)數(shù)。

          ? ? ? ? ? ? ? ?無符號(hào):
          ? ? ? ? ? ? ? ? ? ?-1.7976931348623157E+308 to -2.2250738585072014E-308
          ? ? ? ? ? ? ? ? ? ?0
          ? ? ? ? ? ? ? ? ? ?2.2250738585072014E-308 to 1.7976931348623157E+308
          ? ? ? ? ? ? ? ?有符號(hào):
          ? ? ? ? ? ? ? ? ? ?0
          ? ? ? ? ? ? ? ? ? ?2.2250738585072014E-308 to 1.7976931348623157E+308
          ? ? ? ? ? ?**** 數(shù)值越大,越不準(zhǔn)確 ****


          char (m)

          ? ? ? ? ? ?char數(shù)據(jù)類型用于表示固定長度的字符串,可以包含最多達(dá)255個(gè)字符。其中m代表字符串的長度。
          ? ? ? ? ? ?PS: 即使數(shù)據(jù)小于m長度,也會(huì)占用m長度

          ? ? ? ? ? ?
          varchar(m)

          ? ? ? ? ? ?varchars數(shù)據(jù)類型用于變長的字符串,可以包含最多達(dá)255個(gè)字符。其中m代表該數(shù)據(jù)類型所允許保存的字符串的最大長度,只要長度小于該最大值的字符串都可以被保存在該數(shù)據(jù)類型中。

          ? ? ? ? ? ?注:雖然varchar使用起來較為靈活,但是從整個(gè)系統(tǒng)的性能角度來說,char數(shù)據(jù)類型的處理速度更快,有時(shí)甚至可以超出varchar處理速度的50%。因此,用戶在設(shè)計(jì)數(shù)據(jù)庫時(shí)應(yīng)當(dāng)綜合考慮各方面的因素,以求達(dá)到最佳的平衡

          text

          ? ? ? ? ? ?text數(shù)據(jù)類型用于保存變長的大字符串,可以組多到65535 (2**16 ? 1)個(gè)字符。

          mediumtext

          ? ? ? ? ? ?A TEXT column with a maximum length of 16,777,215 (2**24 ? 1) characters.

          longtext

          ? ? ? ? ? ?A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 ? 1) characters.

          enum

          ? ? ? ? ? ?枚舉類型,
          ? ? ? ? ? ?An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)
          ? ? ? ? ? ?示例:

          ? ? ? ? ? ? ? ?CREATE TABLE shirts (
          ? ? ? ? ? ? ? ? ? ?name VARCHAR(40),
          ? ? ? ? ? ? ? ? ? ?size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
          ? ? ? ? ? ? ? ?);
          ? ? ? ? ? ? ? ?INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');

          set

          ? ? ? ? ? ?集合類型
          ? ? ? ? ? ?A SET column can have a maximum of 64 distinct members.
          ? ? ? ? ? ?示例:
          ? ? ? ? ? ? ? ?CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
          ? ? ? ? ? ? ? ?INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');

          DATE
          ? ? ? ? ? ?
          ? ? ? ? ? ?YYYY-MM-DD(1000-01-01/9999-12-31)

          TIME

          ? ? ? ? ? ?HH:MM:SS('-838:59:59'/'838:59:59')

          YEAR

          ? ? ? ? ? ?YYYY(1901/2155)

          DATETIME

          ? ? ? ? ? ?YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 ? ?Y)

          TIMESTAMP

          ? ? ? ? ? ?YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時(shí))

          MySQL表內(nèi)容操作


          表內(nèi)容操作無非就是增刪改查,當(dāng)然用的最多的還是查,而且查這一塊東西最多,用起來最難,當(dāng)然對(duì)于大神來說那就是so easy了,對(duì)于我這種小白還是非常難以靈活運(yùn)用的,下面咱來一一操作一下


          1、增

          insert into 表 (列名,列名...) values (值,值,...)
          insert into 表 (列名,列名...) values (值,值,...),(值,值,值...)
          insert into 表 (列名,列名...) select (列名,列名...) from
          例:
          ? ?insert into tab1(name,email) values('zhangyanlin','[email protected]')

          2、刪

          delete from 表 ? # 刪除表里全部數(shù)據(jù)
          delete fromwhere id=1 and name='zhangyanlin' # 刪除ID =1 和name='zhangyanlin' 那一行數(shù)據(jù)

          3、改

          updateset name = 'zhangyanlin' where id>1

          4、查

          select * from
          select * fromwhere id > 1
          select nid,name,gender as gg fromwhere id > 1

          查這塊的條件太多太多我給列舉出來至于組合還得看大家的理解程度哈


          a、條件判斷where

          select * fromwhere id > 1 and name != 'aylin' and num = 12;
          select * fromwhere id between 5 and 16;
          select * fromwhere id in (11,22,33)
          select * fromwhere id not in (11,22,33)
          select * fromwhere id in (select nid from 表)

          b、通配符like

          select * fromwhere name like 'zhang%' ?# zhang開頭的所有(多個(gè)字符串)
          select * fromwhere name like 'zhang_' ?# zhang開頭的所有(一個(gè)字符)

          c、限制limit

          select * fromlimit 5; ? ? ? ? ? ?- 前5行
          select * fromlimit 4,5; ? ? ? ? ?- 從第4行開始的5行
          select * fromlimit 5 offset 4 ? ?- 從第4行開始的5行

          d、排序asc,desc

          select * fromorder byasc ? ? ? ? ? ? ?- 根據(jù) “列” 從小到大排列
          select * fromorder bydesc ? ? ? ? ? ? - 根據(jù) “列” 從大到小排列
          select * fromorder by1 desc,列2 asc ? ?- 根據(jù) “列1” 從大到小排列,如果相同則按列2從小到大排序

          e、分組group by

          select num fromgroup by num
          select num,nid fromgroup by num,nid
          select num,nid from 表 ?where nid > 10 group by num,nid order nid desc
          select num,nid,count(*),sum(score),max(score),min(score) fromgroup by num,nid
          select num fromgroup by num having max(id) > 10

          特別的:group by 必須在where之后,order by之前

          良許個(gè)人微信


          添加良許個(gè)人微信即送3套程序員必讀資料


          → 精選技術(shù)資料共享

          → 高手如云交流社群





          本公眾號(hào)全部博文已整理成一個(gè)目錄,請(qǐng)?jiān)诠娞?hào)里回復(fù)「m」獲??!

          推薦閱讀:

          這能忍?阿里、騰訊、京東、百度聯(lián)合出手了!!

          又一家網(wǎng)盤,涼涼了

          鴻蒙代碼全部開源,一起來寫個(gè)Demo唄!


          5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機(jī),樹莓派,等等。在公眾號(hào)內(nèi)回復(fù)「1024」,即可免費(fèi)獲取??!


          瀏覽 69
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产豆花在线 | 香蕉视频18禁 | 黄色片一级 | 精品国产精品一区二区金廿莲 | 成人在线视频网站 |