Can in a relational data model, a single column have multiple data types?

Viewed 377

I had this question, in one assignment, and the answer is false. Can someone explain cuz I saw couple questions here trying to do this

2 Answers

In most SQL databases, this is not strictly possible. However, there are a few exceptions. In SQLite, any type of data can be stored in any column. However, SQLite columns support the concept of "affinity." There are 5 affinity types, including TEXT, NUMERIC, INTEGER, REAL, and BLOB. Inserting numeric data into a column with a TEXT affinity will result in that data first being converted to text.

On other databases, such as MySQL, columns do have rigid types, but there are flexibilities in other ways. For example, MySQL supports implicit casting such that the following comparison against a numeric column col is allowed and possible:

WHERE col > '123'

In this case, MySQL will implicitly convert the string literal 123 to an integer before doing the comparison.

A column value is an atomic value.

A single atomic value has exactly one data type: the declared for the column that contains the value.

So the answer is: no, a column in a relational database (that honors the SQL standard) can not have multiple data types.

Related