python—day37 database basic type

Create table syntax:

  1) create table table name (field name type [(width) constraint])

  Type: What kind of data type must be used to restrict the value of the field

  Constraints: Constraints are adding some additional restrictions beyond the type;

  Notice:

    1. In the same table, the field names cannot be the same

    2. Width and constraints are optional, field names and types are required

    3. No comma after the last field

  2) Basic data type integer

  1. Function: id number, various numbers, age, level;

  2. Classification: tinyint, int, bigint;

  3. Test: The default integer types are all signed;

     create table t1(x tinyint); -128,127 signed

    insert into t1 values(128),(-129);  

    create table t2(x tinyint unsigned);  0,255 unsigned

    insert into t2 values(-1),(256);   

    create table t3(x int unsigned);

    insert into t3 values(4294967296);

    create table t4(x int(12) unsigned);

    insert into t4 values(4294967296123);

    emphasize:

    For integers, the width after the data type is not a storage limit, but a display width limit; so in the creation of a table, if the field uses an integer type, there is no need to specify the display width at all, the default display width is enough to display the complete data originally stored;

  When displaying, if there are not enough 8 bits, fill it with 0. If it exceeds 8 bits, it will be displayed normally;

    create table t5(x int(8) unsigned zerofill);

    insert into t5 values(4294967296123);

    insert into t5 values(1);

    select * from t5;

  3) View sql_mode

  mysql> show variables like "%sql_mode%";

 

  Modify sql_mode to strict mode: in this mode, if the inserted data exceeds the limit, an error will be reported immediately;

  set global sql_mode = "strict_trans_tables";

 

  4) Floating point

  1. Function: store height, weight, salary

  2. Classification: float, double, decimal

  3. Test:

  Same point:

    (1): For the three, more than 30 decimal places can be stored;

  difference:

    (1): Sorting of precision from low to high: float, double, decimal

    (2): Both float and double types store more integer bits than decimal

    create table t6(x float(255,30));

    create table t7(x double(255,30));

    create table t8(x decimal(65,30));

    insert into t6 values(1.111111111111111111111111111111);

    insert into t7 values(1.111111111111111111111111111111);

    insert into t8 values(1.111111111111111111111111111111);

  mysql>select * from t6;

  

    mysql>select * from t7;

  

  mysql>select * from t8;

  

 

   5) Character type:

  1. Function: name, address, description information

  2. Classification: char fixed length, varchar variable length

  3.测试:字符的宽度限制单位是字符个数;

  create table t9(x char(4));#超出4个字符会报错,不够4个字符则用空格补全;

  create table t10(x varchar(4));#超过4个字符会报错,不够4个字符那么字符有几个就存几个;

  insert into t9 values('hello');

  insert into t10 values('hello');

  insert into t9 values('a');

  insert into t10 values('a');

  set global sql_medo = "strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH";

  select char_length(x) from t9;

  select char_length(x) from t10;

  注意:

  针对char类型,mysql在存储时会将数据用空格补全存放到硬盘中;

  但会在读出结果时自动去掉末尾空格,因为末尾的空格在以下场景中是无用;

  insert into t9 valuse('egon  '),('alex'),('lxx');

  mysql>select * from t9 where x='egon';

  x字段明确地等于一个值,该值后填充空格是没有用的;

  select * from t10 where x like 'lxx';

  x字段模糊匹配一个值,该值后填充空格是有用的;

对比char 与 varchar

  name char(5)

  #缺点:浪费空间!

  #优点:存取速度快!

  egon alex lxx  wxx  yx   ;

  name varchar(5)

  #缺点:存取速度都慢

  #优点:节省空间

(1bytes+egon)(1bytes+alex)(1bytes+lxx)

  注意:

    如果在存取的值都一样的情况下char更省空间;

  6)日期类型:

  1.作用:时间关系

  2.分类:date、time、datetime、year

    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939523&siteId=291194637