mysql-column attribute

data type (column type)

Numeric type

  • Numeric data are numbers
  • The system divides numeric types into integer and decimal types

Integer

  • Because sql needs to consider how to save disk space, because the system subdivides integers into 5 categories
  1. tinyint: Mini integer, stored in one byte, representing 256 states (commonly used)
  2. smallint: small integer, stored in 2 bytes, representing up to 65536 states
  3. mediumint: medium integer, use 3 bytes to store
  4. int: standard integer, use 4 bytes to store (commonly used)
  5. bigint: large integer, use 8 bytes to store
  • Create an integer table
create table if not exists my_int(
    int_1 tinyint,
    int_2 int
)charset utf8;
  • insert data
-- 有效数据
-- 查看数据为正确数据100,100
insert into my_int values(100,100);

-- 无效数据: 类型限定
-- 因为只能存储数值型,类型限制
insert into my_int values('a','188');    

-- 错误: 超出范围
-- 因为tinyint是有符号的,存储范围是-128-127,若是无符号则存储范围是0-255
insert into my_int values(255,10000);
  • All numeric types in sql are signed by default: that is, positive and negative
  • When unsigned data is required, the data type needs to be limited - unsigned, unsigned starts from 0
  • Add an unsigned field
alter table my_int add int_3 tinyint unsigned;

-- 增加一条数据,此时的255可以存储进去
insert into my_int values(127,222,255);
  • When looking at the data structure, it will be found in the type column that there will be int(11), tinyint(3) unsigned, tinyint(4) structures behind the column type
  • tinyint(3) unsigned represents unsigned data, 3 represents the display width, 255 is the width of 3 bits, the positive sign is omitted
  • tinyint(4) represents signed data, 4 represents the display width (including the sign), and -111 is a 4-bit display width (including the negative sign)
  • The display width has no special meaning, it just tells the user the form that can be displayed by default. Although the length of the display width can be customized, it cannot control the size of the data stored by itself, that is, the modification of the display width does not affect the stored data. size
  • The simple field width has no practical significance, so it will be used with zero fill, so that when the data width is not enough to display the width, it will automatically fill with zerofill, and use 0 in front of it to fill to the display width requirements width, zerofill will cause the value to automatically become unsigned
-- 新增一个宽度为2的零填充行\
alter table my_int add column tinyint(2) zerofill;

-- 插入数据
insert into my_int values(100,100,100,1),(200,200,200,2);

-- 插入的数据1,2 会显示为01,02
  • The meaning of zerofill with display width: ensure consistent data format

decimal

  • In SQL, floating-point type is divided into floating-point type and fixed-point type
  1. Float: floating decimal point, limited precision, will lose precision
  2. D Typical: Fixed decimal point, fixed precision, no loss of precision

floating point

  • Floating-point data is a precision data: it loses precision (rounding) after it exceeds the specified range.
  • Floating point: float, double two types
  • Floating point usage:
  1. Direct float: means no fractional part
  2. float(M,D): M represents the total length, D represents the length of the fractional part, and the length of the integer part is equal to MD
-- 创建表
create table if not exists my_float(
    float_1 float, -- 没有小数部分
    float_2 float(10,2), -- 总长度为10,整数为8
    float_3 float(5,2)
)charset utf8;

-- 插入数据 
-- 插入数据
insert into my_float values(1000.10,1000.10,1000.10); -- 符合条件
insert into my_float values(1234567890,12345678.90,1234.56); -- 符合条件
insert into my_float values(3e38,3.01e7,1234.56);
insert into my_float values(9999999999,99999999.99,9999.99); -- 最大值

-- 超出长度插入数据
insert into my_float values(123456,1234.123456768,123.9876543); -- 小数部分OK
insert into my_float values(123456,1234.12,12345.56);   -- 整数部分超出
  • Floating point data insertion: the integer part cannot exceed the length, but the decimal part can exceed the length (the system will automatically round up)
  • If the floating point number exceeds the precision range, it will be rounded up
  • If the integer part of the floating point number exceeds the specified length due to system carry, the system is allowed to set up.

fixed-point

  • Absolute guarantee that the integer part will not be rounded (no loss of precision)
  • The fractional part may be rounded (theoretically, the fractional part will not lose precision)
-- 创建定点型表,与float对比
create table if not exists my_decimal(
    f1 float(10,2),
    d1 decimal(10,2)
)charset utf8;

-- 插入数据
insert into my_decimal values(12345678.90,12345678.90); -- 有效数据
insert into my_decimal values(1234.123456,1234.1234356); -- 小数部分超出:ok

insert into my_decimal values(99999999.99,99999999.99); -- 没有问题
insert into my_decimal values(99999999.99,99999999.999); -- 进位超出范围
  • The integer part of the fixed-point number 一定cannot exceed the length (carry is not allowed), and the length of the fractional part can be arbitrarily exceeded (the system will automatically round up)
  • There is no problem with floating-point numbers if the carry causes the length to overflow, but not with fixed-point numbers.

String type

char

  • Fixed-length string: When the disk (two-dimensional table) defines the structure, the storage length of the final data has been determined
  • char(L): L stands for length, the length that can be stored, the unit is character, the maximum length value can be 255
  • cahr(4): In utf8 environment, 4*3=12 bytes are required

