赞
踩
create table t1(num tinyint unsigned);
create table t1 ( id int, a bit(8));
insert into t1 values(10, 10);
select * from tt4; #发现很怪异的现象,a的数据10没有出现
+------+------+
| id | a |
+------+------+
| 10 | |
+------+------+
insert into t1 values(65, 65);
select * from t1;
+------+------+
| id | a |
+------+------+
| 10 | |
| 65 | A |
+------+------+
create table t2(gender bit(1));
insert into t2 values(0);
insert into t2 values(1);
insert into t2 values(2); -- 当插入2时,已经越界了
ERROR 1406 (22001): Data too long for column 'gender' at row 1
create table t3(id int, salary float(4,2));
insert into t3 values(100, -99.99);
insert into t3 values(101, -99.991); #多的这一点被拿掉了
select * from t3;
+------+--------+
| id | salary |
+------+--------+
| 100 | -99.99 |
| 101 | -99.99 |
+------+--------+
create table t4(id int, salary float(4,2) unsigned);
insert into t4 values(100, -0.1);
show warnings;
+---------+------+-------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------+
| Warning | 1264 | Out of range value for column 'salary' at row 1 |
+---------+------+-------------------------------------------------+
mysql> insert into t4 values(100, -0);
mysql> insert into t4 values(100, 99.99);
decimal(5,2) # 表示的范围是 -999.99 ~ 999.99
decimal(5,2) # unsigned 表示的范围 0 ~ 999.99
create table t5 ( id int, salary float(10,8), salary2 decimal(10,8));
insert into t5 values(100, 23.12345612, 23.12345612);
mysql> select * from t5;
+------+-------------+-------------+
| id | salary | salary2 |
+------+-------------+-------------+
| 100 | 23.12345695 | 23.12345612 |
+------+-------------+-------------+
mysql> create table t6(id int, name char(2));
mysql> insert into t6 values(100, 'ab');
mysql> insert into t6 values(101, '中国');
mysql> select * from tt9;
+------+--------+
| id | name |
+------+--------+
| 100 | ab |
| 101 | 中国 |
+------+--------+
create table t7(id int ,name varchar(6)); #--表示这里可以存放6个字符
insert into t7 values(100, 'hello');
insert into t7 values(100, '我爱你,中国');
select * from tt10;
+------+--------------------+
| id | name |
+------+--------------------+
| 100 | hello |
| 100 | 我爱你,中国 |
+------+--------------------+
create table tt11(name varchar(21845))charset=utf8; #--验证了utf8确实是不能超过21844
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
create table tt11(name varchar(21844)) charset=utf8;
Query OK, 0 rows affected (0.01 sec)
# 创建表 create table birthday (t1 date, t2 datetime, t3 timestamp); # 插入数据: insert into birthday(t1,t2) values('1997-7-1','2008-8-8 12:1:1'); #--插入两种时间 select * from birthday; +------------+---------------------+-------------------+ | t1 | t2 | t3 | +------------+---------------------+-------------------+ | 1997-07-01 | 2008-08-08 12:01:01 | 2024-1-1 18:28:55 | #--添加数据时,时间戳自动补上当前时间 +------------+---------------------+-------------------+ # 更新数据: update birthday set t1='2000-1-1'; select * from birthday; +------------+---------------------+-------------------+ | t1 | t2 | t3 | +------------+---------------------+-------------------+ | 2000-01-01 | 2008-08-08 12:01:01 | 2024-1-2 18:32:09 | #--更新数据,时间戳会更新成当前时间 +------------+---------------------+-------------------+
enum('选项1', '选项2', '选项3', ...);
set('选项值1','选项值2','选项值3', ...);
create table votes(
-> username varchar(30),
-> hobby set('登山', '游泳', '篮球', '武术'), #--使用数字标识每个爱好的时候,采用比特位位置来与set中的爱好对应起来
-> gender enum('男','女')); #--使用数字标识的时候,就是正常的数组下标
//插入数据
insert into votes values('雷锋', '登山,武术', '男');
insert into votes values('Juse', '登山,武术', 2);
select * from votes where gender=2;
+----------+---------------+--------+
| username | hobby | gender |
+----------+---------------+--------+
| Juse | 登山,武术 |女 |
+----------+---------------+--------+
+-----------+---------------+--------+
| username | hobby | gender |
+-----------+---------------+--------+
| 雷锋 | 登山,武术 | 男 |
| Juse | 登山,武术 | 女 |
| LiLei | 登山 | 男 |
| LiLei | 篮球 | 男 |
| HanMeiMei | 游泳 | 女 |
+-----------+---------------+--------+
mysql> select * from votes where hobby='登山';
+----------+--------+--------+
| username | hobby | gender |
+----------+--------+--------+
| LiLei | 登山 | 男 |
+----------+--------+--------+
select find_in_set('a', 'a,b,c');
+---------------------------+
| find_in_set('a', 'a,b,c') |
+---------------------------+
| 1 |
+---------------------------+
select find_in_set('d', 'a,b,c');
+---------------------------+
| find_in_set('d', 'a,b,c') |
+---------------------------+
| 0 |
+---------------------------+
select * from votes where find_in_set('登山', hobby);
+----------+---------------+--------+
| username | hobby | gender |
+----------+---------------+--------+
| 雷锋 | 登山,武术 | 男 |
| Juse | 登山,武术 | 女 |
| LiLei | 登山 | 男 |
+----------+---------------+--------+
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。