PostgreSQL query -- column does not exist - sql

I am trying to update a table using a temporary table.
Schema | Name | Type | Owner
------------+----------------------+----------+----------
pg_temp_11 | tmp_x | table | postgres
public | entities | table | postgres
However I am getting this error:
UPDATE entities SET "Name" = "tmp_x.Name" FROM tmp_x WHERE "entities.Ent_ID" = "tmp_x.Ent_ID";
ERROR: column "tmp_x.Name" does not exist -- the column Name exists
LINE 1: UPDATE entities SET "Name" = "tmp_x.Name" FROM tmp_x WHERE "...
What is the problem? The quotes around table columns?

You are surrounding multiple individual objects with double quotes. If you are using object delimiters (double quotes), they need to be on each item, not on the entire combination:
UPDATE entities SET "Name" = "tmp_x"."Name" FROM tmp_x WHERE "entities"."Ent_ID" = "tmp_x"."Ent_ID";

Related

Updating a PostgreSQL column that contains dot (.) in its name

I should update the value of a row, but the column name has the dot.
I tried name.name but nothing, even though it seems to work on MySQL.
How can I do with postgresql? I swear that before creating this thread I searched all over.
Thanks
UPDATE:
Thanks for the quick answers, I tried to use "" but this is the result
ERROR: column "name.name" of relation "my_table" does not exist
My query:
update my_table set "name.name"='a081613e-2e28-4cae-9ff7-4eaa9c918352';
You can use "" around the column name
Wrap name with double quotation marks: "name.name"
UPD:
UPDATE: Thanks for the quick answers, I tried to use "" but this is the result
Are you sure then that it's your case?
psql (13.2)
Type "help" for help.
postgres=# CREATE DATABASE example_db;
CREATE DATABASE
postgres=# \c example_db
You are now connected to database "example_db" as user "postgres".
example_db=# CREATE TABLE example_table ("example.field" int);
CREATE TABLE
example_db=# \d example_table
Table "public.example_table"
Column | Type | Collation | Nullable | Default
---------------+---------+-----------+----------+---------
example.field | integer | | |
example_db=# SELECT "example.field" FROM example_table;
example.field
---------------
(0 rows)
example_db=# SELECT "example_table"."example.field" FROM example_table;
example.field
---------------
(0 rows)
example_db=#

How to call a column named "group" in Snowflake?

I have a table in Snowflake with the following structure:
| id | group | subgroup |
_________________________
| 1 | verst | burg |
| 2 | travel| plane |
| 3 | rest | bet |
I need to call only the column "group", so I tried the following code:
select t2.group
from table as t2
but the following error arises
SQL compilation error: syntax error line 1 at position 7 unexpected 'group'. syntax error line 2 at position 0 unexpected 'from'.
I have also tried using:
select group
from table as t2
select "group"
from table as t2
but I always get the same error.
I know I can call the whole table using * but the real table where I get this data from has many more columns and we want to display this data in a dashboard. Additionally, I am not the owner of the table since it is filled by a microservice, so I cannot change the column names and I can't modify the microservice process.
I would appreciate any suggestion.
Given the table could not be created without double quotes, you need to know how it was created to know how to refer to the column. Which is to say it the create code was CREATE TABLE awsome ("GrOuP" string); there you will need to type "GrOuP"
There is also a session setting to ignore case in double quotes that might help.
see QUOTED_IDENTIFIERS_IGNORE_CASE
But by default things are upper case, thus try "GROUP"
Putting group in double quotes worked fine when I tried it:
create or replace temporary table foo ( "group" string );
insert into foo values ('Hello world.');
select "group" from foo;

How can I update the table in SQL?

I've created a table called Youtuber, the code is below:
create table Channel (
codChannel int primary key,
name varchar(50) not null,
age float not null,
subscribers int not null,
views int not null
)
In this table, there are 2 channels:
|codChannel | name | age | subscribers | views |
| 1 | PewDiePie | 28 | 58506205 | 16654168214 |
| 2 | Grandtour Games | 15 | 429 | 29463 |
So, I want to edit the age of "Grandtour Games" to "18". How can I do that with update?
Is my code right?
update age from Grandtour Games where age='18'
No, in update, you'll have to follow this sequence:
update tableName set columnWanted = 'newValue' where columnName = 'elementName'
In your code, put this:
update Channel set age=18 where name='Grandtour Games'
Comments below:
/* Channel is the name of the table you'll update
set is to assign a new value to the age, in your case
where name='Grandtour Games' is referencing that the name of the Channel you want to update, is Grandtour Games */
alter table changes the the schema (adding, updating, or removing columns or keys, that kind of thing).
Update table changes the data in the table without changing the schema.
So the two are really quite different.
Here is your answer -
-> ALTER is a DDL (Data Definition Language) statement
UPDATE is a DML (Data Manipulation Language) statement.
->ALTER is used to update the structure of the table (add/remove field/index etc).
Whereas UPDATE is used to update data.
Hope this helps!

