Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, M.

Asked: August 08, 2001 - 7:51 pm UTC

Last updated: December 31, 2003 - 5:50 pm UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

Hi,

I would like to know how to create a user defined data type in Oracle. For example, I want to add a new data type BOOLEAN.

In SQL server, we can use sp_addtype. But in Oracle, I didn't find a similar one yet. "CREATE TYPE" seems to be used to create a object type rather than a data type.

Thanks for any help.

and Tom said...

An Object type is a datatype.

It is the way we do it in Oracle.

However if what you are looking for is a "macro pre-processor" (eg: the sp_addtype lets you say "boolean is really a number(1)" -- its an alias, not really a datatype) you can use the macro pre-processor of SQLPlus. Many people do this:

define boolean = number(1)
......

create table t ( x &boolean, y date, z varchar2(25) );



Don't forget however that boolean is already a datatype in PLSQL, you may be introducing some major confusion using that particular name.


followup to comment one

Parag - if you feel you could answer in a better, more complete fashion -- please do so. I'm just telling it like it is. SQLServer does things *differently* then Oracle, Oracle does things *differently* then SQLServer -- if we in fact were identical, what would be the point of having two? Software from different vendors does different things. You will do things in a different fashion.


Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Parag Mehta, August 09, 2001 - 10:04 am UTC

Actually i am not highly Impressed by this Answere.

Keep up the good work

A reader, August 09, 2001 - 1:11 pm UTC

I read your site every business day. Often I learn something valuable. I especially like your demonstrative examples. BTW, Love your book.

Not quite powerful enough

Jonas, August 08, 2003 - 6:28 pm UTC

While your preprocessor example works, it doesn't elegantly handle associating named check constraints to your predefined type. If you change the type, it would probably break the constraint.

If I have a "name" column defined in 20 tables, I sure would love to make sure that they are all the same type (for example - varchar2(char 64)). While Object types can meet this need, they provide an extra level of indirection. It seems like this can become cumbersome if there are multiple object types within a single table.

Many E/R tools support derived types, which is a beautiful
thing. But the problem is one has to rely on this tool for all schema changes, which can be extremely limiting.

How would you go about solving this problem?

Do you consider object types a valid solution to standardize a type for a single column?

I could use grep compare types between like named columns, but god forbid someone chooses a non-standard naming convention for a given column.

Just looking for a robust solution.

Also, is there any way I can provide feedback to the product manager's that this is a desired feature (in my book anways)?

Thanks for all your help.


Tom Kyte
August 10, 2003 - 12:06 pm UTC



Only a tool can enforce these things, as "limiting" (that is part of their actual design goal, to limit) as they are.

Even if you had a "type" -- no one is forced to use that type correctly. Only a repository based tool that enforces strict control can do that.

Synonym/Alias for datatype

A reader, December 31, 2003 - 5:37 pm UTC

Hi Tom,

After reading this, am I right that there is not way (yet) in Oracle to create a synonym or alias for another datatype such as date?

What I want to achieve is to create a "datetime" type which is the same as "date".

<<Yes, I asked you earlier about how to write database compatible codes. So bad even the date and time datatypes are named differently in Oracle and SQL Server. Where is the ANSI standards... >>


Tom Kyte
December 31, 2003 - 5:50 pm UTC

there is not. no.


You can order for a fee the ANSI standards however

a) there is no body that certifies anyone to them anymore
b) there are lots of levels of compliance
c) we are all at different levels of compliance


LCD is LCD is LCD.... lowest is lowest is lowest. might as well just use grep, sed and awk. (oh, but that won't work on windows ;)