MySQL
case sensitivity
table names
database
SQL

Are table names in MySQL case sensitive?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When designing and developing databases in MySQL, a frequent question that surfaces is whether table names are case sensitive. This aspect is crucial for database administrators and developers as it affects how queries are written and how the database is interacted with. Understanding MySQL's handling of case sensitivity can prevent potential issues related to portability, consistency, and query errors.

Understanding Case Sensitivity in MySQL

MySQL's case sensitivity behavior depends largely on the filesystem of the operating system on which it runs. This behavior differs between operating systems and storage engines:

  • Linux and Unix: These operating systems have case-sensitive filesystems by default. Consequently, table names in MySQL are case sensitive.
  • Windows and macOS (non-APFS): On these systems, filesystems are typically not case sensitive, and therefore MySQL table names are not case sensitive.

The above behavior is contingent upon the default storage engine, which is InnoDB in recent MySQL versions.

`lower_case_table_names` System Variable

MySQL provides a system variable named `lower_case_table_names` that controls the storage and comparison of table names. The value of this variable determines how MySQL treats case sensitivity for table and database names:

  • 0: Table and database names are stored and compared as given. This is case sensitive and is typical for Unix-based systems.
  • 1: Table and database names are stored in lowercase, and comparisons are not case sensitive. Common for Windows systems.
  • 2: Table and database names are stored as provided, but comparisons are not case sensitive. This behavior is specific to case-insensitive Unique and default collation for macOS.

Let’s see an example and how different systems handle case sensitivity:

  • InnoDB: Adheres to the `lower_case_table_names` system setting.
  • MyISAM: Also respects the `lower_case_table_names` configuration.
  • Portability: Moving a database from a non-case-sensitive environment (e.g., Windows) to a case-sensitive environment (e.g., Linux) can lead to issues if there is inconsistent casing in the queries and table definitions.
  • Convention: Establishing a naming convention (e.g., lowercase for all table names) can prevent numerous case sensitivity-related issues.
  • Configuration Adherence: Be mindful of the `lower_case_table_names` setting if the application is expected to run cross-platform or if there is a possibility of changing the server's operating system over time.

Course illustration
Course illustration

All Rights Reserved.