views:

55

answers:

3

Is it possible to store "Filename" and "filename" in a mysql-database?

A: 

MySQL will always preserve the case when you store strings.

When you search for a string it normally does a case-insensitive search. This depends on the collation, but the default is case-insensitive.

To do a case-sensitive search you can change the collation:

SELECT x FROM table WHERE y LIKE '%Filename%' COLLATE latin1_general_cs

See case sensitivity for more info.

Greg
Yes, but is it possible to save "File" and "file"? and not get that as a duplicated entry?
Johan
A: 

Is it possible to store "Filename" and "filename" in a mysql-database?

If the column you're inserting to is UNIQUE, make sure that its collation is case sensitive:

CREATE TABLE t_ci (test VARCHAR(50)  PRIMARY KEY COLLATE latin1_swedish_ci);

INSERT
INTO    t_ci
VALUES
('Filename'),
('filename');

-- Fails

CREATE TABLE t_cs (test VARCHAR(50)  PRIMARY KEY COLLATE utf8_bin);

INSERT
INTO    t_cs
VALUES
('Filename'),
('filename');

- Succeeds
Quassnoi
+3  A: 

I assume you mean, store ‘Filename’ and ‘filename’ in a column that has a UNIQUE constraint (such as a PRIMARY KEY)?

If so, then yes, but you must tell MySQL to use a case-sensitive collation when CREATE​ing or ALTER​ing the table. This can be done with VARCHAR(...) COLLATE some_collation_cs (see) or simply VARCHAR(...) BINARY.

bobince
Thank you! Setting the column to `varbinary` did it.
Johan