How to automatically convert a MySQL column to lowercase - mysql

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)

Related

How to make a column in SQL case insensitive?

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

How to increment the numbers in mysql with a character prefix which is fixed

I want to create a table where I will get a column like this
c00001
c00002
c00003
c00004
and so on.
I use xampp mysql but any SQL server command would be helpful. This means I want to increment this id followed by a varchar.
You can't accomplish this in MySQL using triggers or generated columns because neither of those can access an auto-increment column value.
Your best bet may we to use a view to build the strings you want based on the auto-increment values.
Try something like this:
create or replace view v_your_table as
select *,
concat('c',lpad(id,5,'0')) as str_id
from your_table;

Update a field in SQL

I have several tables that have a common field (column) called LastName in a MySQL database. Several of the rows in these tables are in mixed case so they don't get selected properly when doing a SELECT.
How can I convert those columns to all UPPER CASE? I can easily handle any new entries to convert them to upper case, but the existing records I'm not so sure about.
would do the job
update table set LastName=UPPER(LastName);
NOTE - if you are running from MySQL workbench you may have to disable safety mode or add a where clause (eg WHERE id>0) otherwise it wont run.
this would work:
UPDATE table_name SET `column_name` = UPPER( `column_name` )
You can use the string function UPPER() to make the column value to upper
update Your_table set LastName=UPPER(LastName)

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.

In MySQL how can I tell what character set a particular table is using?

I have a large mysql table that I think might be using the wrong character set. If so I'll need to change it using
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8
But since this is a very large table, I'd rather not run this command unless I have to. So my question is, how can I ask mysql what the character set is on a particular table?
I can call status in mysql to see the database's character set, but that doesn't necessarily mean all the tables have the same character set, right?
Try:show create table my_table;
if your table uses wrong character set, this query will break the data.
so, it must be converted another way.
if you don't want to convert, the only proper way to get correct data (i.e. tell mysql not to recode it) is SET NAMES <table charset> query. but sorting and filtering wouldn't work
to see current charset you can use SHOW CREATE TABLE query