• Mysql学习日记-02外键 ,索引, sql语句的补充


    主键:

      一个表只能有一个主键

      主键可以由多列组成

    外键:

      就是把一个表的成员 和 另外一个表的成员联系起来

      比如 student 里有(学号,姓名)  而 score里有(学号,成绩) 这个时候就可以把   student(学号) = score (学号)关联起来

      语法规则是 : 

    create  table score (

        sid int  not null auto_increment primary  key,

        number iint 

    )

    create  table student (

         nid   int  not null auto_increment primary  key,

        name   varchar(20),

        constraint fk_xxx  foreign key (nid )  references   score (nid)

      )

    对于自增补充:

        是对表里面的序号的步长设置
      desc t10;

      show create table t10;

      show create table t10 G;

      alter table t10 AUTO_INCREMENT=20;

    MySQL: 自增步长
      基于会话级别:
      show session variables like 'auto_inc%'; 查看全局变量
      set session auto_increment_increment=2; 设置会话步长
      # set session auto_increment_offset=10;
    基于全局级别:
      show global variables like 'auto_inc%'; 查看全局变量
      set global auto_increment_increment=2; 设置会话步长
      # set global auto_increment_offset=10;

    SqlServer:自增步长:
        基础表级别:
          CREATE TABLE `t5` (
            “nid` int(11) NOT NULL AUTO_INCREMENT,
            `pid` int(11) NOT NULL,
            `num` int(11) DEFAULT NULL,
            PRIMARY KEY (`nid`,`pid`)
          ) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=2 DEFAULT CHARSET=utf8

          CREATE TABLE `t6` (
            `nid` int(11) NOT NULL AUTO_INCREMENT,
            `pid` int(11) NOT NULL,
            `num` int(11) DEFAULT NULL,
             PRIMARY KEY (`nid`,`pid`)
            ) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=20 DEFAULT CHARSET=utf8

    索引:

                   

            create table t1(
               id int ....,
                num int,
                xx int,
              unique 唯一索引名称 (列名,列名),
                constraint ....
                )

           索引即是绑定一对一     加速查找      

            约束constraint不能重复(可以为null)  主键不能重复(不能为null)

    外键与索引的结合  :

    比如  一个用户只能拥有一个博客     ,一对一

    create table userinfo1(                                    
    id int auto_increment primary key,
    name char(10),
    gender char(10),
    email varchar(64)
    )engine=innodb default charset=utf8;

    create table admin(
    id int not null auto_increment primary key,
    username varchar(64) not null,
    password VARCHAR(64) not null,
    user_id int not null,
    unique uq_u1 (user_id),
    CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)
    )engine=innodb default charset=utf8;

    多对多    :

         用户表

         主机表

          用户主机关系表

    create table userinfo2(
    id int auto_increment primary key,
    name char(10),
    gender char(10),
    email varchar(64)
    )engine=innodb default charset=utf8;

    create table host(
    id int auto_increment primary key,
    hostname char(64)
    )engine=innodb default charset=utf8;


    create table user2host(
    id int auto_increment primary key,
    userid int not null,
    hostid int not null,
    unique uq_user_host (userid,hostid),
    CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
    CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
    )engine=innodb default charset=utf8;

    总结:对sql语句数据行的操作补充 必须注重细节!括号 逗号不要忘

    逻辑关系要理顺

    create table tb12(
        id int auto_increment primary key,
        name varchar(32),
        age int
    )

    增  :  

            inset into tb12(name ,age ) values('kk',12);

           inset into  tb12 (name,age) values('kk',12),('alex',13);

          insert into tb12(name,age) select name,age from tb11;

    删:

         

    delete from tb12;
    delete from tb12 where id !=2
    delete from tb12 where id =2
    delete from tb12 where id > 2
    delete from tb12 where id >=2
    delete from tb12 where id >=2 or name='alex'

      

    改:

    update tb12 set name='alex' where id>12 and name='xx'
    update tb12 set name='alex',age=19 where id>12 and name='xx'

    查:

    select * from tb12;

    select id,name from tb12;

    select id,name from tb12 where id > 10 or name ='xxx';

    select id,name as cname from tb12 where id > 10 or name ='xxx';

    select name,age,11 from tb12;

    其他:

    select * from tb12 where id != 1
    select * from tb12 where id in (1,5,12);
    select * from tb12 where id not in (1,5,12);
    select * from tb12 where id in (select id from tb11)
    select * from tb12 where id between 5 and 12;

    通配符:

    select * from tb12 where name like "a%"
    select * from tb12 where name like "a_"

    分页:

    select * from tb12 limit 10;(10页)

    select * from tb12 limit 0,10;(从第一个开始数,10个)
    select * from tb12 limit 10,10;
    select * from tb12 limit 20,10;

    select * from tb12 limit 10 offset 20;

    排序:
    select * from tb12 order by id desc; 大到小   记忆  D大
    select * from tb12 order by id asc; 小到大  记忆 a小
    select * from tb12 order by age desc,id desc;

    分组:用于数目的统计 比如表中一个学生可以选择多个课程   用group by students 统计下来其实只有一个人

    select count(id),max(id),part_id from userinfo5 group by part_id;

    count
    max
    min
    sum
    avg

    **** 如果对于聚合函数结果进行二次筛选时?必须使用having ****
    select count(id),part_id from userinfo5 group by part_id having count(id) > 1;

    select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;

    连表操作:

    其实可要记住一个就行了  

    select * from userinfo5,department5

    select * from userinfo5,department5 where userinfo5.part_id = department5.id

    select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
    select * from department5 left join userinfo5 on userinfo5.part_id = department5.id
    # userinfo5左边全部显示


    # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
    # department5右边全部显示



    select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
    将出现null时一行隐藏


    select * from
    department5
    left join userinfo5 on userinfo5.part_id = department5.id
    left join userinfo6 on userinfo5.part_id = department5.id


    select
    score.sid,
    student.sid
    from
    score

    left join student on score.student_id = student.sid

    left join course on score.course_id = course.cid

    left join class on student.class_id = class.cid

    left join teacher on course.teacher_id=teacher.tid

  • 相关阅读:
    SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
    Docker 下 nginx + tomcat 负债均衡
    Docker 安装 tomcat 并挂载宿主目录到容器
    Docker 安装 nginx 并挂载宿主目录到容器中
    SpringBoot 常见创建方式
    Java SPI 机制实现解耦
    TCP 粘包问题
    Docker 安装和常用命令
    Docker 安装 ActiveMQ
    INTEL
  • 原文地址:https://www.cnblogs.com/kangkang1999/p/13228084.html
Copyright © 2020-2023  润新知