mysql数据库基本操作1

1.测试整型是否有符号(严格模式已经设置了,超出范围会报错,不设置不会报错,但是最多存储最大范围值)

mysql> create table t1(a tinyint);
Query OK, 0 rows affected (0.03 sec)

默认是有符号的(127 -128),超过范围报错
mysql> insert into t1 values(200);
ERROR 1264 (22003): Out of range value for column 'a' at row 1

修改成无符号的(255)
mysql> alter table t1 modify a tinyint unsigned;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into t1 values(200);
Query OK, 1 row affected (0.01 sec)

mysql> select *from t1;
+------+
| a |
+------+
| 200 |
+------+
1 row in set (0.00 sec)

2.测试宽度

mysql> create table t1(id1 int,id2 int(5));
Query OK, 0 rows affected (0.03 sec)

不显示宽度会默认int(11)
mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id1 | int(11) | YES | | NULL | |
| id2 | int(5) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.04 sec)

mysql> insert into t1 values(1,1);
Query OK, 1 row affected (0.01 sec)

mysql> select *from t1;
+------+------+
| id1 | id2 |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)

修改字段类型,加入zerofill参数
mysql> alter table t1 modify id1 int zerofill;
Query OK, 1 row affected (0.66 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> desc t1;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id1 | int(10) unsigned zerofill | YES | | NULL | |
| id2 | int(5) | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
2 rows in set (0.05 sec)

mysql> alter table t1 modify id2 int(5) zerofill;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> desc t1;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id1 | int(10) unsigned zerofill | YES | | NULL | |
| id2 | int(5) unsigned zerofill | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
2 rows in set (0.04 sec)

长度不足自动在前面填充0
mysql> select *from t1;
+------------+-------+
| id1 | id2 |
+------------+-------+
| 0000000001 | 00001 |
+------------+-------+
1 row in set (0.01 sec)

测试id2宽度超过5(7个长度)
mysql> insert into t1 values(1,1111111);
Query OK, 1 row affected (0.00 sec)

可以看出,插入大于宽度限制的值并没有得到限制
mysql> select * from t1;
+------------+---------+
| id1 | id2 |
+------------+---------+
| 0000000001 | 00001 |
| 0000000001 | 1111111 |
+------------+---------+
2 rows in set (0.00 sec)

3.测试字符串

mysql> create table vc (v varchar(4),c char(4));
Query OK, 0 rows affected (0.04 sec)

mysql> desc vc;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| v | varchar(4) | YES | | NULL | |
| c | char(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.13 sec)

v和c列同时插入字符串'ab ':
mysql> insert into vc values('ab ','ab ');
Query OK, 1 row affected (0.01 sec)

这样好像看不出来差异
mysql> select * from vc;
+------+------+
| v | c |
+------+------+
| ab | ab |
+------+------+
1 row in set (0.00 sec)

字符串长度,varchar可变长字符串长度为4,char会自动删除后面空格,长度为2
mysql> select length(v),length(c) from vc;
+-----------+-----------+
| length(v) | length(c) |
+-----------+-----------+
| 4 | 2 |
+-----------+-----------+
1 row in set (0.01 sec)

这样更清楚一些
mysql> select concat(v,'+'),concat(c,'+') from vc;
+---------------+---------------+
| concat(v,'+') | concat(c,'+') |
+---------------+---------------+
| ab + | ab+ |
+---------------+---------------+
1 row in set (0.01 sec)

4.测试枚举和集合

1)枚举
mysql> create table t (gender enum('M','F'));
Query OK, 0 rows affected (0.04 sec)

mysql> desc t;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| gender | enum('M','F') | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
1 row in set (0.04 sec)

mysql> INSERT INTO t VALUES('M'),('1'),('f'),(NULL);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

多选一,如果enum('M','F')里没有,默认插入第一个'M'
mysql> select *from t;
+--------+
| gender |
+--------+
| M |
| M |
| F |
| NULL |
+--------+
4 rows in set (0.00 sec)

2)集合
mysql> create table t (col set ('a','b','c','d'));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t values('a,b'),('a,d,a'),('a,b'),('a,c'),('a');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

多选多,如果像('a,d,a')这种有重复的,只取一次,结果为'a,d'
mysql> select *from t;
+------+
| col |
+------+
| a,b |
| a,d |
| a,b |
| a,c |
| a |
+------+
5 rows in set (0.00 sec)

5.日期和时间

1)date,time,datetime
mysql> create table t (d date, t time,dt datetime);
Query OK, 0 rows affected (0.04 sec)

mysql> desc t;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d | date | YES | | NULL | |
| t | time | YES | | NULL | |
| dt | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.04 sec)

mysql> insert into t values (now(),now(),now());
Query OK, 1 row affected, 1 warning (52.38 sec)

mysql> select *from t;
+------------+----------+---------------------+
| d | t | dt |
+------------+----------+---------------------+
| 2019-07-11 | 20:02:02 | 2019-07-11 20:02:02 |
+------------+----------+---------------------+
1 row in set (0.00 sec)
由此看来,datetime是date和time的组合

2)timestamp
mysql> create table t (id1 timestamp);
Query OK, 0 rows affected (0.03 sec)

系统自动创建了默认值CURRENT_TIMESTAMP(系统日期)
mysql> desc t;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| id1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
1 row in set (0.04 sec)

插入一个null值试下
mysql> insert into t values(null);
Query OK, 1 row affected (0.01 sec)

果然是系统日期
mysql> select *from t;
+---------------------+
| id1 |
+---------------------+
| 2019-07-11 20:11:05 |
+---------------------+
1 row in set (0.00 sec)

原文地址:https://www.cnblogs.com/gaohuayan/p/11172449.html