Thursday, 17 November 2011

Create a table with only scale specification for a column

Hi All,

Just came across a peculiar way of specifying only the column scale and not its precision, so thought of sharing with all. It looks pretty simple but could be a handy tip for table designs.

Scenario: Create a table CONT_EMPS where Salary column must store values upto 2 places of decimals (i.e. scale is 2). Here, note that no precision has been specified for the Salary column. Lets see how to do it.

SQL> CREATE TABLE CONT_EMPS
2 (EMPID NUMBER,
3 EMPNAME VARCHAR2(256),
4 SAL NUMBER(*,2))
5 /
Table created.


SQL> DESC CONT_EMPS
Name         Null?    Type
------------ -------- ----------------------------
EMPID                 NUMBER
EMPNAME               VARCHAR2(256)
SAL                   NUMBER(38,2)


When I specify '*' in place of precision, Oracle treats it as the maximum precision allowed for the column. Additionally, this convention doesn't applies to PL/SQL data type declaration.
 
Bye for now.
Saurabh

Related articles

6 comments:

  1. Good to know. Is it specific to oracle only or any database?

    ReplyDelete
  2. HI Saurabh,
    Thanks for the post......
    well @siva, just tested Saurabh post on 10g.It worked fine

    ReplyDelete
  3. Hi Siva

    SQL Server supports number datatype precision upto 38 digits. MySQL supports its till 65 digits.

    As per Oracle documentation, Oracle supports the portability of numbers with precision equal or less than 38 digits. Therefore, reg the mentioned feature, I believe it is supported by Oracle only.

    ReplyDelete
  4. hi i came to youre site, and I have read some good information on it.

    ReplyDelete
  5. This will be the right blog for everyone who wants to learn about this topic. You recognize so a lot its just about difficult to argue with you (not that I in fact would want…HaHa). You absolutely put a new spin on a topic thats been written about for years. Good things, just very good!

    ReplyDelete
  6. Appreciate it for helping out, great information.

    ReplyDelete