zoukankan      html  css  js  c++  java
  • mysql(2.索引与约束以及设计)

    一、约束

               1.非空约束

               not null

        2.主键约束

             create table t1(

               id int not null  primary,

               name varchar(20) not null

              )

           注意:不能为空,不能重复,一个表中只能有一个主键

                 3.唯一约束

          create table t2(

                                 id int(10) not null,

              name varchar(255),

            unique name_age(name,age)

          )

        注意:被约束的字段不能重复

        4.外键  foreign key

                        部门表

            create table dept(

              dept_id int(30) not null auto_increment primary key,
              dept_name varchar(255) not null
                )

           

      -- 员工表
          create table emp(
            id int(10) not null auto_increment primary key,
            name varchar(50) not null,
            dept_id int(10) not null,
            constraint f_key foreign key (dept_id) references db2.dept(dept_id) on delete cascade
              )

      5. 默认值 default
        create table t5 (
            id int not null auto_increment PRIMARY key,
            name VARCHAR(255) not null DEFAULT 'wusir'
              )  

     

    二.数据库设

    1.概念
    1.有效存储数据
    2.满足用户的多种需求

    2.关系
    1-1 :最少需要1张表
    1-n :最少需要2张表
    n-n :最少需要3张表

    3.数据库三范式
    1.保证每列的原子性
    2.保证每列都与主键相关
    3.保证每列都和主键直接相关,而不能是间接相关
    三范式的详解:http://www.cnblogs.com/wangfengming/p/7929118.html

    三. 索引

    1.概念:相当于书的目录,快速找到数据 好处:可以帮助你提高查询效率,数据量越大越明显
    缺点: 新增和删除数据时,效率较低


    2.索引方法:
    1.hash 是以key-value 的形式进行索引存储
    2.BTree 是以二叉树方式进行索引存储。(默认存储索引类型)


    3.索引分类
    1. 普通索引 create INDEX name_index on person(name);
    2. 唯一索引 create unique INDEX name_age on person(name,age);
    3. 主键索引 alter table person MODIFY id int PRIMARY key;
    4. 组合索引 create unique INDEX name_age on person(name,age);
    5. 全文索引 full text :原理是分词查找



    练习:http://www.cnblogs.com/wangfengming/p/7978183.html

              



    --聚合函数:

    1.max() 最大
    2.min() 最小
    3.avg() 平均
    4.sum() 求和
    5.count() 总个数

    --分组函数


    1.group by

    和他配合的是 having
    普通的条件用where
    left join 条件用 on

    --排序:

    order by asc ,desc


    --去重
    distinct
    group by

    --分页
    limit
    参数1:从第几条开始,起始位置为0
    参数2:显示的条数

    --多表联合查询(会产生笛卡尔乘积)

    1.左连接查询: A left join B on 条件 left join C on 条件

    2.右连接查询: right join

    3.内连接查询:inner join

    create table liuyan(
    id int PRIMARY KEY auto_increment,
    title VARCHAR(32) not NULL,
    author VARCHAR(16),
    addtime VARCHAR(12) NOT NULL,
    content text NOT NULL,
    isdelete tinyint not null DEFAULT 0
    )
    
    1.在留言表最后添加一列状态(status  char(1)  默认值为0)
       alter table liuyan add status TINYINT DEFAULT 0 after isdelete
    2.修改留言表author的默认值为’youku’,设为非空
      alter table liuyan modify author varchar(16) not null default 'youku'
    3.删除liuyan表中的isdelete字段
      ALTER TABLE liuyan DROP isdelete
    4.为留言表添加>5条测试数据
    
    
    insert into liuyan values
    (null,'介绍','大雄','1000','哥不是一匹好马,但也不是一头普通的毛驴',null),
    (null,'叮当猫','熊熊','2000','你牙缝里有韭菜,扣出来贼哥吃',null),
    (null,'花花','苗苗','3000','苗苗问花花:卖萌是褒义词还是贬义词?',null),
    (null,'霞哥','雄大','4000','斗战色佛',null),
    (null,'晨晨','逗比','5000','你笑起来像一朵菊花,菊花残,man腚伤',null);
    
    
    要求将id值大于3的信息中author字段值改为admin
    update liuyan set author='admin' where id>3
    
    删除id号为4的数据。
    delete from liuyan from id=4
    
    
    为留言表添加>15条测试数据,要求分三个用户添加
    
      懒.....
    
    查询所有留言信息
      select id,title,author,addtime,content,status from liuyan;
    
    查询某一用户的留言信息
      select id,title,author,addtime,content,status from liuyan where author='用户名称';
    
    查询所有数据,按时间降序排序
      select id,title,author,addtime,content,status from liuyan order by addtime desc;
    
    .获取id在2到6之间的留言信息,并按时间降序排序
     select id,title,author,addtime,content,status from liuyan where id between 2 and 6;
    
    .统计每个用户留了多少条留言,并对数量按从小到大排序。
     select author,count(id) as'留言条数' from liuyan group by author order by count(id) desc
    
    .将id为8、9的两条数据的作者改为’doudou’.
       update liuyan set author ='doudou' where id in(8,9);
    
    .取出最新的三条留言。(使用limit)。
      select * from ( select id,title,author,addtime,content,status from liuyan  ORDER BY addtime desc)hahg LIMIT 3
    
    
    .查询留言者中包含”d”字母的留言信息,并按留言时间从小到大排序
     select id,title,author,addtime,content,status from liuyan where author like'%d%' order by addtime asc;
    
    .删除”作者”重复的数据,并保留id最大的一个作者
     delete from liuyan where author in(
        select author from (select author from liuyan group by author having count(1)>1) a
     )
     and id not in(
         select id from (select max(id) id from liuyan group by author having count(1)>1) b
    )=
     
  • 相关阅读:
    Xshell的一些使用方法和注意事项
    adobe premiere pro cc2015.0已停止工作 解决办法
    视频播放效果--video.js播放mp4文件
    centos 7.0 编译安装php 7.0.3
    centos 7.0 安装nginx 1.9.10
    centos 7.0 firewall 防火墙常用命令
    webstorm 更改默认服务器端口
    css3 动画效果 定义和绑定执行
    css 图片垂直居中总结
    JS 面向对象随笔
  • 原文地址:https://www.cnblogs.com/1996-11-01-614lb/p/8024556.html
Copyright © 2011-2022 走看看