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)

设置无线网络很常见,但选择或更改网络类型可能会令人困惑,尤其是在您不知道后果的情况下。如果您正在寻找有关如何在Windows11中将网络类型从公共更改为私有或反之亦然的建议,请继续阅读以获取一些有用的信息。Windows11中有哪些不同的网络配置文件?Windows11附带了许多网络配置文件,这些配置文件本质上是可用于配置各种网络连接的设置集。如果您在家中或办公室有多个连接,这将非常有用,因此您不必每次连接到新网络时都进行所有设置。专用和公用网络配置文件是Windows11中的两种常见类型,但通

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

随着短视频平台的盛行,视频矩阵账号营销已成为一种新兴营销方式。通过在不同平台上创建和管理多个账号,企业和个人能够实现品牌推广、粉丝增长和产品销售等目标。本文将为您探讨如何有效运用视频矩阵账号,并介绍不同类型的视频矩阵账号。一、视频矩阵账号怎么做?要想做好视频矩阵账号,需要遵循以下几个步骤:首先要明确你的视频矩阵账号的目标是什么,是为了品牌传播、粉丝增长还是产品销售。明确目标有助于制定相应的策略。2.选择平台:根据你的目标受众,选择合适的短视频平台。目前主流的短视频平台有抖音、快手、火山小视频等。

Go函数可以返回多个不同类型的值,返回值类型在函数签名中指定,并通过return语句返回。例如,函数可以返回一个整数和一个字符串:funcgetDetails()(int,string)。实战中,一个计算圆面积的函数可以返回面积和一个可选错误:funccircleArea(radiusfloat64)(float64,error)。注意事项:如果函数签名未指定类型,则返回空值;建议使用显式类型声明的return语句以提高可读性。

使用动态语言一时爽,代码重构火葬场。相信你一定听过这句话,和单元测试一样,虽然写代码的时候花费你少量的时间,但是从长远来看,这是非常值得的。本文分享如何更好的理解和使用Python的类型提示。1、类型提示仅在语法层面有效类型提示(自PEP3107开始引入)用于向变量、参数、函数参数以及它们的返回值、类属性和方法添加类型。Python的变量类型是动态的,可以在运行时修改,为代码添加类型提示,仅在语法层面支持,对代码的运行没有任何影响,Python解释器在运行代码的时候会忽略类型提示。因此类型提

C++函数有以下类型:简单函数、const函数、静态函数、虚函数;特性包括:inline函数、默认参数、引用返回、重载函数。例如,calculateArea函数使用π计算给定半径圆的面积,并将其作为输出返回。

随着互联网的快速发展,自媒体已经成为了信息传播的重要渠道。自媒体平台为个人和企业提供了一个展示自己、传播信息的舞台。目前,市场上主要的自媒体平台有微信公众号、今日头条、一点资讯、企鹅媒体平台等。这些平台各有特色,为广大自媒体从业者提供了丰富的创作空间。接下来,我们将对这些平台进行详细介绍,并探讨自媒体平台的类型。一、主要的自媒体平台有哪些?微信公众号是腾讯推出的自媒体平台,为个人和企业用户提供信息发布和传播服务。它分为服务号和订阅号两种类型,服务号主要为企业提供服务,而订阅号则侧重于资讯传播。由

Java注解用于为代码元素提供元数据,可用于元编程、错误检查、代码生成、文档生成和反射,其中Spring框架广泛使用注解进行配置,简化了应用程序开发。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
