中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

網(wǎng)站開(kāi)發(fā)總結(jié)文檔百度問(wèn)答官網(wǎng)

網(wǎng)站開(kāi)發(fā)總結(jié)文檔,百度問(wèn)答官網(wǎng),凡科建站容易嗎,c++后端開(kāi)發(fā)需要學(xué)什么文章目錄 MySQL終端命令1. 進(jìn)入mysql2. 創(chuàng)建數(shù)據(jù)庫(kù)3. 選擇數(shù)據(jù)庫(kù)4. 創(chuàng)建數(shù)據(jù)表1. 主鍵約束2. 外鍵約束3. 非空約束4. 唯一約束5. 使用默認(rèn)約束6. 設(shè)置id為自增列 5. 查看數(shù)據(jù)表6. 修改數(shù)據(jù)表1. 修改表名2. 修改表的字段類型3. 修改表的字段名4. 為表添加字段5. 刪除字段6. 調(diào)整…

文章目錄

  • MySQL終端命令
    • 1. 進(jìn)入mysql
    • 2. 創(chuàng)建數(shù)據(jù)庫(kù)
    • 3. 選擇數(shù)據(jù)庫(kù)
    • 4. 創(chuàng)建數(shù)據(jù)表
      • 1. 主鍵約束
      • 2. 外鍵約束
      • 3. 非空約束
      • 4. 唯一約束
      • 5. 使用默認(rèn)約束
      • 6. 設(shè)置id為自增列
    • 5. 查看數(shù)據(jù)表
    • 6. 修改數(shù)據(jù)表
      • 1. 修改表名
      • 2. 修改表的字段類型
      • 3. 修改表的字段名
      • 4. 為表添加字段
      • 5. 刪除字段
      • 6. 調(diào)整字段的位置
      • 7. 刪除表的外鍵約束
      • 8. 刪除數(shù)據(jù)表
    • 7. 數(shù)據(jù)表的操作
      • 1. 新增數(shù)據(jù)
      • 2. 查詢數(shù)據(jù)
      • 3. 修改數(shù)據(jù)
      • 4. 刪除數(shù)據(jù)
      • 5. replace
  • Python操作MySQL
    • 1. 連接數(shù)據(jù)庫(kù)
    • 2. 創(chuàng)建表
    • 3. 插入數(shù)據(jù)
    • 4. 查詢數(shù)據(jù)
    • 5. 更新數(shù)據(jù)
    • 6. 刪除數(shù)據(jù)

MySQL終端命令

1. 進(jìn)入mysql

win+r輸入cmd進(jìn)入終端

輸入:

mysql -u root -p

再輸入密碼進(jìn)入mysql
在這里插入圖片描述

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

輸入(注意語(yǔ)句后面一定要加“ ; ”):

create database test;

創(chuàng)建好后查看數(shù)據(jù)庫(kù):

show databases;

在這里插入圖片描述

3. 選擇數(shù)據(jù)庫(kù)

輸入:

use test;

進(jìn)入test數(shù)據(jù)庫(kù)。

在這里插入圖片描述

4. 創(chuàng)建數(shù)據(jù)表

1. 主鍵約束

輸入:

create table example1 (id int(3),name varchar(20),age int(3),primary key(id, name));

在這里插入圖片描述

2. 外鍵約束

create table example2(id int primary key, name varchar(20));create table example2sub(id int primary key, name varchar(20), age int, c_id int, constraint p_c_id foreign key(c_id) references example2(id));

在這里插入圖片描述
p_c_id是外鍵約束的名字,c_id是添加了外鍵約束的列,id是父表中定義的主鍵列

3. 非空約束

create table example3(id int, name varchar(20) not null);

在這里插入圖片描述

4. 唯一約束

create table example4(id int unique, name varchar(20));

在這里插入圖片描述

5. 使用默認(rèn)約束

create table example5(id int, name varchar(20) default "hahaha");

在這里插入圖片描述

6. 設(shè)置id為自增列

create table example6(id int primary key auto_increment, name varchar(20));

在這里插入圖片描述

5. 查看數(shù)據(jù)表

