欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報激活碼778899分享:一文入門mysql 數(shù)據(jù)庫

柚子快報激活碼778899分享:一文入門mysql 數(shù)據(jù)庫

http://yzkb.51969.com/

一、數(shù)據(jù)庫概述

什么是數(shù)據(jù)庫

? ? ? ?數(shù)據(jù)庫是一個用于存儲和管理數(shù)據(jù)的倉庫。數(shù)據(jù)按照特定的格式存儲,可以對數(shù)據(jù)庫中的數(shù)據(jù)進行增加、修改、刪除和查詢操作。數(shù)據(jù)庫的本質是一個文件系統(tǒng),按照一定的邏輯結構組織數(shù)據(jù),以方便高效地訪問和維護。

什么是數(shù)據(jù)庫管理系統(tǒng)

? ? ? ?數(shù)據(jù)庫管理系統(tǒng)(DataBase Management System,DBMS)是一個操作和管理數(shù)據(jù)庫的軟件。它用于建立、使用和維護數(shù)據(jù)庫,對數(shù)據(jù)庫進行統(tǒng)一管理和控制,以保證數(shù)據(jù)庫的安全性和完整性。用戶通過數(shù)據(jù)庫管理系統(tǒng)訪問數(shù)據(jù)庫中表內的數(shù)據(jù)。

常見的數(shù)據(jù)庫管理系統(tǒng)

MYSQL :

MySQL 不僅用于小型數(shù)據(jù)庫,還可以擴展到大型企業(yè)級應用場景。它在許多高流量的網站和大型企業(yè)系統(tǒng)中得到了廣泛應用。 雖然 Oracle 收購了 MySQL,但開源版本仍然存在且免費。MySQL 有多個版本(如社區(qū)版、企業(yè)版等),其中一些版本收費,但免費的社區(qū)版仍在維護和更新。 Oracle :收費的大型關系數(shù)據(jù)庫管理系統(tǒng),主要用于大型企業(yè)級應用。

SQLServer:MicroSoft 公司收費的中型的數(shù)據(jù)庫。SQL Server 不僅適用于 C# 和 .NET 語言,還可以與其他編程語言(如 Java、Python、PHP 等)一起使用。

SQLite : 嵌入式的小型數(shù)據(jù)庫,SQLite 不僅應用于手機端,還可以應用于桌面應用程序和物聯(lián)網(IoT)設備等。

PostgreSQL:PostgreSQL 是一種開源的關系數(shù)據(jù)庫管理系統(tǒng),功能強大且可擴展,適用于各種規(guī)模的應用場景。它在許多大型企業(yè)和開源項目中得到了廣泛應用。

NoSQL:除了上述描述的關系數(shù)據(jù)庫管理系統(tǒng)外,還有一類稱為 NoSQL(Not Only SQL)數(shù)據(jù)庫,它們使用非關系模型來存儲和管理數(shù)據(jù)。這些數(shù)據(jù)庫包括 MongoDB(文檔存儲)、Redis(鍵值存儲)、Apache Cassandra(列存儲)和 Neo4j(圖數(shù)據(jù)庫)等。這些數(shù)據(jù)庫在大數(shù)據(jù)、分布式系統(tǒng)和實時應用場景中具有優(yōu)勢。

數(shù)據(jù)庫表

數(shù)據(jù)庫中以表為組織單位存儲數(shù)據(jù)。

表類似我們的Java類,每個字段都有對應的數(shù)據(jù)類型。

那么用我們熟悉的java程序來與關系型數(shù)據(jù)對比,就會發(fā)現(xiàn)以下對應關系。

類----------表

類中屬性----------表中字段

對象----------記錄

二、MySql數(shù)據(jù)庫

1.安裝:

可以觀看我之前的作品

2.登錄,在windows命令窗口

mysql -u(用戶名) -p(密碼) -h(主機ip地址) -P(主機ip端口號)

3.基礎指令

