如何在 MySQL 服务器 5.5 的列中插入字符和整数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14292412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert Character as well as Integer value in a column of MySQL server 5.5?
提问by ALV
I want to insert integer as well as character value in same column depend on condition But I don't know which datatype i need to use....
我想根据条件在同一列中插入整数和字符值但我不知道我需要使用哪种数据类型....
回答by John Woo
The data type you need to use is VARCHAR
because you can still insert string representation of an integer on that column. But if you choose to use INT
, you can't insert a string on it.
您需要使用的数据类型是VARCHAR
因为您仍然可以在该列上插入整数的字符串表示形式。但是如果选择使用INT
,则无法在其上插入字符串。
CREATE TABLE tableName
(
columnName VARCHAR(10)
);
INSERT INTO tableName VALUES ('a'); -- OK
INSERT INTO tableName VALUES ('1'); -- OK
both queries will be inserted on the table.
两个查询都将插入到表中。
in the case you'll use INT
如果你会使用 INT
CREATE TABLE tableName
(
columnName INT
);
INSERT INTO tableName VALUES (1); -- OK
INSERT INTO tableName VALUES ('a'); -- FAILED
the tendency is that you can't store string.
趋势是你不能存储字符串。
回答by vidyadhar
you can use any string data type according to requirement
您可以根据需要使用任何字符串数据类型
( char, varchar )
but don't forgot to enclose them in single quotes
但不要忘记用单引号将它们括起来
INSERT INTO tableName VALUES ('1One');
回答by palako
If you use varchar, all of these will produce the effect you want:
如果你使用 varchar,所有这些都会产生你想要的效果:
INSERT INTO atul (c1) values ('a');
INSERT INTO atul (c1) values ('1');
INSERT INTO atul (c1) values (1);
回答by Ajmal
You can use the varchar
type for that field.
您可以使用该varchar
字段的类型。
For example:
例如:
create table volt_meter(
voltage varchar(5);
)
INSERT into volt_meter values('11kv');