varchar

  • Variable-length string: varchar, when allocating space, it is allocated according to the largest space, but how much is actually used is determined according to the specific data

  • Varchar(L): L indicates that the theoretical length of the character is 65536 characters, but there will be 1 to 2 more bytes to determine the actual length of the storage. When the data is less than 255, there will be one more byte to determine the actual storage. Length, when the data is greater than 255 and less than 65535, 2 additional funds will be added to determine the actual storage length, but in fact, if the length exceeds 255, neither fixed length nor variable length, use the text string text

  • Varchar(10): store 10 Chinese characters, utf8 environment, 10 * 3 + 1 = 31(bytes)

​ Store 3 Chinese characters: 3 * 3 + 1 = 10(bytes)

  • Fixed length and variable length options

  • Fixed-length disk space is wasteful, but efficient. If the data is basically the same length, use fixed length, such as mobile phone number, ID card, etc.
  • The variable-length disk space is more economical, but the efficiency is low. If the length of the data cannot be determined, and different data changes, use variable-length, such as name, address, etc.

text、blob

  • If the amount of data is relatively large, generally more than 255 characters will use a text string
  • Text strings are classified according to the rough data format: text and blob
  • text: store text, binary data is actually a storage path
  • blob: store binary data, basically not used

enum

  • Enumeration string: enum. The possible results are designed in advance. In fact, the stored data must be one of the specified data, which is equivalent to the radio in html
  • How to use enumeration
  • Definition: enum(list of possible elements), enum('man','woman','secrecy')
  • Use: store data, only the data defined above can be stored
-- 创建表
create table if not exists my_enum(
    gender enum('man','woman','secrecy')
)charset utf8;

insert into my_enum values('man'),('woman'),('secrecy'); -- 插入成功

insert into my_enum values('男');    -- 错误: 没有该元素

-- 将字段结果取出来进行+0运算,会发现gender+0 是数字1,2,3分别对应man,woman,secrecy
select gender + 0, gender from my_enum;

-- 数值插入枚举元素,这样插入数据1代表man,2代表woman,3代表secrecy
insert into my_enum values(1),(2),(3);
  • The role of enumeration
  1. Standard data format, the data can only be one of the specified data
  2. Saves storage space (enums have an alias: radio), because enums store values ​​instead of strings themselves
  • Enumeration storage is to place all the specified data in the memory in the order in which the elements appear, starting from 1, so you can also use the number to insert data
  • Enumeration principle: When enumerating data specification (when defining), the system will automatically establish a corresponding relationship between numbers and enumeration elements (the relationship is placed in the log): Then when data is inserted, the system will automatically Characters are converted into corresponding numbers for storage, and then when data extraction is performed, the system automatically converts the values ​​into corresponding strings for display.
  • Because enumerations actually store values, values ​​can be inserted directly.

set

  • Collections are very similar to enumerations, but actually store values, not strings (collections are equivalent to checkboxes, multi-select boxes)
  • How to use the collection
  • Definition: set(list of elements)
  • Use: You can use the elements in the list (multiple), separated by commas
-- 创建表
create table if not exists my_set(
hobby set('篮球','足球','乒乓球','羽毛球','排球','台球','网球','棒球')
)charset utf8;

-- 插入数据
insert into my_set values('足球,台球,网球');
insert into my_set values(3); 

-- 查看集合数据
select hobby + 0, hobby from my_set;
-- 98转成二进制 = 64 + 32 + 2 = 01100010, 对应的存储是 足球,台球,网球
-- 3转成二进制 = 64 + 32 + 2 = 00000011, 所以insert into my_set values(3); 实际存储的是'篮球','足球'

-- 颠倒元素出现的顺序,不会影响存储,系统还是按照元素列表去匹配顺序
insert into my_set values('网球,台球,足球');
  • The main user of the collection is to standardize data and save space. Because the maintenance cost of the background language is relatively high, it is rarely used.

time date type

  • Datetime: time and date, the format is YYYY-mm-dd HH:ii:ss, the range is from 1000 to 9999, there are 0 values: 0000-00-00 00:00:00
  • Date: Date, which is the date part of datetime
  • Time: time (segment), between a specified interval, -time to +time
  • Timestamp: Timestamp, not a timestamp, just the YYYY-mm-dd HH:ii:ss format from 1970 is exactly the same as datetime
  • Year: year, two forms, year(2) and year(4): 1901-2156

-- 创建时间日期表
create table my_date(
d1 datetime,
d2 date,
d3 time,
d4 timestamp,
d5 year
)charset utf8;


-- 插入数据
insert into my_date values('2015-9-28 11:50:36','2015-9-28','11:50:54','2015-9-28 11:51:08',2015);

-- 时间使用负数
insert into my_date values('2015-9-28 11:50:36','2015-9-28','-11:50:54','2015-9-28 11:51:08',2015);
insert into my_date values('2015-9-28 11:50:36','2015-9-28','-211:50:54','2015-9-28 11:51:08',2015);
insert into my_date values('2015-9-28 11:50:36','2015-9-28','-2 11:50:54','2015-9-28 11:51:08',2015); -- -2过去2天:48

-- year可以使用2位或者4位
insert into my_date values('2015-9-28 11:50:36','2015-9-28','11:50:54','2015-9-28 11:51:08',69);
insert into my_date values('2015-9-28 11:50:36','2015-9-28','11:50:54','2015-9-28 11:51:08',70);

-- timestamp: 修改记录
update my_date set d1 = '2015-9-28 11:55:45' where d5 = 2069;

Guess you like

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