七、MySQL表的创建
字段类型
1、数值类型
类型 | 大小 | 范围(有符号) | 范围(无符号) | 用途 |
tinyint | 1字节 | (-128,127) | (0,255) | 小整数值 |
int | 4字节 | (-2147483648, 2147483647) | (0,4294967295) | 大整数值 |
float | 4字节 | 单精度浮点型 | ||
double | 8字节 | 双精度浮点型 |
创建表语句
mysql> create table testnum(
-> ttinyint tinyint,
-> tint int,
-> tfloat float(6,2),
-> tdouble double(6,2),
-> );
创建表的主体结构:
create table if not exists 表名(
字段名称 字段类型 约束条件 字段说明,
字段名称 字段类型 约束条件 字段说明,
...
)
表插入数据语句
指定字段名称插入值
insert into 表名(字段1,字段2...) values(值1,值2...)
不指定字段插入之
insert into 表名 values(值1,值2...)
2、字符串类型
类型 | 大小 | 用途 |
char | 0-255字节 | 定长字符串 |
varchar | 0-255字节 | 变长字符串 |
text | 0-65535字节 | 长文本数据 |
longtext | 0-4294697295字节 | 极大文本数据 |
字符串类型注意事项:
- char和varchar的区别
- char执行效率高于varchar (但占用空间大)
- varchar相对于char节省空间
- char和varchar 类型的长度范围都在0-255之间
- varchar类型传入的值小于给定的长度 不会使用空格填充
八、INSERT 数据的添加
- 指定字段添加值
insert into 表名(字段1,字段2....) values(值1,值2...)
insert into user(sex,username) values(0,'lucky'); - 不指定字段添加值
insert into 表名 values(值1,值2...)
insert into user values(null,0,'lucky','我是lucky老师'); - 指定字段添加多个值
insert into 表名(字段1,字段2....) values(值1,值2...),(值1,值2...)...
insert into user(sex,username) values(1,'苍苍'),(0,'蒹葭'); - 不指定字段添加多个值
insert into 表名 values(值1,值2...),(值1,值2...)...
insert into user values(null,1,'xxx','xxx'),(null,0,'xxl','xxl');
注意事项: 指定字段与不指定字段在添加值的时候 按照从左至右依次对应给值
九、SELECT查询
- 不指定字段的查询(不建议)
select * from 表名 - 指定字段的数据查询(建议)
select 字段名1,字段名2... from 表名
select username,userinfo from user; - 对查询的字段起别名
select username as u from user;
select username u from user;
十、UPDATE修改
- 修改一个字段的值
update 表名 set 字段名=值;
update user set username='帅气的lucky' where id = 3; - 修改多个字段的值
update 表名 set 字段名1=值1,字段名2=值2...;
update user set sex=0,userinfo='xxx的个人简介' where id=7; - 给字段的值在原有的基础上改变值
update user set sex=sex+2;
注意:
在进行数据的修改的时候 一定记得给定where条件 如果没有给定where条件 则修改的为整张表当前字段的值
十一、DELETE 删除
主体结构:
delete from 表名 [where ...]
实例:
delete from user; 删除user表中所有的数据
注意:
删除 一定注意添加 where 条件 否则会删除整张表中的数据 并且auto_increment自增所记录的值不会改变 所以需要将自增归位
truncate 表名; 清空表数据
十二、WHERE条件
实例表结构:
+----------+-------------+------+-----+-----------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+-----------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | sex | tinyint(4) | NO | | 1 | | | username | varchar(20) | YES | | NULL | | | age | tinyint(4) | NO | | 18 | | | userinfo | varchar(50) | NO | | 我是帅气的lucky老师啊 | | +----------+-------------+------+-----+-----------------------+----------------+
(1) 比较运算符
>
将id大于5 的性别 更改为0 年龄改为20岁
update user set sex=0,age=20 where id>5;<
将id小于3 的性别 更改为0 年龄改为23岁
update user set sex=0,age=23 where id<3;
查看id小于4的 性别和用户名的字段数据
select sex,username from user where id<4;>=
删除 id大于等于6的数据
delete from user where id>=6;<=
查询年龄小于等于23的数据
select * from user where age<=23;- =
查询性别为0的数据
select * from user where sex=0; !=/<>
查询 用户名不等于lucky的所有数据
select * from user where username!='lucky';
select * from user where username<>'lucky';
(2) 逻辑运算符
- and 逻辑与 俩侧为真结果为真
查询年龄在18到23之间 不包括本身
select * from user where age>18 and age<23;
修改年龄为30 id大于1 小于等于2
update user set age=30 where id>1 and id<=2; - or 逻辑或运算 俩侧条件满足一侧就可以
select * from user where age=10 or age=30;
select * from user where age>=10 or age<=30;
(3) order by 排序 升序/降序
升序
查询数据 按照年龄升序(默认)
select * from user order by age;
select * from user order by age asc;
查询数据 按照年龄降序
select * from user order by age desc;
(4) limit 取值
结构:
limit x 取出x条数据
limit x,y 从x的位置取出y条数据
取出3条数据
select * from user limit 3;
取出年龄最大/最小的一条数据
select * from user order by age desc limit 1;
select * from user order by age limit 1;
(6) like 模糊查询
- ’%字符‘ 查询以字符结尾的数据
查询以三字为结束的username的数据
select * from user where username like '%三'; - '字符%' 查询以字符开头的数据
select * from user where username like '赵%'; - '%字符%' 查询包含字符的数据
查询 userinfo中包含lucky的数据
select * from user where userinfo like '%lucky%';
十三、 聚合函数
- count 统计个数
- max 最大值
- min 最小值
- Sum 求和
- avg 求平均数
select count(*) as count,max(age),min(age),avg(age),sum(age) from user;
十四、 数据库的导入导出
- 导入
mysql -uroot -p 库名<demo.sql - 导出
mysqldump -uroot -p 库名>demo.sql
十五、Python操作MySQL
安装:
pip install pymysql
使用: import pymysql
(1) 链接MySQL数据库
db = pymysql.connect(主机名,用户名,密码,数据库名)
db = pymysql.connect(host='localhost', user='root', password='123456', database='test')
(2) 设置字符集
db.set_charset('utf8')
(3) 创建游标对象
cursor = db.cursor()
(4) 执行SQL语句
cursor.execute(sql语句)
(5) 获取结果集
获取所有
cursor.fetchall()
获取一条
cursor.fetchone()
(6) 获取受影响的行数
cursor.rowcount
(7) 事物
pymysql默认开启了事物处理 所以在添加数据的时候 需要commit 或者rollback
实例:
try:
sql = 'insert into user values(null,1,"曹操",100,"曹操第一奸雄","魏国")'
print(sql)
cursor.execute(sql)
db.commit()
except:
db.rollback()
对于支持事务的数据库, 在Python数据库编程中,当游标建立之时,就自动开始了一个隐形的数据库事务。
commit()方法游标的所有更新操作,rollback()方法回滚当前游标的所有操作。每一个方法都开始了一个新的事务。
(8) 关闭数据库连接
db.close()
(9) 拼凑正常完整的sql语句
print("select name,password from user where name=\""+username+"\"")
print("select name,password from user where name='"+username+"'")
print("select name,password from user where name='%s'"%(username))
print("select name,password from user where name='{}'".format(username))
print(f"select name,password from user where name='{username}'")