網(wǎng)站開(kāi)發(fā)總結(jié)文檔百度問(wèn)答官網(wǎng)
文章目錄
- 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()