Home » SQL & PL/SQL » SQL & PL/SQL » How to partition Table Data Alphabetically
How to partition Table Data Alphabetically [message #615192] Sat, 31 May 2014 03:20 Go to next message
jgjeetu
Messages: 373
Registered: July 2013
Location: www.Orafaq.com/Forum
Senior Member

I want to create a table which has several columns,
and there is one column for ex. name
Now I want to partition that table on the basis of name column
and my requirement is that name that starts with A should go in partition 1 , name starts with B should go in partition 2 and so on.

Please tell how to achieve that.
Thanks
Re: How to partition Table Data Alphabetically [message #615194 is a reply to message #615192] Sat, 31 May 2014 03:32 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
And who is going to provide the test case? If not, at least create and insert statements please?

jgjeetu wrote on Sat, 31 May 2014 13:50
my requirement is that name that starts with A should go in partition 1 , name starts with B should go in partition 2 and so on.


What if the name starts with 'a' and not 'A'? Don't you think they are different?

[Updated on: Sat, 31 May 2014 04:27]

Report message to a moderator

Re: How to partition Table Data Alphabetically [message #615198 is a reply to message #615192] Sat, 31 May 2014 05:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Oracle version?

Re: How to partition Table Data Alphabetically [message #615200 is a reply to message #615192] Sat, 31 May 2014 06:02 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
PARTITION BY RANGE(NAME)
 (
  PARTITION A VALUES LESS THAN('B'),
  PARTITION B VALUES LESS THAN('C'),
  .
  .
  .
 )


SY.
Re: How to partition Table Data Alphabetically [message #615201 is a reply to message #615200] Sat, 31 May 2014 06:17 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Solomon Yakobson wrote on Sat, 31 May 2014 16:32
PARTITION BY RANGE(NAME)
 (
  PARTITION A VALUES LESS THAN('B')


This would not only insert names starting with 'A', but would also insert if any name starts with numbers or a blank or any character with ASCII value less than 'B'.

And more importantly, it will not allow names starting with 'a' in lower case.

SQL> DROP TABLE myemp;
Table dropped
SQL> CREATE TABLE myemp (
  2         empno   NUMBER PRIMARY KEY,
  3         ename    VARCHAR2(30),
  4         deptno   NUMBER)
  5    PARTITION BY RANGE (ename) (
  6         PARTITION pa VALUES LESS THAN('B'));
Table created
SQL> INSERT INTO myemp SELECT 122, 'ALLEN', 30 FROM dual; -- normal case, goes to correct partition
1 row inserted
SQL> INSERT INTO myemp SELECT 123, 'aLLEN', 30 FROM dual; -- won't insert
INSERT INTO myemp SELECT 123, 'aLLEN', 30 FROM dual
ORA-14400: inserted partition key does not map to any partition
SQL> INSERT INTO myemp SELECT 124, '1LLEN', 30 FROM dual; -- would get inserted, so incorrect
1 row inserted
SQL> INSERT INTO myemp SELECT 125, ' LLEN', 30 FROM dual; -- would get inserted, so incorrect
1 row inserted

SQL> SELECT * FROM myemp PARTITION (pa);
     EMPNO ENAME                              DEPTNO
---------- ------------------------------ ----------
       122 ALLEN                                  30
       124 1LLEN                                  30
       125  LLEN                                  30


'aLLEN' is not inserted. And it mapped '1LLEN' and ' LLEN' partition key to partition 'pa'.
Re: How to partition Table Data Alphabetically [message #615202 is a reply to message #615201] Sat, 31 May 2014 06:47 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Names normally start with capital letters. Also, OP explicitly wrote: "my requirement is that name that starts with A should go in partition 1 , name starts with B should go in partition 2 and so on".

SY.

[Updated on: Sat, 31 May 2014 06:52]

Report message to a moderator

Re: How to partition Table Data Alphabetically [message #615203 is a reply to message #615202] Sat, 31 May 2014 07:37 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
One could list partition on a virtual column of substr(ename,1,1) with 26 partitions plus a default partition for all the cases Lalit mentioned. That would be a precise solution. It does, however, make assumptions on character set. OP has not said how one should handle diacritics such as Ä .
Re: How to partition Table Data Alphabetically [message #615204 is a reply to message #615203] Sat, 31 May 2014 07:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
One could list partition on a virtual column of substr(ename,1,1)


This is what I had in mind when I asked for Oracle version.

[Updated on: Sat, 31 May 2014 07:45]

Report message to a moderator

Re: How to partition Table Data Alphabetically [message #615205 is a reply to message #615204] Sat, 31 May 2014 07:51 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
In general, we can create virtual column on any Oracle version supporting FBI.

SY.
Re: How to partition Table Data Alphabetically [message #615206 is a reply to message #615205] Sat, 31 May 2014 08:18 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Solomon Yakobson wrote on Sat, 31 May 2014 08:51
In general, we can create virtual column on any Oracle version supporting FBI.


Oops, after thinking about it for a moment, it wouldn't be possible to implement partitioning via FBI added column since we get catch 22. You can't create FBI before the table and we can't partition table without having FBI added virtual column.

SY.
Re: How to partition Table Data Alphabetically [message #615244 is a reply to message #615192] Sun, 01 June 2014 06:34 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Aside from the technical feasability, I think we need to step back and ask "WHY"?

What do you hope to accomplish with such a partitioning scheme? If we know the answer to that we might be able to offer better advice. It's quite possible that it simply won't accomplish what you think it will.

- There will not be an even distribution. For example, in the US, there are far, far more Smith's than Zagrobelny's.

- Even if, by convention, "most" names start with upper-case, that is not always the case. (d'Sousa, d'Angelo). And you certainly can't guarantee that some data entry person won't create a name with lower-case initial letter.
Re: How to partition Table Data Alphabetically [message #615246 is a reply to message #615244] Sun, 01 June 2014 07:06 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
EdStevens wrote on Sun, 01 June 2014 07:34
- There will not be an even distribution. For example, in the US, there are far, far more Smith's than Zagrobelny's.


And? Why partitions have to be of similar size? And if OP wants to do that, then S partition can be split into SA_to_SK, SL_to_SP, ...

EdStevens wrote on Sun, 01 June 2014 07:34
- Even if, by convention, "most" names start with upper-case, that is not always the case. (d'Sousa, d'Angelo). And you certainly can't guarantee that some data entry person won't create a name with lower-case initial letter.


Then OP would have to create partition for d', de, al, etc. if there is a need to keep it that way or put check constraint enforcing upper case or a trigger uppercasing names.

SY.

[Updated on: Sun, 01 June 2014 07:09]

Report message to a moderator

Re: How to partition Table Data Alphabetically [message #615297 is a reply to message #615246] Sun, 01 June 2014 15:01 Go to previous message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Solomon,

I probably wasn't as clear as I should have been. I didn't mean to suggest that data had to be evenly distributed across partitions. It just seemed to me (perhaps reading too much between the lines) that even distribution was what the OP is trying to achieve, and I was trying to point out that his plan would not achieve that. My overall point was that we all need to step back from the technical problems of implementation and find out what the OP is really trying to achieve and why - and why he thinks partitioning by first letter of last name will do it.
Previous Topic: about excute procedure
Next Topic: how to display only char data only or numeric data only not both
Goto Forum:
  


Current Time: Thu Apr 25 12:16:20 CDT 2024