(1)數(shù)據(jù)庫基本指令:

show? databases;? 顯示所有的數(shù)據(jù)庫

create database 數(shù)據(jù)庫名 charest=utf8;? 創(chuàng)建數(shù)據(jù)庫

drop database 數(shù)據(jù)庫名;? 刪除數(shù)據(jù)庫

use 數(shù)據(jù)庫名; 使用數(shù)據(jù)庫

select database();? 查看當前使用的數(shù)據(jù)庫

(2)數(shù)據(jù)表基本指令:

show tables;? 顯示數(shù)據(jù)庫中的所有數(shù)據(jù)表

create? table 表名 (列名 類型 約束1 約束2...,列名? 類型? 約束1? 約束2...);? 創(chuàng)建數(shù)據(jù)表

desc 表名;? 查看數(shù)據(jù)表結構

drop table 表名;? 刪除數(shù)據(jù)表

(3)數(shù)據(jù)表中列的增刪改:

alter table 數(shù)據(jù)表名 drop 列名;? 刪除列

alter table 數(shù)據(jù)表名? add? 列名 類型 約束1? 約束2...;? 創(chuàng)建列

alter table 數(shù)據(jù)表名? change 列名 新列名 類型 約束1? 約束2...;? 修改列

(4)列的約束條件:

1.主鍵 :primary? key ,可以確定唯一的一行

2.非空 :not null?,不能為空,必須有值

3.自增長: auto_increment? ,每次+1

4.唯一: unique, 列中內容不重復

5.默認:default,默認值

6.外鍵:foreign,與其他表關聯(lián),是其他表的主鍵

7.檢查:check,Mysql 8 才有,檢查滿足條件

4.高級指令

(1)、CURD ?增刪改查

查:

1.select * from 數(shù)據(jù)表名 where 條件; 慎用,非常耗時

2.select 列名,列名,列名 from 數(shù)據(jù)表名 where 條件;

3.select ?列名 as 別名,列名 as 別名 from 數(shù)據(jù)表名 where 條件;

條件:

1.比較運算符:?=? ?>? >=?

2.邏輯運算符:and or ?not

3.成員運算符: in [a,b,c]

4.between a and b

5.判斷是否為空:is null? is not null

6.like? ?% 代表多個字符,_ 代表一個字符

關聯(lián)查詢:表與表之前存在關聯(lián)關系

嵌套查詢:第一次查詢的結果作為第二次查詢的條件

表連接:

1.笛卡爾連接:一個表中每一行都和另外一個表中所有行連接

2.內連接 :inner join?

舉例:select user.id as 用戶id, user.username as ?用戶名, role.nick as 昵稱, role.level as 等級 from user inner join role ?on user.id = role.user_id;

3.外連接:左外連接,右外連接

左外連接:left join,以左表為主,左表條件滿足 正常顯示 不滿足 右側補null。

舉例:select * from user left join role on user.id = role.user_id;

右外連接:right join,以右表為主,右表條件滿足 正常顯示 不滿足 左側補null。

舉例:select * from role right join user on user.id = role.user_id;

4.全連接:full join,left join ? union ?right join,?mysql不支持關鍵字 full join

舉例:?select * from user right join role on user.id = role.user_id union ?select * from user left join role on user.id = role.user_id;

1.?insert into ?表名 values (列1, 列2,列n),(列1, 列2, 列n)....??列的個數(shù)與值的個數(shù)一致

2 . insert into ?表名 ?(列1, 列2) values (值1, 值2), (值1, 值2)...? ? 可以指定哪些列的數(shù)據(jù),沒有默認值的列必須插入

3. insert into ? 表名 ?set ?列1=值1,列2=值2;??插入1行指明插入的列與其值

update 數(shù)據(jù)表名 set 列名1 = 值1 where 條件;??如果沒有條件就修改整個表

delete from 表名 where 條件;??如果沒有條件就清空表

