标签:charset for 存储 update type float 日期 范围 des
一 设置主键 primary key
方法一
create table t1(id int primary key,name char(4));
show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(11) NOT NULL, `name` char(4) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+---------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
方法二
create table t2(id int,name char(4),primary key(id));
show create table t2;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------+ | t2 | CREATE TABLE `t2` ( `id` int(11) NOT NULL DEFAULT ‘0‘, `name` char(4) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+---------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
二 联合唯一
mysql> create table t3(id int primary key,ip char(15),port int ,unique key(ip,port)); Query OK, 0 rows affected (0.27 sec) mysql> desc t3; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | ip | char(15) | YES | MUL | NULL | | | port | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
三 枚举与集合类型
枚举
mysql> create table t4(id int ,sex enum(‘male‘,‘female‘)); Query OK, 0 rows affected (0.26 sec) mysql> insert t4 values -> (1,‘male‘), -> (2,‘男‘); Query OK, 2 rows affected, 1 warning (0.06 sec) Records: 2 Duplicates: 0 Warnings: 1 mysql> select * from t4; +------+------+ | id | sex | +------+------+ | 1 | male | | 2 | | +------+------+ 2 rows in set (0.00 sec)
集合:
mysql> create table t5(id int ,hobbies set(‘football‘,‘games‘,‘soccer‘,‘shopping‘)); Query OK, 0 rows affected (0.29 sec) mysql> insert t5 values -> (1,‘football,shopping‘), -> (2,‘beauty‘); Query OK, 2 rows affected, 1 warning (0.06 sec) Records: 2 Duplicates: 0 Warnings: 1 mysql> select * from t5; +------+-------------------+ | id | hobbies | +------+-------------------+ | 1 | football,shopping | | 2 | | +------+-------------------+ 2 rows in set (0.00 sec)
四 字符类型
char:
范围:0-255
定长,简单粗暴,浪费空间(待存储的数据长度<宽度限制),存取速度快。
varchar:
范围:0-21844
特点:
变长,精准,节省空间(待存储的数据长度<宽度限制),存取速度慢。
五 日期类型
date
time
datetime
timestamp
year
mysql> create table t6(date_time datetime,time_stamp timestamp); Query OK, 0 rows affected (0.25 sec) mysql> insert t6 values -> (now(),now()); Query OK, 1 row affected (0.06 sec) mysql> select * from t6; +---------------------+---------------------+ | date_time | time_stamp | +---------------------+---------------------+ | 2017-10-23 19:27:34 | 2017-10-23 19:27:34 | +---------------------+---------------------+ 1 row in set (0.00 sec) mysql> desc t6; +------------+-----------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------+------+-----+-------------------+-----------------------------+ | date_time | datetime | YES | | NULL | | | time_stamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +------------+-----------+------+-----+-------------------+-----------------------------+ 2 rows in set (0.01 sec)
六 数值类型
1 整型
使用默认参数就行。pass
2 小数
float
double
decimal
由上而下,精度越来越高。
七 约束
primary key
unique key
not null
zerofill
default
auto_increment
foreign key
unsigned
mysql> create table t7(id int ,sex char(6) default ‘male‘); Query OK, 0 rows affected (0.28 sec)
mysql> desc t7; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | sex | char(6) | YES | | male | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
标签:charset for 存储 update type float 日期 范围 des
原文地址:http://www.cnblogs.com/654321cc/p/7718490.html