目录
1. 准备
1. 安装
参考(52条消息) MYSQL下载及安装完整教程_创世界---的博客-CSDN博客
2. 启动与链接
3. 测试链接
查看已有数据库
注:Mysql以 ; 为一条语句间隔。
2. 数据库管理
- 查看已有的数据库(文件夹)
show databases;
- 创建数据库
create database 数据库名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; create database exp_1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- 删除数据库
drop database 数据库名字; drop database exp_1;
- 进入数据库
use 数据库名字; use exp_1;
- 查看数据库中的所有数据表
show tables;
3. 数据表管理
3.1 操作1
- 创建表[先进入某数据库,再创建]
create table 表名称( 列名称 类型, 列名称 类型, 列名称 类型 )default charset=utf8;
一般采用主键唯一自增的方式创建,见tb_1。
create table tb_1_1( id int, name varchar(16) not null, -- 不允许为空 age int null -- 允许为空(默认) )default charset=utf8; create table tb_1_2( id int, name varchar(16), age int default 3 -- 插入数据时,age列的值默认为3 )default charset=utf8; create table tb_1( id int not null auto_increment primary key, -- 主键(不允许为空,不允许重复,且自增) name varchar(16), age int )default charset=utf8;
- 删除表
drop table 表名称;
- 查看表属性
desc 表名称;
- 表中插入数据
-- 单行插入 insert into 表名(列1名,列2名) values(列1值,列2值); -- 批量插入 insert into 表名(列1名,列2名) values(列1值,列2值),(列1值,列2值);
insert into tb_1(name,age) values("小胡",20); insert into tb_1(name,age) values("小尚",16),("小李",22);
查看表中数据
select * from tb_1;
3.2 常见数据类型
- tinyint
1. 有符号 -128 ~ 127(默认)
2. 无符号 0 ~ 255
create table tb_1_3( id int not null auto_increment primary key, age tinyint -- 默认有符号 -128~127 )default charset=utf8; create table tb_1_3( id int not null auto_increment primary key, age tinyint unsigned -- 设置为无符号0~255 )default charset=utf8;
- int
1. int -2147483648~2147483647
2. int unsigned 0~4294967295
- bigint
1. bigint
2. bigint unsigned
示例
create table tb_1_4(
id bigint not null auto_increment primary key,
salary int,
age tinyint
)default charset=utf8;
- float
- double
- decimal
准确的小数值,decimal(m, d),m是数字总个数(负号不算),d是小数点后的个数。m的最大值为65,d最大值为30。
create table tb_1_5( id bigint not null auto_increment primary key, price decimal(5, 2) )default charset=utf8; insert into tb_2(price) values(12.8); -- 12.8 insert into tb_2(price) values(2.6482); -- 2.65(四舍五入) insert into tb_2(price) values(1234.8); -- 错误ERROR
- char(m)
定长字符串。m<=255。
create table tb_1_6( id int not null auto_increment primary key, mobile char(11) )default charset=utf8;
char(11)固定用11个字符进行存储,若插入不足11个字符,底层也按11个字符存储。占用空间,查询速度快(对齐)。
- varchar(m)
变长字符串。m与编码有关,中文m = 65535/3。
create table tb_1_7( id int not null auto_increment primary key, mobile varchar(11) )default charset=utf8;
真实数据多长就存储多长。节省空间,查询速度慢一点(不对齐)。
- text
text65535个字符。
create table tb_1_8( id int not null auto_increment primary key, title varchar(11), content text )default charset=utf8;
- mediumtext
- longtext
- datetime
YYYY-MM-DD HH:MM:SS
- date
YYYY-MM-DD
3.3 案例--用户表
create table tb_2( id int not null auto_increment primary key, name varchar(11) not null, pwd char(64) not null, email varchar(64) not null, age tinyint, salary decimal(10,2), ctime datetime )default charset=utf8; insert into tb_2(name,pwd,email,age,salary,ctime) values("李华","123456","123@qq.com",19,10000.20,"2023-03-01 16:51:31");
https://dev.mysql.com/doc/refman/
4. 数据行操作
- 新增数据
-- 单行插入 insert into 表名(列1名,列2名) values(列1值,列2值); -- 批量插入 insert into 表名(列1名,列2名) values(列1值,列2值),(列1值,列2值);
- 删除数据
delete from 表名; -- 删除表内所有内容 delete from where 条件; -- 删除符合条件的
示例
delete from tb_1 where id = 1; -- 支持 = != > < >= <= delete from tb_1 where id = 2 and name="小谢"; -- 支持and or delete from tb_1 where id in (1,5); -- 删除id=1和id=5
- 修改数据
update 表名 set 列=值; update 表名 set 列=值,列=值; update 表名 set 列=值 where 条件;
示例
update tb_2 set password = "753951"; update tb_2 set email = "423@126.com" where id > 1; update tb_2 set age=age+10 where id > 2;
- 查询数据
select * from 表名; -- 查询所有数据 select 列名,列名 from 表名; -- 查询某些列的数据 select 列名,列名 from 表名 where 条件; -- 查询符合条件的某些列的数据
5. python操作MySQL
增删改查。
对于sql语句不要使用python的字符串格式化format,使用%s excute(,[])。
- 增加数据
import pymysql while True: user = input('用户名:') if user.upper() == 'Q': break; pwd = input('密码:') mobile = input('手机号:') # 1.连接MySQL 并对标到数据库,此处是unicom conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="", charset='utf8', db='unicom') cursor = conn.cursor() # 2.发送指令 #方式一 # cursor.execute("insert into admin(username,password,mobile) values('小明','123','123')") #方式二 sql = "insert into admin(username,password,mobile) values(%s,%s,%s)" cursor.execute(sql, [user, pwd, mobile]) #执行一个数据库查询和命令 #方式三 # sql = "insert into admin(username,password,mobile) values(%(n1)s, %(n2)s,%(n3)s)" # cursor.execute(sql, {'n1': '小明', 'n2': '123', 'n3':'123'}) conn.commit() # 提交当前事务 # 3.关闭 cursor.close() conn.close()
- 删除语句
import pymysql # 1.连接MySQL 并对标到数据库,此处是unicom conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="", charset='utf8', db='unicom') cursor = conn.cursor() # 2.发送指令 sql = "delete from admin where id > %s" cursor.execute(sql, [1,]) cursor.commit() # 3.关闭 cursor.close() conn.close()
- 修改语句
import pymysql # 1.连接MySQL 并对标到数据库,此处是unicom conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="", charset='utf8', db='unicom') cursor = conn.cursor() # 2.发送指令 sql = "update admin set mobile=%s where id > %s" cursor.execute(sql, ['1520', 1]) cursor.commit() # 3.关闭 cursor.close() conn.close()
- 查询语句
import pymysql # 1.连接MySQL 并对标到数据库,此处是unicom conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="", charset='utf8', db='unicom') cursor = conn.cursor() # 2.发送指令 sql = "select * from admin where id > %s" cursor.execute(sql, [1,]) data_list = cursor.fetchall() # 获取所有数据 for list in data_list: print(list) data = cursor.fetchone() # 获取第一条数据 print(data) # 3.关闭 cursor.close() conn.close()