Modifying dynamic column contents

I am trying to create a new column inside a dynamic column.
My table Templates just has two columns: ID, Structure (blob)
I run this query:
UPDATE `Templates` SET `Structure` = COLUMN_ADD(`Structure`, 'general', '') where `Templates`.`ID` = 1
Structure Result (Using COLUMN_JSON for display):
{"general":""}
Then I run this query:
UPDATE `Templates` SET `Structure` = COLUMN_ADD(COLUMN_GET(`Structure`, 'general' as CHAR), 'Inner', 'value') WHERE `Templates`.`ID` = 1
Structure Result:
{"Inner":"value"}
The result I want after both queries:
{"general": {"Inner":"value"}}
How can I get a column added to the dynamic "general" column instead of replacing the contents?
First, here is what happens with your query.
MariaDB [test]> CREATE TABLE Templates (ID INT, Structure BLOB);
Query OK, 0 rows affected (0.24 sec)
MariaDB [test]> INSERT INTO Templates VALUES (1, COLUMN_CREATE('general',''));
Query OK, 1 row affected (0.05 sec)
MariaDB [test]> SELECT COLUMN_JSON(Structure) FROM Templates;
+------------------------+
| COLUMN_JSON(Structure) |
+------------------------+
| {"general":""} |
+------------------------+
1 row in set (0.00 sec)
At this point you have in Structure one dynamic column with name general and empty string as a value.
Then you do this:
UPDATE `Templates`
SET `Structure` = COLUMN_ADD(
COLUMN_GET(`Structure`, 'general' as CHAR),
'Inner',
'value'
) ...
Your COLUMN_GET gets the value of general dynamic column, which is an empty string, and uses it as the first argument for COLUMN_ADD. It's a useless exercise, because if you want to run COLUMN_ADD on an empty string, you can just say so in the query or use COLUMN_CREATE; and if you want to actually add something to the existing value of the blob, you need to use the name of the blob.
So, COLUMN_ADD works on an empty string -- in other words, creates a clean new value for Structure, discarding everything it had -- and adds a dynamic column with name Inner and value value. That's why you are getting this:
MariaDB [test]> SELECT COLUMN_JSON(Structure) FROM Templates;
+------------------------+
| COLUMN_JSON(Structure) |
+------------------------+
| {"Inner":"value"} |
+------------------------+
1 row in set (0.00 sec)
Apparently, what you want to do instead is to set the value of general column to a new dynamic column.
You don't need to fetch general column for that, because COLUMN_ADD(x,y,z) will replace the value if a column y already exists in blob x. But you need to construct a new dynamic column for the new value of general.
So, what you should do is
UPDATE `Templates`
SET `Structure` = COLUMN_ADD(
`Structure`,
'general',
COLUMN_CREATE('Inner','value')
) ...
This accounts for a more general case when Structure also contains other columns, not only general, and you want to preserve them. If it's not the case, and you want to make sure the blob contains only general, then you can do
UPDATE `Templates`
SET `Structure` = COLUMN_CREATE(
'general',
COLUMN_CREATE('Inner','value')
)

Changing a column type from integer to string

Using PostgreSQL, what's the command to migrate an integer column type to a string column type?
Obviously I'd like to preserve the data, by converting the old integer data to strings.
You can convert from INTEGER to CHARACTER VARYING out-of-the-box, all you need is ALTER TABLE query chaning column type:
SQL Fiddle
PostgreSQL 9.3 Schema Setup:
CREATE TABLE tbl (col INT);
INSERT INTO tbl VALUES (1), (10), (100);
ALTER TABLE tbl ALTER COLUMN col TYPE CHARACTER VARYING(10);
Query 1:
SELECT col, pg_typeof(col) FROM tbl
Results:
| col | pg_typeof |
|-----|-------------------|
| 1 | character varying |
| 10 | character varying |
| 100 | character varying |
I suggest a four step process:
Create a new string column. name it temp for now. See http://www.postgresql.org/docs/9.3/static/ddl-alter.html for details
Set the string column. something like update myTable set temp=cast(intColumn as text) see http://www.postgresql.org/docs/9.3/static/functions-formatting.html for more interesting number->string conversions
Make sure everything in temp looks the way you want it.
Remove your old integer column. Once again, see http://www.postgresql.org/docs/9.3/static/ddl-alter.html for details
Rename temp to the old column name. Again: http://www.postgresql.org/docs/9.3/static/ddl-alter.html
This assumes you can perform the operation while no clients are connected; offline. If you need to make this (drastic) change in an online table, take a look at setting up a new table with triggers for live updates, then swap to the new table in an atomic operation. see ALTER TABLE without locking the table?