欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

mysql支持的数据类型及其测试_MySQL

程序员文章站 2022-06-10 12:18:55
...

1.基础知识

1.1如何来查看mysql的帮助手册

?int

Help float;

1.2创建表的规则

CREATE TABLE [IF NOT EXISTS] tbl_name(

字段名 字段类型 [完整性的约束条件]);

1.3如何向表中插入数据

INSERT [INTO] tab_name [(字段名称……)] VALUES (值……)

1.4Mysql数据类型

1.数值型

整数类型

TINYINT:范围0~255;-128~127占用1字节

SMALLINT;范围0~65535-32768~32767;占用2字节

MEDIUMINT;3字节

INT:4字节

BIGINT;8字节

浮点类型

FLOAT(D,,M):占用4字节;两个参数M总共包括几位,D小数点后几位

DOUBLE(M,D):8字节;

定点类型

DECIMAL(D,M):内部以字符串形式存储,占用大小为M+2个字节;

2.字符串类型

CHAR(M)定长 0-255 占M个字节 M代表字符串长度

VARCHAR(M)变长,范围 0-65535,占M+1个字节

TINYTEXT、MEDIUMTEXT、 TEXT、LONGTEXT :只能保存字符数据,内容长度+1个字节

ENUM:最多65535个值,保存值所对应的编号,最多可以存所列举值中的一个

SET:可以选择多个值,最多64个值,保存排列的编号,顺序不区分

3.日期与时间类型

TIME 小时 分钟秒 3个字节

DATE 年月日 3个字节

DATETIME日期时间 8个字节

TIMESTAMP时间戳 4个字节

YEAR 年份 1个字节

2.实验操作

2.1数值型

(1)整型

查看当前服务器下已有的数据库:

mysql> SHOW DATABASES;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+

4 rows in set (0.00 sec)

创建数据库ran并查看是否创建成功:

mysql> CREATE DATABASE ran

-> ;

Query OK, 1 row affected (0.08 sec)

mysql> SHOW DATABASES;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| ran |

| test |

+--------------------+

5 rows in set (0.00 sec)

如何使用mysql的帮助手册:\s或者help

查看INT类型的使用范围等,

mysql> ? int

Name: 'INT'

Description:

INT[(M)] [UNSIGNED] [ZEROFILL]

A normal-size integer. The signed range is-2147483648 to 2147483647.

The unsigned range is 0 to 4294967295.

URL: http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

查看FLOAT类型的使用范围等,

mysql> help float

Name: 'FLOAT'

Description:

FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]

A small (single-precision) floating-pointnumber. Permissible values

are -3.402823466E+38 to -1.175494351E-38,0, and 1.175494351E-38 to

3.402823466E+38. These are the theoreticallimits, based on the IEEE

standard. The actual range might beslightly smaller depending on your

hardware or operating system.

M is the total number of digits and D isthe number of digits following

the decimal point. If M and D are omitted,values are stored to the

limits permitted by the hardware. Asingle-precision floating-point

number is accurate to approximately 7decimal places.

UNSIGNED, if specified, disallows negativevalues.

Using FLOAT might give you some unexpectedproblems because all

calculations in MySQL are done with doubleprecision. See

http://dev.mysql.com/doc/refman/5.5/en/no-matching-rows.html.

URL: http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

创建数据表之前应当使用数据库,登陆mysql后默认没有进入任何数据库,使用数据库ran并查看当前数据库ran所拥有的表,如下所示ran还没有创建任何表:

mysql> USE ran

Database changed

mysql> SHOW TABLES;

Empty set (0.00 sec)

为测试mysql中的整型创建数据表test1并查看是否创建成功:

mysql> CREATE TABLE IF NOT EXISTS test1(

-> num1 TINYINT,

-> num2 SMALLINT,

-> num3 MEDIUMINT,

-> num4 INT,

-> num5 BIGINT

-> );

Query OK, 0 rows affected (0.06 sec)

mysql> SHOW TABLES;

+---------------+

| Tables_in_ran |

+---------------+

| test1 |

+---------------+

1 row in set (0.00 sec)

向数据表中插入数据,测试mysql的整型:

mysql> INSERT INTO test1VALUES(127,65535,8388607,2147483647,231456789);

ERROR 1264 (22003): Out of range value forcolumn 'num2' at row 1

SMALLINT最大值为32767,超出范围报错,改成32767后插入成功:

