mysql(4)、表存储的数据类型

整数类型

正负指定:默认为有符号整数,在整数类型后面指定unsigned将其变为无符号整数.

create table t1(
    id int primary key auto_increment,
    name char(32);
    age tinyint unsigned
    );

常见类型和范围如下

大小 类型 范围(正负) 范围(正数)
1byte tinyint -128~127 0-255
2bytes smallint ... 0-65536-1
4bytes int ... 0-4294967296-1
8bytes bigint ... 0-2**64-1

浮点型

floatFLOAT[(M,D)] [UNSIGNED] [ZEROFILL],其中m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30

double:随着小数的增多,精度比float要高,但也会变得不准确.

decimal:相对于另外两种数据类型更为精确.内部以字符串形式存储.最大位数为65位数,支持最大小数点后30位数.

日期类型

year:单独插入时间时,需要以字符串的形式,按照对应的格式插入;插入年份时,尽量使用4位值,当插入两位年份时,<=69,以20开头,例如50,存储结果2050 ;>=70,以19开头,例如71,存储结果1971.

create table t10(born_year year); #无论year指定何种宽度,最后都默认是year(4)
insert into t10 values  
    -> (1900),
    -> (1901),
    -> (2155),
    -> (2156);
    
select * from t10;
+-----------+
| born_year |
+-----------+
|      0000 |
|      1901 |
|      2155 |
|      0000 |
+-----------+

# 存储两位数年份
create table t12(y year);
insert into t12 values  
    -> (50),
    -> (71);
MariaDB [db1]> select * from t12;
+------+
| y    |
+------+
| 2050 |
| 1971 |
+------+

date & time & datetime:

> create table t11(d date, t time, dt datetime);
> desc t11;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d     | date     | YES  |     | NULL    |       |
| t     | time     | YES  |     | NULL    |       |
| dt    | datetime | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

> insert into t11 values(now(),now(),now());
# 自动获取对应的格式的内容
> select * from t11;
+------------+----------+---------------------+
| d          | t        | dt                  |
+------------+----------+---------------------+
| 2017-07-25 | 16:26:54 | 2017-07-25 16:26:54 |
+------------+----------+---------------------+

timestamp:

> create table t12(time timestamp);
> insert into t12 values();  # 默认输入系统时间
> insert into t12 values(null);
> select * from t12;

+---------------------+
| time                |
+---------------------+
| 2017-07-25 16:29:17 |
| 2017-07-25 16:30:01 |
+---------------------+

小练习:

> create table student(
    -> id int,
    -> name varchar(20),
    -> born_year year,
    -> birth date,
    -> class_time time,
    -> reg_time datetime);

> insert into student values
    -> (1, 'alex', "1995", "1995-11-11", "11:11:11", "2017-11-11 11:11:11"),
    -> (2, 'egon', "1997", "1997-12-12", "12:12:12", "2017-12-12 12:12:12"),
    -> (3, 'wsb', "1998", "1998-01-01", "13:13:13", "2017-01-01 13:13:13");

> select * from student;
+------+------+-----------+------------+------------+---------------------+
| id   | name | born_year | birth      | class_time | reg_time            |
+------+------+-----------+------------+------------+---------------------+
|    1 | alex |      1995 | 1995-11-11 | 11:11:11   | 2017-11-11 11:11:11 |
|    2 | egon |      1997 | 1997-12-12 | 12:12:12   | 2017-12-12 12:12:12 |
|    3 | wsb  |      1998 | 1998-01-01 | 13:13:13   | 2017-01-01 13:13:13 |
+------+------+-----------+------------+------------+---------------------+

DATETIME & TIMESTAMP区别:

  1. DATETIME的日期范围是1001——9999年,TIMESTAMP的时间范围是1970——2038年;
  2. DATETIME存储时间与时区无关,TIMESTAMP存储时间与时区有关;
  3. DATETIME使用8字节的存储空间,TIMESTAMP的存储空间为4字节。因此,TIMESTAMPDATETIME的空间利用率更高;
  4. DATETIME的默认值为null,TIMESTAMP的字段默认值为当前时间CURRENT_TIMESTAMP

字符串

假设每列字符都是ASCII字符,表字段设定varchar(4)char(4)区别如下:

CHAR(M)

  • The length of a CHAR column is fixed to the length that you declare when you create the table.
  • When CHAR values are stored, they are right-padded with spaces to the specified length.
  • When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
  • The length can be any value from 0 to 255.
# CHAR单列存储最大字符个数为255
mysql> alter table t1
    -> modify name char(255);
Query OK, 1 row affected (0.08 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> alter table t1
    -> modify name char(256);
ERROR 1074 (42000): Column length too big for column 'name' (max = 255); use BLOB or TEXT instead

VARCHAR(M)

  • Values in VARCHAR columns are variable-length strings.
  • The length can be specified as a value from 0 to 65,535.
  • The effective maximum length of a VARCHAR is subject to the maximum row size (65,535
    bytes, which is shared among all columns) and the character set used.
  • VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.
mysql> alter table t1
    -> modify name varchar(21844);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table t1
    -> modify name varchar(21845); 
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
Limits on Table Column Count and Row Size
  • MySQL has hard limit of 4096 columns per table, but the effective maximum may be less for a given table.
  • MYSQL maximum row size limit is 65,535 bytes,even if the storage engine is capable of supporting larger rows.

若表使用的字符编码是 utf8,那么最大长度限定为65536/3=21845,因为一个汉字需要三个字节表示

mysql> alter table t2 charset utf8;

mysql> alter table t2 modify name varchar(65535);
ERROR 1074 (42000): Column length too big for column 'name' (max = 21845); use BLOB or TEXT instead

若使用字符编码是 latin1,那么最大长度限定为65535-3

# 修改表字符集为ASCII
mysql> alter table t2 charset latin1;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table t2 modify name varchar(65532);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table t2 modify name varchar(65533);
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

char & varchar & text & blog区别

枚举 & 集合

枚举类型:单选

create table t1(
    id int,
    gender enum('male','female')
    );

集合类型:多选

create table t1(
    id int,
    hobby set('reading','music','sport')
);

# 赋值时候若有多个项目以逗号隔开:      
insert into table t1 values
    (1,'reading, music');   

猜你喜欢

转载自www.cnblogs.com/fqh202/p/9428029.html
今日推荐