有办法确定SQL列的数据类型吗?

huangapple go评论103阅读模式
英文:

Are there ways to determine datatype for column in SQL?

问题

有没有方法确定SQL中列的数据类型?

我有一个名为"person"的表。"Person"表连接到一个名为"attribute"的表。这个表有名为"name"和"value"的列。"value"列可以包含整数、日期或布尔类型的文本数据。例如,身高、眼睛颜色等。

我可以将所有数据类型都设置为文本,然后在客户端处理数据类型。但是否有更好的方法?

英文:

Are there ways to determine datatype for a column in SQL?

I have a table called person. Person connects with a table called attribute. This table has columns called name and value. A value column can have text data that is integer, data or boolean. For example, height, eye color, etc.

I could give all data type text, and handle the data type on the client. But are there any better ways?

答案1

得分: 0

是的,有几种方法可以确定 SQL 中列的未知数据类型。以下是一些方法:

使用 DESC 命令:您可以使用 DESC 命令来查看表的结构,包括每个列的数据类型。只需运行以下 SQL 命令,将 "table_name" 替换为您感兴趣的表的名称:

DESC table_name;

这将为您提供表中列的列表,以及它们的数据类型和任何其他相关信息。

使用 DATA_TYPE 函数:DATA_TYPE 函数可用于返回表中特定列的数据类型。以下是一个示例 SQL 命令:

SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name';

这将返回指定表中指定列的数据类型。

使用 CAST 或 CONVERT 函数:如果您不确定列的数据类型,您可以使用 CAST 或 CONVERT 函数尝试将数据转换为特定的数据类型。以下是一个示例 SQL 命令:

SELECT CAST(column_name AS VARCHAR(255)) FROM table_name;

这将尝试将指定列中的数据转换为最大长度为 255 个字符的 VARCHAR 数据类型。如果转换成功,您可以使用返回的数据类型来确定列的原始数据类型。

需要注意的是,这些方法有时可能不是绝对可靠的,特别是如果列中的数据不一致或包含空值。然而,它们可以是了解表结构和列的数据类型的有用工具。

英文:

Yes, there are several ways to determine the unknown data type for a column in SQL. Here are a few methods:

Use the DESC command: You can use the DESC command to view the structure of a table, including the data types of each column. Simply run the following SQL command, replacing "table_name" with the name of the table you're interested in:

DESC table_name;

This will give you a list of columns in the table, along with their data types and any other relevant information.

Use the DATA_TYPE function: The DATA_TYPE function can be used to return the data type of a specific column in a table. Here's an example SQL command:

SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name';

This will return the data type of the specified column in the specified table.

Use the CAST or CONVERT function: If you're unsure about the data type of a column, you can use the CAST or CONVERT function to try to convert the data to a specific data type. Here's an example SQL command:

SELECT CAST(column_name AS VARCHAR(255)) FROM table_name;

This will attempt to convert the data in the specified column to a VARCHAR data type with a maximum length of 255 characters. If the conversion is successful, you can use the returned data type to determine the original data type of the column.

It's important to note that these methods may not always be foolproof, especially if the data in the column is inconsistent or contains null values. However, they can be useful tools for getting a better understanding of the structure of a table and the data types of its columns.

答案2

得分: 0

你在谈论EVA表(实体/值/属性表),又称键/值表。您正在讨论其中一个主要问题:如何处理不同的数据类型。

如果您在表中使用字符串列,当然可以存储任何可能的值。'123' 是一个数字,'2023-02-27' 是一个日期,'true' 是一个布尔值。您可以在每次使用此列时进行转换。但是,如果您的布尔值不是如预期的 'true' 或 'false',而是 'yes',例如,您的转换可能会失败。同样,对于数字,您可能会遇到小数分隔符问题('1.234' 与 '1,234'),等等。

您可以通过为您的EVA表提供每种数据类型的列,并始终仅填充一个值来处理此问题。然后,您的日期位于日期列中,如果它实际上是整数,则位于整数列中。

与EVA表一起工作很麻烦。只有在常见表格和常见列不足以满足需求时才会使用它们,例如,当处理非常不同类型的产品时,其中一个产品具有电压,而另一个具有领口形状或类似的属性。

根据这个想法,您似乎希望处理的是那些“身高”和“眼睛颜色”不一定适用的人。也许是不断变换形态的外星人?

我的建议是:如果可以避免的话,不要使用EVA表。使用普通列:person.height,person.eye_color。

英文:

You are talking of an EVA table (entity/value/attribute table) aka key/value table. And you are addressing one of its main problems: How to handle different data types.

If you use a string column in your table, you can of course store every possible value. '123' is a number, '2023-02-27' a date and 'true' a boolean. You can convert this everytime you work with this column. But if your boolean value isn't 'true' or 'false' as expected, but 'yes' for instance, your conversion may fail. Same with numbers where you hit the decimal separator problem ('1.234' vs '1,234'), etc.

You can handle this by providing your EVA table with one column per data type and always only fill one value. Then your date resides in the date column, and if it is an integer instead, it is in the integer column.

EVA tables are a nuisance to work with. You'd only use them when common tables with common columns don't suffice, e.g. when dealing with products of very different types where one product has a voltage and the other a collar shape or the like.

Following this thought, you seem to want to deal with persons where "height" and "eye color" don't necessarily apply. Aliens changing their form every other minute, maybe?

My advice: Don't use EVA, if you can avoid this. Use normal columns: person.height, person.eye_color.

huangapple
  • 本文由 发表于 2023年2月27日 14:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定