Python day42:数据库增删改查进阶语句:分组/排序/限制/通配符/连表/外键变种(一对一及多对多)

## 外键的变种:

```python
1:唯一索引:unique()
   create table t5(id int,numint,unique(num))engine=Innodb charset=utf8;
    作用:    
        num列的值不能重复
        加速查找
   create table t6(id int,num int,unique(id,num))engine=Innodb charset=utf8;
联合唯一索引作用:(可以联合多个值)    
            num列和id列的值的组合不能重复
            加速查找   
外键:一对一类型:
        通过unique实现,将外键值通过unique约束,这个值将不能重复.
外键:多对多类型:
        也是通过unique实现,将多个外键联合通过unique约束,联合的值不能重复!
```

## 数据行的增删该查 进阶语句:

```python
增:
insert into 表名 (列名1,列名2) values (值1,值2),(值1,值2),(值1,值2)
insert into 表名 (列名1,列名2) select 列名1,列名2 from表名
删除:
    delete from 表名 where id >=10
    delete from 表名 where id=10 and/or name="owen"
修改:
    update 表名 set name='egon',age=23 where id>=10;
查询:
    select * from 表名 where  id >10 and id<15;
    select * from 表名 where  id > 10;
    between and: 闭区间 
    select * from t4 where id between 9 and 12;
    in: 在某一个集合中
    select * from t4 where id in (9,10,11....);
    
    # select * from t4 where id in (select id from t3 where id between 2 and 4) 可以这样使用的, 但是不建议使用;
通配符:  配合 like  使用
    alex
    select * from 表 where name like 'ale%'  - ale开头的所有(多个字符串)
    select * from 表 where name like 'ale_'  - ale开头的所有(一个字符
限制条件:
    select * from 表名 limit 索引偏移量, 取出多少条数据;
    select * from t3 limit 0, 10;  第一页
    elect * from t3 limit 10, 10;  第二页
    page = input('page:')    
    page 索引偏移量  数据量(offset)
                                      1         0              10
                                      2         10             10
                                      3         20             10
                                      4         30             10
    age   (page-1)*offset   offset
    分页核心SQL:
        'select * from t3 limit (page-1)*offset, offset;
排序:
    order by降序:select * from t4 order by 列名 desc; descending
        升序:select * from t4 order by 列名 asc; ascending
    多列:create table t7(id int auto_increment primary key,num int not null default 0,age int not null default 0
)charset=utf8;
        insert into t7 (num, age) values (2, 12),(3,13),(4, 12);
        select * from t4 order by num desc, name asc;
如果前一列的值相等的话, 会按照后一列的值进行进一步的排序.
分组:
select age, 聚合函数(count(num)/sum(num)/max(num)/min(num)/avg(num)) from 表名 group by 列名;
    select age, avg(num) from t7 group by age;
    select age, count(num) from t7 group by age;
    select age, count(num) as cnt from t7 group by age;  显示别名 as
    aving的二次删选:
        select age, count(num) as cnt from t7 group by age  having cnt>1;
    here 和 having的区别:
    1). having与where类似,可筛选数据
    2). where针对表中的列发挥作用,查询数据
    3). having针对查询结果中的列发挥作用,二次筛选数据, 和group by配合使用
 
连表操作:
    select * from userinfo, department; (笛卡尔积)
    select * from userinfo, department where userinfo.depart_id=department.id;
    左连接:select * from userinfo left join department on userinfo.depart_id=department.id;
    左边的表全部显示, 右边没有用到不显示
    右连接:select * from userinfo right join department on userinfo.depart_id=department.id;
    右边的表全部显示, 左边没关联的用null表示
    内连接:左右两边的数据都会显示
ps: a.只需要记住左连接 left join
    b.可以连接多张表 通过某一个特定的条件
    注意查询的顺序:
    select name,sum(score) from 表 where id > 10 group by score having age> 12  order by age desc limit 2, 10 
    
```

猜你喜欢

转载自www.cnblogs.com/huhongpeng/p/11042359.html
今日推荐