Skip to content
Advertisement

Create Table variable datatype that would allow to save integer/floats [SQL]

as the title states, when creating a table, when definining an variable + datatype like:

CREATE TABLE ExampleTable{
ID INTEGER,
NAME VARCHAR(200),
Integerandfloat 
}

Question: You can define a variable as integer or as float etc. however, is there a datatype that can hold both values, integer as well as a float number ?

Advertisement

Answer

Some databases support variant data types that can have an arbitrary type. For instance, SQL Server has sql_variant.

Most databases also allow you to create your own data type (using create type). However, the power of that functionality depends on the database.

For the choice between a float and an integer, there isn’t much choice. An 8-byte floating point representation covers all 4-byte integers, so you can just use a float. However, float is generally not very useful in relational databases. Fixed-point representations (numeric/decimal) are more common and might also do what you want.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement