第三十五 MYSQL 语句

时间:2019-06-13
本文章向大家介绍第三十五 MYSQL 语句,主要包括第三十五 MYSQL 语句使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

为什么使用数据库:

   数据如果比较多时我们需要考虑使用数据库来经行存储

数据库的分类:

  关系型数据库:

    1.有约束

    2.基于硬盘的存储(即将数据永久存储到硬盘上,落地)

    典型代表:Mysql   oracle(国企) sqserver(微软) aqllite   db2

  非关系型数据库:

    1.没有约束(key-->value)

    2.基于内存存储(将数据放入内存中)

    典型代表:Memcache, redis(微博),mongodb

Mysql的架构:

  即c/s架构,基于socket 通信方式,有客户端,服务端

Mysql的安装:

  版本:(5.5,5.7)此区间的是比较稳定的,使用的最多。

数据库的操作:

  数据库--》于文件夹

  表---------》文件

  数据行---》文件中的内容

  1.数据库:

    增:sql语句:

        create database 数据库名 ]  charset=utf8;

    删:

        drop database [ 数据库名 ] ;

    改:

        没有修改数据库的指令,删除重新建

    查:

        show databases;

    使用:

        use  [ 数据库名 ];

  

2. 数据表:

  新建:
  use db1;

  版本0:
  SQL语句:
  create table 表名 (
          列名1 列类型
          );

例子:
  create table t1 (
         id int,
         name char(32)
         );

  增加
    指令:
      insert into 表名 (列1, 列2) values (值1, 值2);

    例子:
      insert into t1 (id, name) values (1, 'zekai');
      insert into t1 (id, name) values (2, '你好');

    改进1:
      create table 表名 (
              列名1 列类型
              )engine=Innodb charset=utf8;

    ps:
      引擎: Innodb 和 MyIsam
      5.5 版本以上 默认是Innodb

    create table t2 (
            id int,
            name char(32)
             )engine=Innodb charset=utf8;

    insert into t2 (id, name) values (1, '你好');
    insert into t2 (id, name) values (1, 'xxx');

   改进2:

    create table 表名 (
            列名1 列类型 auto_increment primary key
            )engine=Innodb charset=utf8;

    create table t4 (
           id int auto_increment primary key,
           name char(32) not null default ''
           )engine=Innodb charset=utf8;

    auto_increment : 自增
    primary key : 主键索引 (作用: 加快查找的速度)
    not null : 不能为空
    default : 默认值

注意: 后面一列写完之后, 不能加逗号 (*********)

一种:
  insert into t3 (id, name) values (1, '你好');
  insert into t3 (id, name) values (2, 'xxx');
二种:
  insert into t3 (name) values ('hello');
  insert into t3 (name) values ('xxx');

  最终的格式:
     create table 表名 (
              列1 列属性 [是否为null 默认值],
              列2 列属性 [是否为null 默认值],
              .....
              列n 列属性 [是否为null 默认值]
              )engine = 存储引擎 charset = 字符集

  最终的例子:
     create table t4 (
            id int auto_increment primary key,
            name char(32) not null default '',
            pwd char(32) not null default ''
            )engine=Innodb charset=utf8;

  查看:
  指令:
    select 列名 from 表名;
  例子:
    select * from t1;

列类型:

  a. 数值型
  create table t4 (
          id unsigned mediumint auto_increment primary key,
          name char(32) not null default '',
          pwd char(32) not null default ''
         )engine=Innodb charset=utf8;

  tinyint :
  范围:
  有符号: -128到127
  无符号: 0 到 255 unsigned
  smallint
  范围:
  有符号: -32768到32767
  无符号: 0 到 65535 unsigned

  mediumint
  范围:
  有符号: -8388608到8388607
  无符号: 0 到 16777215 unsigned
  int
  bigint

  区别:
  a. 取值范围不一样, 根据自己公司的业务来去选择
  b. 无符号和有符号的意思

  float(M,D) 浮点型
  decimal(M,D) 定点型 比float更加的精准

  例如: 3.1415151519868789789
  float: 3.141515000000000000
  decimal : 3.1415151519868789789

  126.35

  M:小数总共多少位 decimal(5, )
  D:小数点后面几位 decimal(5, 2)

  使用场景:
  比如 说存 salary 工资 : 6000.23 decimal(,2)

  b. 字符串类型

  char : 定长 char(32) 这一列的值就是32 优点: 速度快 缺点: 浪费
  varchar : 变长 varchar(32) 优点: 不浪费, 节省空间 缺点: 速度慢
  根据自己公司的业务来去选择:

  create table userinfo (
            id unsigned mediumint auto_increment primary key,
            name varchar(128) not null default '',
            pwd char(32) not null default '',
            create_time datetime not null default '1970-01-01 00:00:00'
           )engine=Innodb charset=utf8;
      一般情况下, 如果没有100%的把握, 都是用varchar()

text: 文本 范围比较大, 如果存储大量字符的话, 可以使用这个字段

  c. 时间类型
    date 2019-6-12
    推荐使用datetime


指令:
drop table 表名;

连带着将数据表中的所有数据都会删掉

ps: 工作中, 线上数据库, 这个命令根本不会让你用到

实例:
drop table t1;

  查询
    show tables;

    desc 表名; : 查看表的结构

    show create table 表名 : 查看表的创建过程

    关于主键自增: (不是重点)
    show session variables like 'auto_inc%';
    set session auto_increment_increment = 2;

    show global variables like 'auto_inc%';
    set global auto_increment_increment = 2;

  修改
    create table t4 (
            id int auto_increment primary key,
            name char(32) not null default '',
            pwd char(32) not null default ''
           )engine=Innodb charset=utf8;

  修改字段:
    alter table 表名(t3) change 原列名(name) 新列名(username varchar(32) not null default '');

  新增字段:
    alter table 表名(t3) add 新列(pwd char(32) not null default '');

  删除字段:
    alter table 表名(t3) drop 列名(pwd);

3. 数据行:

  增
    insert into t3 (id, name) values (1, '你好');

  查询

    select * from t3; : 将表中的 所有的列全部列出
    select 列名, 列名, 列名 from t3 : 将某一列的值查出

  删

    delete from 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, 继续会延续上一个 ID

    truncate 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, ID 会重新开始

    truncate 速度快

    ps: 工作中, 线上数据库, 这个命令根本不会让你用到

    delete from 表名(t3) where name = 'xxxxx';

  修改

    update t3 set username='zekai';

    update t3 set username='xxxx' where id=3;

    update t3 set username='xxxx', pwd='xxxxx' where id=3;

七. 外键: (***********************)

    缺点:
1. 数据重复
2. 如果 部门过长的话, 太占用空间
    解决方法:

重新设计一张表, 这张表 中存放部门的相关信息
    部门表:

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

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

create table userinfo (
id int auto_increment primary key,
name varchar(32) not null default '',
depart_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);
insert into userinfo (name, depart_id) values ('root2', 2); 错误的

注意:
创建多个外键的时候, 名称不能一样

=====> 一对多

原文地址:https://www.cnblogs.com/sry622/p/11014636.html