Use Trim on all records in a mysql table - mysql

Is this possible in phpMyAdmin, to execute that query on all records within a table (to get rid of any whitespace)

You may have to list the field name but you'd only need to do so once per field.
UPDATE 'table_name' SET 'field_name' = TRIM('field_name')
(I would advise testing this before running it on your live data)

Try to cheat:
update venues set postcode=TRIM(postcode)+''

Related

DELETE query results in 'Query Interrupted' MySQL Workbench?

I can successfully delete records manually by click-selecting & deleting row(s) but executing delete queries result in 'Query Interrupted'.
My deletion queries are in the form:
DELETE FROM table where column = value;
The select statement uses the same values:
SELECT * FROM table WHERE column = value;
and returns desired results.
What could be causing the delete statement to fail? Are there limits on the amount of records you can delete at once in workbench?
If you wish to delete the entire contents of a table you can use Truncate.
TRUNCATE [TABLE] tbl_name
Please see the docs: https://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
Using the DELETE function is usually used for deleting single rows.
According to the documentation, in the Preferences >> SQL Editor >> Other, the Safe Updates setting is on by default.
Safe Updates (rejects UPDATEs and DELETEs with no restrictions)
Enabled by default. Prevents UPDATE and DELETE queries that lack a corresponding key in a WHERE clause, or lack a LIMIT clause, from executing. This option requires a MySQL server reconnection.
When selected, this preference makes it possible to catch UPDATE and DELETE statements with keys that are not used properly and that can probably accidentally change or delete a large number of rows.
I think what this says is that if the setting is on, then the column you are filtering by in the DELETE or UPDATE statement must be the primary key, it cannot be just any column.
If you change the setting to off, then you might need to restart MySQL Workbench for the change to take effect (at least under Linux).
There is a default thousand-row limit in MySQL-Workbench. The SELECT query will return results but DELETE will fail if the number of records to be deleted exceeds one thousand. One option is to limit the results in the query itself or you can adjust the settings as stated in the documentation.

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)

Is there a way to know how many session that open a particular table

I'm trying to generate unique id using php for a table ex: 2013-10-24-01 (yyyy-mm-dd-last_index_plus_1).
The problem is, if there are two session simultaneous open the php
which is access a same table, the generate unique id will be same on them.
I come up with idea that determined the number of session accessing that table
and then plus the last_index by that number,
but i don't know how to accomplished it with mysql.
Is it possible or not?
Thanks
As noted in the comment I am not sure why you want PHP to actually do this, and why the auto_increment is not good enough, but if you need a unique ID you can do the following:
Make sure to add a unique key on the column you are inserting into - if something goes wrong your database will make sure you won't get the two identical keys. In theory you could try until the query succeeds from PHP
You can use the "select for update" (http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html) when selecting the last known sequence to make sure that another process will not try to read before you have inserted it.
The above is possible to do in mysql using a trigger, as you did not provide any structure, the below is just an example of how it could look. It uses a sequence-table for selecting the last id, and the mysql date_format function to format the inserted date.
delimiter |
create trigger `trigger_name`
before insert on `table_name`
for each row begin
set new.last_id = concat(date_format(now(), '%y-%m-%d'), '-', (select max(id) from `sequence_table` for update));
end|
delimiter ;

Can't Update and Delete data - SQL

I've just create table and add data to rows. Now I need to delete some rows, but I can't do it using mysql-wokbench. Delete rows is disabled. Also I can't edit data, when I choose "Edita table Data". Can you help me? Why is it so?
I CAN NOT DELETE BY THE HAND, USING THAT WORKBENCH. BUT I CAN'T FROM SQL QUERY.
In order to be able to edit the result set from a query like SELECT * from table the table must have a primary key defined, as this is what WB uses to find a certain record.
SHOW GRANTS FOR CURRENT_USER;
See if you have the rights for that.

SHOW TABLES and MYSQL_NUM_ROWS

I have just noticed that where mysql_num_rows should return the number of rows returned for either SELECT or SHOW commands, returns 0 for SHOW TABLE command specifically.
Instead it shows the affected rows count instead of the num rows.
Can anyone please tell me if this is a bug or if am I missing anything here?
SHOW TABLE command is used to Show you table name in your database . On the other hand , mysql_num_rows is used to count how many result got from your query. This query is depend on your requirement basis ...
As stated on the PHP documentation page:
Retrieves the number of rows from a result set. This command is only
valid for statements like SELECT or SHOW that return an actual result
set.
My guess is that SHOW TABLES is not a technical query that would produce the type of result set that mysql_num_rows enumerates.
These "helper" functions (such as SHOW, EXPLAIN, DESCRIBE etc.) won't let you issue their results like you would in a regular table.
But if you're looking for how you can do this, for SHOW TABLES you can do
SELECT `table_name` FROM `information_schema`.`tables`
WHERE `table_schema`=DATABASE()
-- DATABASE() selects current database name
-- you can use the name of any database as a string instead
So basically you can use the information_schema database to get that information.
It was a bug in mysql, fixed it with the update.