• mysql内容总体回顾


    数据类型:

      数值类型: 整形 浮点型

      字符串: char(定长)varchar(不定长)

           char(定长):插入数据或查询数据都快,因为char在磁盘上插入数据时的存储空间是固定的,简单粗暴,直接就是定长空间,那么就不需要考虑数据的长度,所以在进行数据查询时,速度也快,因为在读取数据时,也不需要考虑数据长度,(简单粗暴) 就按照定长的空间来取数据.

        varchar(不定长):插入和查询速度相对较慢,因为它在内容 存储数据的时候,按照数据的长度进行存储的,那么每次存储数据都需要计算一下数据的长度,按照长度来开辟存储空间,那么在数据的存储空间前面还需要1-2个字节空间长度来存储数据的长度,也就是说格式大概是长度+内容,也导致了在读取数据时,首先要先读取数据的长度,然后根据长度再读取后面的内容,导致速度较慢,但是多数情况下可以节省存储空间.

      日期类型:

        year  2018

        data  2018-01-01

        time   12:10:10

        datatime(*)   2019.1.22 12:10:10              *****平时工作用datatime

        timestamp  范围:1970-01-01 00:00:00/2037 年某时

    year:
        mysql> create table t10(born_year year); #无论year指定何种宽度,最后都默认是year(4)
        mysql> insert into t10 values  
            -> (1900),
            -> (1901),
            -> (2155),
            -> (2156);
        mysql> select * from t10;
        +-----------+
        | born_year |
        +-----------+
        |      0000 |
        |      1901 |
        |      2155 |
        |      0000 |
        +-----------+
    
    
    date,time,datetime:
        mysql> create table t11(d date,t time,dt datetime);
        mysql> desc t11;
        +-------+----------+------+-----+---------+-------+
        | Field | Type     | Null | Key | Default | Extra |
        +-------+----------+------+-----+---------+-------+
        | d     | date     | YES  |     | NULL    |       |
        | t     | time     | YES  |     | NULL    |       |
        | dt    | datetime | YES  |     | NULL    |       |
        +-------+----------+------+-----+---------+-------+
    
        mysql> insert into t11 values(now(),now(),now());
        mysql> select * from t11;
        +------------+----------+---------------------+
        | d          | t        | dt                  |
        +------------+----------+---------------------+
        | 2017-07-25 | 16:26:54 | 2017-07-25 16:26:54 |
        +------------+----------+---------------------+
    
    
    
    timestamp:
        mysql> create table t12(time timestamp);
        mysql> insert into t12 values();
        mysql> insert into t12 values(null);
        mysql> select * from t12;
        +---------------------+
        | time                |
        +---------------------+
        | 2017-07-25 16:29:17 |
        | 2017-07-25 16:30:01 |
        +---------------------+
    
    
    
    ============注意啦,注意啦,注意啦===========
        1. 单独插入时间时,需要以字符串的形式,按照对应的格式插入
        2. 插入年份时,尽量使用4位值
        3. 插入两位年份时,<=69,以20开头,比如50,  结果2050      
                        >=70,以19开头,比如71,结果1971
        mysql> create table t12(y year);
        mysql> insert into t12 values  
            -> (50),
            -> (71);
        mysql> select * from t12;
        +------+
        | y    |
        +------+
        | 2050 |
        | 1971 |
        +------+
    
    
    
    ============综合练习===========
        mysql> create table student(
            -> id int,
            -> name varchar(20),
            -> born_year year,
            -> birth date,
            -> class_time time,
            -> reg_time datetime);
    
        mysql> insert into student values
            -> (1,'sb1',"1995","1995-11-11","11:11:11","2017-11-11 11:11:11"),
            -> (2,'sb2',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12"),
            -> (3,'sb3',"1998","1998-01-01","13:13:13","2017-01-01 13:13:13");  
    
        mysql> select * from student;
        +------+------+-----------+------------+------------+---------------------+
        | id   | name | born_year | birth      | class_time | reg_time            |
        +------+------+-----------+------------+------------+---------------------+
        |    1 | sb1 |      1995 | 1995-11-11 | 11:11:11   | 2017-11-11 11:11:11 |
        |    2 | sb2 |      1997 | 1997-12-12 | 12:12:12   | 2017-12-12 12:12:12 |
        |    3 | sb3 |      1998 | 1998-01-01 | 13:13:13   | 2017-01-01 13:13:13 |
        +------+------+-----------+------------+------------+---------------------+
    View Code

      枚举类型:enum("1","2") 单选

    枚举类型(enum)
                An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)
                示例:
                    CREATE TABLE shirts (
                        name VARCHAR(40),
                        size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
                    );
                    INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');

      集合类型:set("1","2","3") 多选

     集合类型(set)
                A SET column can have a maximum of 64 distinct members.
                示例:
                    CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
                    INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');

    完整性约束:

      not null :不为空

        在字段1处设置了not null

        insert into t1(字段2,字段3.....) values(字段2的值.....)

      default : 默认值 

      unique:  唯一,不许重复(设置默认值只能用一次)

      primary key: 主见,唯一非空 not null +unique

            必须有一个主键字段,且只能有一个

            (1) 自动查看你所有的字段里面是否有 not null+unique,如果有默认就将这个字段设置为主键字段

            (2) 如果没有,自动设置一个看不到的字段作为主键.因为所有数据根据主键进行存储

        auto_increment 自增,一般加在主键后边,从1开始,每次+1,可以设置步长.

        foreign key :外键 建立表之间关系用的

            一对多: 

              t1(id)  一对多t2

              t1里面的一条数据,可以对应t2 表里面的多条数据

              t1 字段不为空,且唯一

              t2表里面加一个字段 ,ti_id 1,1,1

              建立外键关系时,t1表的id字段是被关联的字段;不为空,且唯一.

            

    create table t2(
    
                --字段名 数据类型(宽度 约束条件),
    
                id int primary key auto_increment,   
    
                name char (10) not null,
    
                sex enum("男","女") default("男") not null
    
                id_card char(18) not null unique,
    
                 t1_id int not null ,
    
                constraint fk_t2_t1  foreign key (t1_id) references t1(id)
    
    )
    

      

            一对一:

    create table t2(
    
    
                --字段名 数据类型(宽度 约束条件),
    
    
                id int primary key auto_increment,   
    
    
                name char (10) not null,
    
    
                sex enum("男","女") default("男") not null
    
    
                id_card char(18) not null unique,
    
    
                 t1_id int not null  unique,
    
    
                constraint fk_t2_t1  foreign key (t1_id) references t1(id)
    
    
    )
    

      

               t1_id int not null unique ,constraint fk_t2_t1  foreign key (t1_id) references t1(id) ##比多对一多一个unique

            多对多:

              建立第三个表t3

                t3  id   t1_id(指向t1的id)   t2_id(指向t1的id)

    库的操作

        增 :create database 库名

        删: drop database 库名

            改: alter database 库名 charset utf8;

        查: show databases;

          show create database 库名 G;

          c 结束输入

    表的操作:

          增:

          create table t2(

                --字段名 数据类型(宽度) 约束条件,(字段名和数据类型是必须的))

        删 :drop table 表名;

        改: (表字段的修改,表结构的修改):

          alter table 表名 rename 新表名

         alter table 表名 modify 字段名 数据类型 完整性约束

                  change 旧字段名 新字段名 数据类型 完整性约束

         alter table 表名  add 字段名 数据类型 完整性约束 first;

                                  after 字段名;

         alter table 表名 add foreign key (t1_id) references t1(id)

        查: show tables;

    行记录的操作:

          增; insert into 表名 values()

           删; delete from 表名 where condition 1=1 或id=10

         改: update 表名 set name="'粑粑'" where id = 10;

         查询:

          单表查询:  select *from t1 where id =1(1=1)

                            select distinct 字段... form 库名.表名

              where condition

              group by

               having

              order by

              limit

        多表查询:

                           笛卡尔积 :将两表的所有记录全部对应一遍

            select* from emp ,dep where emp.dep_id = dep.id;

          这样获得了一个包含量表所有字段对应关系数据的一张虚拟表

          连表操作:

            inner join left join right join      union

          子查询: 将子查询的结果作为另一个查询   语句的筛选条件;

         

  • 相关阅读:
    JUnit快速入门
    CoreJava笔记之线程
    CoreJava笔记之JavaBean、静态方法static和final
    CoreJava基础之构造器
    JAVA环境配置
    软件测试工程师应该具备的能力
    Apache和Tomcat的整合过程(转载)
    ios界面适配和图标的各种大小
    iPhone6和iPhone6 plus的iOS8设计尺寸参考指南
    博主写的非常详细的ios网络请求、uiwebview与js交互
  • 原文地址:https://www.cnblogs.com/kevin-red-heart/p/10297167.html
Copyright © 2020-2023  润新知