mysql day03

1、SQL查询
  1、distinct : 不显示字段的重复值
    1、语法 :select distinct 字段1,字段2 from 表名;

    2、示例
      1、表中都有哪些国家
        select distinct country from sanguo;
      2、表中一共有几个国家
        select count(distinct country) as n from sanguo;
    3、注意
      1、distinct和from之间的所有字段值都相同才会去重
2、约束
  1、作用 :保证数据的一致性、有效性
  2、约束分类
    1、默认约束(default)
      sex enum("M","F","S") default "S",
    2、非空约束(not null)
      不允许该字段的值为 NULL
      id int not null,
      id int not null default 0,
3、索引
  1、定义
    对数据库中表的一列或多列的值进行排序的一种结构(BTree)
  2、优点
    加快数据的检索速度
  3、缺点
    1、当对表中数据更新时,索引需要动态维护,降低数据的维护速度
    2、索引需要占用物理存储空间
4、运行时间检测
  1、开启运行时间检测 :mysql> set profiling=1;
    运行指令
  6、查看执行时间
    show profiles;
5、索引
  1、普通索引(index)
    1、使用规则
      1、可设置多个字段,字段值无约束
      2、把经常用来查询的字段设置为索引字段
      3、KEY标志 :MUL
    2、创建
      1、创建表时
        create table t1(
    index(name),
    index(id));
      2、已有表中
        alter table 表名 add index(字段名) ;
        create index 字段名 on 表名(字段名);
    3、查看索引
      1、desc 表名; -->KEY标志为 MUL
      2、show index from 表名\G;

    4、删除index
      drop index 索引名 on 表名;
  2、唯一索引(unique)
    1、使用规则
      1、可设置多个字段
      2、约束 :字段值不允许重复,但可以为 NULL
      3、KEY标志 :UNI
    2、创建
      1、创建表时
        unique(phnumber),
    unique(cardnumber)
      2、已有表
        create unique index 索引名 on 表名(字段名);
    3、查看、删除同普通索引
      删除 :drop index 索引名 on 表名;
  3、主键索引(primary key)&&自增长(auto_increment)
    1、使用规则
      1、只能有1个字段为主键字段
      2、约束 :字段值不允许重复,也不能为 NULL
      3、KEY标志 :PRI
      4、通常设置记录编号字段 id,能够唯一锁定一条记录
    2、创建
      1、创建表时
        1、id int primary key auto_increment,
       name varchar(20) not null
       )auto_increment=10000,charset=utf8,engine=InnoDB;
       alter table 表名 auto_increment=10000;
    2、
      id int auto_increment,
      name varchar(20),
      primary key(id)
      2、已有表
        alter table 表名 add primary key(id);
    alter table 表名 modify id int auto_increment;
    3、删除主键
      1、先删除自增长属性(modify)
        alter table 表名 modify id int;
      2、删除主键
        alter table 表名 drop primary key;
6、数据导入
  1、作用 :把文件系统中内容导入到数据库中
  2、语法格式
    load data infile "文件名"
    into table 表名
    fields terminated by "分隔符"
    lines terminated by "\n";

  3、将socreTable.csv导入到数据库中
    1、在数据库中创建对应的表
      create table score(
      id int,
      name varchar(15),
      score float(5,2),
      phnumber char(11),
      class char(7)
      )character set utf8;
    2、执行数据导入
      1、查看搜索路径
        show variables like "secure_file_priv";
    ## /var/lib/mysql-files
      2、拷贝文件
        sudo cp  ~/scoreTable.csv  /var/lib/mysql-files/
      3、执行数据导入
        load data infile "/var/lib/mysql-files/scoreTable.csv"
        into table score
        fields terminated by ","
        lines terminated by "\n";

7、数据导出
  1、把数据库表的记录到处到系统文件里
  2、语法格式
    select ... from 表名
    into outfile "文件名"
    fileds terminated by "分隔符"
    lines terminated by "\n";
  3、练习
    1、把MOSHOU库下的sanguo表中,英雄的姓名、攻击值和国家给导出来,sanguo.csv
      1、查看搜索路径
        show variables like "%secure%";
      2、执行数据导出语句
        select name,gongji,country from MOSHOU.sanguo
    into outfile "/var/lib/mysql-files/sanguo.csv"
    fields terminated by ","
    lines terminated by "\n";

    Error: ... secure_file_priv ...
    2、把 mysql 库下的user表中 user、host的值导出到系统文件 user.txt
      select user,host from mysql.user
      into outfile "/var/lib/mysql-files/user.txt"
      fields terminated by "   "
      lines terminated by "\n";
 
8、表的复制
  1、语法
    create table 表名    select ... from 表名 where 条件;
  2、示例
    1、复制MOSHOU.sanguo表,sanguo2
      create table MOSHOU.sanguo2 select * from MOSHOU.sanguo;
    2、复制MOSHOU.sanguo中的id、name、country的记录,sanguo3
      create table MOSHOU.sanguo3 select id,name,country from MOSHOU.sanguo; 
    3、复制MOSHOU.sanguo中的name、country,每页显示2条记录,复制第3页的内容
      create table MOSHOU.sanguo4 select name,country from sanguo limit 4,2;
  3、复制表结构
    create table 表名 select ... from 表名 where false;

  2、唯一索引(unique)
  3、主键索引(primary key)
  4、外键(foreign key)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值