先選擇數(shù)據(jù)庫(kù),再查看該數(shù)據(jù)庫(kù)的表

use test;show tables;

在這里插入圖片描述
查看表中各個(gè)列的定義:

desc example4;

在這里插入圖片描述

6. 修改數(shù)據(jù)表

1. 修改表名

alter table example1 rename my_example1;

在這里插入圖片描述

2. 修改表的字段類型

alter table my_example1 modify name varchar(55);

在這里插入圖片描述

3. 修改表的字段名

alter table my_example1 change name my_name varchar(55);

在這里插入圖片描述

4. 為表添加字段

alter table my_example1 add column gender varchar(2) not null;

在這里插入圖片描述

5. 刪除字段

alter table my_example1 drop column age;

在這里插入圖片描述

6. 調(diào)整字段的位置

將my_example1表中的my_name改為第一列

alter table my_example1 modify my_name varchar(55) first;

在這里插入圖片描述

將id調(diào)整到gender后

alter table my_example1 id int after gender;

在這里插入圖片描述

7. 刪除表的外鍵約束

alter table example2sub drop foreign key p_c_id;

在這里插入圖片描述

8. 刪除數(shù)據(jù)表

drop table example4, example5, example6;

在這里插入圖片描述

7. 數(shù)據(jù)表的操作

1. 新增數(shù)據(jù)

create table user(id int primary key auto_increment, name varchar(100), age int, phone_num varchar(20));insert into user(name, age, phone_num) values('xiaoli', 21, 111), ('qiansan', 18, 222), ('zhangsan', 30, 333);

在這里插入圖片描述

2. 查詢數(shù)據(jù)

查詢?nèi)繑?shù)據(jù)

select * from user;

在這里插入圖片描述
查詢年齡大于20的用戶

select name, age from user where age > 20;

在這里插入圖片描述

3. 修改數(shù)據(jù)

將用戶id為1的年齡更新為22

update user set age = 22 where id = 1;

在這里插入圖片描述

4. 刪除數(shù)據(jù)

刪除年齡在25以上的用戶

delete from user where age > 25;

在這里插入圖片描述

5. replace

如果表中存在相同主鍵的數(shù)據(jù),replace的作用相當(dāng)于修改操作;否則就是插入操作

select * from user;replace into user(id, name, age, phone_num) values(1, 'xiaoli', 21, 444), (2, 'qiansan', 18, 888), (3, 'zhangsan', 30, 999);select * from user;

在這里插入圖片描述

Python操作MySQL

1. 連接數(shù)據(jù)庫(kù)

數(shù)據(jù)庫(kù)的訪問(wèn)是通過(guò)連接對(duì)象來(lái)實(shí)現(xiàn)的。程序模塊中必須提供連接對(duì)象構(gòu)造函數(shù)。

import pymysql# connect是連接對(duì)象構(gòu)造函數(shù)
# db是connect對(duì)象實(shí)例
db = pymysql.connect(# 連接的數(shù)據(jù)庫(kù)服務(wù)器主機(jī)名,默認(rèn)為本地主機(jī)(localhost)host = 'localhost',	# 連接數(shù)據(jù)庫(kù)的用戶名,默認(rèn)為當(dāng)前用戶user = 'root',# 連接密碼password = '020202ly',# 連接的數(shù)據(jù)庫(kù)名database = 'testmysql',
)# 創(chuàng)建一個(gè)游標(biāo)對(duì)象
cursor = db.cursor()cursor.execute("select version()")# fetchone():從查詢結(jié)果中獲取下一行數(shù)據(jù),返回值為一個(gè)值的序列,如果沒(méi)有更多數(shù)據(jù)則返回None。
# 通過(guò)游標(biāo)獲取sql語(yǔ)句執(zhí)行的結(jié)果。如果沒(méi)有下面的代碼,則執(zhí)行python代碼并不能看到在MySQL中執(zhí)行的結(jié)果。
result = cursor.fetchone()print(result)# 關(guān)閉游標(biāo)
cursor.close()# 關(guān)閉數(shù)據(jù)連接
db.close()

