Spiga

MySQL Database versioning strategy

April 14, 09 by Gabi Solomon

IF you just got into versioning your code then you are probably really happy with your acomplishment.
But do you have your database versioned ? This is the questioned asked in this here by Jeff Atwood and also asked by a lot of users on stackoverflow.

After reading a lot on this topic i decided on a strategy on how to keep my database under version control.
As a warning it isn’t the most easy strategy and it really takes some discipline from all the developers.
Read the rest of this entry »

mysql error : Incorrect information in file: ./table.frm

March 11, 09 by Gabi Solomon

After tinkering with my VPS settings to optimize it ( it was having some low memory problems ) and after restarting the server, just when i tought everything was ok i noticed that i managed to break some websites.

they all got this error:

mysql error : Incorrect information in file: ./table.frm

After a bit of googling i found the problem … apparently it could be because there is no support for InnoDB … oops. In my quest for optimization i disabled the support for InnoDB tables thinking that there werent any InnoDB tables in the websites hosted on that VPS .. apparently they were :( .

Hope you get here faster then i found the solution.
Cheers

Multilanguage database design approach

December 26, 08 by Gabi Solomon

Preinfo

Before you go right to the comment section and recommend gettext or other similar ways, know that i am talking about content that is manageable from an admin panel or is added by the user

Also this article is based on personal experience and is not necessary the best way to do this.

Building a metalanguage website poses a lot of problems, and one of them is how you store the content in the database for each language.

If you do a google search you will find little resources about it, and most of the are on forums. This seem a bit strange to me, so after i had decided on a database schema for a metalanguage website i decided to post it here in the hope that other people might find it useful and save them some googling time.

As far as i searched there are more or less 4 databases schemas for metalanguage website.

1. Column approach

This approach is very common and basically it duplicates the column of content for each language.

table pages
-- id (int)
-- title_en (varchar)
-- title_es (varchar)
-- content_en (varchar)
-- content_es (varchar)

The way you would query it is by automatically selecting the right columns according to the language chosen:

SQL:
  1. SELECT `id`, `title_en` AS `title`, `content_en` AS `content` FROM `pages`

Or you could select all and do the column selection from php :

PHP:
  1. echo $rowPage['title' . $_SESSION['currentLanguage']];

Advantages

  • It doesn't have duplicate content, since there is only one row for each record, and only the language columns are duplicated
  • Easy to implement

Disadvantages

  • You need to build the watch what column you are working with depending on the language
  • Hard to maintain. Although this is a easy way for 2-3 languages its becomes a real drag when you have a lot of columns or a lot of languages
  • Hard to add a new language

2. Multirow approach

Another approach that i saw but i have never worked with it. It is simillar to the one above but instead of duplicating the content in columns it does it in rows.

table pages
-- id (int)
-- language_id (int)
-- title (varchar)
-- content (varchar)

So you will basically have 3 rows for the same page if you have 3 languages. The main problem i see with this approach is that it would be a bit tricky to know witch id you will use for the table relations.

Sorry but since i dont really have experience with this i cant show you sql & php examples.

Advantages

  • Ease in adding a new language

Disadvantages

  • Need to watch the table relations
  • A lot of duplicate content. You will have duplicate content for all the columns that are not translated

3. Single Translation table approach

This is an approach that becomes a little more complex then the other 2, but it is more suited for dinamic websites and which have a large number of languages or which intend to add a new language in the future and want to do it with ease.

table languages
-- id (int)
-- name (varchar)

table pages
-- id (int)
-- language_id (int)
-- title (int fk)
-- content (int fk)

table translation
-- id (int)

table translation_entry
-- translation_id (int)
-- language_id (int)
-- content (text)

In this approach you would store the id from the translation table in the title and content columns from the pages table, and then do a join with the translation_entry table based on the language id.

Advantages

  • Proper normalization
  • Ease in adding a new language

Disadvantages

  • Longer joins and query to get the content
  • All the translated content goes into one table
  • For me it just looks hard to work with and maintain

4. Coupled Translation table approach [my aproach :D ]

This is a variation of the above approach that to me seems easier to maintain and work with.
Instead of having just one translation table, you have one for each table. and you move the columns from the pages that need to be translated to the translation table.

table languages
-- id (int)
-- name (varchar)

table pages
-- id (int)

table pages_translation
-- id (int)
-- page_id (int)
-- language_id (int)
-- title (text)
-- content (text)

To get your data you just do a simple join:

SQL:
  1. SELECT * FROM `pages` JOIN `pages_translation` ON `pages`.`id` = `pages_translation`.`page_id` WHERE `map_landmarks_translation`.`language_id`='1'

Advantages

  • Proper normalization
  • Ease in adding a new language
  • Easy to query
  • Columns keep there names

Disadvantages

  • You have to create translation tables for all your tables that have columns that need to be translated

Conclusion

I am sure that there are other methods of doing a multilingual website, this are just the ones that i thought are most commonly used. My solution is the best, its just the best for me, because it works for my project and its easier for me to work with compared to other approaches.
In the end the best approach is the one that is the best for you. The one that you find the most easier to work with and maintain.

Cheers,
and good coding.

MYSQL: make a combination of 2 columns unique

August 19, 08 by Gabi Solomon

Here is a recently situation i came across, what if you have a table and you want to make a combination of 2 columns unique. The columns Individually are not unique, but combination is.

To alter 1 column to a unique key you would use :

SQL:
  1. ALTER TABLE user MODIFY COLUMN id INT NOT NULL UNIQUE;

Well for a combination of 2 or more columns to be unique you would use:

SQL:
  1. ALTER TABLE user ADD UNIQUE (id,domain);

Hope this helps you.
Cheers

SQL Query : Copy one Column to another column in MySQL

by Gabi Solomon

I wanted to do add a new column to a table and to copy the values from that a diferent to column ( from the same table ) to the new column. And even though i thought of very integrates query's at first, the solution was quite simple :D ( as always ).

SQL:
  1. ALTER TABLE `table_name` ADD `new_column` TEXT NOT NULL;
  2.  UPDATE `table_name` SET `new_column` = `old_column`;

Nice and simple.
Cheers

How to Find and Replace Text in MySQL Table using SQL

August 05, 08 by Gabi Solomon

The database engine MySQL has a string function called REPLACE that performes similar to the php function str_replace(). This really comes in handy when you want a certain text replaced in a table data, either being a name or a certain uinique number or maybe some speling mistake.

Syntax

MySQL reference :

REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.

This function is multi-byte safe.

Examples:

update `orders_table` set `traking_id` = replace(`traking_id`, ‘1111111’, ‘999999999’);
// this will replace all the 1111111 with 999999999 in the table orders_table for the column traking_id

SELECT REPLACE(’www.mysql.com’, ‘w’, ‘Ww’);
Above statement will return ‘WwWwWw.mysql.com’ as result.

It is easy to work around a web development software, all you need to know is the basis of website design. Hire a web design manager if you cannot figure that out and proceed with website hosting. Once done with that, you can move on with internet marketing.

parsing csv file with php and inserting into mysql database

May 22, 08 by Gabi Solomon

As some of my posts they usualy start by a question on a forum i write ( you can find them in the about page ). So this time some one asked how do you insert an excel file into a mysql database, and since this is a common problem for many beginers, and had to answer this before i decided to make a post about it so next time just post the link to this ( will decrease my spent time by not writing the same thing twice and bring some trafic ... a win-win situation :) ) ).

