‘ Database ’ category archive

Using UNION in MySQL

June 04, 08 by the programmer

If you want to combine results from two select queries in MySQL you can use the “UNION” operator.

In the following examle the results from the users query and the customers query are combined and the final result will be a table with all the results.

(

SELECT

user_id, user_name

FROM

users

)

UNION

(

SELECT

user_id, user_name

FROM

customers

)

Note that the same column names must be in the select clause in both of the queries

Maximum length for MySQL Identifiers, database names, table names, columns, index…

June 01, 08 by the programmer

Those are the maximum lenght for each MySQL identifier.

You can write names longer than the values in the table below

Identifier Maximum Length (characters)
Database 64
Table 64
Column 64
Index 64
Stored Function or Procedure 64
Trigger 64
View 64
Alias 255

HSQLDB is case sensitive!

May 21, 08 by the programmer

Hmm,

This is not good, i just found out that the HSQLDB database is case sensitive by default

This means that if you have the following strings stored in two rows in a database table

“minanov com is a blog for programmers” and

“MINANOV.COM IS A BLOG FOR PROGRAMMERS”

and you run the following SQL:

SELECT

stored_text

FROM

my_table

WHERE

stored_text = ‘minanov com is a blog for programmers’

will return only one record, the lower case one “minanov com is a blog for programmers“.

We can solve this in couple of ways:

Solution 1:

This solution can only be applied to newly created databases, because the option must be set before a database is created. If you already have a database it won`t affect the existing data. You have to execute the following command before creating the tables.

SET IGNORECASE

SET IGNORECASE { TRUE | FALSE };

Disables (ignorecase = true) or enables (ignorecase = false) the case sensitivity of text comparison and indexing for new tables. By default, character columns in new databases are case sensitive. The sensitivity must be switched before creating tables. Existing tables and their data are not affected. When switched on, the data type VARCHAR is set to VARCHAR_IGNORECASE in new tables. Alternatively, you can specify the VARCHAR_IGNORECASE type for the definition of individual columns. So it is possible to have some columns case sensitive and some not, even in the same table.

Only an administrator may do this.

Solution 2:

HSQLDB can be extended with a JAVA function.

First you create a public function in some package in your application

com.minanov.hsqldb_utils

public static boolean containsMatch(String target, String search) {

return target.toLowerCase().contains(search.toLowerCase());

}

After we create the function we can call it in our SQL statemants

SELECT

column_for_storing_text

FROM

my_table

WHERE

com.minanov.hsqldb_utils.containsMatch(’column_for_storing_text‘, “text_that_we_search”)


Solution 3:

This solution is very simple. All you have to do is before putting the search string in the SQL as a parameter to eighter make it all lower case or all upper case.

After you make the search string lower or upper case you need to lower or upper the column that you search in.

We will make convert both strings to all uppercase letters.

This is how will that look like in an SQL query:

String text_that_we_search = “minanov com is a blog for programmers“;

String text_that_we_search_to_upper_case = text_that_we_search.toUpperCase();


String sql =”

SELECT

stored_text

FROM

my_table

WHERE

UCASE(stored_text) = “+text_that_we_search_to_upper_case;

Since both strings are in upper case we will have no problem and the above sql will return all the resullts.

That’ll folks :)

Easy PHP MYSQL database comparation tool

May 20, 08 by the programmer

Every programmer comes to a situation when he has to compare two databases.

This can be the development and the production database in order to synchronize them or it can be any other two databases that needs to be structurally compared.

I found a very nice and simple free tool written in PHP that does the job very well.

The tool is called mysqldiff and it is a php application. Once you install it on your server you can compare any two MySQL databases.

Check it out.

You can download the tool from here

http://www.mysqldiff.org/downloads.php