How to make a column in SQL case insensitive? - mysql

The problem is the page linking process in wikimedia whereby I create a link [[like this]] or [[Like This]] creating two different links. A third and separate link would be [[LIKE this]] ... I was hoping to make the database case insensitive so they would all link to the same page. Here are some proposed solutions: I was attempting solution #6. https://meta.wikimedia.org/wiki/Case_sensitivity_of_page_names
http://archive.is/Dm5YI#selection-376.1-393.32
Case insensitive means:
https://iglooo.000webhostapp.com/index.php?title=Computer_science
would return the same results as:
https://iglooo.000webhostapp.com/index.php?title=Computer_Science
Instead of a separate page like it currently does.
‘collate latin1_bin’ actually forces the mySQL column to be case sensitive.
Here is what I did, the key drop is required for the alter table:
alter table page drop key name_title;
alter table page modify column page_title varchar(255) NOT NULL default '';
There is table named 'page' that needs to become case insensitive. The above attempt at this has failed. Please help.
I am using the latest mediawiki distrobution.

If you don't clean the data going into the system, there is no way to store it in a case insensitive way. That being said, you can query on the data using a function.
select
foo.bar
from
foo
where
upper(foo.bar) = upper(MY_PARAMETER)
Note, the right hand upper() cluase would be better handled in your application logic, but it's perfectly possible to do it here.

Avoiding the alter table and the change of format for you column you can simply use a collate
in you query
col_name = 'Computer_science' COLLATE latin1_general_ci
in this way the select column is used as case insesitive (by passing) you bin collation in table definition

Related

Conditionally add a column to a mysql table

I have a project where I want to be able to manage several instances of the same database on several people's localhosts. I want each developer to be able to reset their DB back to the canonical origin no matter what state they get their DB into. To this end I maintain a standard database file. It holds the schema using CREATE TABLE table_name IF NOT EXISTS {
However, I want to be able to add to the starting table structures as needed as this project moves along. To do this, I would love to be able to do something like the following ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name but that does not seem to exist. I did notice a stored procedure floating around the internet that solves this, but I wanted to know if something simpler is able to achieve the goal I have in mind. Thank you for the time and help.
It won't be possible to do it with plain SQL. Stored procedure should work the best: read information_schema and check if the column is present. If not - execute the alter statement.
One option is to execute your ALTER statement without checking anything:
ALTER TABLE table_name ADD column_name VARCHAR(40);
(change VARCHAR(40) to whatever you need)
If the column didn't already exist, then the statement creates it.
If the column already existed, the statement does nothing and returns an error. Just ignore the error and continue.

Forcing a field in a MySQL table to be lower-case

I have the field below in a MySQL table. How can I force it to be in lower-case letters only?
`username` varchar(30) NOT NULL,
In the database use the MySQL LOWER() function around the inserts and/or the retrieval. You could also consider just doing it on the application side (meaning you still store the original...
Example
INSERT INTO mytable (username) VALUES(LOWER('TestUser'))
SELECT LOWER(username) FROM mytable
You could use an 'before insert' trigger on the field you want to lowercased.
More infos here : MySQL Trigger
(possible duplicate from here : mySQL phpmyadmin - lowercase data by default)
Rather then implementing it at application level you could write a database trigger that would change the content of the insert/update to lowercase as and when the insert/update happens, independent of the application.

Postgresql: how to change the table definition

The problem: I'm searching for an alternative solution to change column definition in postgresql.
I would like to do something that is similar to the mysql solution:
ALTER TABLE table_name
CHANGE [COLUMN] old_col_name new_col_name column_definition
Is there a way to use CHANGE COLUMN similar to what I've described previously? Is there any way to easily change the column definition in postgres?
I'd like to use a similar method to my example because I'm building the alter query from code.
There is no direct equivalent to MySQL's CHANGE COLUMN clause in PostgreSQL. You'll need to specify the column definitions parts one by one. See documentation for ALTER TABLE. Fortunately you don't need to detect differences between old definition and new in typical cases, if the change is redundant PostgreSQL will just ignore it. e.g ALTER COLUMN x DROP NOT NULL will still work when the column is already nullable.

Must I include all column attributes in an SQL statement when I want to alter only one?

I have a MySQL database that, I would like to annotate by adding comments to all of the columns,
From what I read on the MySQL documentation, it is necessary to include the data type and all attributes when changing any single one of these.
For example, I must type
ALTER TABLE `dbname`.`tablename`
CHANGE COLUMN `columnname` `columnname`
INT(11) NULL DEFAULT NULL COMMENT 'this is my comment`;
It would be much faster for me to avoid having to restate the column info for each change, for example, by only having to submit a command such as:
ALTER TABLE `dbname`.`tablename`
CHANGE COLUMN `columnname`
COMMENT 'this is my comment`;
Are there any options for adding comments that do not require me to restate the table structure?
The documentation is pretty clear that CHANGE COLUMN requires the full column definition:
CHANGE [COLUMN] old_col_name new_col_name column_definition
Anything optional would be in brackets.
Your best bet is probably to write a little one-off script to produce your ALTER TABLE commands based on the table's current schema. You should be able to extract the column definitions from whatever data access layer you're using.

How to automatically convert a MySQL column to lowercase

Is there a property that I can add to a column so that it converts its value to lowercase? Instead of doing it for every single value through PHP?
You could probably do it through a trigger that fires on insert or update. Myself, I'd rather just create a view that has a lower-case version of the column in question. The SQL for the view might look like
SELECT ID, LOWER(MY_COLUMN) AS MY_COLUMN_LOWERCASE
FROM MY_TABLE;
Yes, but don't do it.
If you want exclusively lowercase characters in a column, convert them when you insert (or update) them.
If you need a column to be case insensitive in comparisons, use a case insensitive collation (which are used by default in e.g. utf8 columns)
Maybe you mean this?
UPDATE table SET column = LOWER(column)