Wednesday, July 15, 2009

Table Name Case Sensitivity

Case sensitivity of table names in MySQL is determined by the host operating system. There are three settings in MySQL that control table name case sensitivity:

  1. Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. Name comparisons are case sensitive.
  2. Table names are stored in lowercase on disk and name comparisons are not case sensitive. MySQL converts all table names to lowercase on storage and lookup. This behaviour also applies to database names and table aliases.
  3. Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case sensitive. This works only on file systems that are not case sensitive.

Unix/Linux defaults to 0, Windows to 1, and OS X to 2. Although I primarily use MySQL on Linux, I prefer to set it to 1. Why? Many reasons, but the main one being that I use an Entity Relationship Diagram (ERD) program that exports to MySQL. And in ERDs you write the entity name, which becomes the table name, in uppercase. But when writing queries the convention is to use lowercase. Another reason is for portability. If migrating your database from one operating system to another, setting 1 is compatible with them all.

To change the default you need to edit your my.cnf file:

[mysqld]
lower_case_table_names = 1

Note that you need to put this in the [mysqld] section, don't just append it to the end of the file. Don't forget to restart MySQL.

Fun with Find

Find is very good at, well, finding files. But it is vastly more useful when combined with other commands. Suppose you wanted to delete all Gif files you had across multiple directories.

cd /some/path
find . -name '*.gif' -exec rm -f {} \;

Or combine find with grep to search inside all or specific files.

find . -exec grep -Hn monkey {} \;
find . -name '*.txt' -exec grep -Hn monkey {} \;

The former searches all files recursively (from the current working directory) and the latter only searches files ending in .txt.

Tuesday, July 14, 2009

Force an HTTP URL to HTTPS via Apache's mod_rewrite

If you want all visitors to a specific URL who arrive via HTTP to automatically be redirected to HTTPS, you can do so with the help of mod_rewrite. With mod_rewrite installed (most distributions install it by default with Apache, or at least offer it as a package) and enabled, a simple addition to your vhost configuration is all that is required. The following illustrates how a URL ending in /mail would be rewritten:


  
    
      RewriteEngine on
      RewriteCond %{HTTPS} !^on$ [NC]
      RewriteRule . https://mydomain.net/mail[L]
   
  

Note that mod_ssl is also required buy you'll rarely find a binary install of Apache that doesn't include it.