mysql可以在VALUES子句中使用不同类型的数据类型然后列数据类型吗?

时间:2021-09-13 17:13:09

Hi all(Note that I have similar topics with different questions),

大家好(注意我有不同问题的类似主题),

I'm learning MySQL. I came across some multiple-choice questions. If I doubted what the answer was or was not convinced it's correct I started searching google, * and the mysql site. However for some I still couldn't confirm what the correct answer was.

我正在学习MySQL。我遇到了一些多项选择题。如果我怀疑答案是什么或者不相信它是正确的我开始搜索谷歌,*和mysql网站。但对于一些人,我仍然无法确认正确答案是什么。

Question: True or false? "The number and datatypes of values specified in the VALUES clause of an INSERT statement must match the number and datatypes of corresponding specified in the INTO clause".

问题:是真还是假? “INSERT语句的VALUES子句中指定的值的数量和数据类型必须与INTO子句中指定的相应数量和数据类型相匹配”。

I think this is an interesting question as if a column is specified VARCHAR I can just use an int to insert into it. From what I think MySQL casts what you specified if possible. So this would be false. As an example:

我认为这是一个有趣的问题,好像一个列被指定VARCHAR我只能使用一个int插入它。根据我的想法,如果可能,MySQL会根据您的指定进行投射。所以这将是错误的。举个例子:

CREATE TABLE a (name VARCHAR(1));
INSERT INTO a (1);
INSERT INTO a (1.1);

The other way around it works as well. Create a INT column. Insert even a string literal and MySQL still allows it.

另一种方式也适用。创建INT列。甚至插入一个字符串文字,MySQL仍允许它。

If you have any remark about this question/answer or for any of my remarks I'm happy to hear it!

如果您对这个问题/答案有任何评论,或者对于我的任何评论,我很高兴听到它!

Thanks a lot in advance!

非常感谢提前!

1 个解决方案

#1


0  

The correct answer actually depends on the SQL mode, as in STRICT mode you'll receive error if the data type is wrong, and in other modes MySQL will do the best it can (for example will truncate everyting after the decimal point if you are trying to insert float in int column) to execute the query and will just produce warnings about the implicit casts it did.

正确的答案实际上取决于SQL模式,因为在STRICT模式下,如果数据类型错误,您将收到错误,而在其他模式中,MySQL将尽其所能(例如,如果您是,则会截断小数点后面的每一个)尝试在int列中插入float来执行查询,并且只会产生有关它所做的隐式转换的警告。

#1


0  

The correct answer actually depends on the SQL mode, as in STRICT mode you'll receive error if the data type is wrong, and in other modes MySQL will do the best it can (for example will truncate everyting after the decimal point if you are trying to insert float in int column) to execute the query and will just produce warnings about the implicit casts it did.

正确的答案实际上取决于SQL模式,因为在STRICT模式下,如果数据类型错误,您将收到错误,而在其他模式中,MySQL将尽其所能(例如,如果您是,则会截断小数点后面的每一个)尝试在int列中插入float来执行查询,并且只会产生有关它所做的隐式转换的警告。