- 1.填充字段
- mysql> create database test1;
- mysql> use test1;
- Database changed
- mysql> create table t1(id1 int,id2 int(5));
- mysql> insert into t1 values(1,1);
- mysql> select * from t1;
- +------+------+
- | id1 | id2 |
- +------+------+
- | 1 | 1 |
- +------+------+
- mysql> alter table t1 modify id1 int zerofill;
- mysql> alter table t1 modify id2 int(5) zerofill;
- mysql> select * from t1;
- +------------+-------+
- | id1 | id2 |
- +------------+-------+
- | 0000000001 | 00001 |
- +------------+-------+
- mysql> insert into t1 values(1,1111111);
- mysql> select * from t1;
- +------------+---------+
- | id1 | id2 |
- +------------+---------+
- | 0000000001 | 00001 |
- | 0000000001 | 1111111 |
- +------------+---------+
- 2.自增长字段的建立
- mysql> create table ai1(id int auto_increment not null primary key);
- mysql> create table ai2(id int auto_increment not null,primary key(id));
- mysql> create table ai3(id int auto_increment not null,unique(id));
- 3.带小数的字段设置
- mysql> alter table t1 rename t1_test;
- mysql> desc t1_test;
- +-------+---------------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+---------------------------+------+-----+---------+-------+
- | id1 | int(10) unsigned zerofill | YES | | NULL | |
- | id2 | int(5) unsigned zerofill | YES | | NULL | |
- +-------+---------------------------+------+-----+---------+-------+
- mysql> create table t1( id1 float(5,2) default null, id2 double(5,2) default null, id3 decimal(5,2) default null);
- mysql> insert into t1 values(1.23,1.23,1.23);
- mysql> select * from t1;
- +------+------+------+
- | id1 | id2 | id3 |
- +------+------+------+
- | 1.23 | 1.23 | 1.23 |
- +------+------+------+
- mysql> insert into t1 values(1.234,1.234,1.23);
- mysql> select * from t1;
- +------+------+------+
- | id1 | id2 | id3 |
- +------+------+------+
- | 1.23 | 1.23 | 1.23 |
- | 1.23 | 1.23 | 1.23 |
- +------+------+------+
- mysql> insert into t1 values(1.234,1.234,1.234);
- mysql> select * from t1;
- +------+------+------+
- | id1 | id2 | id3 |
- +------+------+------+
- | 1.23 | 1.23 | 1.23 |
- | 1.23 | 1.23 | 1.23 |
- | 1.23 | 1.23 | 1.23 |
- +------+------+------+
- mysql> show warnings;
- mysql> select * from t1;
- +------+------+------+
- | id1 | id2 | id3 |
- +------+------+------+
- | 1.23 | 1.23 | 1.23 |
- | 1.23 | 1.23 | 1.23 |
- | 1.23 | 1.23 | 1.23 |
- +------+------+------+
- mysql> alter table t1 modify id1 float;
- mysql> alter table t1 modify id2 double;
- mysql> alter table t1 modify id3 decimal;
- mysql> insert into t1 values(1.234,1.234,1.234);
- mysql> show warnings;
- +-------+------+------------------------------------------+
- | Level | Code | Message |
- +-------+------+------------------------------------------+
- | Note | 1265 | Data truncated for column 'id3' at row 1 |
- +-------+------+------------------------------------------+
- mysql> select * from t1;
- +-------+-------+------+
- | id1 | id2 | id3 |
- +-------+-------+------+
- | 1.23 | 1.23 | 1 |
- | 1.23 | 1.23 | 1 |
- | 1.23 | 1.23 | 1 |
- | 1.234 | 1.234 | 1 |
- +-------+-------+------+
- mysql> create table t2(id bit(1));
- mysql> desc t2;
- +-------+--------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+--------+------+-----+---------+-------+
- | id | bit(1) | YES | | NULL | |
- +-------+--------+------+-----+---------+-------+
- mysql> insert into t2 values(1);
- mysql> select bin(id),hex(id) from t2;
- +---------+---------+
- | bin(id) | hex(id) |
- +---------+---------+
- | 1 | 1 |
- +---------+---------+
- mysql> insert into t2 values(2);
- mysql> show warnings;
- +---------+------+---------------------------------------------+
- | Level | Code | Message |
- +---------+------+---------------------------------------------+
- | Warning | 1264 | Out of range value for column 'id' at row 1 |
- +---------+------+---------------------------------------------+
- mysql> alter table t2 modify id bit(2);
- mysql> insert into t2 values(2);
- mysql> select bin(id),hex(id) from t2;
- +---------+---------+
- | bin(id) | hex(id) |
- +---------+---------+
- | 1 | 1 |
- | 1 | 1 |
- | 10 | 2 |
- +---------+---------+
- mysql> create table t(d date,t time ,dt datetime);
- mysql> desc t;
- +-------+----------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+----------+------+-----+---------+-------+
- | d | date | YES | | NULL | |
- | t | time | YES | | NULL | |
- | dt | datetime | YES | | NULL | |
- +-------+----------+------+-----+---------+-------+
- 4.与时间有关的字段设置
- mysql> insert into t values(now(),now(),now());
- mysql> select * from t;
- +------------+----------+---------------------+
- | d | t | dt |
- +------------+----------+---------------------+
- | 2015-10-02 | 06:04:42 | 2015-10-02 06:04:42 |
- +------------+----------+---------------------+
- mysql> alter table t rename t_test;
- mysql> create table t(id1 timestamp);
- mysql> desc t;
- +-------+-----------+------+-----+-------------------+-----------------------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-----------+------+-----+-------------------+-----------------------------+
- | id1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
- +-------+-----------+------+-----+-------------------+-----------------------------+
- mysql> insert into t values(null);
- mysql> select * from t;
- +---------------------+
- | id1 |
- +---------------------+
- | 2015-10-02 06:05:20 |
- +---------------------+
- mysql> alter table t add id2 timestamp;
- mysql> show create table t \G;
- *************************** 1. row ***************************
- Table: t
- Create Table: CREATE TABLE `t` (
- `id1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- `id2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
- ERROR:
- No query specified
- mysql> create table t8( id1 timestamp not null default current_timestamp, id2 datetime default null );
- mysql> show variables like 'time_zone';
- +---------------+--------+
- | Variable_name | Value |
- +---------------+--------+
- | time_zone | SYSTEM |
- +---------------+--------+
- mysql> select * from t8 ;
- mysql> insert into t8 values(now(),now());
- mysql> select * from t8;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 06:07:16 | 2015-10-02 06:07:16 |
- +---------------------+---------------------+
- mysql> set time_zone='+9:00';
- mysql> select * from t8;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 22:07:16 | 2015-10-02 06:07:16 |
- +---------------------+---------------------+
- mysql> desc t8;
- +-------+-----------+------+-----+-------------------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-----------+------+-----+-------------------+-------+
- | id1 | timestamp | NO | | CURRENT_TIMESTAMP | |
- | id2 | datetime | YES | | NULL | |
- +-------+-----------+------+-----+-------------------+-------+
- mysql> desc t;
- +-------+-----------+------+-----+---------------------+-----------------------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-----------+------+-----+---------------------+-----------------------------+
- | id1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
- | id2 | timestamp | NO | | 0000-00-00 00:00:00 | |
- +-------+-----------+------+-----+---------------------+-----------------------------+
- mysql> insert into t(id1) values('19700101080001');
- mysql> select * from t;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- +---------------------+---------------------+
- mysql> insert into t(id1) values(19700101080001);
- mysql> select * from t;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- +---------------------+---------------------+
- mysql> insert into t(id1) values(19700101080000);
- mysql> select * from t;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- +---------------------+---------------------+
- 4 rows in set (0.00 sec)
- mysql> show warnings;
- Empty set (0.00 sec)
- mysql> desc t1;
- +-------+---------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+---------------+------+-----+---------+-------+
- | id1 | float | YES | | NULL | |
- | id2 | double | YES | | NULL | |
- | id3 | decimal(10,0) | YES | | NULL | |
- +-------+---------------+------+-----+---------+-------+
- 3 rows in set (0.01 sec)
- mysql> desc t;
- +-------+-----------+------+-----+---------------------+-----------------------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-----------+------+-----+---------------------+-----------------------------+
- | id1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
- | id2 | timestamp | NO | | 0000-00-00 00:00:00 | |
- +-------+-----------+------+-----+---------------------+-----------------------------+
- 2 rows in set (0.00 sec)
- mysql> insert into t(id1) value('2038-01-19 11:14:07');
- mysql> select * from t;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 2038-01-19 11:14:07 | 0000-00-00 00:00:00 |
- +---------------------+---------------------+
- mysql> insert into t(id1) value('2038-01-19 11:14:08');
- mysql> select * from t;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 2038-01-19 11:14:07 | 0000-00-00 00:00:00 |
- | 2038-01-19 11:14:08 | 0000-00-00 00:00:00 |
- +---------------------+---------------------+
- mysql> insert into t(id1) value('2097-01-19 11:14:08');
- mysql> select * from t;
- +---------------------+---------------------+
- | id1 | id2 |
- +---------------------+---------------------+
- | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- | 2038-01-19 11:14:07 | 0000-00-00 00:00:00 |
- | 2038-01-19 11:14:08 | 0000-00-00 00:00:00 |
- | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
- +---------------------+---------------------+
- mysql> alter table t rename t_date;
- mysql> create table t(y year);
- mysql> desc t;
- +-------+---------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+---------+------+-----+---------+-------+
- | y | year(4) | YES | | NULL | |
- +-------+---------+------+-----+---------+-------+
- mysql> insert into t values(2100);
- mysql> select * from t;
- +------+
- | y |
- +------+
- | 2100 |
- +------+
- mysql> create table t6(dt datetime);
- mysql> insert into t6 values('2007-9-3 12:10:10');
- mysql> insert into t6 values('2007/9/3 12+10+10');
- mysql> insert into t6 values('20070903121010');
- mysql> insert into t6 values(20070903121010);
- mysql> select * from t6;
- +---------------------+
- | dt |
- +---------------------+
- | 2007-09-03 12:10:10 |
- | 2007-09-03 12:10:10 |
- | 2007-09-03 12:10:10 |
- | 2007-09-03 12:10:10 |
- +---------------------+
- 5.varchar与char字段的长度
- mysql> create table vc(v varchar(4),c char(4));
- mysql> insert into vc values('ab ','ab ');
- mysql> select length(v),length(c) from vc;
- +-----------+-----------+
- | length(v) | length(c) |
- +-----------+-----------+
- | 3 | 2 |
- +-----------+-----------+
- mysql> select concat(v,'+'),concat(c,'+') from vc;
- +---------------+---------------+
- | concat(v,'+') | concat(c,'+') |
- +---------------+---------------+
- | ab + | ab+ |
- +---------------+---------------+
- mysql> drop table t;
- mysql> create table t (c binary(3));
- Query OK, 0 rows affected (0.02 sec)
- mysql> insert into t set c='a';
- Query OK, 1 row affected (0.01 sec)
- mysql> select *,hex(c),c='a',c='a\0',c='a\0\0' from t;
- +------+--------+-------+---------+-----------+
- | c | hex(c) | c='a' | c='a\0' | c='a\0\0' |
- +------+--------+-------+---------+-----------+
- | a | 610000 | 0 | 0 | 1 |
- +------+--------+-------+---------+-----------+
- mysql> alter table t rename t_binary;
- 6.枚举型字段
- mysql> create table t(gender enum('M','F'));
- mysql> insert into t values('m'),('1'),('f'),(null);
- mysql> select * from t;
- +--------+
- | gender |
- +--------+
- | M |
- | M |
- | F |
- | NULL |
- +--------+
- mysql> alter table t rename t_enum;
- 7.set类型字段设置
- mysql> create table t(col set ('a','b','c','d' ));
- mysql> insert into t value('a,b'),('a,d,a'),('a,b'),('a,c'),('a');
- mysql> select * from t;
- +------+
- | col |
- +------+
- | a,b |
- | a,d |
- | a,b |
- | a,c |
- | a |
- +------+
-
mysql 学习---->字段
