I've copied and pasted an SQL statement which simply adds a column into the table:
ALTER TABLE `users` ADD COLUMN `favourites​` TEXT;
However, where I have copied and pasted, the favourites name has some how managed to pick up a hidden character.
I have left the hidden character in the example above for you to see/or not see as it may be!
It's favourites?, with what appears to be a question mark.
THE PROBLEM: I need to delete this column and re-add it manually so that the hidden character is not present. The problem is that any SQL statement I do, it doesn't recognise the the column name favourites because of the hidden character and I don't know how to target it.
Has anyone got any idea how to get around this?
Do the same use show
SHOW COLUMNS FROM your_table;
for obtain the column name and then copy the column you need in your delete command
alter table your_table drop column your_column_copied
and the add the column with the right name
alter table your_table add column your_column
otherwise, if is impossible get the column_name, you can create a temp table without the wrong column with create/select command
create table (col1, col2, col3)
select col1,col2, col3
from you_table
then drop the original table and rename the temporary table and last add your column with right name
You could use dynamic query:
DECLARE #sql nvarchar(800)
SELECT #sql = 'ALTER TABLE users DROP COLUMN ' + COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'users' and COLUMN_NAME LIKE '%favour%'
EXEC sp_executesql #sql
You can obtain the column name by querying INFORMATION_SCHEMA and prepare statement with the obtained column name. Something like this:
DECLARE #StrangeColumnName NVARCHAR(16) := ''
SELECT #StrangeColumnName := COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'users' AND COLUMN_NAME LIKE 'favourites%'
DECLARE #SqlText NVARCHAR(32) := 'ALTER TABLE status DROP COLUMN ?'
EXECUTE #SqlText USING #StrangeColumnName
Maybe open the information schema of the table and copy the column name from there? i don't know which Database are you using. Please update for more information.
If you have access to phpMyAdmin or, if you can create a small script to run this script:
SELECT COLUMN_NAME, FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'your_tbl_name'
OR
SHOW COLUMNS
FROM 'your_tbl_name
and copy the column name from the page.
next, you can drop that column using
alter table your_tbl_name drop column column_name;
and you already know how to add a column in mysql so, i guess that should solve your problem.
I hope you do know that you can not comment if your reputation is below 50 and if you didn't provide enough information, those who might actually have an answer for you, but have below 50 rep, will have to post it in answers. or would you like to eliminate those who are 50 rep as candidates for helping you?
In order to delete a column you can use:
alter table <tblname> drop column <colname>
and then after deleting the column you can add the column by writing below code:
ALTER TABLE users ADD COLUMN favourites​ TEXT;
Some possibilities:
Using phpmyadmin
Using a tool to talk directly to the database like navicat etc
Related
In MySQL, how to change all columns names of all tables to remove the string "_euro" from columns names?
I just could find a way to search tables having some columns containing "_euro" in their names:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
AND TABLE_SCHEMA='my_database'
For example, for the column named price_total_euro I want to rename it as price_total
Create a script with the following SQL:
SELECT CONCAT("ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," RENAME COLUMN ",COLUMN_NAME," TO ",REPLACE(COLUMN_NAME,"_euro",""),"; ")
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
output will be, multiple lines which look like this:
ALTER TABLE test.t1_euro RENAME COLUMN t1_euro TO t1;
Review the script, and execute it on your database (after making a backup....)
put your select into a stored procedure and make a cursor and loop on all and do the alter operation
you can find example for a procedure here : https://stackoverflow.com/questions/15786240/mysql-create-stored-procedure-syntax-with-delimiter#:~:text=Getting%20started%20with%20stored%20procedure%20syntax%20in%20MySQL%3A,Why%20didn%27t%20this%20work%3F%20...%20More%20items...%20
if your using python with MySQL Connector you could place the column headers into a list and then run it through a for loop?
for name in table_names:
if name.endswith("_euro"):
new_name = name.replace("_euro", "")
I have a MySQl database with more than 200 tables. I want to do following on ALL tables in this database.
Update all table name by adding a constant to the name
Add column (Alter table) to each table
Update each table to set newly added column
Can someone please suggest an efficient way of doing this
Thanks
bhim
You need to write a couple of SQL statements that will generate the rename / add column SQL statements.
Then you can run the SQL Statements.
You haven't provided table names, or schemas, etc. so I can give guidance but not exact results.
So assuming your "adding a constant to the name" is prefixing "const_" to it, you could do something like:
SELECT 'RENAME TABLE ''' || table_name || ''' TO ''const_' || table_name || ''' FROM information_schema.tables WHERE table_catalog = 'YourCatalog' and table_schema = 'YourSchema';
This would give you the rename table command as the output, which you could pick up and put in a text editor to tidy up.
You'll need to execute a few queries against INFORMATION_SCHEMA.tables to figure out the right filter to get the right criteria for the tables list.
And you can do similar for the Add column statement.
Some useful references:
Information schema: https://dev.mysql.com/doc/refman/5.7/en/tables-table.html
Rename a table: https://blog.marceloaltmann.com/en-how-to-rename-table-in-mysql-pt-como-renomear-tabelas-no-mysql/
Add a column: http://www.mysqltutorial.org/mysql-add-column/
I'm not seeing much regarding this after some searching
Do I need to drop the view and recreate it or is there a way to edit a column name?
I tried ALTER VIEW tableName oldColumnName newColumnName
But got a syntax error
You can use the ALTER keyword instead of CREATE but the syntax is the same.
This means ALTER VIEW does the same as CREATE VIEW but drops the existing view first. You must specify the complete new query that defines the view.
You can simply use :
CREATE VIEW viewname AS
SELECT colname "newcolname", colname "newcolname" FROM table-name;
its like give alias name to col of view..
for me following code worked fine to rename a column
ALTER
ALGORITHM=MERGE
VIEW viewname AS
SELECT emp_id as employee_id,first_name
FROM employee;
PS: it is for folks who tried rename column col1 to col2 and other ways.
You can use this to move column_name after another_column_name
ALTER TABLE table_name
MODIFY column_name datatype AFTER another_column_name;
or
ALTER TABLE table_name
MODIFY column_name datatype BEFORE another_column_name;
to move column_name before another_column_name
I created a table with 85 columns but I missed one column. The missed column should be the 57th one. I don't want to drop that table and create it again. I'm looking to edit that table and add a column in the 57th index.
I tried the following query but it added a column at the end of the table.
ALTER table table_name
Add column column_name57 integer
How can I insert columns into a specific position?
ALTER TABLE by default adds new columns at the end of the table. Use the AFTER directive to place it in a certain position within the table:
ALTER table table_name
Add column column_name57 integer AFTER column_name56
From mysql doc
To add a column at a specific position within a table row, use FIRST or AFTERcol_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.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
I googled for this for PostgreSQL but it seems to be impossible.
Try this
ALTER TABLE tablename ADD column_name57 INT AFTER column_name56
See here
if you are saying ADD COLUMN column_name then it will throw error
u have to try
ALTER TABLE temp_name ADD My_Coumn INT(1) NOT NULL DEFAULT 1
remember if table already has few record and u have to create new column then either u have to make it nullable or u have to define the default value as I did in my query
ALTER TABLE table_name ADD COLUMN column_name integer
ALTER TABLE table_name ADD COLUMN column_name57 INTEGER AFTER column_name56
SET
#column_name =(
SELECT COLUMN_NAME
FROM
information_schema.columns
WHERE
table_schema = 'database_name' AND TABLE_NAME = 'table_name' AND ordinal_position = 56
);
SET
#query = CONCAT(
'ALTER TABLE `table_name` ADD `new_column_name` int(5) AFTER ',
#column_name
);
PREPARE
stmt
FROM
#query;
EXECUTE
stmt;
DEALLOCATE
stmt;
As workaround one could consider the use of column renaming.
I.e. add the new column at the end, and then until the new column is at the right position, add a temporary column for each column whose position is after the new column, copy the value from the old column to the temporary one, drop the old column and finally rename the temporary column.
see also: https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1770086700346491686
I tried to alter the table like so:
table_name add column column_name after column column_name;
The first column_name is the new column name, the second column_name is the existing column where you plan to insert into after.
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