(2)、分組、分頁、排序

1. 去重:distinct? ,單獨列展示 并對列去重

舉例:?select ?distinct user_id from role;

2. 分組:group by?,一般會結合count (*)進行統(tǒng)計

舉例:?select info as '組信息', count(*) as 個數(shù) from role group by info;

3. 分頁:limit ,一個數(shù)字n 代表 前n個,兩個數(shù)字start, count,每頁顯示count個 ?第n頁公式:(n-1)* count, count

4. 排序:?order by, asc 升序、desc 降序

舉例:select * from role order by user_id desc , id ?desc ?;? id 降序

(3)、用戶操作

1.root系統(tǒng)管理員+ 其他用戶,% 代表所有ip都可以訪問

2.創(chuàng)建用戶:create user '用戶名'@'%' identified by '123456';

3.分配權限:grant 權限列舉 to ‘用戶名’@‘%’ on ?數(shù)據(jù)名.表名 ,all 所有權限,*.*??所有數(shù)據(jù)庫中的所有表

4. 刷新權限:?flush privileges;

5. 刪除用戶:drop user ?'用戶名'@'%';

6. 修改密碼:alter user ‘用戶名’@‘%’ ?identified by ‘123456’;

(4)、外鍵

1.創(chuàng)建、添加外鍵:constraint ?外鍵名 foreign key ? (列名) ?references ?表 (列名)on update ?cascade ?on??delete ? cascade;

2. 刪除外鍵:alter table 表名 drop foreign key 外鍵名;

(5)、函數(shù)

1. 查看當前數(shù)據(jù)庫:select database();

2.?查看當前用戶:?select ?user();

3.?查看版本:?select version();

4. 現(xiàn)在時間:?select now();

5. 時間戳:?select unix_timestamp();

(6)、特殊數(shù)據(jù)類型

1. int

2.?varchar、char

3.?datetime,default current_timestamp,default current_timestamp on update current_timestamp

4.?enum,enum("男“,”女“, ”保密“) ?default "保密"

三、pymysql

1、pymysql 基礎使用

(1).安裝

終端輸入:pip install pymysql

(2). 導包

import pymysql

(3). 構建連接實例

con = pymysql.connect(user="qyc", password='123456')

?(4). 使用數(shù)據(jù)庫

con.select_db("python2407")

(5). 創(chuàng)建游標(游動的鼠標),用于執(zhí)行sql語句

cur = con.cursor()

5.1?使用游標執(zhí)行sql

row = cur.execute("show tables")

print(f"{row}個表")

5.2 打印所有表 fetchall

datas = cur.fetchall()

for data in datas:

print(data[0])

?5.3?打印一個表(游標前進一位) fetchone

datas = cur.fetchone()

print(datas[0])

datas = cur.fetchone()

print(datas[0])

5.4 打印n個表??fetchmany(size=n) 取n個

datas = cur.fetchmany(size=2)

print(datas)

5.5?游標滾動,默認相對(relative), (absolute)絕對,正值前進,負值后退

cur.scroll(1) # 打印 第一個

data = cur.fetchone()

print(data)

cur.scroll(0, mode="absolute") # 同樣打印第一個

data = cur.fetchone()

print(data)

(6).?關閉游標,關閉連接

cur.close() con.close()

2、查詢插入?yún)?shù)

(1). 查詢單個數(shù)據(jù)

cur.execute("select * from role where nick =%s or level = %s", args=('射手', 3))

#結果:(4, '射手', 3, 1002)

(2).查詢多行

cur.executemany("select * from role where id = %s and level =%s", [(3, 1), (5, 15)])

#結果:(5, '打野', 15, 1003)

(3). 插入單行數(shù)據(jù)

row = cur.execute("insert into student values (%s,%s,%s)", (0, '趙六', 30))

插入前:

插入后:

(4). 插入多行數(shù)據(jù)

