日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

python开发小型数据库_Python开发【第十七篇】:MySQL(一)

發(fā)布時(shí)間:2023/12/19 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python开发小型数据库_Python开发【第十七篇】:MySQL(一) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、概述

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

答:數(shù)據(jù)的倉(cāng)庫(kù),如:在ATM的示例中我們創(chuàng)建了一個(gè) db 目錄,稱其為數(shù)據(jù)庫(kù)

2、什么是 MySQL、Oracle、SQLite、Access、MS SQL Server等 ?

答:他們均是一個(gè)軟件,都有兩個(gè)主要的功能:

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

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

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

3、什么是SQL ?

答:上述提到MySQL等軟件可以接受命令,并做出相應(yīng)的操作,由于命令中可以包含刪除文件、獲取文件內(nèi)容等眾多操作,對(duì)于編寫的命令就是是SQL語(yǔ)句。SQL􏰉􏵮􏵯􏰟,是􏵱􏰚􏵲􏵳􏵴􏰇􏰈􏵱􏰚􏵲􏵳􏵴􏰇􏰈結(jié)構(gòu)化語(yǔ)言(Structured Query Language􏰕􏰐􏵵􏰯)的縮寫,SQL􏰜􏰖􏰩􏰽􏵶􏱥􏲲􏰄􏰫􏰬􏰭􏵁􏵷􏰐􏰇􏰈是一種專門用來(lái)與數(shù)據(jù)庫(kù)通信的語(yǔ)言。

二、下載安裝

MySQL是一個(gè)關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),目前屬于 Oracle 旗下公司。MySQL 最流行的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),在 WEB 應(yīng)用方面MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)) 應(yīng)用軟件之一。

想要使用MySQL來(lái)存儲(chǔ)并操作數(shù)據(jù),則需要做幾件事情:

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

b. 安裝MySQL客戶端

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

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

1

2

3

4

5

6

7

8

9

下載

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

安裝

windows:

點(diǎn)點(diǎn)點(diǎn)

Linux:

yum install mysql-server

Mac:

點(diǎn)點(diǎn)點(diǎn)

服務(wù)端啟動(dòng)

1

mysql.server start

客戶端連接

1

2

3

4

5

6

7

連接:

mysql -h host -uuser -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 或者 Control+D

三、數(shù)據(jù)庫(kù)操作

1、顯示數(shù)據(jù)庫(kù)

1

SHOW DATABASES;

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

mysql - 用戶權(quán)限相關(guān)數(shù)據(jù)

test - 用于用戶測(cè)試數(shù)據(jù)

information_schema - MySQL本身架構(gòu)相關(guān)數(shù)據(jù)

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

1

2

3

4

5

# utf-8

CREATE DATABASE 數(shù)據(jù)庫(kù)名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

# gbk

CREATE DATABASE 數(shù)據(jù)庫(kù)名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

3、使用數(shù)據(jù)庫(kù)

1

USE db_name;

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

4、用戶管理

1

2

3

4

5

6

7

8

9

10

創(chuàng)建用戶

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

刪除用戶

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

修改用戶

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

修改密碼

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

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

5、授權(quán)管理

1

2

3

show grantsfor '用戶'@'IP地址' -- 查看權(quán)限

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

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

all privileges 除grant外的所有權(quán)限

select 僅查權(quán)限

select,insert 查和插入權(quán)限

...

usage 無(wú)訪問(wèn)權(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ǔ)過(guò)程

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ù)器位置的訪問(wèn)

replication slave 由復(fù)制從屬使用

對(duì)于權(quán)限

對(duì)于目標(biāo)數(shù)據(jù)庫(kù)以及內(nèi)部其他:

數(shù)據(jù)庫(kù)名.*數(shù)據(jù)庫(kù)中的所有

數(shù)據(jù)庫(kù)名.表 指定數(shù)據(jù)庫(kù)中的某張表

數(shù)據(jù)庫(kù)名.存儲(chǔ)過(guò)程 指定數(shù)據(jù)庫(kù)中的存儲(chǔ)過(guò)程

*.* 所有數(shù)據(jù)庫(kù)

對(duì)于數(shù)據(jù)庫(kù)

用戶名@IP地址 用戶只能在改IP下才能訪問(wèn)

用戶名@192.168.1.% 用戶只能在改IP段下才能訪問(wèn)(通配符%表示任意)

用戶名@% 用戶可以再任意IP下訪問(wèn)(默認(rèn)IP地址為%)

對(duì)于用戶和IP

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'

示例

四、數(shù)據(jù)表基本

1、創(chuàng)建表

1

2

3

4

create table 表名(

列名 類型 是否可以為空,

列名 類型 是否可以為空

)ENGINE=InnoDB DEFAULT CHARSET=utf8

是否可空,null表示空,非字符串

not null -不可空

null - 可空

是否可以為空

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

create table tb1(

nid int not null defalut 2,

num int notnull

)

默認(rèn)值

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

create table tb1(

nid int notnull auto_increment primary key,

num int null

)

create table tb1(

nid int notnull auto_increment,

num int null,

index(nid)

)

注意:1、對(duì)于自增列,必須是索引(含主鍵)。

2、對(duì)于自增可以設(shè)置步長(zhǎng)和起始值

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;

自增

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

create table tb1(

nid int notnull auto_increment primary key,

num int null

)

create table tb1(

nid int notnull,

num int notnull,

primary key(nid,num)

)

主鍵

外鍵,一個(gè)特殊的索引,只能是指定內(nèi)容

creat table color(

nid int notnull primary key,

name char(16) notnull

)

create table fruit(

nid int notnull primary key,

smt char(32) null ,

color_id int notnull,

constraint fk_cc foreign key (color_id) references color(nid)

)

外鍵

2、刪除表

1

drop table 表名

3、清空表

1

2

delete from 表名

truncate table 表名

4、修改表

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

添加列: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;

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

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

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

tinyint[(m)] [unsigned] [zerofill]小整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:

有符號(hào):

-128 ~ 127.

無(wú)符號(hào):

0 ~ 255特別的: MySQL中無(wú)布爾值,使用tinyint(1)構(gòu)造。

int[(m)][unsigned][zerofill]整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:

有符號(hào):

-2147483648 ~ 2147483647無(wú)符號(hào):

0 ~ 4294967295特別的:整數(shù)類型中的m僅用于顯示,對(duì)存儲(chǔ)范圍無(wú)限制。例如: 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無(wú)符號(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ù)。

無(wú)符號(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ù)。

無(wú)符號(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ù)類型用于表示固定長(zhǎng)度的字符串,可以包含最多達(dá)255個(gè)字符。其中m代表字符串的長(zhǎng)度。

PS: 即使數(shù)據(jù)小于m長(zhǎng)度,也會(huì)占用m長(zhǎng)度

varchar(m)

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

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

texttext數(shù)據(jù)類型用于保存變長(zhǎng)的大字符串,可以組多到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 TABLEshirts (

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','

總結(jié)

以上是生活随笔為你收集整理的python开发小型数据库_Python开发【第十七篇】:MySQL(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。