zoukankan      html  css  js  c++  java
  • 42 外键 数据行的操作

    外键;

    create table department (
    id int auto_increment primary key,
    depart_name varchar(32) not null default ''
    )engine=Innodb charset=utf8;

    nsert into department (depart_name) values ('公关'), ('关关'),('关公');

    create table userinfo (
    id int auto_increment primary key,
    name varchar(32) not null default '',
    epart_id int not null default 1,

    # constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
    constraint fk_userinfo_depart foreign key (depart_id) references department(id)
    )engine=Innodb charset=utf8;

    insert into userinfo (name, depart_id) values ('root1', 1);
    作用;
    1.约束
    2.节省空间


    注意;
    1.不能将创建外键的语句单独拿出来
    (新增字段)
    alter table userinfo add constraint(约束) fk_userinfo_depart foreign key (depart_id) references department(id);
    删除外键
    alter table usertable drop foreign key 外键名称(fk_userinfo_depart);

    2.外键关联的时候,必须关联的是表的主键ID
    3.练习的时候, 将语句写在文本中, 然后考过去执行
    4.主键索引;
    1.加速查找
    2.不能为空
    3.不能重复

    外键变种;(重点)


    1.唯一索引;
    作用;
    1.unique列的值不能为重复,
    2.加速查找
    create table t1 (
    id int ,
    num int ,
    unique(num)
    )engine=Innodb charset=utf8;

    联合唯一索引;
    create table t2 (
    id int ,
    num int ,
    unipue(id,num)
    )engine=Innodb charset=utf8;

    2. 一对多:
    一对多或称为多对一
    三张表:出版社,作者信息,书

    一对多(或多对一):一个出版社可以出版多本书

    关联方式:foreign key

    部门表:
    id depart_name
    1 公关部
    2 公共部
    3 保安部

    员工表:
    id name age depart_id(外键)
    1 lxxx 12 2
    2 xxxx 13 1
    3 xxxx 13 2

    3. 一对一:
    两张表:学生表和客户表

    一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系

    关联方式:foreign key+unique

    用户表:
    id name age
    1 zekai 23
    2 eagon 34
    3 lxxx 45
    4 owen 83

    博客表:
    id url user_id (外键 + 唯一约束)
    1 /linhaifeng 2
    2 /zekai 1
    3 /lxxx 3
    4 /lxxx 4


    4.多对多:
    三张表:出版社,作者信息,书

    多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
       关联方式:foreign key+一张新的表

    用户表:
    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),(值n,值n);
    inster into 表名 (列1,列2) select 列1,列2 from 表名;


    删除;
    delete from 表名;
    delete from 表名 where id >10;
    delete from 表名 where id < 10
    delete from 表名 where id <= 10
    delete from 表名 where id >= 10
    delete from 表名 where id != 10
    delete from 表名 where id = 10 and name='xxx'; and : 并且 两个条件都必须要成立
    delete from 表名 where id = 10 or name='xxx'; or : 或者 只要满足一个条件成立

    修改;
    update 表名 set name='qe',age=12 where id >10;
    查询;
    基本;
    select * from 表名;
    select name,age from 表名;
    高级:

    a. where 条件查询:
    select * from 表名 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....);

    on 是建立内外连接

    select student_id,num as sw from score left join course on score.course_id = course.cid where course.cname = '生物'

    select * from t4 where id in (select id from t3 where id between 2 and 4)

    是可以这样使用的, 但是不建议大家使用;

    b.通配符;
    select * from 表 where name like 'aa%' :以aa开头的所有(匹配多个字符串)
    select * from 表 where name like 'aa_' :以aa开头的所有(匹配一个字符串)

    c.限制取一条;
    select * from 表名 limit; 索引偏移量,取出多少条

    select * from t3 limit 0, 10; 第一页
    select * from t3 limit 10, 10; 第二页
    page = input('page:')

    page 索引偏移量 数据量(offset)
    1 0 10
    2 10 10
    3 20 10
    4 30 10

    page (page-1)*offset offset

    分页核心SQL:

    select * from t3 limit (page-1)*offset, offset;
    d.排序
    用 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;

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


    e.分组

    select age 聚合函数(count(num)/sum(num)/max(num)/min(num)/agv(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

    having的二次筛选;
    select age,count(num) as cnt from t7 group by age having cnt >2;


    where 和 having的区别:
    1). having与where类似,可筛选数据
    2). where针对表中的列发挥作用,查询数据
    3). having针对查询结果中的列发挥作用,二次筛选数据, 和group by配合使用


    f. 连表操作
    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.可以连接多张表 通过某一个特定的条件

    注意查询的顺序:
    elect name,sum(score) from 表 where id > 10 group by score having age> 12 order by age desc limit 2, 10

  • 相关阅读:
    C语言-错误处理
    C语言-排序和查找
    PCB设计要点
    C语言-调试
    c++ 概述
    C语言-指针
    C语言-数组与指针 字符与字符串
    C语言-(void*)类型指针
    C语言-字符操作函数
    C语言-链表
  • 原文地址:https://www.cnblogs.com/komorebi/p/11025499.html
Copyright © 2011-2022 走看看