Tuesday, July 31, 2012

What is the difference between CHAR and VARCHAR data types?

CHAR and VARCHAR are the basic built-in types that the SQL standart supports. This short article is about the difference between them.

Difference between CHAR and VARCHAR
CHAR(n)VARCHAR(n)
A fixed-length character string with user-specified length n. A variable-length character string with user-specified maximum length n.

VARCHAR is a short version of "character varying".

The CHAR data type stores fixed length strings. And we should be mindful of this.

For example, if we store a string "test" in the field of type CHAR(10), 6 spaces are appended to the string to make it 10 characters long. In contrast, if we were store the string in the field of type VARCHAR(10), no spaces would be added.

When comparing two values of type CHAR, if they are of different lengths extra spaces are automatically added to the shorter one to make them the same size.

When comparing a CHAR type with a VARCHAR type, extra spaces may not be added (it depends on the database system).

As result, if even the same value "test" is stored in the fields of different types (CHAR and VARCHAR), a comparison of these fields may return false.

Authors of the book "Database System Concepts" Abraham Silberschatz, Henry F. Korth, S. Sudarshan recommend to use the VARCHAR type instead of the CHAR type to avoid these problems.

I used this great book to write the article. The authors of the book are really great men. They did a lot of work writing 1349 pages about database system concepts. I really admire them for doing that.. and enjoy reading the book. Many things become clear.

I'm sure that I'll be better programmer after reading it.

Wednesday, February 29, 2012

How to enable MySQL query log

I have found it useful to enable the general query log on my local computer to explore mysql queries.
If you want to follow the advice, "Be aware that this log type is a performance killer".

Steps:

  1. Open the /etc/mysql/my.cnf file.
  2. Activate general_log_file and general_log options in the Logging and Replication section:
general_log_file        = /var/log/mysql/mysql.log
general_log             = 1
The logs are available in the /var/log/mysql/mysql.log file.

Tuesday, January 24, 2012

Method preg_match() for Cyrillic characters in PHP

Sometimes I encounter the problem with preg_match() method, because it doesn't treat Cyrillic characters the proper way. The problem also corresponds to other non-latin characters.
There is a solution for the search in the string that contains non-latin characters.
<?php 
  preg_match("/^[a-zA-Z\p{Cyrillic}]+$/u", "AбВгд");
?>
Do not forget about the Pattern Modifier - u
u (PCRE_UTF8)

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8.

This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.

Friday, September 23, 2011

Comparison of float values in PHP

That surprise is not pleasant at all. Especially if you work with money.
<?php
  $a = 0.1 + 0.7;
  $b = 0.8;
  var_dump($a, $b, $a == $b); 

// float(0.8) float(0.8) bool(false) 
?>
To avoid this type of problems let's follow the advice of Richard Johnson, that was published in the April 2013 issue of Web & PHP magazine:
"Be sure you are using ints and working in the smallest non-divisible monetary value (cents or pence). Alternatively make use of the BC or GMP maths functions which can also be useful if you need to do precise decimal operations."

Sunday, September 11, 2011

What php.ini file is used by console

If you are not sure about what php.ini file is used by console, you can find out that by some methods. There is one of them. You can do it by parsing information about php, that will be displayed by php -i.
php -i | grep php\.ini
or
find / -name php.ini