1. 程式人生 > >1Python全棧之路系列之MySQL數據庫基本操作

1Python全棧之路系列之MySQL數據庫基本操作

大型數據庫 數據庫管理 數據庫軟件 程序員 sql數據庫

Python全棧之路系列之MySQL數據庫基本操作


MySQL數據庫介紹

MySQL是一種快速易用的關系型數據庫管理系統(RDBMS),很多企業都在使用它來構建自己的數據庫。


MySQL由一家瑞典公司MySQL AB開發、運營並予以支持。它之所以非常流行,原因在於具備以下這些優點:

  1. 基於開源許可發布,無需付費即可使用。

  2. 自身的功能非常強大,足以匹敵絕大多數功能強大但卻價格昂貴的數據庫軟件。

  3. 使用業內所熟悉的標準SQL數據庫語言。

  4. 可運行於多個操作系統,支持多種語言,包括 PHP、PERL、C、C++ 及 Java 等語言。

  5. 非常迅速,即使面對大型數據集也毫無滯澀。

  6. 非常適用於 PHP 這種 Web 開發者最喜歡使用的語言。

  7. 支持大型數據庫,最高可在一個表中容納 5千多萬行。每張表的默認文件大小限制為 4GB,不過如果操作系統支持,你可以將其理論限制增加到 800 萬 TB。

  8. 可以自定義。開源 GPL 許可保證了程序員可以自由修改 MySQL,以便適應各自特殊的開發環境。

  • 關系型數據庫管理系統(RDBMS)具有以下特點:

  1. 能夠實現一種具有表、列與索引的數據庫。

  2. 保證不同表的行之間的引用完整性。

  3. 能自動更新索引。

  4. 能解釋 SQL 查詢,組合多張表的信息。

RDBMS術語

術語描述
數據庫(Database)數據庫是帶有相關數據的表的集合
表(Table)表是帶有數據的矩陣。數據庫中的表就像一種簡單的電子表格
列(Column)每一列(數據元素)都包含著同種類型的數據,比如郵編
行(Row)行(又被稱為元組、項或記錄)是一組相關數據,比如有關訂閱量的數據
冗余(Redundancy)存儲兩次數據,以便使系統更快速
主鍵(Primary Key)主鍵是唯一的。同一張表中不允許出現同樣兩個鍵值。一個鍵值只對應著一行
外鍵(Foreign Key)用於連接兩張表
復合鍵(Compound Key)復合鍵(又稱組合鍵)是一種由多列組成的鍵,因為一列並不足以確定唯一性
索引(Index)它在數據庫中的作用就像書後的索引一樣
引用完性(Referential Integrity)用來確保外鍵一直指向已存在的一行

安裝MySQL數據庫

連接已經啟動的MySQL數據庫指令

[email protected]
/* */:~$ mysql -uroot -pas -h 192.168.56.1 -P 3306

數據庫基本操作

查看當前的所有數據庫

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
默認數據庫描述
information_schema提供了訪問數據庫元數據的方式,元數據如數據庫名或表名,列的數據類型或訪問權限等
test用戶用來測試的數據庫庫
mysql用戶權限相關數據
performance_schema用於收集數據庫服務器性能參數
sys包含了一系列視圖、函數和存儲過程

創建數據庫

-- 創建字符串為utf-8的數據庫
CREATE DATABASE dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- 創建字符串為gbk的數據庫
CREATE DATABASE dbname DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

進入數據庫

use dbname;

刪除數據庫

drop database dbname;

用戶相關

創建ansheng用戶,允許所有主機連接,密碼設置為as

create user [email protected]%‘ identified by ‘as‘;

修改ansheng用戶的用戶名為as

rename user [email protected]%‘ to [email protected];

修改anshengme用戶的密碼為123

set password for [email protected] = Password(‘123‘);

刪除anshengme用戶

drop user [email protected];

用戶權限相關數據保存在mysql數據庫的user表中,所以也可以直接對其進行操作(不建議)

用戶授權

-- 查看權限
show grants for [email protected];
-- 授權
grant 權限 on 數據庫.表 to [email protected];
-- 取消權限
revoke 權限 on 數據庫.表 from [email protected];

數據表基本操作

創建表

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

是否可以為空

create table tb_name(
    `username_not`  varchar(30) NOT NULL ,    -- 不可空
    `username_null`  varchar(30) NULL         -- 可空
)ENGINE=InnoDB DEFAULT CHARSET=utf8

默認值,創建列時可以指定默認值,當插入數據時如果未主動設置,則自動添加默認值

create table tb_name(
    nid int not null defalut 2,
    num int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8

自增,如果為某列設置自增列,插入數據時無需設置此列,默認將自增(表中只能有一個自增列)

create table tb_name(
    nid int not null auto_increment primary key,
    num int null
)ENGINE=InnoDB DEFAULT CHARSET=utf8

create table tb_name(
    nid int not null auto_increment,
    num int null,
    index(nid)
)ENGINE=InnoDB DEFAULT CHARSET=utf8
  1. 對於自增列,必須是索引(含主鍵)。

  2. 對於自增可以設置步長和起始值

show session variables like ‘auto_inc%‘;
set session auto_increment_increment=2;
set session auto_increment_offset=10;

shwo global variables like ‘auto_inc%‘;
set global auto_increment_increment=2;
set global auto_increment_offset=10;

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

create table tb1(
    nid int not null auto_increment primary key,
    num int null
)

create table tb1(
    nid int not null,
    num int not null,
    primary key(nid,num)
)

外鍵,一個特殊的索引,只能是指定內容

create table color(
    nid int not null primary key,
    name char(16) not null
)
create table fruit(
    nid int not null primary key,
    smt char(32) null ,
    color_id int not null,
    constraint fk_cc foreign key (color_id) references color(nid)
)

刪除表

drop table tb_name;

清空表

-- 如果清空的表又自增列,那麽在清空之後會繼續上次自增的值繼續自增
delete from tb_name;
-- 如果清空的表又自增列,那麽在清空之後再次添加數據自增的值會從新開始計算
truncate table tb_name;

修改表

-- 添加列
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 外鍵名稱;

-- 修改默認值
ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;

-- 刪除默認值
ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

#Python全棧之路


1Python全棧之路系列之MySQL數據庫基本操作