DvirSade
DvirSade

Reputation: 13

MySQL table called DB.Table instead Table?

I have a strange problem in MySQL. I create a new table, then I can not make it any change. The software looks for a table named "TED.Students" (TED is the database name) instead of "student". Here's the code:

mysql> CREATE DATABASE TED;
Query OK, 1 row affected (0.00 sec)
mysql> USE TED;
Database changed
mysql> CREATE TABLE students
    -> (
    -> StudentID int,
    -> FirstName varchar(255),
    -> LastName varchar(255)
    -> );
Query OK, 0 rows affected (0.57 sec)
mysql> INSERT INTO Students
    -> VALUES (1, Dvir, Sade);
ERROR 1146 (42S02): Table 'TED.Students' doesn't exist
mysql> SHOW TABLES;
+---------------+
| Tables_in_TED |
+---------------+
| students      |
+---------------+
1 row in set (0.01 sec)
mysql> CREATE TABLE students ( StudentID int, FirstName varchar(255 ) , LastName varchar(255) );
ERROR 1050 (42S01): Table 'students' already exists

I searched online but could not find an explanation of the problem. Probably I'm missing something really simple, but I do not know what is. Until now I did not have this problem. The only thing that has changed is I moved from Windows to Linux (Ubuntu), but why should be a difference?

Upvotes: 0

Views: 48

Answers (2)

VMai
VMai

Reputation: 10336

You're not aware of case sensitivity:

INSERT INTO students
    VALUES (1, Dvir, Sade);

should work. Please regard the difference between Students and students

See the manual to Identifier Case Sensitivity

Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database, table, and trigger names. (Emphasis by me) This means such names are not case sensitive in Windows, but are case sensitive in most varieties of Unix. One notable exception is Mac OS X, which is Unix-based but uses a default file system type (HFS+) that is not case sensitive. However, Mac OS X also supports UFS volumes, which are case sensitive just as on any Unix.

By default, table aliases are case sensitive on Unix, but not so on Windows or Mac OS X. The following statement would not work on Unix, because it refers to the alias both as a and as A:

mysql> SELECT col_name FROM tbl_name AS a -> WHERE a.col_name = 1 OR A.col_name = 2;

However, this same statement is permitted on Windows. To avoid problems caused by such differences, it is best to adopt a consistent convention, such as always creating and referring to databases and tables using lowercase names. This convention is recommended for maximum portability and ease of use.

Upvotes: 1

ebo
ebo

Reputation: 2747

The switch between Windows and Linux causes identifiers to be case-sensitive. As per the documentation:

In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix.

Upvotes: 0

Related Questions