Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MySQL support user defined data types

Tags:

mysql

Is it possible to define (alias) a base data type in MySQL?

Currently I would like to define UUID as char(32), and then use UUID as the type throughout the schema definition. As we're prototyping at the moment, UUID is very likely to change - I'd like to ensure that this change is reflected consistently throughout the schema.

I'm thinking something like:

alias type UUID char(32);

Thanks in advance!

like image 971
kwutchak Avatar asked Jun 29 '10 06:06

kwutchak


People also ask

Does MySQL support user-defined types?

Mysql also supports the concept of User-defined variables, which allows passing of a value from one statement to another. A user-defined variable in Mysql is written as @var_name where, var_name is the name of the variable and can consist of alphanumeric characters, ., _, and $.

Which data types are supported by MySQL?

In MySQL there are three main data types: string, numeric, and date and time.

Which is not a valid data type in MySQL?

Boolean Data Type As a rule, they are used for logical operations. MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT).

What is user-defined data type in SQL?

User-defined data types (UDDT) in SQL Server Types A user-defined data type uses the existing data types with a set of constraints or rules. To create a UDDT, right-click on the user-defined data type folder and New user-defined data type.


1 Answers

In this case a text preprocesor like M4 or any C-language preprocesor may be useful.

If you have the following in file tables.sql:

define(UUID, char(32))
create table mytable1 (my_uuid UUID);
create table mytable2 (my_uuid UUID);

Running

$ m4 tables.sql

you'll get:

create table mytable1 (my_uuid char(32));
create table mytable2 (my_uuid char(32));
like image 137
roy Avatar answered Sep 19 '22 07:09

roy