day42

day42   外键(一对一,一对多,多对多)
 
回顾
数据库:
        增:
            create database 数据库;
        删:
            drop database 数据库名;
        查:
            show databases;
 
数据表:
        增:
            create table 表名(
                 列名1 列类型1  [not null  default ''],
                列名2 列类型2  [not null  default '']  
            )engine=Innodb charset=utf8;
        列类型:数值类型   (范围,有无符号)   , 字符串类型 char varchar  text  float  时间类            型:datetime
        删:drop table 表名;
        改:alter table 表名 change 旧列名 新列的声明;
               alter table 表名 add 新列声明; 
                alter table 表名 drop 旧列名;
        查:show tables;
 
数据行:
     增; insert into 表名 (列1,列2.....) values (值1,值2.........);
 
    查:select * from 表名;
    select name,age from 表名;
 
    删:
        delect from 表名; 一行行删,ID接着原来的
        truncate 表名;   全部一起删,ID从头开始
 
    改:
        update 表名 set name='linux' where条件;
 
 
外键
 
    作用:1约束 2 节省空间
问题,两张单独的表建完了,后期想往后面加外键
1.添加外键的
方法:alter table userinfo add contraint fk_userinfo_depart(自己起的名字) foreign key (depart_id)列名 references    表名department(id)
 
删除一个外键
alter table userinfo drop foreign key 外键名称
2.外键关联的时候,必须关联的是表的主键ID(那个自增的)
 
 
 
主键索引primary key :1.加速查找  2.不能为空(不要加not null,默认就这个) 3.不能重复
 
 
今日内容:
一.外键的变种:
1.唯一索引
    create table t5(
        id int
        num int
        unique(num)
)engine=Innodb charset=utf8;
 
联合唯一索引
    create table t5(
        id int
        num int
        unique(id,num)
)engine=Innodb charset=utf8;
 
唯一索引的作用:1.num列的值是不能重复的  2.加速查找
Duplicate entry '1-1' for key 'userid'   联合唯一索引错误提示ERROR
 
联合唯一索引:  id和num的列的值不能重复  加速查找
如果加了(1,2)  就不能加(1,2) 了
 
2一对多
 
部门表
 
id                depart_name
1                    公关部
2                    公共部
3                    保安部
 
员工表
id              name          age          depart_id(外键)
1                   liunx        12                    2
2                  liunx2        13                    1
 
 
就是这个外键可以重复可以有很多个公关部
 
3一对一:
 
用户表:
    
id           name            age            
1           kkk                20             
2           kkk2             30
3           kkk3              40
4           kkk4              50
 
博客表
id          url                     user_id(外键 + 唯一约束)
1            /kkk                    1
2            /kkk2                    2
3            /kkk3                    3
 
不允许同一个用户开两个博客园, 就是user_id 是唯一
 
 
4.多对多
 
 
 
 
求root1下有多少机器方便,但是求c1下有多少用户比较难
 
 
怎么做,多加一张表
 
 
 
这个叫多对多,根据root!找多少机器,机器有几个用户都方便求
 
user_id 和host_id就可以整联合唯一  (1,1)   第八行在多出一个(1,1)这个值就重复了
 
 
在创建的时候user_id和host_id必须是外键,然后是联合唯一索引 unique(user_id,host_id)
 
 
二.数据行的操作
增:
    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 t3;
查询一个数据,然后往(表名)里面插
 
删除
    delect from 表名;
    delect from 表名 where id=1;
 
修改
    update 表名 set name='linux',age='20' where id > 10 and ..........;
 
 
查询;
    基本的
    select * from 表名;
    select name,age from 表名;
高级
    1.where条件查询
        selecT* from 表名 where id=10;
        select * from 表名 where id > 10;
 
between  9 and 12 闭区间 (包含9和12)
 
in :在什么里面 
select ..........where id in(9,11)   
一般不这么写,效率非常低
select .................. where id in (select id from t3 where id between 2 and 4); 
 
2通配符
    s%  以S开头的匹配
    s_   s开头的后面是一个字符
 
select * from t3 where username like 'k%'
 
匹配以k开头的后面多个字符的
3限制取几条
 
select * from t3 limit 0,10;
会显示前十条数据
select * from t3 limit 索引偏移量(从第几条开始),取出多少条数据;
 
如果
一页显示10个数据
第n页就是 10乘以(n-1)
分页
select * from t3 limit 10*(n-1) 10;
 
4排序
单列
order by 
按照id来排的,默认是升序
select * from t4 order by id;
降序
select * from t4 order by id desc;
descending下降的
asc升序
 
如果是字母就是asicc码排的
 
多列:
    select * from t4 order by num desc,name asc;
优先级是前面的,如果有相同的再比name
 
5分组
    select age,sum(num) from t7 group by age;
对age相同的这一列num这一列求和
 
max(num)
min(num)
avg(num)
count(num) 数数字
 
as 显示别名count(num) as cnt 这个时候就显示cnt了
如果group by 进行 二次筛选,用 having cnt > 1
having的二次筛选
select age,sum(num) from t7 group by age having cnt > 1;
###不同点
where针对列发挥作用
having针对查询结果的列发挥作用,二次筛选 和group by配合使用
 
6连表操作
 
 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;
结果是和上面一样的
 
右连接
right join
 
区别:左边的表全部显示,如果右边的没用到的就会不显示
    右连接,右边的表全部显示
 
内连接:左右两边的数据都会显示
只要记住左连接就可以了
 
连了五张表(可以连多张表)
 
 
那个* 是代表五张表里的所有列,如果有冲突,可以选择自己需要的列
 
 
 
注意查询的顺序
 
where     group by  (having)      order by        limit
 
 
 
posted @ 2019-06-13 19:04  轩辕12  阅读(129)  评论(0编辑  收藏  举报