Python mysql数据库操作

  • 1

    数据库是按照一定的数据结构来存储和管理数据的仓库,数据库可分为关系数据库和非关系数据库

    mysql是数据库管理软件

      库级操作语句
      show databases; 显示所有的库
      create database 库名称; 创建一个库
      drop database 库名称;  删除一个库
      use 库名称; 进入一个库
      表级操作语句
      show tables;  显示所有的表
      create table 表名称 (id int , name char(20)...);  创建一个表
      show create table 表名称; 显示创建的表的信息
      drop table 表名称; 删除一个表
      表数据操作
      
      插入数据
      insert into 表名称 (id, name, age...) values (1, '名字', 20...); 指定字段插入
      
      insert into 表名称 values (1, '名称', 20); 全字段插入
      
      insert into 表名称 (id, name, age) values (3, 'sb', 20), (4, 'id', 30), (5, 'hb', 18);  多行插入
      
      
      查询数据
      select name = 'sb' from 表名称; 指定字段查询
      
      select *from 表名称; 全字段查询
      
      select *from 表名称 where name='sb'; 条件查询
      
      
      
       修改数据
       update 表名称 set name = '--' where name='sb'; 修改值
       
       
       删除数据
       delete from 表名称; 清空表
       delete from 表名称 where id = 1; 条件删除
    
  • 2

    数据库查询功能

    有个表格 myTable;

      筛选条件查询
      * 比较运算符
      select *from myTable where id < 5;
      select *from myTable where id > 5;
      select *from myTable where id != 5;
      select *from myTable where id = 5;
      select *from myTable where id <= 5;
      select *from myTable where id >= 5;
      
      为空
      select *from myTable where id is null;
      不为空
      select *from myTable where id is not null;
      
      
      
      * 逻辑运算符
      查询name 是liping 并且age是20的
      select *from myTable where name = 'liping' and age = 20;
      
      查询name 是liping 或者 age是20的
      select *from myTable where name = 'liping' or age = 20;
      
      查询name 不是liping的
      select *from myTable where not name = 'liping';
      
      
      
      
      *排序
      根据age排序 默认是升序
      select *from myTable order by age;
      
      
      根据age降序排序
      select *from myTable order by age desc;
      
      
      
      *限制查询
      查询前两个
      select *from myTable limit 2;
      
      查询从第二个开始,往后两个
      select *from myTable limit 2,2;
      
      
      去重查询
      select distinct *from myTable;
      
      
      模糊查询  %多个字符 _单个字符
      select *from mytable where name like 'l%';
      
      select *from mytable where name like 'l_';
      
      
      
      范围查询
      >3 <5
      select *from myTable where id >3 and id < 5;
      
      
      在1到3之间
      select *from myTable where id between 1 and 3;
      
      
      查询包含在 in ()里面到内容
      select *from myTable where name in ('liping', 'h');
      聚合查询
      根据age计算有几条数据        
      select count(age) from myTable;
      
      平均值
      select avg(age) from myTable;
      
      和
      select sum(age) from myTable;
      
      最大值
      select max(age) from myTable;
      
      最小值
      select count(age) from myTable;
      
      
      根据字段查询出一组数据,显示成一行
      select group_concat(name) from myTable;
      
      
      查询某个字段 查询到字段一定要跟 group by后面到字段一样 查询的结果是去重的
      select name from myTable group by name;
      
      
      
      首先查询 id > 3的数据, 然后再筛选id < 6的数据
      select id from myTable where id > 3 group by id having id < 6;
      子查询
      查询age< 平均值的数据
      select *from myTable where age < (select avg(age) from myTable);
      表连接查询
      笛卡尔连接查询 前一个表的每个字段和后面的表每个数据配对
      select *from myTable inner join myTable2;
      
      
      内连接 两个表的某个字段相等的数据查询出来
      select *from myTable as a inner join myTable2 as b on a.id = b.id;
      
      
      左连接查询,以左边表为主
      select *from myTable as a left join myTable2 as b on a.id = b.id;
      
      
      右连接查询,以右表为主
      select *from myTable as a right join myTable2 as b on a.id = b.id;
    
  • 3

      表结构修改
    
      alter
      
      查询表结构
      desc 表名;
      
      修改表名
      alter table 表名 rename to 新名;
      
      修改字段名
      alter table 表名称 change 旧字段名  新字段名  字段类型;
      
      修改字段类型
      alter table 表名称 modify 字段名称  字段类型;
      
      
      增加字段
      alter table 表名称 add 字段名 字段类型;
      
      
      删除字段
      drop table 表名称 drop 字段名;
      约束条件
      default 默认值
      not null 不能为空
      unique key 唯一的key
      primary key 主键约束 = 非空 + 唯一
      auto_increment 自动增长
      
      //创建一个id主键自增长的表
      create table testTb (id int primary key auto_increment, name varchar(20) not null);
      
      
      //创建一个id主键自增长的表, 默认从100开始增长
      create table testTb (id int primary key auto_increment, name varchar(20) not null) auto_increment=100;
      
      
      //插入的时候可以插入空值或0, id自动增长
      insert into testTb values (null, 'aa'), (null, 'bb');
    
    
      //外键约束
      主表
      create table a (id primary key, name varchar(20));
      
      附表
      设置 b 表的 b_id 为外键属于 a 表 id 
      create table b (b_id primary key, age int , foreign key(b_id) references a(id));
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值