mysql> INSERT INTO test1VALUES(127,32767,8388607,2147483647,231456789);

Query OK, 1 row affected (0.03 sec)

查询表中所有记录

SELECT * FROM tab_name;

mysql> SELECT * FROM test1;

+------+-------+---------+------------+-----------+

| num1 | num2 | num3 | num4 | num5 |

+------+-------+---------+------------+-----------+

| 127 | 32767 | 8388607 | 2147483647 | 231456789 |

+------+-------+---------+------------+-----------+

1 row in set (0.00 sec)

字符串也可以变成整型,mysql支持数据类型的转换

mysql> INSERT INTO test1VALUES('1','2','3',45,09);

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM test1;

+------+-------+---------+------------+-----------+

| num1 | num2 | num3 | num4 | num5 |

+------+-------+---------+------------+-----------+

| 127 | 32767 | 8388607 | 2147483647 | 231456789 |

| 1 | 2 | 3| 45 | 9 |

+------+-------+---------+------------+-----------+

2 rows in set (0.00 sec)

mysql> SELECT 1+'1';

+-------+

| 1+'1' |

+-------+

| 2 |

+-------+

1 row in set (0.00 sec)

mysql> SELECT 1+'KAIKEBA';

+-------------+

| 1+'KAIKEBA' |

+-------------+

| 1 |

+-------------+

1 row in set, 1 warning (0.00 sec)

mysql> SELECT 1+'3kaikeba';

+--------------+

| 1+'3kaikeba' |

+--------------+

| 4 |

+--------------+

1 row in set, 1 warning (0.00 sec)

(2)浮点数类型与定点类型

为了测试mysql的浮点数和定点数建立数据表test2:

mysql> CREATE TABLE IF NOT EXISTS test2(

-> Num1 FLOAT(6,2),

-> Num2 DOUBLE(6,2),

-> NUM3 DECIMAL(6,2)

-> );

Query OK, 0 rows affected (0.05 sec)

mysql> SHOW TABLES;

+---------------+

| Tables_in_ran |

+---------------+

| test1 |

| test2 |

+---------------+

2 rows in set (0.00 sec)

向数据表test2中插入范围内的数据

mysql> INSERT INTO test2VALUES(9999.99,9999.99,9999.99);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM test2;

+---------+---------+---------+

| Num1 | Num2 | NUM3 |

+---------+---------+---------+

| 9999.99 | 9999.99 | 9999.99 |

+---------+---------+---------+

1 row in set (0.00 sec)

当插入的数据整数超出了范围时,报错:

mysql> INSERT INTO test2VALUES(19999.99,9999.99,9999.99);

ERROR 1264 (22003): Out of range value forcolumn 'Num1' at row 1

小数位超过范围时,插入成功,但是报警告信息

ysql> INSERT INTO test2VALUES(23.34567,12.2345,67.8902);

Query OK, 1 row affected, 1 warning (0.02sec)

mysql> SELECT * FROM test2;

+---------+---------+---------+

| Num1 | Num2 | NUM3 |

+---------+---------+---------+

| 9999.99 | 9999.99 | 9999.99 |

| 23.35 | 12.23 | 67.89 |

+---------+---------+---------+

2 rows in set (0.00 sec)

数值是否会四舍五入呢?都进行四舍五入了。定点数超出范围是会产生一条警告,而浮点型不会有警告。

mysql> INSERT INTO test2VALUES(12.5678,23.5678,34.5678);

Query OK, 1 row affected, 1 warning (0.05sec)

mysql> SELECT * FROM test2;

+---------+---------+---------+

| Num1 | Num2 | NUM3 |

+---------+---------+---------+

| 9999.99 | 9999.99 | 9999.99 |

| 23.35 | 12.23 | 67.89 |

| 12.57 | 23.57 | 34.57 |

+---------+---------+---------+

3 rows in set (0.00 sec)

2.2日期时间类型

(1)Time类型3个字节

mysql> CREATE TABLE IF NOT EXISTStesttime(

-> a Time

-> );

Query OK, 0 rows affected (0.08 sec)

插入时间的第一种形式:

mysql> INSERT testtime VALUES('13:14:24');

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

+----------+

1 row in set (0.03 sec)

插入数字0也可以:

mysql> INSERT testtime VALUES(0);