若程序成功執(zhí)行,則會(huì)打印MySQL的版本。
在這里插入圖片描述

2. 創(chuàng)建表

import pymysqldb = pymysql.connect(host = 'localhost',user = 'root',password = '020202ly',database = 'testmysql',charset = 'utf8'
)cursor = db.cursor()sql = """
create table example(id int not null auto_increment,name varchar(45),age int null,primary key(id))
"""# 運(yùn)行sql語(yǔ)句,創(chuàng)建數(shù)據(jù)表example
cursor.execute(sql)# 查詢創(chuàng)建的新表的結(jié)構(gòu)
cursor.execute("desc example")
# fetchall():從查詢結(jié)果中獲取結(jié)果的所有行數(shù)據(jù),返回值包含序列的序列。
result = cursor.fetchall()print(result)cursor.close()db.close()

執(zhí)行結(jié)果在這里插入圖片描述
使用pprint.pprint(result)打印出來(lái)的結(jié)果如下:
在這里插入圖片描述
可以看到使用pprint打印的結(jié)果可讀性更強(qiáng),看著更舒服。

pprint也是python中的一個(gè)打印模塊,在使用前需要導(dǎo)入包。pprint()的作用也就是使打印出來(lái)的數(shù)據(jù)結(jié)構(gòu)更加完整,每行為一個(gè)數(shù)據(jù)結(jié)構(gòu),更加方便閱讀打印輸出結(jié)果。

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

需要注意的是,在execute后需要調(diào)用commit方法提交對(duì)數(shù)據(jù)的修改,否則數(shù)據(jù)并不會(huì)真的插入到數(shù)據(jù)庫(kù)中。

import pymysqldb = pymysql.connect(host = 'localhost',user = 'root',password = '020202ly',database = 'testmysql',charset = 'utf8'
)cursor = db.cursor()sql = """insert into example(id, name, age) values(1, '小明', 20), (2, '小紅', 40)
"""# 運(yùn)行sql語(yǔ)句,創(chuàng)建數(shù)據(jù)表example
try:cursor.execute(sql)db.commit()print('數(shù)據(jù)提交成功!!')
except Exception as e:# 調(diào)用此方法將導(dǎo)致數(shù)據(jù)庫(kù)回滾到事事務(wù)開(kāi)始時(shí)的狀態(tài)。# 關(guān)閉數(shù)據(jù)庫(kù)連接之前沒(méi)有明確調(diào)用commit()提交數(shù)據(jù)更新,將導(dǎo)致一個(gè)隱含的回滾動(dòng)作,rollback()被執(zhí)行。db.rollback()cursor.close()
db.close()

在這里插入圖片描述

4. 查詢數(shù)據(jù)

用fetchone()方法返回一行數(shù)據(jù),用fetchall()方法返回多行數(shù)據(jù)。

import pymysql
import pprint
db = pymysql.connect(host = 'localhost',user = 'root',password = '020202ly',database = 'testmysql',charset = 'utf8'
)cursor = db.cursor()cursor.execute("select * from example")# fetchone方法
result1 = cursor.fetchone()
print("fetchone: ")
pprint.pprint(result1)# 在第一次查詢獲取數(shù)據(jù)后一定要再執(zhí)行一次查詢語(yǔ)句,因?yàn)樯厦鎓etchone獲取了一條數(shù)據(jù),游標(biāo)向后移動(dòng),就只剩一條數(shù)據(jù)了。
cursor.execute("select * from example")# fetchall方法
result2 = cursor.fetchall()
print("fetchall: ")
pprint.pprint(result2)cursor.close()
db.close()

在這里插入圖片描述

5. 更新數(shù)據(jù)

更新操作和插入操作類似,在修改完成后需要調(diào)用commit方法提交修改。

import pymysql
import pprint
db = pymysql.connect(host = 'localhost',user = 'root',password = '020202ly',database = 'testmysql',charset = 'utf8'
)cursor = db.cursor()cursor.execute("select * from example")# fetchone方法
result = cursor.fetchall()
print('更新前')
pprint.pprint(result)cursor.execute('update example set age = 30 where id = 2')cursor.execute("select * from example")# fetchall方法
result = cursor.fetchall()
print("更新后")
pprint.pprint(result)cursor.close()
db.close()

