mysql总复习

扫码查看

数据库操作

库操作

create database 库名 charset utf8;        //创建表

show databases;         //查看所有库
show create database 库名;    // 查看创建库的语句
select database();          //查看当前的数据库
use 库名;             //使用数据库

drop database 库名;       //删除库

alter database 库名 charset utf8;     //更改库字符编码

表操作

// 创建表
create table t1 (
    id int primary key,
    name varchar(32)
) charset utf8;



// 修改表名
alter table t1 rename t11;
// 添加字段
alter table 表名 add 字段名 列类型 ;
alter table 表名 add 字段名 列类型 first;
alter table 表名 add 字段名 列类型 after 字段;
// 修改字段名
alter table 表名 change 旧字段名 新字段名 数据类型;
//修改字段属性
alter table 表名 modify 字段名 新数据类型;



// 删除字段
    alter table 表名 drop 字段名;
// 删除表
    drop table 表名;



// 查看所有表
show tables;
// 查看字段
desc 表名;
// 查看创建表的语句
show create table 表名


// 复制表结构
create table 表名 like 旧表名;

数据行操作

// 增加数据行
    insert into 表名(列1,列2) values (值1,值2);


// 查询表的数据
    select * from 表名;


// 指定列修改值
    update 表名 set 列1 = 新值1,列2=新值2,where 条件;


// 根据条件删除列
    delete from 表名 where 条件;
//全选删除
    truncate 表名;

表关系操作

单表操作

1.// 分组 group by
    将所有记录按照某个相同的字段分类
    select count(1),字段名 from 表名 group by 字段名;
2.// 二次筛选 having
    可以在分组的基础上进行二次筛选
    select id avg(age) from 表名 group by id having avg(age)>30;
3.// 排序 order by
    将所有记录按照某一字段(id)进行排序 desc降序,asc升序
    select * from 表名 order by id;
    select * from 表名 order by id desc ;     //降序
    select * from 表名 order by id desc,age asc;//id降序 age升序
4.//限制,分页 limit
    可以限制取数据的行数  limit m,s;  // m代表第一行索引(0开始),s取几行
    select * from 表名 limit 5;   //取5行数据
    select * from 表名 limit 1,5;     //从第二行取5条数据

外键创建

constraint 外键名 foreign key (想要约束的字段) references 引用表(字段);

  约束     外键名      外键            参考      参考的表中字段
constraint 外键名 foreign key (外键) references 引用表(字段)

constraint 外键名 foreign key (外键的约束) references 引用表(字段)

多表联查

left join on

select * from 表a left join 表b on 表a.字段= 表b.字段;

right join on

select * from 表a right join 表b on 表a.字段 = 表b.字段

inner join on

select * from 表a inner join 表b on 表a.字段 = 表b.字段

pymysql模块

安装模块    pip install pymysql
1.导入模块
import mysql

2.连接数据库参数
    conn = pymysql.connect(host='localhost',user='root',password='',database='test',charset='utf8')

3.设置游标(返回的是字典类型)
    cursor = conn.cursor(cursor = pymysql.cursor.DictCursor)

4.执行sql语句
    sql = 'select * from userinfo'
    cursor.execute(sql)         // 执行一个
    cursor.executemany()        // 执行多个

5.获取数据
    res = cursor.fetchall()         // 取出所有
    res = cursor.fetchone()         // 取出一个
    res = cursor.fetchmany(size)     // 取出size个数据

6.关闭游标与连接
    conn.close()
    cursor.close()

索引

主键索引

primary key     //主键索引
    加速查找 + 不能重复 + 不能为空

// 增加主键索引
    1.// 创建表时
    create table t1(id int primary key)charset utf8;
    2.// 添加
    alter table 表名 add primary key(字段名);
    alter table 表名 change 旧字段 新字段 列类型 primary key;
    alter table 表名 modify 字段名 新类型 primary key;

// 删除主键索引
    alter table 表名 drop primary key;

唯一索引

unique()
    加速查找 + 不能重复

// 增加唯一索引
    1.创建表时设置
    create table 表名( id int , unique 索引名(id) );
    2.添加
    alter table 表名 add unique index 索引名(字段名);
    create unique index 索引名 on 表名 (字段名)

//删除唯一索引
     alter table 表名 drop index 索引名;

普通索引

index()
    加速查找

// 增加普通索引
    1.//创建时添加
    create table 表名(id int ,index 索引名(id))
    2.//添加
    alter table 表名 add index 索引名(字段名);
    create index 索引名 on 表名(字段名);

// 删除普通索引
    alter table 表名 drop index 索引名;
01-06 15:21
查看更多