zoukankan      html  css  js  c++  java
  • Mysql 3 —— 建表

    建表:
    其实建表的过程就是一个画表头的过程,就是一个声明字段的过程。

    create table 表名(
    列1声明 列1参数,
    列2声明 列2参数,
    .....
    列n声明 列n参数
    )engine myisam/innodb/bdb charset utf8/gbk;

    create table member(
    id int unsigned auto_increment primary key,
    username char(20) not null default '',
    gender char(1) not null default '',
    weight tinyint unsigned not null default 0,
    birth date not null default '0000-00-00',
    salary decimal(8,2) not null default 0.00,
    lastlogin int unsigned not null default 0
    ) engine myisam charset utf8;

    修改表的语法
    一张表,创建完毕,有了N列
    之后还有可能要增加货删除或修改列
    Alter table 表名 add 列名称 列参数 列声明
    alter table m1 add birth date not null default '0000-00-00';

    alter table 表名 add 列名称 列类型 列参数 after 某列 [把新列加在某列后]
    alter table m1 add gender char(1) not null default '' after username;
    alter table m1 add pid int not null default 0 first; #如果想建一个列,且在表的最前面,用first

    删除列
    alter table 表名 drop 列名称
    alter table m1 drop pid;

    修改列,
    alter table 表名 modify 列名称 新类型和新参数
    到了某世纪,性别有男/女/雌雄同体/伪娘
    这是我们想把char(1) 改位char(4)

    修改列名称及列类型
    alter table 表名 change 旧列名 新列名 新类型 新参数
    alter table m1 change id uid int unsigned;


    知识点 列类型 
    存储同样的数据,不同的列类型,所占的空间和效率是不一样的,这就是我们建表前要列类型的意义
    所以 重点写列类型的存储范围和所占据的字节关系。

    Mysql 三大列类型
    数值型 
    整形 tinyint(占据一个字节,-128-127,0-255)、smallint(2)、mediumint(3)、int(4)、bigint(8)

    tinyint 1个字节,8个位
    [][][][][][][][]
    0000 0000 ----> 0
    1111 1111 ---->2^8-1=255

    新手知道,计算机为了表示一个数是负数,会把最高位(左侧)的0/1,当成符号来看,如为0,则是正数,如为1,则是负数

    0 0000000 ---->0
    0 1111111 ---->127

    1 0000000 ---->-0
    1 1111111 ---->-127

    二进制补码问题
    如以上理解,+0和-0重复了,浪费了一种存储的可能性,因此计算机中的负数,不是找着“后面的绝对值直接乘-1得到的“,二十用补码规则换算的。
    所以 负数 = 绝对值位-128
    1 1111111 ---->-1
    1 0000000 ---->-128

    一般而言,设某类型 N字节
    N字节,8N位
    0 ----> 2^8N-1
    -2^(8N-1) ----> +2^(8N-1)-1

    int 系列的声明时的参数 (m) unsigned zerofill
    int 系列不加特殊说明时,默认是有符号的
    加unsigned表示无符号,可以影响存储的范围
    加一个学分列
    alter table class add score tinyint unsigned not null default 0;

    分析M参数
    zerofill zero是零,fill是填充,代表0代表
    M必须和zerofill配合才有意义
    加一个学号列
    alter table class add snum smallint(5) zerofill unsigned not null default 0;

    小数(浮点型/定点型)
    float(M,D),decimal(M,D)
    M叫"精度"---->代表"总位数",而D是"标度",代表小位数(小数点右边的位数)
    加一个奖金列 alter table salary add bonus float(5,2) unsigned not null default 0.00;
    float 能存10^38,10^-38 如果M<=24,占4个字节,否则占8字节
    用来表示数据中的小数,除了float---浮点
    还有一种叫定点,定点是把整数部分和小数部分分开存储,比float精确

    字符串型
    char,varchar,text,blob

    char(6)定长字符串
    查找行记录时,如果都是定长,可以通过行数乘行的长度算出文件指针的偏移量
    对于定长N,不论够不够指定长度,实际都占据N个长度,如果不够N个长度,用空格在末尾补置N个长度
    利用率i/M<=100%
    00 (char型,如果不够M个字符,内部用空格补齐,取出时再把右侧空格删掉)

    varchar(100)也是存储0-100个字符
    对于varchar(N),不用空格补齐,但列内容前,有1-2个字节来标志该列的内容长度
    i/(i+1-2)<100%

    速度上定长快些

    注意:char(M),varchar(M)限制的是字符,不是字节
    即 char(2) charset utf8, 能存两个utf8字符,比如'中国'

    text 文本类型,一般用来是存储文章内容,新闻内容等
    声明text列时,不必给默认值。

    create table test2(
    article text
    );

    alter table test2 add img blob;

    blob,是二进制类型,用来存储图像、音频等二进制信息
    意义:2进制,0-255都有可能出现
    blob在于防止因为字符集的问题导致信息丢失

    日期/时间类型
    2020-10-24
    date 类型 YYYY-MM-DD 1000-01-01到9999-12-31
    time 类型 HH-MM-SS -838:59:59‘和’838:59:59‘
    datetime类型,日期时间类型 YYYY-MM-DD HH:MM:SS

    create table test3(
    -> star varchar(20) not null default '',
    -> birth date not null default '0000-00-00'
    -> )engine myisam charset utf8;

    mysql> insert into test3
    -> values
    -> ('张国荣','1961-03-12')
    -> ;

    alter table test3 add sign time not null default '00:00:00';

    create table test4(sname varchar(20) not null default '',logintime datetime not null default '0000-00-00 00:00:00')engine myisam charset utf8;

    insert into test4 values ('张三','2009-10-13 15:34:45');

    date 3个字节
    datetime 8个字节
    time 3个字节

    一个比较有意思的列 timestamp
    create table tests4(ts timestamp default CURRENT_TIMESTAMP,id int)engine myisam charset utf8;

    year 类型 1个字节,255个变化 1901-2155 还可以存0000年
    mysc1>year类型,还可以简写成2位
    mysa1>createtable test? (
    ya year(2)
    )engine myisam charset utf8;

    mysq1> insert in test7 values ('95'),('12');

    转载请注明出处:https://www.cnblogs.com/stu-jyj3621
  • 相关阅读:
    如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息
    STL的心得(4)运用(MFC)
    STL的心得(3)---运用(控制台)
    【Css】清除浮动,独占一行
    [Html]加链接提示
    【JS】清除子节点
    【CSS】使Div在父元素中水平居中
    [JS]回前页
    【JS】鼠标移动到链接上变手型
    【jQuery】改变控件的使能状态
  • 原文地址:https://www.cnblogs.com/stu-jyj3621/p/14284983.html
Copyright © 2011-2022 走看看