溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python全棧MySQL數(shù)據(jù)庫基本操作的詳細介紹

發(fā)布時間:2020-05-25 14:33:09 來源:網絡 閱讀:281 作者:三月 欄目:數(shù)據(jù)庫

下文給大家?guī)碛嘘PPython全棧MySQL數(shù)據(jù)庫基本操作的詳細內容,相信大家一定看過類似的文章。我們給大家?guī)淼挠泻尾煌??一起來看看正文部分吧,相信看完Python全棧MySQL數(shù)據(jù)庫基本操作你一定會有所收獲。

MySQL數(shù)據(jù)庫介紹

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


MySQL由一家瑞典公司MySQL AB開發(fā)、運營并予以支持。它之所以非常流行,原因在于具備以下這些優(yōu)點:

  1. 基于開源許可發(fā)布,無需付費即可使用。

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

  3. 使用業(yè)內所熟悉的標準SQL數(shù)據(jù)庫語言。

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

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

  6. 非常適用于 PHP 這種 Web 開發(fā)者最喜歡使用的語言。

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

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

  • 關系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS)具有以下特點:

  1. 能夠實現(xiàn)一種具有表、列與索引的數(shù)據(jù)庫。

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

  3. 能自動更新索引。

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

RDBMS術語

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

安裝MySQL數(shù)據(jù)庫

連接已經啟動的MySQL數(shù)據(jù)庫指令

ansheng@Darker:~$ mysql -uroot -pas -h 192.168.56.1 -P 3306

數(shù)據(jù)庫基本操作

查看當前的所有數(shù)據(jù)庫

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

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

-- 創(chuàng)建字符串為utf-8的數(shù)據(jù)庫
CREATE DATABASE dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- 創(chuàng)建字符串為gbk的數(shù)據(jù)庫
CREATE DATABASE dbname DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

進入數(shù)據(jù)庫

use dbname;

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

drop database dbname;

用戶相關

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

create user 'ansheng'@'%' identified by 'as';

修改ansheng用戶的用戶名為as

rename user 'ansheng'@'%' to 'anshengme'@'192.168.56.1';

修改anshengme用戶的密碼為123

set password for 'anshengme'@'192.168.56.1' = Password('123');

刪除anshengme用戶

drop user 'anshengme'@'192.168.56.1';

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

用戶授權

-- 查看權限
show grants for 'root'@'localhost';
-- 授權
grant 權限 on 數(shù)據(jù)庫.表 to '用戶'@'IP地址';
-- 取消權限
revoke 權限 on 數(shù)據(jù)庫.表 from '用戶'@'IP地址';

數(shù)據(jù)表基本操作

創(chuàng)建表

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

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

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

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

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;

清空表

-- 如果清空的表又自增列,那么在清空之后會繼續(xù)上次自增的值繼續(xù)自增
delete from tb_name;
-- 如果清空的表又自增列,那么在清空之后再次添加數(shù)據(jù)自增的值會從新開始計算
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全棧MySQL數(shù)據(jù)庫基本操作的詳細介紹,大家覺得是自己想要的嗎?如果想要了解更多相關,可以繼續(xù)關注我們的行業(yè)資訊板塊。

 

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI