Mysql 入门

清疚 2021-09-03 06:17 453阅读 0赞

数据库就是一种特殊的文件,其中存储着需要的数据

一:操作数据库

1.1 命令行 连接

  1. mysql -uroot -p
  2. 回车后输入密码,当前设置的密码为mysql

1.2 查看所有数据库

  1. show databases;

1.3 使用数据库

  1. use 数据库名;

1.4 查看当前使用的数据库

  1. select database();

1.5 创建数据库

  1. create database 数据库名 charset=utf8;
  2. 例:
  3. create database python charset=utf8;

1.6 删除数据库

  1. drop database 数据库名;
  2. 例:
  3. drop database python;

二:数据表操作

2.1 查看当前数据库中所有表

  1. show tables;

2.2 查看表结构

  1. desc 表名;

2.3 创建表

  1. 例:创建班级表
  2. create table classes(
  3. id int unsigned auto_increment primary key not null,
  4. name varchar(10)
  5. );

2.4 修改表-添加字段

  1. alter table 表名 add 列名 类型;
  2. 例:
  3. alter table students add birthday datetime;

2.5 修改表-修改字段:重命名版

  1. alter table 表名 change 原名 新名 类型及约束;
  2. 例:
  3. alter table students change birthday birth datetime not null;

2.6 修改表-修改字段:不重命名版

  1. alter table 表名 modify 列名 类型及约束;
  2. 例:
  3. alter table students modify birth date not null;

2.7 修改表-删除字段

  1. alter table 表名 drop 列名;
  2. 例:
  3. alter table students drop birthday;

2.8 删除表

  1. drop table 表名;
  2. 例:
  3. drop table students;

2.9 查看表的创建语句

  1. show create table 表名;
  2. 例:
  3. show create table classes;

三 . MySQL 查询

3.1 普通查询

  • 查询所有字段

    select from 表名;
    例:
    select
    from students;

  • 查询指定字段

    select 列1,列2,… from 表名;
    例:
    select name from students;

  • 使用 as 给字段起别名

    select id as 序号, name as 名字, gender as 性别 from students;

  • 可以通过 as 给表起别名

    — 如果是单表查询 可以省略表明
    select id, name, gender from students;

    — 表名.字段名
    select students.id,students.name,students.gender from students;

    — 可以通过 as 给表起别名
    select s.id,s.name,s.gender from students as s;

  • 在select后面列前使用distinct可以消除重复的行

    select distinct 列1,… from 表名;
    例:
    select distinct gender from students;

3.2 条件查询

使用where子句对表中的数据筛选,结果为true的行会出现在结果集中

  • 语法如下:

    select from 表名 where 条件;
    例:
    select
    from students where id=1;

  • where后面支持多种运算符,进行条件的处理

    • 比较运算符 = > >= < <= != <>
    • 逻辑运算符 and or not
    • 模糊查询 like

      • %表示任意多个任意字符
      • _表示一个任意字符

        1. select * from students where name like '黄%' or name like '%靖';
    • 范围查询

      • in表示在一个非连续的范围内

        1. select * from students where id in(1,3,8);
      • between … and …表示在一个连续的范围内

        1. select * from students where id between 3 and 8;
  • 空判断 判空is null

    1. select * from students where height is null;

优先级

  • 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符

3.3 排序查询

为了方便查看数据,可以对数据进行排序

  1. select * from 表名 order by 1 asc|desc [,列2 asc|desc,...]
说明
  • 将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
  • 默认按照列值从小到大排列(asc)
  • asc从小到大排列,即升序
  • desc从大到小排序,即降序

例:显示所有的学生信息,先按照年龄从大–>小排序,当年龄相同时 按照身高从高–>矮排序

  1. select * from students order by age desc,height desc;

3.4 聚合函数

  • count(*)表示计算总行数,括号中写星与列名,结果是相同的
  • max(列)表示求此列的最大值
  • min(列)表示求此列的最小值
  • sum(列)表示求此列的和
  • avg(列)表示求此列的平均值

    select count(*) from students;

3.5 分组

  • group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组
  • group by + group_concat()

    • group_concat(字段名)可以作为一个输出字段来使用,
    • 表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合
  • group by + 集合函数

    • 通过group_concat()的启发,我们既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对这个值的集合做一些操作
  • group by + having

    • having 条件表达式:用来分组查询后指定一些条件来输出查询结果
    • having作用和where一样,但having只能用于group by
  • group by + with rollup

    • with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和

    select gender,group_concat(id) from students group by gender;
    +————+—————————+
    | gender | group_concat(id) |
    +————+—————————+
    | 男 | 3,4,8,9,14 |
    | 女 | 1,2,5,7,10,12,13 |
    | 中性 | 11 |
    | 保密 | 6 |
    +————+—————————+

    分别统计性别为男/女的人年龄平均值
    select gender,avg(age) from students group by gender;
    +————+—————+
    | gender | avg(age) |
    +————+—————+
    | 男 | 32.6000 |
    | 女 | 23.2857 |
    | 中性 | 33.0000 |
    | 保密 | 28.0000 |
    +————+—————+

    select gender,count() from students group by gender having count()>2;
    +————+—————+
    | gender | count(*) |
    +————+—————+
    | 男 | 5 |
    | 女 | 7 |
    +————+—————+

    select gender,count() from students group by gender with rollup;
    +————+—————+
    | gender | count(
    ) |
    +————+—————+
    | 男 | 5 |
    | 女 | 7 |
    | 中性 | 1 |
    | 保密 | 1 |
    | NULL | 14 |
    +————+—————+

3.6 分页

当数据量过大时,在一页中查看数据是一件非常麻烦的事情

语法

  1. select * from 表名 limit start,count

说明

  • 从start开始,获取count条数据

例1:查询前3行男生信息

  1. select * from students where gender=1 limit 0,3;

3.7 总结

查询的完整格式 ^_^ 不要被吓到 其实很简单 ! _ !

  1. SELECT select_expr [,select_expr,...] [
  2. FROM tb_name
  3. [WHERE 条件判断]
  4. [GROUP BY {col_name | postion} [ASC | DESC], ...]
  5. [HAVING WHERE 条件判断]
  6. [ORDER BY {col_name|expr|postion} [ASC | DESC], ...]
  7. [ LIMIT {[offset,]rowcount | row_count OFFSET offset}]
  8. ]
  • 完整的select语句

    select distinct *
    from 表名
    where ….
    group by … having …
    order by …
    limit start,count

  • 执行顺序为:

    • from 表名
    • where ….
    • group by …
    • select distinct *
    • having …
    • order by …
    • limit start,count
  • 实际使用中,只是语句中某些部分的组合,而不是全部

四:python 操作 MYSQL

25b9da767b9068e8bc44174e00f59986.png

4.1 引入模块

  • 在py文件中引入pymysql模块

    from pymysql import *

4.2 Connection 对象

  • 用于建立与数据库的连接
  • 创建对象:调用connect()方法

    conn=connect(参数列表)

  • 参数host:连接的mysql主机,如果本机是’localhost’

  • 参数port:连接的mysql主机的端口,默认是3306
  • 参数database:数据库的名称
  • 参数user:连接的用户名
  • 参数password:连接的密码
  • 参数charset:通信采用的编码方式,推荐使用utf8

4.3 对象的方法

  • close()关闭连接
  • commit()提交
  • cursor()返回Cursor对象,用于执行sql语句并获得结果

4.4 Cursor对象

  • 用于执行sql语句,使用频度最高的语句为select、insert、update、delete
  • 获取Cursor对象:调用Connection对象的cursor()方法

    cs1=conn.cursor()

4.5 对象的方法

  • close()关闭
  • execute(operation [, parameters ])执行语句,返回受影响的行数,主要用于执行insert、update、delete语句,也可以执行create、alter、drop等语句
  • fetchone()执行查询语句时,获取查询结果集的第一个行数据,返回一个元组
  • fetchall()执行查询时,获取结果集的所有行,一行构成一个元组,再将这些元组装入一个元组返回

4.6 对象的属性

  • rowcount只读属性,表示最近一次execute()执行后受影响的行数
  • connection获得当前连接对象

