"Patrick" <patrick@stripped> wrote on 10/03/2005 03:43:20 PM:
> There are many ways to approach this. How are you receiving the IP
> data? Are you reading a file or other stream or are you trying to
> process the table creation by reading a column from a previously
> populated table through a select statement?
> The functions, inet_ntoa() and inet_addr(), are part of most
> networking libraries. These are the common functions to convert
> dotted quad notation. If you wanted to write you own function, an
> IPv4 address is broken down as follows:
> \ \ \ \
> \ \ \ DDD
> \ \ CCC x CCC
> \ BBB x BBB x BBB
> AAA x AAA x AAA x AAA
I am not sure of your algorithm. Perhaps I am just not understanding your
> Add the results and you have your unique 32bit number.
> eg. 10.10.10.1 = 10,000 + 1,000 + 100 + 1
> = 11,101
Let me try a different number(one a little less friendly to base10):
the address 220.127.116.11:
4^4 + 4^3 + 4^2 + 4 = 256 + 64 + 16 + 4 = 340
Which would be the same as: 18.104.22.168 = 340
your method does not seem to generate unique numbers for each IP
Maybe I just don't get your description.
Mathematically speaking: The base10 representation of the 32-bit number
that 22.214.171.124 corresponds to would be:
(2 * 256*256*256) + (30 * 256*256) + (40 * 256) + 10 = 35530762
Which is the same thing we get from
| inet_aton('126.96.36.199') |
| 35530762 |
1 row in set (0.04 sec)
as was mentioned earlier...
> If you are not able to pre-process (scrub) the incoming data
> programmatically, you would need to create a UDF in MySQL to perform
> the conversion, or, alternatively, if you want to use MySQL SELECT
> statement as-is could replace the 'period' with an 'underscore'
> using MySQL's built-in string functions like so:
> Assumptions: Reading IP address from an existing table named
> IP_Addresses with a column named ip_address and a column named status.
> SELECT "CREATE TABLE ",REPLACE(ip_address,'.','_'), " [insert
> create options here]" FROM IP_Addresses WHERE status ="ACTIVE"
Dynamic SQL? Not with that statement. He is going to need to create his
SQL statement client-side and send it pre-formatted to the server. MySQL
5.0 has the beginnings of dynamic SQL and I am not 100% sure it would
accept what you typed.
> You would obviously add your "CREATE TABLE" options and "INTO
> OUTFILE" options as needed.
> This would be an alternative to converting IPv4 to 32bit Integer.
> I hope this helps...
> If at all possible, it is probably best to continue in the MySQL
> list, there are some pretty clever people out there
> CocoNet Corporation
> SW Florida's First ISP
> 825 SE 47th Terrace
> Cape Coral, FL 33904
> ----- Original Message -----
> From: Chance Ellis
> To: Patrick
> Sent: Monday, October 03, 2005 2:22 PM
> Subject: Re: Table names with periods
> I have been trying to figure out how I can convert an IP address
> to a 32bit integer within a SQL create statement.
You are mixing purposes. MySQL has a CREATE TABLE statement but it
requires a string literal. You cannot build a CREATE TABLE statement on
the fly -inside- MySQL. You have to build your statement client-side and
send it (as a complete statemnt) to MySQL to process.
MySQL has a function that converts IP addresses into numbers (see above)
but you cannot combine that with a CREATE TABLE statement.
> Is this possible or am I thinking about this all wrong? The input
Yes, I think you are all wrong. You are being too "literal" in your design
choices. Generally if your data storage design requires you to add tables
whenever you add a new "whatever", that is a bad design. The better thing
to do is to create one table that can hold the entire class of "whatevers"
and differentiate between them with data markers. I assume that each of
these IP-named tables would look identical to every other (same column
names, same column types, ...)? The preferred method of modelling this is
to create one table (that looks just like each IP table was going to look)
and adding a column to it for the IP address. I know I am not the first
person to recommend this design (I can think of at least two others that
have also tried).
Just so that we aren't all telling you to possibly do the wrong thing: Why
do you feel that individual IP tables is a correct DATABASE design? It may
be an acceptable PROGRAMMING design (one IP list object per address) but
this is probably one of those points where good DB design and good OO
> I am given is a straight IP address. I have no way of modifying it
> other than some option in the SQL create statement is possible. I
> want to create a new table for each IP address. Without getting too
> much into the details, these are my requirements and I have been
> wasting alot of time trying to figure out how to change this string
> in the create statement.
The reason it has been so hard to do what you wanted to do is because what
you wanted to do is hard to maintain. Either trust us on our design
recommendation or tell us more about your problem. Depending on what you
tell us, perhaps neither design is appropriate. Please, and this goes for
others reading this later, never be afraid of getting into "too many
details". The details make all of the difference!! The better you can
describe your situation, the more likely you are to get a useful response.
Unimin Corporation - Spruce Pine