zoukankan      html  css  js  c++  java
  • 外键变种介绍,数据库具体操作进阶

    唯一索引:使用unique来实现他的作用是让列的值在表中只能出现一次,作用其二是加速查找顺序。操作很简单就是将

    需要设置为唯一索引的列加入到unique的括号内。

        create table t5(
                            id int,
                            num int,
                            unique(num)
                        )engine=Innodb charset=utf8;

    联合唯一索引:是将查找频率较高的一个或多个一起加到unique中,可以方便查找。

    create table t6(
                            id int,
                            num int,
                            unique(id, num)
                        )engine=Innodb charset=utf8;

    作用是加入联合唯一索引的形成的数值组合不能重复,如(3,1)只能出现一次。,也起到一个加速查找的作用。

    一对一:类比手机一个手机账号只能注册一个淘宝账号。这样一张手机号列表和淘宝账号外键的对应关系就是一个手机号对应一个手机号。

                            手机号列表:
                                id    name     age  
                                1      zekai    15189656598987  
                                2      eagon    55461231841215
                                3      lxxx     451321531231513
                                4      owen     545451512313133
            
                            淘宝账号表:
                                id            url          user_id  (外键 + 唯一约束)
                                1       /linhaifeng       2
                                2       /zekai              1
                                3       /lxxx             3
                                4       /lxxx             4

    多对多:类比图书列表和作者列表,一个作者可以写出好几本书,一本书也可以是由多个作者合作写出的,这样的对应关系上就是双向得了,在建立外键时被外键关联的数据表需要被先建出来,但是在多对多的情况中,先建立哪张表都是不合适的。所以要建立第三张表用来主要存储图书表和作者表之间的对应关系。

    下面的例子是用户表和主机表中的对应关系,思路和表达的意思都是一个套路:

        用户表:
                                id    name    phone 
                                1    root1    1234
                                2    root2    1235
                                3    root3    1236
                                4    root4    1237
                                5    root5    1238
                                6    root6    1239
                                7    root7    1240
                                8    root8    1241
                                
                            主机表:
                            
                                id    hostname    
                                1    c1.com    
                                2    c2.com    
                                3    c3.com    
                                4    c4.com    
                                5    c5.com    
    
                            为了方便查询, 用户下面有多少台主机以及某一个主机上有多少个用户, 我们需要新建第三张表:
                                user2host:
                                
                                    id    userid    hostid
                                        1    1    1
                                        2    1    2
                                        3    1    3
                                        4    2    4
                                        5    2    5
                                        6    3    2
                                        7    3    4    
                            创建的时候, userid 和 hostid 必须是外键, 然后联合唯一索引 unique(userid, hostid)
                            
                            Django orm 也会设计

    数据行的操作:

     增加一行:insert  into 表名(列名1,列名2)values(值1,值2)

    增加多行:insert into 表名(列名1,列名2)values(值1,值2),(值1,值2),(值1,值2)...........

    同表中的内容复制  insert into 表名(列名1,列名2)select 列名1 ,列名2 from 表名      ————(注意select后面两个列名之间要有逗号隔开)

    删除    删除表中所有数据 :delete from 表名

    依据条件查找后删除:delete * from 表名 where id(> < >= <=  !=) 10  ——以id作为删除的条件删除表中数据

    delete * from 表名 where id=10 and name='xxx' 使用and 的意思是当两个条件都成立的时候才会执行删除指令

    delete * from 表名 where id=10 and name='xxx'  or 是指只要满足一个其中一个条件就会执行删除操作

    修改: update 表名 set name='xiaoming',age=23 where id>10

    查询:基操 select * from 表名;  select name,age from 表名;

    高操 :where 条件查询:select * from 表名 where id(> <  = <=  >=  !=)10;

               select * from 表名 where id>10 and id <15;

              between  and 闭区间查找: select * from 表名 where id between id between 9 and 12;

              in 在某个集合中select * from 表名 where id in (9,11,12,13)

               between and 和 in 之间可以联合起来形成子查询,可以使用但是会降低查找效率。

    通配符:   select *  from 表名 where name like 'ale%';      %表示匹配后面所有的字符

                                 select  * from 表名 where name like 'ale_'        _表示匹配后面一位的字符

    限制偏移量查询: select * from 表名 limit 索引偏移量,规定一次可以取出的数据量;

         select * from  表名 limit 10,10 表示从第0条开始取 一次取10条

         select * from  表名 limit 10,10 表示从第10条开始取 一次取10条

    分页的核心:select * from t3 limit (页数(page)减去1) * 规定的偏移量(offset),偏移量(offset)

    排序:order by 

      降序:select * from 表名 order by 列名 des(descen)

          升序: select * from 表名 order by 列名 acs(ascending)

    升序降序可以组合使用 select * from 表名 order by 列名 desc,name asc;

    如果前一列的值相等的话,会按照后一列的值进一步的排序

    分组:select age ,聚合函数(count(列名)/sum(列名)/max(列名)/min(列名)/avg(列名)) from 表名group by age;

    having 可以进行数据的二次筛选。

    select age ,count(列名) as cnt from 表名 group by age having cnt >1     聚合函数不好写可以起别名代替。

    注意查询的顺序:

    select name,sum(score) from 表 where id > 10 group by score having age> 12 order by age desc limit 2, 10

    where 和 having的区别:

    having与where类似,可筛选数据

    where针对表中的列发挥作用,查询数据

    having针对查询结果中的列发挥作用,二次筛选数据, 和group by配合使用

    链表操作:select * from userinfo, department; (笛卡尔积) 直接写会出现笛卡尔积 ,数据因外键关联会重复打印出来,

    解决方法可以设定外键关联id 与被关联表的id 相等:

    select * from userinfo, department where userinfo.depart_id=department.id;

    左连接:select * from userinfo left join department on userinfo.depart_id=department.id;  左边表的数据会全部显示,右边没有用到的不会显示。

  • 相关阅读:
    BitSet源码
    BitSet
    webrtc在ubuntu14.04上的编译过程(12.04亦可)
    使用 ssh -R 建立反向/远程TCP端口转发代理
    爬虫与反爬虫
    Linux IO模式及 select、poll、epoll详解
    PF_RING 总结
    40行代码的人脸识别实践
    初学者必读:IBM长文解读人工智能、机器学习和认知计算
    C 格式化显示时间(time.h)
  • 原文地址:https://www.cnblogs.com/1624413646hxy/p/11040519.html
Copyright © 2011-2022 走看看