第一篇:Mysql数据类型
mysql數據類型
一,常見的信息種類
1,數值類型: 身高、 體重、成績、 工資
整數-----------》INT(大整數)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?無符號 ?0~23^32 ??? ?-------------unsigned? ? ? ? ? ? ? ? ? ? ? ?//指定無符號
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?有符號 ?-2^31~2^32 ?
? 浮點型-------->帶小數的
? ? ? ? ? ? 單精度float ?? ?2^32 ??
? ? ? ? ? ?雙精度double?? ?但精度的2倍
eg:? 工資
gz float (7,2)范圍;
xxxxx.xx
99999.99
-99999.99
# mysql -uroot -p123456
mysql> select database();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//查看當前的位置
創建db1庫:
創建t1表:
mysql> create table?t2(
? ? -> age tinyint unsigned,
? ? -> pay float(7,2)
? ? -> );
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t2 ? ? ? ? ? ?|
+---------------+
1 row in set (0.00 sec)
查看表字段
mysql> desc t2;
+-------+---------------------+------+-----+---------+-------+
| Field | Type ? ? ? ? ? ? ? ?| Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| age ? | tinyint(3) unsigned | YES ?| ? ? | NULL ? ?| ? ? ? |
| pay ? | float(7,2) ? ? ? ? ?| YES ?| ? ? | NULL ? ?| ? ? ? |
+-------+---------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
?
表中插入記錄
mysql> insert into t2 values(19,15000);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+------+----------+
| age ?| pay ? ? ?|
+------+----------+
| ? 19 | 15000.00 |
+------+----------+
1 row in set (0.00 sec)
mysql> insert into t2 values(29,4000);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t2;
+------+----------+
| age ?| pay ? ? ?|
+------+----------+
| ? 19 | 15000.00 |
| ? 29 | ?4000.00 |
+------+----------+
2 rows in set (0.00 sec)
2,字符類型
生產中用的多的:? ?
? ? ? ? ? ? ? ? ? ? ? ? 定長:char(字符數)-----------最大255-----------速度快
eg:
mysql> create table t3(
? ? -> name char(10),
? ? -> homedir varchar(15),
? ? -> email varchar(30)
? ? -> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc t3;
+---------+-------------+------+-----+---------+-------+
| Field ? | Type ? ? ? ?| Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name ? ?| char(10) ? ?| YES ?| ? ? | NULL ? ?| ? ? ? |
| homedir | varchar(15) | YES ?| ? ? | NULL ? ?| ? ? ? |
| email ? | varchar(30) | YES ?| ? ? | NULL ? ?| ? ? ? |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t3 values("zl","shanxi","echozsl@163.com");
Query OK, 1 row affected (0.00 sec)
mysql> select * from t3;
+------+---------+-----------------+
| name | homedir | email ? ? ? ? ? |
+------+---------+-----------------+
| zl ? | shanxi ?| echozsl@163.com |
+------+---------+-----------------+
1 row in set (0.00 sec)
?
3,時間類型
年? ? ? ? ? ?year? ? ? ? ? ? ?yyyy? ? ? ? ? ?2019
時間? ? ? ? time? ? ? ? ? ? hhmmss??? ? 220520(22點05分20秒)
日期? ? ? ? date? ? ? ? ? ?yyyymmdd? ?20190304
時間日期類型? ? ? ? ? timestamp和datetime
yyyymmddhhmmss
20190304221120
eg:
mysql> create table t4(
? ? -> name char(10),
? ? -> brithday date,
? ? -> start year,
? ? -> uptime time,
? ? -> party datetime
? ? -> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc t4;
+----------+----------+------+-----+---------+-------+
| Field ? ?| Type ? ? | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| name ? ? | char(10) | YES ?| ? ? | NULL ? ?| ? ? ? |
| brithday | date ? ? | YES ?| ? ? | NULL ? ?| ? ? ? |
| start ? ?| year(4) ?| YES ?| ? ? | NULL ? ?| ? ? ? |
| uptime ? | time ? ? | YES ?| ? ? | NULL ? ?| ? ? ? |
| party ? ?| datetime | YES ?| ? ? | NULL ? ?| ? ? ? |
+----------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into t4 values("zl",19920314,1992,121129,19920314102101);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t4;
+------+------------+-------+----------+---------------------+
| name | brithday ? | start | uptime ? | party ? ? ? ? ? ? ? |
+------+------------+-------+----------+---------------------+
| zl ? | 1992-03-14 | ?1992 | 12:11:29 | 1992-03-14 10:21:01 |
+------+------------+-------+----------+---------------------+
1 row in set (0.00 sec)
?
時間函數:
mysql> select now();
+---------------------+
| now() ? ? ? ? ? ? ? |
+---------------------+
| 2019-03-03 22:29:14 |
+---------------------+
1 row in set (0.00 sec)
mysql> select time(now());
+-------------+
| time(now()) |
+-------------+
| 22:29:40 ? ?|
+-------------+
1 row in set (0.00 sec)
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2019-03-03 ?|
+-------------+
1 row in set (0.00 sec)
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| ? ? ? ?2019 |
+-------------+
1 row in set (0.00 sec)
?
>select now();?? ??? ?//獲取當前系統日期時間
>select time(now());?? ?//系統時間
>select date(now());?? ?//系統日期
>select year(now());?? ?//系統年
>select year(20170715063000);?? ??? ?//指定時間里的2017年
>select date(20170820080000);?? ??? ?//2017-8-20
>select time(20170930123010);?? ??? ?//12:30:10
>select month(now());
>select day(now());
eg:各種時間函數的的使用
mysql> select now();? ? ? ? ? ? ? ? ? ? ? ? ? ?//查看當前的日期和時間
+---------------------+
| now() ? ? ? ? ? ? ? |
+---------------------+
| 2019-03-03 22:29:14 |
+---------------------+
1 row in set (0.00 sec)
mysql> select time(now());
+-------------+
| time(now()) |
+-------------+
| 22:29:40 ? ?|
+-------------+
1 row in set (0.00 sec)
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2019-03-03 ?|
+-------------+
1 row in set (0.00 sec)
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| ? ? ? ?2019 |
+-------------+
1 row in set (0.00 sec)
?
mysql> select sysdate();? ? ? ? ? ? ? ? ? ? ? //sysdate()查看系統的日期和時間
+---------------------+
| sysdate() ? ? ? ? ? |
+---------------------+
| 2019-03-04 00:07:45 |
+---------------------+
1 row in set (0.00 sec)
mysql> select curdate();? ? ? ? ? ? ? ? ? ? ? //curdate()獲取當前日期
+------------+
| curdate() ?|
+------------+
| 2019-03-04 |
+------------+
1 row in set (0.00 sec)
mysql> select curtime();? ? ? ? ? ? ? ? ? ? ?//curtime獲取當前的時間
+-----------+
| curtime() |
+-----------+
| 00:08:51 ?|
+-----------+
1 row in set (0.00 sec)
mysql> select year(now()),month(now()),day(now());? ? ? ? ? ? ? ? ? ?//獲取當前日期時間中的年份、月份、日
+-------------+--------------+------------+
| year(now()) | month(now()) | day(now()) |
+-------------+--------------+------------+
| ? ? ? ?2019 | ? ? ? ? ? ?3 | ? ? ? ? ?4 |
+-------------+--------------+------------+
1 row in set (0.00 sec)
mysql> select month(sysdate()),day(sysdate());? ? ? ? ? ? ? ? ? ? ? //獲取系統的月份、日
+------------------+----------------+
| month(sysdate()) | day(sysdate()) |
+------------------+----------------+
| ? ? ? ? ? ? ? ?3 | ? ? ? ? ? ? ?4 |
+------------------+----------------+
1 row in set (0.00 sec)
mysql> select time(sysdate());? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//獲取系統的時間
+-----------------+
| time(sysdate()) |
+-----------------+
| 00:10:54 ? ? ? ?|
+-----------------+
1 row in set (0.00 sec)
?
4,枚舉類型
enum----------->(單選)
set--------------->(多選)
eg:
mysql> select database();
+------------+
| database() |
+------------+
| db1 ? ? ? ?|
+------------+
1 row in set (0.00 sec)
mysql> create table t5(name char(10),
? ? -> likes set("eat","sleep","game","file"),? ? ? ? ? ? ? ? ? ? ? ? //枚舉類型(set)
? ? -> sex enum("boy","girl","no")? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // (enum)
? ? -> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc t5;
+-------+----------------------------------+------+-----+---------+-------+
| Field | Type ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Null | Key | Default | Extra |
+-------+----------------------------------+------+-----+---------+-------+
| name ?| char(10) ? ? ? ? ? ? ? ? ? ? ? ? | YES ?| ? ? | NULL ? ?| ? ? ? |
| likes | set('eat','sleep','game','file') | YES ?| ? ? | NULL ? ?| ? ? ? |
| sex ? | enum('boy','girl','no') ? ? ? ? ?| YES ?| ? ? | NULL ? ?| ? ? ? |
+-------+----------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t5 values("Nicole","eat,game","girl");
Query OK, 1 row affected (0.00 sec)
mysql> select * from t5;
+--------+----------+------+
| name ? | likes ? ?| sex ?|
+--------+----------+------+
| Nicole | eat,game | girl |
+--------+----------+------+
1 row in set (0.00 sec)
?
-導圖
總結
以上是生活随笔為你收集整理的第一篇:Mysql数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一篇:数据库基本管理(mysql)
- 下一篇: 第二篇:Mysql---约束条件、修改表