row = cur.executemany("insert into student values (%s,%s,%s)", [(0, '呂布', 6), (0, '李白', 3)])

插入前:

插入后:

3、pymysql 輔助類

筆者編寫了一個小小案例,應該挺好理解的,創(chuàng)建類,封裝了進入mysql的方法,每次初始化的時候都可以進入mysql

import pymysql

class MySqlHelper:

def __init__(self, db="python2407"):

self.con = None

self.cur = None

self.con = pymysql.connect(user="root", password="123456", database=db)

self.cur = self.con.cursor()

def query_one(self, query, args=None):

self.cur.execute(query, args)

return self.cur.fetchone()

def query_all(self, query, args=None):

self.cur.execute(query, args)

return self.cur.fetchall()

def update(self, query, args=None):

row = self.cur.execute(query, args)

self.con.commit()

return row

def __del__(self):

if self.cur:

self.cur.close()

if self.con:

self.con.close()

class UserMysqlHelper:

@staticmethod

def login(username, password):

sql = MySqlHelper()

user = sql.query_one("select * from user where username=%s and password = %s ", (username, password))

return user

@staticmethod

def regist(username, password):

sql = MySqlHelper()

row = sql.update("insert into user (username,password) values (%s,%s)", (username, password))

return row

def main():

while True:

menu = """0、退出

1、登錄

2、注冊

"""

print(menu)

option = input("輸入數(shù)字選擇對應選項")

if option not in [str(i) for i in range(3)]:

print(f"輸入不合法,重新輸入")

else:

if option == "0":

break

elif option == "1":

while True:

username = input("輸入用戶名")

if 2 <= len(username) <= 4:

break

else:

print(f"輸入不合法,重新輸入")

while True:

password = input("請輸入密碼")

if 2 <= len(password) <= 10:

break

else:

print(f"輸入不合法,重新輸入")

user = UserMysqlHelper.login(username, password)

if user:

print(f"登陸成功")

elif option == "2":

while True:

username = input("輸入用戶名")

if 2 <= len(username) <= 4:

break

else:

print(f"輸入不合法,重新輸入")

while True:

password = input("請輸入密碼")

if 2 <= len(password) <= 10:

break

else:

print(f"輸入不合法,重新輸入")

while True:

password2 = input("請再次輸入密碼")

if 2 <= len(password2) <= 10:

break

else:

print(f"輸入不合法,重新輸入")

if password != password2:

print(f"密碼不一致,重新注冊")

else:

row = UserMysqlHelper.regist(username, password)

if row > 0:

print(f"注冊成功")

else:

print(f"注冊失敗,重新注冊")

if __name__ == '__main__':

main()

4. pymysql 執(zhí)行函數(shù)與過程

(1). 構建過程函數(shù)

構建函數(shù):

?輸入:

CREATE DEFINER=`root`@`localhost` PROCEDURE `regist_user`(IN `n` int)

BEGIN

#Routine body goes here...

# 清空用戶表

# 注冊n個用戶

# 聲明一個變量i 他的類型為int 他的初始值為1

DECLARE i INT DEFAULT 1;

# 使用循環(huán) 創(chuàng)建n個賬戶

WHILE i <= n DO

INSERT INTO `user` VALUES (0, CONCAT('name',i), '123456');

SET i = i + 1;

END WHILE;

END

函數(shù)執(zhí)行前:

單擊 運行

輸入?yún)?shù):100

函數(shù)運行后:多了100個數(shù)據(jù)

(2)、構建函數(shù)

保存,運行,輸入?yún)?shù)? a,b

?結果:

以上就是小編使出渾身解數(shù)寫的mysql了,創(chuàng)作不易,請大家多多點贊支持!

柚子快報激活碼778899分享:一文入門mysql 數(shù)據(jù)庫

http://yzkb.51969.com/

相關文章

評論可見,查看隱藏內容

本文內容根據(jù)網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉載請注明,如有侵權,聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19392360.html

發(fā)布評論

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