I have a database, a table that is having empty rows '' nothing in them.
I need to fill all those empty cells with certain singe value.
How can I do it?
Thanks in advance.
You can't batch edit all columns one column at each time
UPDATE table_name SET COLUMN_name ='value' WHERE column_name=''
If you want to update all columns at once, write a php code for that and in a loop perform sql query!
Hope it helps!
You have several options, based on your definition of "empty rows".
if empty means null:
update tableName
set columnName = 'value'
where
columnName is null;
if empty means not null, instead an empty string:
update tableName
set columnName = 'value'
where
columnName = '';
UPDATE table_name
SET column1=value
WHERE length(column1)=0;
This should do you job.
Related
I need replace string1 to string2 in column big table. My table weight is1.7GB. For update I will use:
UPDATE table
SET column = REPLACE( column, 'search', 'replace' );
So, I have several questions:
Will table lock during procedure? Should I hide the public part of my site?
How long will this procedure be executing?
How better to run this? Maybe 'screen'? if my ssh connection will closed.
MySQL 5.5.53
You shouldn't run replace on all the rows of the table.
Instead add a where condition as #reds said in the comments.
This will make your update much more efficient and is the correct way to do it.
UPDATE table SET column = 'replace' where column = 'search';
In my case:
UPDATE table SET column=REPLACE(column, 'search', 'replace') WHERE column LIKE "%search%";
I need to change the default value of sql table fields from 'none' to 'null' where the datatypes are char varchar longtext etc. And I want to keep the default value if its not 'none'. And there are more than 500 tables I need to alter. Is there any way to do so? And should I use PL? Is there any other methods to do so?Thanks in advance.
I assume you want to update the none to NULL and the column is of type varchar
update table_name set column = null where column = 'none';
you need to update schema of table as well, need to set the default value as NULL using alteration.
The code is:
ALTER TABLE 'tablename' ALTER COLUMN 'columnname' SET DEFAULT 'none'
First I selected all tables from database using this query:
$result = mysql_query("SHOW TABLES FROM 'database'";
And selected all columns using a loop:
mysql_fetch_field
Then I run the code: And it worked .Thank you every one who tried to help me.
I need to create a table if it doesnt exist, and add missing columns in the proper order if the table already exists.
I know how to do it with lots of queries, and if statements and so on, but what I am asking here is what the best solution would be.. Maybe there is a special query to do this, or a smart way.
I would do it this way:
create table if not exists (all columns as they should be)
compare all the columns (if some are missing they will be added, else not)
Is this the best way or are there better ways to do it?
ADDITIONAL INFO
the colums need to be added at the right position. I have a list of strings representing all the columns in the proper order. using vb.net I am iterating through these strings.
Check out this for instance. It's basically about querying the data dictionary and adding columns only if they do not exist:
IF NOT EXISTS(SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tablename'
AND table_schema = 'db_name'
AND column_name = 'columnname') THEN
ALTER TABLE `TableName` ADD `ColumnName` int(1) NOT NULL default '0';
END IF;
Putting it in a procedure makes it quite handy.
p.s. note about column positions: from the docs
To add a column at a specific position within a table row, use FIRST
or AFTER col_name. The default is to add the column last. You can also
use FIRST and AFTER in CHANGE or MODIFY operations to reorder columns
within a table.
You can use following codes for that:
if not exists(select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'table_name' and COLUMN_NAME = 'column_name')
BEGIN
ALTER TABLE table_name ADD
ToUser uniqueidentifier NULL
END
How can I write a SQL query to replace all occurrences of space in a table with underscore and set all characters to lowercase?
To update a single column in a single table, you can use a combination of LOWER() and REPLACE():
UPDATE table_name SET column_name=LOWER(REPLACE(column_name, ' ', '_'))
To "duplicate" the existing column, and perform the updates on the duplicate (per your question in a comment), you can use MySQL's ALTER command before the UPDATE query:
ALTER TABLE table_name ADD duplicate_column_name VARCHAR(255) AFTER column_name;
UPDATE table_name SET duplicate_column_name = LOWER(REPLACE(column_name, ' ', '_'));
Just be sure to update the data-type in the ALTER command to reflect your actual data-type.
When using the UPDATE statement in SQL, always remember to include a WHERE clause -- so says MYSQL Workbench! :D
My Answer though:
REPLACE(string1, find_in_string, replacementValue);
hIs there any way to update all the columns of a mysql table for a particular record in one go to a particular value.
For e.g. I have a table that has around 70 columns , and they are by default set to 0 at the time of creating the table,when I add a new record via PHPmyadmin by just filling in one or two values and submitting it all the other fields are set to 0 , but I want to set all the fields to 1
many times ,so I need to set all the columns to 1 individually via PHPmyadmin
To speed-en up the process and
I tried
UPDATE tablename SET * = '1' WHERE id = '2' , but it does not work.
If anyone can provide a solution on similar lines , it would be great.
EDIT:
Is there a way without specifying all the 70 columns in the SQL statement? that what I am looking for. I do know how to update normally specifying columns in the SQL statement. Thank you.
If you are looking for a way to update all 70 columns to a single value with a short, simple statement, then I recommend that you write a stored procedure to do the update. That way you only need to write out the full update syntax once, and can re-use it over and over by calling the stored procedure.
CREATE PROCEDURE update_all_columns (p_new_value SMALLINT, p_id INT) ...
CALL update_all_columns(1,2);
Another trick is to use the information_schema.columns table to generate the update statement, making it less tedious to code the stored procedure.
Something like this:
SELECT concat('UPDATE ',
table_name,
' SET ',
group_concat(column_name separator ' = p_new_value, '),
' = p_new_value',
' WHERE id = p_id;') as sql_stmt
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'tablename'
AND column_name != 'id'
You have to name each column in an update statement.