数据在MYSQL中被截断

时间:2019-02-22 10:15:24

标签: mysql mysql-workbench

我正在尝试创建表并插入一些数据,但出现错误:

  

第1行的“天”列的数据被截断了

数据从列天开始被截断。我将数据类型保留为DATE。

CREATE TABLE Reserves (
    sid INT UNSIGNED NOT NULL,
    bid INT, 
    days date
);

INSERT INTO Reserves(sid,bid,days) VALUES(22,101,'10/10/98');
INSERT INTO Reserves(sid,bid,days) VALUES(22,102,'10/10/98');
INSERT INTO Reserves(sid,bid,days) VALUES(22,103,'10/8/98');
INSERT INTO Reserves(sid,bid,days) VALUES(22,104,'10/7/98');
INSERT INTO Reserves(sid,bid,days) VALUES(31,102,'11/10/98');
INSERT INTO Reserves(sid,bid,days) VALUES(31,103,'11/6/98');
INSERT INTO Reserves(sid,bid,days) VALUES(31,103,'11/6/98');
INSERT INTO Reserves(sid,bid,days) VALUES(31,104,'11/12/98');
INSERT INTO Reserves(sid,bid,days) VALUES(64,101,'9/5/98');
INSERT INTO Reserves(sid,bid,days) VALUES(64,102,'9/8/98');
INSERT INTO Reserves(sid,bid,days) VALUES(74,103,'9/8/98');

2 个答案:

答案 0 :(得分:2)

您插入了错误的日期格式,应该这样做:

INSERT INTO Reserves(sid,bid,days) VALUES(22,101,'1998-08-10');

代替

INSERT INTO Reserves(sid,bid,days) VALUES(22,103,'10/8/98');

这就是MySQL截断数据的原因,因为他无法猜测所有的日期格式。

DATE格式:YYYY-MM-DD

答案 1 :(得分:2)

您也可以将插入内容更改为:

INSERT INTO Reserves(sid,bid,days) VALUES(22,101, STR_TO_DATE('10/10/98', '%d/%m/%Y') );