Query OK, 1 row affected (0.06 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

| 00:00:00 |

+----------+

2 rows in set (0.00 sec)

插入时间是不用分隔符:

mysql> INSERT INTO testtimeVALUES('101010');

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

| 00:00:00 |

| 10:10:10 |

+----------+

3 rows in set (0.00 sec)

插入时间是在前面加上天:

mysql> INSERT INTO testtime VALUES('310:10:10');

Query OK, 1 row affected (0.16 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

| 00:00:00 |

| 10:10:10 |

| 82:10:10 |

+----------+

4 rows in set (0.00 sec)

mysql> INSERT testtime VALUES('0');

Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

| 00:00:00 |

| 10:10:10 |

| 82:10:10 |

| 15:51:51 |

| 00:00:00 |

+----------+

6 rows in set (0.00 sec)

mysql> INSERT testtime VALUES(123546);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

| 00:00:00 |

| 10:10:10 |

| 82:10:10 |

| 15:51:51 |

| 00:00:00 |

| 15:53:16 |

| 12:35:46 |

+----------+

8 rows in set (0.00 sec)

时间类型的插入形式总结:

INSERT testtime VALUES(‘13:14:23’);13:14:23

INSERT testtime VALUES(0);00:00:00

INSERT testtime VALUES(‘101010’);10:10:10

INSERT testtime VALUES(‘3 10:10:10’);82:10:10

INSERT testtime VALUES(‘5 10-10-20’);//不成功的

INSERT testtime VALUES(‘0’);00:00:00

INSERT testtime VALUES(124536);12:45:36

得到当前系统时间

NOW();

CURRENT_TIME

CURRENT_TIME()

mysql> INSERT testtime VALUES(NOW());

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

| 00:00:00 |

| 10:10:10 |

| 82:10:10 |

| 15:51:51 |

+----------+

5 rows in set (0.00 sec)

mysql> INSERT testtime VALUES(CURRENT_TIME);

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testtime;

+----------+

| a |

+----------+

| 13:14:24 |

| 00:00:00 |

| 10:10:10 |

| 82:10:10 |

| 15:51:51 |

| 00:00:00 |

| 15:53:16 |

+----------+

7 rows in set (0.00 sec)

//时间类型的最小值;

mysql> INSERT testtimeVALUES('-838:59:59');

Query OK, 1 row affected (0.01 sec)

//时间类型的最大值:

mysql> INSERT testtimeVALUES('838:59:59');

Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM testtime;

+------------+

| a |

+------------+

| 13:14:24 |

| 00:00:00 |

| 10:10:10 |

| 82:10:10 |

| 15:51:51 |

| 00:00:00 |

| 15:53:16 |

| 12:35:46 |

| -838:59:59 |

| 838:59:59 |

+------------+

10 rows in set (0.00 sec)

//超出时间类型范围报错:

mysql> INSERT testtimeVALUES('838:69:59');

ERROR 1292 (22007): Incorrect time value:'838:69:59' for column 'a' at row 1

(2)DATE日期类型3个字节

1000-01-01~9999-12-31

CREATE TABLE IF NOT EXISTS testdate(

A DATE);

INSERT testdate VALUES(‘1000-01-01’);

INSERT testdate VALUES(‘2014/12/16’);

INSERT testdate VALUES(‘20120523’);

INSERT testdate VALUES(‘201416’);

得到当前系统的日期

NOW();

CURRENT_DATE

CURRENT_DATE();

INSERT testdate VALUES(NOW());

INSERT testdate VALUES(CURRENT_DATE);

INSERT testdate VALUES(CURRENT_DATE());

INSERT testdate VALUES(0);

实验结果:

mysql> ? DATE

Name: 'DATE'

Description:

DATE

A date. The supported range is '1000-01-01'to '9999-12-31'. MySQL

displays DATE values in 'YYYY-MM-DD'format, but permits assignment of

values to DATE columns using either stringsor numbers.

URL:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-type-overview.html

mysql> CREATE TABLE IF NOT EXISTStestdate(

-> a DATE);

Query OK, 0 rows affected (0.06 sec)

mysql> INSERT testdateVALUES('1000-01-01');

Query OK, 1 row affected (0.03 sec)

mysql> INSERT testdateVALUES('2014/12/13');

Query OK, 1 row affected (0.03 sec)

mysql> INSERT testdate VALUES(20151223);

Query OK, 1 row affected (0.06 sec)

mysql> INSERT testdateVALUES('201212');

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testdate;

+------------+

| a |

+------------+

| 1000-01-01 |

| 2014-12-13 |

| 2015-12-23 |

| 2012-03-12 |

+------------+

4 rows in set (0.00 sec)

mysql> INSERT testdateVALUES(CURRENT_DATE);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testdate;

+------------+

| a |

+------------+

| 1000-01-01 |

| 2014-12-13 |

| 2015-12-23 |

| 2012-03-12 |

| 2014-10-14 |

+------------+

5 rows in set (0.00 sec)

mysql> INSERT testdate VALUES(0);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testdate;

+------------+

| a |

+------------+

| 1000-01-01 |

| 2014-12-13 |

| 2015-12-23 |

| 2012-03-12 |

| 2014-10-14 |

| 0000-00-00 |

+------------+

6 rows in set (0.00 sec)

mysql> INSERT testdate VALUES(NOW());

Query OK, 1 row affected, 1 warning (0.03sec)

mysql> SELECT * FROM testdate;

+------------+

| a |

+------------+

| 1000-01-01 |

| 2014-12-13 |

| 2015-12-23 |

| 2012-03-12 |

| 2014-10-14 |

| 0000-00-00 |

| 2014-10-14 |

+------------+

7 rows in set (0.00 sec)

(3)DATETIME类型8个字节

CREATE TABLE IF NOT EXISTS testdatetime(

S DATETIME);

INSERT testdatetime VALUES(‘2014-8-3114:14:58’);

INSERT testdatetime VALUES(NOW());

需要指定分隔符

实验结果:

mysql> CREATE TABLE IF NOT EXISTStestdatetime(

-> s DATETIME);

Query OK, 0 rows affected (0.13 sec)

mysql> INSERT testdatetimeVALUES('2014-8-31 13:23:56');

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testdatetime;

+---------------------+

| s |

+---------------------+

| 2014-08-31 13:23:56 |

+---------------------+

1 row in set (0.00 sec)

mysql> INSERT testdatetimeVALUES(NOW());

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testdatetime;

+---------------------+

| s |

+---------------------+

| 2014-08-31 13:23:56 |

| 2014-10-14 16:09:01 |

+---------------------+

2 rows in set (0.00 sec)

mysql> INSERT testdatetimeVALUES('201401 14-25-36');

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testdatetime;

+---------------------+

| s |

+---------------------+

| 2014-08-31 13:23:56 |

| 2014-10-14 16:09:01 |

| 2014-09-01 14:25:36 |

+---------------------+

3 rows in set (0.00 sec)

(4)TIMESTAMP类型4个字节

1970-01-018:01:01到2038-01-19 11:14:07

与DATETIME显示格式一样

CREATE TABLE IF NOT EXISTS testtimestamp(

S TIMESTAMP);

实验结果:

mysql> CREATE TABLE IF NOT EXISTStesttimestamp(

-> s TIMESTAMP);

Query OK, 0 rows affected (0.06 sec)

mysql> INSERT testtimestampVALUES(NOW());

Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM testtimestamp;

+---------------------+

| s |

+---------------------+

| 2014-10-14 16:14:48 |

+---------------------+

1 row in set (0.00 sec)

(5)YEAR类型

1971-2055年

CREATE TABLE IF NOT EXISTS testyear(

A YEAR);

\C撤销当前命令执行

两位转换规则:

INSERT testyear VALUES(12);2012(00-69)加上2000

INSERT testyear VALUES(78);1978(70-99)加上1900

INSERT testyear VALUES(0);0000

INSERT testyear VALUES(‘0’);2000

INSERT testyear VALUES(‘00’);2000

INSERT testyear VALUES(2014);2014

INSERT testyear VALUES(‘2015’);2015

在实际开发中有可能用整形保存时间戳来替代和日期时间相关的一些这样的数据。

mysql> CREATE TABLE IF NOT EXISTStestyear(

-> a YEAR);

Query OK, 0 rows affected (0.06 sec)

mysql> INSERT testyear VALUES('2014');

Query OK, 1 row affected (0.03 sec)

mysql> INSERT testyear VALUES(2015);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testyear;

+------+

| a |

+------+

| 2014 |

| 2015 |

+------+

2 rows in set (0.00 sec)

mysql> INSERT testyear VALUES(0);

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testyear;

+------+

| a |

+------+

| 2014 |

| 2015 |

| 0000 |

+------+

3 rows in set (0.00 sec)

mysql> INSERT testyear VALUES('0');

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testyear;

+------+

| a |

+------+

| 2014 |

| 2015 |

| 0000 |

| 2000 |

+------+

4 rows in set (0.00 sec)

mysql> INSERT testyear VALUES('00');

Query OK, 1 row affected (0.09 sec)

mysql> SELECT * FROM testyear;

+------+

| a |

+------+

| 2014 |

| 2015 |

| 0000 |

| 2000 |

| 2000 |

+------+

5 rows in set (0.00 sec)

mysql> INSERT testyear VALUES(12);

Query OK, 1 row affected (0.08 sec)

mysql> SELECT * FROM testyear;

+------+

| a |

+------+

| 2014 |

| 2015 |

| 0000 |

| 2000 |

| 2000 |

| 2012 |

+------+

6 rows in set (0.00 sec)

mysql> INSERT testyear VALUES(70);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testyear;

+------+

| a |

+------+

| 2014 |

| 2015 |

| 0000 |

| 2000 |

| 2000 |

| 2012 |

| 1970 |

+------+

7 rows in set (0.00 sec)

mysql> INSERT testyear VALUES('000');

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testyear;

+------+

| a |

+------+

| 2014 |

| 2015 |

| 0000 |

| 2000 |

| 2000 |

| 2012 |

| 1970 |

| 2000 |

+------+

8 rows in set (0.00 sec)

2.3字符类型

(1)CHAR(M):定长,0-255个字节

VARCHAR(M):变长,占M+1个字节

创建数据表同时插入A字符;

mysql> CREATE TABLE IF NOT EXISTSteststring(

-> str1 CHAR(5),

-> str2 VARCHAR(5)

-> );

Query OK, 0 rows affected (0.05 sec)

mysql> INSERT teststringVALUES('A','A');

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM teststring;

+------+------+

| str1 | str2 |

+------+------+

| A | A |

+------+------+

1 row in set (0.00 sec)

当超过5个字符时报错:

mysql> INSERT teststringVALUES('Abcdef','bcdefA');

ERROR 1406 (22001): Data too long forcolumn 'str1' at row 1

在插入中文之前需要临时转换下客户端的编码方式,否则会出现乱码或意想不到的错误:

SET NAMES GBK;//只针对当前连接有效,或者在他之后有效;如果退出后需要重新设置编码方式。

一个中文代表一个字符

INSERT teststring VALUES(‘开课吧开课’,‘大家快学习’);

怎么样在命令行提示符中输入中文:按ctrl+空格切换输入法就可以办到 

在当前连接下或不退出情况下,可以输入中文了。

否则插入出错。

查看编码方式:

\s

Status

实验结果:

设置mysql数据库之前查看mysql的编码方式:都是UTF8

mysql> \s

--------------

Connection id: 2

Current database: ran

Current user: root@localhost

SSL: Notin use

Using delimiter: ;

Server version: 5.5.19 MySQL Community Server (GPL)

Protocol version: 10

Connection: localhostvia TCP/IP

Server characterset: utf8

Db characterset: utf8

Client characterset: utf8

Conn. characterset: utf8

TCP port: 3306

Uptime: 2hours 5 min 13 sec

Threads: 1 Questions: 117 Slow queries:0 Opens: 57 Flush tables: 1 Open tables: 1 Queries per second avg: 0.015

--------------

mysql> status;

--------------

Connection id: 2

Current database: ran

Current user: root@localhost

SSL: Notin use

Using delimiter: ;

Server version: 5.5.19 MySQL Community Server (GPL)

Protocol version: 10

Connection: localhostvia TCP/IP

Server characterset: utf8

Db characterset: utf8

Client characterset: utf8

Conn. characterset: utf8

TCP port: 3306

Uptime: 2hours 5 min 31 sec

Threads: 1 Questions: 119 Slow queries: 0 Opens: 57 Flush tables: 1 Open tables:1 Queries per second avg: 0.015

//设置mysql客户端的编码方式为GBK

mysql> SET NAMES GBK;

Query OK, 0 rows affected (0.00 sec)

//再次查看mysql的编码方式已经改变:

mysql> \s

--------------

Connection id: 2

Current database: ran

Current user: root@localhost

SSL: Notin use

Using delimiter: ;

Server version: 5.5.19 MySQL Community Server (GPL)

Protocol version: 10

Connection: localhostvia TCP/IP

Server characterset: utf8

Db characterset: utf8

Client characterset: gbk

Conn. characterset: gbk

TCP port: 3306

Uptime: 2hours 6 min 46 sec

Threads: 1 Questions: 122 Slow queries:0 Opens: 57 Flush tables: 1 Open tables: 1 Queries per second avg: 0.016

//插入汉子,一个汉子代表一个字符:

mysql> INSERT teststring VALUES('开课吧开课','大家快学习');

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM teststring;

+------------+------------+

| str1 | str2 |

+------------+------------+

| A | A |

| 开课吧开课 | 大家快学习 |

+------------+------------+

2 rows in set (0.00 sec)

mysql> INSERT teststring VALUES('开课吧开课','大家快学习.');

ERROR 1406 (22001): Data too long forcolumn 'str2' at row 1

CHAR与VARCHAR区别:

(1)存储方式与检索方式不同;保存数据是,当数据长度小于指定长度,CHAR类型会在后面用空格填充指定长度,当检索数据时会去掉后面的空格;PHP可以用TRIM或RTRIM函数将查询出来的数据中的空格去掉。VARCHAR保存时不填充空格,当检索数据时如果有空格,空格保留。

(2)CHAR效率高于VARCHAR,但浪费了空间。长度超过255或者长度不固定时,只能用VARCHAR.

(2)TEXT:只能保存字符数据,存大量文本时可以用TEXT.

TINYTEXT

TEXT

MEDIUMTEXT

LONGTEXT

当长度超过6万多字时,使用TEXT,但是能用VARCHAR的地方尽量不用TEXT类型。

数据检索效率:CHAR>VARCHAR>TEXT

实验结果:

mysql> CREATE TABLE testtext(

-> a TINYTEXT,

-> b TEXT,

-> c MEDIUMTEXT,

-> d LONGTEXT);

Query OK, 0 rows affected (0.11 sec)

(3)ENUM枚举类型

ENUM(‘值1’,’值2’,……):最多65535个值

ENUM:保存值所对应的编号,插入值时只能插入列举值中的一个

CREATE TABLE testenum(

Sex ENUM(‘男’,’女’,’保密’));

INSERT testenum VALUES(‘男’);

INSERT testenum VALUES(1);

INSERT testenum VALUES(2);

INSERT testenum VALUES(3);

实验结果:

mysql> CREATE TABLE testenum(

-> sex ENUM('男','女','保密'));

Query OK, 0 rows affected (0.06 sec)

mysql> INSERT testenum VALUES(1);

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testenum;

+------+

| sex |

+------+

| 男 |

+------+

1 row in set (0.00 sec)

mysql> INSERT testenum VALUES('保密');

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM testenum;

+------+

| sex |

+------+

| 男 |

| 保密 |

+------+

2 rows in set (0.00 sec)

mysql> INSERT testenum VALUES(3);

Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM testenum;

+------+

| sex |

+------+

| 男 |

| 保密 |

| 保密 |

+------+

3 rows in set (0.00 sec)

(4)SET:保存排列的编号,不区分顺序

最多保存64个值,插值时可以插入多个值,必须插入集合中的值。

CREATE TABLE testset(

Fav SET(‘A’,’’B,’C’,’D’));

INSERT testset VALUES(‘A,B,C’);

INSERT testset VALUES(‘B,D’);

INSERT testset VALUES(‘C,D’);

INSERT testset VALUES(‘D,E’);(出错)

做权限管理时,使用集合非常简单。

实验结果:

mysql> CREATE TABLE testset(

-> fav SET('A','B','C','D'));

Query OK, 0 rows affected (0.09 sec)

mysql> INSERT testset VALUES('A,B,C');

Query OK, 1 row affected (0.03 sec)

mysql> INSERT testset VALUES('B,D');

Query OK, 1 row affected (0.02 sec)

mysql> INSERT testset VALUES('B,C');

Query OK, 1 row affected (0.03 sec)

mysql> INSERT testset VALUES('B,E');

ERROR 1265 (01000): Data truncated forcolumn 'fav' at row 1

mysql> SELECT * FROM testset;

+-------+

| fav |

+-------+

| A,B,C |

| B,D |

| B,C |

+-------+

3 rows in set (0.00 sec)

相关标签: 类型 数据