Rename a column with space in it - sql

Someone mistakenly created a table in which all the column names has a leading space in it. For example: 'accountid' is now ' accountid'.
I am going to write a SQL statement to rename these columns. The one I wrote is:
ALTER TABLE mytable RENAME COLUMN ' accountid' TO 'accountid';
However, I got the following error:
Error : ERROR: syntax error at or near "' accountid'"
Can someone instruct me how to rename these? How to change my statement to make it runnable? I use PostgreSQL.
Many thanks.

In PostgreSQL, you use double-quotes for identifiers (if necessary): "
ALTER TABLE mytable RENAME COLUMN " accountid" TO "accountid";
See here and browse to 4.1.1

You can even put other characters:
select c.comment "actor.comment" from post p join comment c on p.id = c.post_id;

Related

ERROR: syntax error at or near "." LINE 4: ON like.takerId = frame.likeId;

i have a table whose name is like. But whenever i have to select data from like, i was getting this error, i figured it out public.like..but when i try to join two tables
SELECT *
FROM frame
INNER JOIN public.like
ON like.takerId = frame.likeId;
i get this error
ERROR: syntax error at or near "."
LINE 4: ON like.takerId = frame.likeId;
i also use public prefix but it throws
ERROR: column like.takerid does not exist
LINE 4: ON public.like.takerId = frame.likeId;
^
HINT: Perhaps you meant to reference the column "like.takerId".
even if it is saying column like.takerid does not exist , then why it gives me HINT: Perhaps you meant to reference the column "like.takerId". I dont know, i think it is problem with like table name, like is a sql syntax, and it assumes like and a sql syntax and throwing me error. Should I change my table name? Or is there any way to make sql case sensetive or how can i tell sql to ignore like. public.like is not working for joining table.
As like is a reserved keyword, you need to use double quotes for each occurance of it (unless it's prefixed with the schema name as you found out)
SELECT *
FROM frame
JOIN public.like ON "like".takerId = frame.likeId;
Or
SELECT *
FROM frame
JOIN "like" ON "like".takerId = frame.likeId;
Or use an alias
SELECT *
FROM frame f
JOIN "like" l ON l.takerId = f.likeId;
But in the long run you should find a different name for the table that does not require quoting.
You should definitely chose another name for your table. LIKE is a reserved command, and it is considered a bad practice to use it, although possible by using ", e.g.
CREATE TABLE public."like" (id int);
INSERT INTO public."like" VALUES (42);
SELECT * FROM "public.like"
EDIT: As pointed out by #a_horse_with_no_name, specifying a schema in temporary tables won't work (check db<>fiddle), so only the table name should be between double quotes as corrected in the snippet above. For temporary tables just omit the schema:
CREATE TEMPORARY TABLE "like" (id int);
INSERT INTO "like" VALUES (42);
SELECT * FROM "like"
Demo: db<>fiddle

MariaDB to calculate table reference in select query

I have a quite dumb client application that wants to get information from MariaDB based on a parameter.
This parameter is a string that contains spaces, like 'valid parameter'.
In MariaDB there is a table for each of the possible string values, and the table name is the string value after spaces have been replaced by underscores and a prefix is added. So I can perform the necessary conversion like this:
SELECT CONCAT('prefix_', REPLACE('valid parameter',' ','_'));
Now the result 'prefix_valid_parameter' is the table to query, so actually I need to fire off
SELECT * from CONCAT('prefix_', REPLACE('valid parameter',' ','_'));
but MariaDB responds with
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '('prefix_', REPLACE('valid parameter',' ','_'))' at line 1
I would have expected either the content of table 'prefix_valid_parameter' or an error stating "table 'prefix_valid_parameter' not found". How can I make the table_reference part of my SQL SELECT statement dynamic?
You need to use dynamic SQL:
set #sql = 'select * from [table]';
execute immediate replace(#sql, '[table]',
concat('prefix_', replace('valid parameter', ' ', '_'))
);
I should add that the need to do this perhaps suggests a flaw in your data model. If the tables have the same structure, it is better to put all the rows in a single table.

MariaDB unable to drop column named "`"

Not sure how this happened, but a column got created named: "`". (just the back tick). When I attempt to drop this column, I end up with a syntax error.
Outside of taking a SQL dump and fixing the dump in a text editor, Does anybody have a suggestion to fix this query?
ALTER TABLE tableName DROP COLUMN "`";
You need to escape the inner backtick with another backtick, so:
alter table tableName drop column ````;

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

mysql updating cell content issue

I am trying to update a value in my database but am recieving the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' ('new_user') VALUES ('1') WHERE 'id'= 5' at line 1
I am trying to UPDATE the table 'users' in the column 'new_user' where the id is equal to $userid. But it don't work. Please help.
$newuservalue = '1';
$notnewuser ="UPDATE 'users' ('new_user') VALUES ('$newuservalue') WHERE 'id'= $userid ";
$query2 = mysql_query($notnewuser) or die(mysql_error());
Well, your syntax is wrong.
It should be:
UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]
So, remove the "values" part of your query and put in the "set" part.
Here's a link to the official documentation.
i have that problems some times, that's the code to insert a new row :d
It should be:
UPDATE users SET new_user='$newuservalue' WHERE id=$userid
you also don't need to put quotes around your column names, that might give some problems as well.