在這里插入圖片描述

6. 刪除數(shù)據(jù)

刪除數(shù)據(jù)操作完后也需要調(diào)用commit方法提交

import pymysql
import pprint
db = pymysql.connect(host = 'localhost',user = 'root',password = '020202ly',database = 'testmysql',charset = 'utf8'
)cursor = db.cursor()cursor.execute("select * from example")
result = cursor.fetchall()
print('刪除前')
pprint.pprint(result)cursor.execute('delete from example where id = 2')
cursor.execute("select * from example")
result = cursor.fetchall()
print("刪除后")
pprint.pprint(result)cursor.close()
db.close()

在這里插入圖片描述

http://www.risenshineclean.com/news/53047.html

相關(guān)文章:

  • 設(shè)置個(gè)網(wǎng)站要多少錢黑馬培訓(xùn)是正規(guī)學(xué)校嗎
  • 網(wǎng)站如何做導(dǎo)航條下拉菜單收錄入口在線提交
  • 建網(wǎng)站要學(xué)什么手機(jī)優(yōu)化大師官方免費(fèi)下載
  • 電商網(wǎng)站開(kāi)發(fā)實(shí)驗(yàn)報(bào)告seo算法是什么
  • 手機(jī)上能不能制作網(wǎng)站開(kāi)發(fā)百度一下百度知道
  • 江蘇做網(wǎng)站公司抖音搜索seo
  • 青島網(wǎng)站推廣優(yōu)化百度號(hào)碼認(rèn)證
  • 東營(yíng)做網(wǎng)站優(yōu)化價(jià)格百度seo排名推廣
  • 株洲做網(wǎng)站公司品牌seo如何優(yōu)化
  • 做本地化的返利網(wǎng)站怎么樣營(yíng)銷型網(wǎng)站方案
  • 合肥做網(wǎng)站一般多少錢百度seo不正當(dāng)競(jìng)爭(zhēng)秒收
  • asp如何做網(wǎng)站四川seo技術(shù)培訓(xùn)
  • 寧波網(wǎng)站建設(shè)方案咨詢域名備案官網(wǎng)
  • 海南新聞最新消息太原seo管理
  • 請(qǐng)人做網(wǎng)站收費(fèi)多少錢杭州谷歌推廣
  • 鄧州做網(wǎng)站網(wǎng)站建設(shè)的推廣渠道
  • 創(chuàng)建網(wǎng)站商城新冠疫情最新數(shù)據(jù)
  • 女人脫內(nèi)衣褲給男人做網(wǎng)站關(guān)鍵詞優(yōu)化seo多少錢一年
  • 政務(wù)公開(kāi)既網(wǎng)站信息化建設(shè)會(huì)議十大電商代運(yùn)營(yíng)公司
  • 個(gè)人執(zhí)業(yè)資格注冊(cè)查詢搜索引擎優(yōu)化排名品牌
  • 自己網(wǎng)站上做淘寶搜索引擎百度關(guān)鍵詞推廣工具
  • 網(wǎng)站推廣項(xiàng)目平臺(tái)推廣廣告宣傳詞
  • 微網(wǎng)站開(kāi)發(fā)中國(guó)十大網(wǎng)站排名
  • 網(wǎng)站源碼綁定域名萬(wàn)秀服務(wù)不錯(cuò)的seo推廣
  • 視頻網(wǎng)站如何推廣電商網(wǎng)站分析
  • 為什么要做網(wǎng)站首頁(yè)設(shè)計(jì)汕頭seo優(yōu)化項(xiàng)目
  • 建德網(wǎng)站優(yōu)化公司百度快照優(yōu)化的優(yōu)勢(shì)是什么
  • 西直門網(wǎng)站建設(shè)公司如何建立自己的網(wǎng)站
  • 如何用ps做網(wǎng)站效果圖百度電腦版網(wǎng)頁(yè)版
  • wordpress增加網(wǎng)址大全seo優(yōu)化一般多少錢