But enough chit-chat, lets get down to the dirty job. I will try to brake it down into small steps and give a few explination at each one.

1. Export the excel file into CSV (comma-separated values)

For those who don't know CSV is a file type that stores tabular data. This is a very basic and quite old format and this is why CSV files are suported on all computer platforms.
Example:

1997 Ford E350 ac, abs, moon 3000.00
1999 Chevy Venture "Extended Edition" 4900.00
1996 Jeep Grand Cherokee MUST SELL!
air, moon roof, loaded
4799.00

The above table of data may be represented in CSV format as follows:

1997,Ford,E350,ac, abs, moon,3000.00
1999,Chevy,Venture "Extended Edition",,4900.00
1996,Jeep,Grand Cherokee,MUST SELL! air, moon roof, loaded,4799.00

As you can see from the above example each row from the table is stored in a separate in the CSV file, and the values from each row are separated with a comma, from wich comma-separated values name, pretty no-brainer deduction. :) )

When you will try to export the excel file into this format you will most likely get an error like :

file.csv may contain features that are not compatible with CSV ...DO U WANT TO KEEP THE WORKSBOOK IN THIS FORMAT ?

This is quite normal since the excel file contains things like formulas, cell styles and other things that cannot be saved in the CSV format.

2. Import the CSV into MySQL database

For this step you actualy have 2 options. One the one you read in the title of this post, by using PHP, but there is an easier way to do it by using PhpMyAdmin.

Importing CSV using PhpMyAdmin
This is done quite easy. Most PMA versions have support for CSV, have not seen a server installed PMA that didnt.
You just need to login into PMA, and go to the table you want to import the data into. Once there you select the Import Tab and on the page select the CSV format, select your file and click import.
One thing you must keep in mind is that you need to have the same number of columns in the mySQL database and the CSV file.

Importing CSV using Php
This method although a bit more tricky for a beginner gives you a bit more freedom with the import procedure. For one thing it doesn't matter how many columns does the CSV file have or the order in it. You can make the script insert the data in almost any manner you want.

So here is the actual code :

PHP:
  1. $columns = "`c1` , `c2`, `c3`, `c4` ";
  2. $handle = fopen("test.csv", "r");
  3. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  4.   foreach( $data as $v ) {
  5.      $insertValues="'".addslashes(trim($v))."'";
  6.   }
  7.   $values=implode(',',$insertValues);
  8.   $sql = "INSERT INTO `tableName` ( $columns ) VALUES ( $values )";
  9.   mysql_query($sql) or die('SQL ERROR:'.mysql_error());
  10. }
  11. fclose($handle);

That is about it ... of course i assume you have a mysql connection opened ... if you dont know what i am talking about you might want to google-it.

There is another alternative, to use a php class from phpclasses.org called Quick CSV import that is going to make things a little easyer for you.

I am not going to be to descriptive about the script, because i think it is pretty basic, it basically makes a mysql insert query for each row. For those who don't understand parts of it i am happily to answer any of you questions.

Hope you have gained even a little knowledge from this article, and i am awaiting your comments.