五:python mysql 增删改查

5.1 基础

  1. from pymysql import *
  2. def main():
  3. # 创建Connection连接
  4. conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8')
  5. # 获得Cursor对象
  6. cs1 = conn.cursor()
  7. # 执行insert语句,并返回受影响的行数:添加一条数据
  8. # 增加
  9. count = cs1.execute('insert into goods_cates(name) values("硬盘")')
  10. #打印受影响的行数
  11. print(count)
  12. count = cs1.execute('insert into goods_cates(name) values("光盘")')
  13. print(count)
  14. # # 更新
  15. # count = cs1.execute('update goods_cates set name="机械硬盘" where name="硬盘"')
  16. # # 删除
  17. # count = cs1.execute('delete from goods_cates where id=6')
  18. # 提交之前的操作,如果之前已经之执行过多次的execute,那么就都进行提交
  19. conn.commit()
  20. # 关闭Cursor对象
  21. cs1.close()
  22. # 关闭Connection对象
  23. conn.close()
  24. if __name__ == '__main__':
  25. main()

5.2 查询一行数据

  1. from pymysql import *
  2. def main():
  3. # 创建Connection连接
  4. conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
  5. # 获得Cursor对象
  6. cs1 = conn.cursor()
  7. # 执行select语句,并返回受影响的行数:查询一条数据
  8. count = cs1.execute('select id,name from goods where id>=4')
  9. # 打印受影响的行数
  10. print("查询到%d条数据:" % count)
  11. for i in range(count):
  12. # 获取查询的结果
  13. result = cs1.fetchone()
  14. # 打印查询的结果
  15. print(result)
  16. # 获取查询的结果
  17. # 关闭Cursor对象
  18. cs1.close()
  19. conn.close()
  20. if __name__ == '__main__':
  21. main()

5.3 查询多行数据

  1. from pymysql import *
  2. def main():
  3. # 创建Connection连接
  4. conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
  5. # 获得Cursor对象
  6. cs1 = conn.cursor()
  7. # 执行select语句,并返回受影响的行数:查询一条数据
  8. count = cs1.execute('select id,name from goods where id>=4')
  9. # 打印受影响的行数
  10. print("查询到%d条数据:" % count)
  11. # for i in range(count):
  12. # # 获取查询的结果
  13. # result = cs1.fetchone()
  14. # # 打印查询的结果
  15. # print(result)
  16. # # 获取查询的结果
  17. result = cs1.fetchall()
  18. print(result)
  19. # 关闭Cursor对象
  20. cs1.close()
  21. conn.close()
  22. if __name__ == '__main__':
  23. main()

发表评论

表情:
评论列表 (有 0 条评论,453人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mysql入门

    连接数据库 A:用命令连接mysql(注意-h -u -p之后都是直接连ip用户名和密码,没有空格,示列如下) mysql -h127.0.0.1 -uroot  -p12

    相关 mysql入门

    连接数据库 A:用命令连接mysql(注意-h -u -p之后都是直接连ip用户名和密码,没有空格,示列如下) mysql -h127.0.0.1 -uroot  -p12

    相关 Mysql入门

    一、SQL包含四种程序设计语言类别的语句: 1、数据定义语言Data Definition Language(DDL), DDL使我们有能力创建或删除表格。也可以定   

    相关 MySQL入门(上)

    什么是数据库? [数据库的详细介绍][Link 1] 想详细了解数据库是什么的可以直接点击上面的连接查看. 在此我们只需要简单了解一下数据库即可. 从字面

    相关 MySQL入门

    MySQL入门 数据库简介 用途:用于存储生活的一切数据,如:身份、住房、车票、网站、… 概念:数据库服务器、数据库、数据表、一行数据(一条)、一列数据

    相关 MySQL入门

      目录 数据库引擎 索引 数据结构 b+tree hash full-text b+tree索引 聚集索引 非聚集索引 b+tree索引的应用 主键索

    相关 Mysql 入门

    > 数据库就是一种特殊的文件,其中存储着需要的数据 一:操作数据库 1.1 命令行 连接 mysql -uroot -p 回车后输入密码,当前