mysql 学习---->字段

MySQL字段设置详解
  1. 1.填充字段  
  2. mysql> create database test1;  
  3.   
  4. mysql> use test1;  
  5. Database changed  
  6. mysql> create table t1(id1 int,id2 int(5));  
  7.   
  8. mysql> insert into t1 values(1,1);  
  9.   
  10. mysql> select * from t1;  
  11. +------+------+  
  12. | id1  | id2  |  
  13. +------+------+  
  14. |    1 |    1 |  
  15. +------+------+   
  16.   
  17. mysql> alter table t1 modify id1 int zerofill;   
  18.   
  19. mysql> alter table t1 modify id2 int(5) zerofill;   
  20.   
  21. mysql> select * from t1;  
  22. +------------+-------+  
  23. | id1        | id2   |  
  24. +------------+-------+  
  25. | 0000000001 | 00001 |  
  26. +------------+-------+  
  27.   
  28. mysql> insert into t1 values(1,1111111);  
  29.   
  30. mysql> select * from t1;  
  31. +------------+---------+  
  32. | id1        | id2     |  
  33. +------------+---------+  
  34. | 0000000001 |   00001 |  
  35. | 0000000001 | 1111111 |  
  36. +------------+---------+   
  37.   
  38. 2.自增长字段的建立  
  39. mysql> create table ai1(id int auto_increment not null primary key);   
  40.   
  41. mysql> create table ai2(id int auto_increment not null,primary key(id));  
  42.   
  43. mysql> create table ai3(id int auto_increment not null,unique(id));   
  44.   
  45. 3.带小数的字段设置  
  46. mysql> alter table t1 rename t1_test;    
  47.   
  48. mysql> desc t1_test;  
  49. +-------+---------------------------+------+-----+---------+-------+  
  50. | Field | Type                      | Null | Key | Default | Extra |  
  51. +-------+---------------------------+------+-----+---------+-------+  
  52. | id1   | int(10) unsigned zerofill | YES  |     | NULL    |       |  
  53. | id2   | int(5) unsigned zerofill  | YES  |     | NULL    |       |  
  54. +-------+---------------------------+------+-----+---------+-------+   
  55.   
  56. mysql> create table t1( id1 float(5,2) default null, id2 double(5,2) default null, id3 decimal(5,2) default null);  
  57.   
  58. mysql> insert into t1 values(1.23,1.23,1.23);  
  59.   
  60. mysql> select * from t1;  
  61. +------+------+------+  
  62. | id1  | id2  | id3  |  
  63. +------+------+------+  
  64. | 1.23 | 1.23 | 1.23 |  
  65. +------+------+------+   
  66.   
  67. mysql> insert into t1 values(1.234,1.234,1.23);  
  68.   
  69. mysql> select * from t1;  
  70. +------+------+------+  
  71. | id1  | id2  | id3  |  
  72. +------+------+------+  
  73. | 1.23 | 1.23 | 1.23 |  
  74. | 1.23 | 1.23 | 1.23 |  
  75. +------+------+------+  
  76.   
  77. mysql> insert into t1 values(1.234,1.234,1.234);   
  78.   
  79. mysql> select * from t1;  
  80. +------+------+------+  
  81. | id1  | id2  | id3  |  
  82. +------+------+------+  
  83. | 1.23 | 1.23 | 1.23 |  
  84. | 1.23 | 1.23 | 1.23 |  
  85. | 1.23 | 1.23 | 1.23 |  
  86. +------+------+------+   
  87.   
  88. mysql> show warnings;  
  89.   
  90. mysql> select * from t1;  
  91. +------+------+------+  
  92. | id1  | id2  | id3  |  
  93. +------+------+------+  
  94. | 1.23 | 1.23 | 1.23 |  
  95. | 1.23 | 1.23 | 1.23 |  
  96. | 1.23 | 1.23 | 1.23 |  
  97. +------+------+------+  
  98.   
  99. mysql> alter table t1 modify id1 float;   
  100.   
  101. mysql> alter table t1 modify id2 double;   
  102.   
  103. mysql> alter table t1 modify id3 decimal;  
  104.   
  105. mysql> insert into t1 values(1.234,1.234,1.234);   
  106.   
  107. mysql> show warnings;  
  108. +-------+------+------------------------------------------+  
  109. Level | Code | Message                                  |  
  110. +-------+------+------------------------------------------+  
  111. | Note  | 1265 | Data truncated for column 'id3' at row 1 |  
  112. +-------+------+------------------------------------------+  
  113.   
  114. mysql> select * from t1;  
  115. +-------+-------+------+  
  116. | id1   | id2   | id3  |  
  117. +-------+-------+------+  
  118. |  1.23 |  1.23 |    1 |  
  119. |  1.23 |  1.23 |    1 |  
  120. |  1.23 |  1.23 |    1 |  
  121. | 1.234 | 1.234 |    1 |  
  122. +-------+-------+------+  
  123.   
  124. mysql> create table t2(id bit(1));   
  125.   
  126. mysql> desc t2;  
  127. +-------+--------+------+-----+---------+-------+  
  128. | Field | Type   | Null | Key | Default | Extra |  
  129. +-------+--------+------+-----+---------+-------+  
  130. | id    | bit(1) | YES  |     | NULL    |       |  
  131. +-------+--------+------+-----+---------+-------+  
  132.   
  133. mysql> insert into t2 values(1);  
  134.   
  135. mysql> select bin(id),hex(id) from t2;  
  136. +---------+---------+  
  137. | bin(id) | hex(id) |  
  138. +---------+---------+  
  139. | 1       | 1       |  
  140. +---------+---------+  
  141.   
  142. mysql> insert into t2 values(2);  
  143.   
  144. mysql> show warnings;  
  145. +---------+------+---------------------------------------------+  
  146. Level   | Code | Message                                     |  
  147. +---------+------+---------------------------------------------+  
  148. | Warning | 1264 | Out of range value for column 'id' at row 1 |  
  149. +---------+------+---------------------------------------------+   
  150.   
  151. mysql> alter table t2 modify id bit(2);  
  152.   
  153. mysql> insert into t2 values(2);  
  154.   
  155. mysql> select bin(id),hex(id) from t2;  
  156. +---------+---------+  
  157. | bin(id) | hex(id) |  
  158. +---------+---------+  
  159. | 1       | 1       |  
  160. | 1       | 1       |  
  161. | 10      | 2       |  
  162. +---------+---------+   
  163.   
  164. mysql> create table t(d date,t time ,dt datetime);  
  165.   
  166. mysql> desc t;  
  167. +-------+----------+------+-----+---------+-------+  
  168. | Field | Type     | Null | Key | Default | Extra |  
  169. +-------+----------+------+-----+---------+-------+  
  170. | d     | date     | YES  |     | NULL    |       |  
  171. | t     | time     | YES  |     | NULL    |       |  
  172. | dt    | datetime | YES  |     | NULL    |       |  
  173. +-------+----------+------+-----+---------+-------+  
  174.   
  175. 4.与时间有关的字段设置  
  176. mysql> insert into t values(now(),now(),now());  
  177.   
  178. mysql> select * from t;  
  179. +------------+----------+---------------------+  
  180. | d          | t        | dt                  |  
  181. +------------+----------+---------------------+  
  182. | 2015-10-02 | 06:04:42 | 2015-10-02 06:04:42 |  
  183. +------------+----------+---------------------+  
  184.   
  185. mysql> alter table t rename t_test;  
  186.   
  187. mysql> create table t(id1 timestamp);  
  188.   
  189. mysql> desc t;  
  190. +-------+-----------+------+-----+-------------------+-----------------------------+  
  191. | Field | Type      | Null | Key | Default           | Extra                       |  
  192. +-------+-----------+------+-----+-------------------+-----------------------------+  
  193. | id1   | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |  
  194. +-------+-----------+------+-----+-------------------+-----------------------------+  
  195.   
  196. mysql> insert into t values(null);   
  197.   
  198. mysql> select * from t;  
  199. +---------------------+  
  200. | id1                 |  
  201. +---------------------+  
  202. | 2015-10-02 06:05:20 |  
  203. +---------------------+  
  204.   
  205. mysql> alter table t add id2 timestamp;  
  206.   
  207. mysql> show create table t \G;  
  208. *************************** 1. row ***************************  
  209.        Table: t  
  210. Create TableCREATE TABLE `t` (  
  211.   `id1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
  212.   `id2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'  
  213. ) ENGINE=InnoDB DEFAULT CHARSET=utf8  
  214. 1 row in set (0.00 sec)  
  215.   
  216. ERROR:   
  217. No query specified  
  218.   
  219. mysql> create table t8( id1 timestamp not null default current_timestamp, id2 datetime default null );  
  220.   
  221. mysql> show variables like 'time_zone';  
  222. +---------------+--------+  
  223. | Variable_name | Value  |  
  224. +---------------+--------+  
  225. | time_zone     | SYSTEM |  
  226. +---------------+--------+   
  227.   
  228. mysql> select * from t8 ;  
  229.   
  230. mysql> insert into t8 values(now(),now());  
  231.   
  232. mysql> select * from t8;  
  233. +---------------------+---------------------+  
  234. | id1                 | id2                 |  
  235. +---------------------+---------------------+  
  236. | 2015-10-02 06:07:16 | 2015-10-02 06:07:16 |  
  237. +---------------------+---------------------+   
  238.   
  239. mysql> set time_zone='+9:00';   
  240.   
  241. mysql> select * from t8;  
  242. +---------------------+---------------------+  
  243. | id1                 | id2                 |  
  244. +---------------------+---------------------+  
  245. | 2015-10-02 22:07:16 | 2015-10-02 06:07:16 |  
  246. +---------------------+---------------------+  
  247.   
  248. mysql> desc t8;  
  249. +-------+-----------+------+-----+-------------------+-------+  
  250. | Field | Type      | Null | Key | Default           | Extra |  
  251. +-------+-----------+------+-----+-------------------+-------+  
  252. | id1   | timestamp | NO   |     | CURRENT_TIMESTAMP |       |  
  253. | id2   | datetime  | YES  |     | NULL              |       |  
  254. +-------+-----------+------+-----+-------------------+-------+  
  255.   
  256. mysql> desc t;  
  257. +-------+-----------+------+-----+---------------------+-----------------------------+  
  258. | Field | Type      | Null | Key | Default             | Extra                       |  
  259. +-------+-----------+------+-----+---------------------+-----------------------------+  
  260. | id1   | timestamp | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |  
  261. | id2   | timestamp | NO   |     | 0000-00-00 00:00:00 |                             |  
  262. +-------+-----------+------+-----+---------------------+-----------------------------+   
  263.   
  264. mysql> insert into t(id1) values('19700101080001');  
  265.   
  266. mysql> select * from t;  
  267. +---------------------+---------------------+  
  268. | id1                 | id2                 |  
  269. +---------------------+---------------------+  
  270. | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |  
  271. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  272. +---------------------+---------------------+  
  273.   
  274. mysql> insert into t(id1) values(19700101080001);   
  275.   
  276. mysql> select * from t;  
  277. +---------------------+---------------------+  
  278. | id1                 | id2                 |  
  279. +---------------------+---------------------+  
  280. | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |  
  281. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  282. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  283. +---------------------+---------------------+  
  284.   
  285. mysql> insert into t(id1) values(19700101080000);  
  286.   
  287. mysql> select * from t;  
  288. +---------------------+---------------------+  
  289. | id1                 | id2                 |  
  290. +---------------------+---------------------+  
  291. | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |  
  292. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  293. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  294. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  295. +---------------------+---------------------+  
  296. rows in set (0.00 sec)  
  297.   
  298. mysql> show warnings;  
  299. Empty set (0.00 sec)  
  300.   
  301. mysql> desc t1;  
  302. +-------+---------------+------+-----+---------+-------+  
  303. | Field | Type          | Null | Key | Default | Extra |  
  304. +-------+---------------+------+-----+---------+-------+  
  305. | id1   | float         | YES  |     | NULL    |       |  
  306. | id2   | double        | YES  |     | NULL    |       |  
  307. | id3   | decimal(10,0) | YES  |     | NULL    |       |  
  308. +-------+---------------+------+-----+---------+-------+  
  309. rows in set (0.01 sec)  
  310.   
  311. mysql> desc t;  
  312. +-------+-----------+------+-----+---------------------+-----------------------------+  
  313. | Field | Type      | Null | Key | Default             | Extra                       |  
  314. +-------+-----------+------+-----+---------------------+-----------------------------+  
  315. | id1   | timestamp | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |  
  316. | id2   | timestamp | NO   |     | 0000-00-00 00:00:00 |                             |  
  317. +-------+-----------+------+-----+---------------------+-----------------------------+  
  318. rows in set (0.00 sec)  
  319.   
  320. mysql> insert into t(id1) value('2038-01-19 11:14:07');  
  321.  
  322. mysql> select * from t;  
  323. +---------------------+---------------------+  
  324. | id1                 | id2                 |  
  325. +---------------------+---------------------+  
  326. | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |  
  327. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  328. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  329. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  330. | 2038-01-19 11:14:07 | 0000-00-00 00:00:00 |  
  331. +---------------------+---------------------+  
  332.   
  333. mysql> insert into t(id1) value('2038-01-19 11:14:08');  
  334.   
  335. mysql> select * from t;  
  336. +---------------------+---------------------+  
  337. | id1                 | id2                 |  
  338. +---------------------+---------------------+  
  339. | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |  
  340. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  341. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  342. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  343. | 2038-01-19 11:14:07 | 0000-00-00 00:00:00 |  
  344. | 2038-01-19 11:14:08 | 0000-00-00 00:00:00 |  
  345. +---------------------+---------------------+  
  346.   
  347. mysql> insert into t(id1) value('2097-01-19 11:14:08');  
  348.   
  349. mysql> select * from t;  
  350. +---------------------+---------------------+  
  351. | id1                 | id2                 |  
  352. +---------------------+---------------------+  
  353. | 2015-10-02 22:05:20 | 0000-00-00 00:00:00 |  
  354. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  355. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  356. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  357. | 2038-01-19 11:14:07 | 0000-00-00 00:00:00 |  
  358. | 2038-01-19 11:14:08 | 0000-00-00 00:00:00 |  
  359. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |  
  360. +---------------------+---------------------+  
  361.   
  362. mysql> alter table t rename t_date;   
  363.   
  364. mysql> create table t(y year);  
  365.   
  366. mysql> desc t;  
  367. +-------+---------+------+-----+---------+-------+  
  368. | Field | Type    | Null | Key | Default | Extra |  
  369. +-------+---------+------+-----+---------+-------+  
  370. | y     | year(4) | YES  |     | NULL    |       |  
  371. +-------+---------+------+-----+---------+-------+   
  372.   
  373. mysql> insert into t values(2100);   
  374.   
  375. mysql> select * from t;  
  376. +------+  
  377. | y    |  
  378. +------+  
  379. | 2100 |  
  380. +------+  
  381.    
  382. mysql> create table t6(dt datetime);   
  383.   
  384. mysql> insert into t6 values('2007-9-3 12:10:10');   
  385.   
  386. mysql> insert into t6 values('2007/9/3 12+10+10');  
  387.   
  388. mysql> insert into t6 values('20070903121010');  
  389.   
  390. mysql> insert into t6 values(20070903121010);  
  391.   
  392. mysql> select * from t6;  
  393. +---------------------+  
  394. | dt                  |  
  395. +---------------------+  
  396. | 2007-09-03 12:10:10 |  
  397. | 2007-09-03 12:10:10 |  
  398. | 2007-09-03 12:10:10 |  
  399. | 2007-09-03 12:10:10 |  
  400. +---------------------+  
  401.   
  402. 5.varcharchar字段的长度  
  403. mysql> create table vc(v varchar(4),c char(4));   
  404.   
  405. mysql> insert into vc values('ab ','ab     ');   
  406.   
  407. mysql> select length(v),length(c) from vc;  
  408. +-----------+-----------+  
  409. | length(v) | length(c) |  
  410. +-----------+-----------+  
  411. |         3 |         2 |  
  412. +-----------+-----------+   
  413.   
  414. mysql> select concat(v,'+'),concat(c,'+'from vc;  
  415. +---------------+---------------+  
  416. | concat(v,'+') | concat(c,'+') |  
  417. +---------------+---------------+  
  418. | ab +          | ab+           |  
  419. +---------------+---------------+  
  420.   
  421. mysql> drop table t;  
  422.   
  423. mysql> create table t (c binary(3));  
  424. Query OK, 0 rows affected (0.02 sec)  
  425.   
  426. mysql> insert into t set c='a';  
  427. Query OK, 1 row affected (0.01 sec)  
  428.   
  429. mysql> select *,hex(c),c='a',c='a\0',c='a\0\0' from t;  
  430. +------+--------+-------+---------+-----------+  
  431. | c    | hex(c) | c='a' | c='a\0' | c='a\0\0' |  
  432. +------+--------+-------+---------+-----------+  
  433. | a    | 610000 |     0 |       0 |         1 |  
  434. +------+--------+-------+---------+-----------+  
  435.   
  436. mysql> alter table t rename t_binary;  
  437.   
  438. 6.枚举型字段  
  439. mysql> create table t(gender enum('M','F'));   
  440.   
  441. mysql> insert into t values('m'),('1'),('f'),(null);  
  442.   
  443. mysql> select * from t;  
  444. +--------+  
  445. | gender |  
  446. +--------+  
  447. | M      |  
  448. | M      |  
  449. | F      |  
  450. NULL   |  
  451. +--------+  
  452.   
  453. mysql> alter table t rename t_enum;  
  454.   
  455. 7.set类型字段设置  
  456. mysql> create table t(col set ('a','b','c','d' ));   
  457.   
  458. mysql> insert into t value('a,b'),('a,d,a'),('a,b'),('a,c'),('a');   
  459.   
  460. mysql> select * from t;  
  461. +------+  
  462. | col  |  
  463. +------+  
  464. | a,b  |  
  465. | a,d  |  
  466. | a,b  |  
  467. | a,c  |  
  468. | a    |  
  469. +------+  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值