Why can't I delete a row in Postgresql-Pgadmin when I already followed the format? - postgresql

I have a table Department with 2 columns, DEPT_CODE the PK and DEPT_NAME and both have data types name. I inserted:
INSERT INTO public."Department"("DEPT_CODE", "DEPT_NAME")
VALUES ('CIS', 'Computer Info Systems');
then I want to delete it so I wrote:
DELETE FROM public."Department"
WHERE 'DEPT_CODE' = 'CIS';
This didn't delete the row (I used the delete script provided by pgadmin). If I don't use quote for the 'DEPT_CODE', it gave an error:
ERROR: column "dept_code" does not exist
LINE 2: WHERE DEPT_CODE = 'CIS';
Idk what to do. Every tutorial I see seemed to be able to delete just fine, why not mine? This is my first time using pgadmin-postgres.

As documented in the manual identifiers (column & table names) need to be enclosed in double quotes. Single quotes are only for string constants.
And because you used the dreaded double quotes for the table and column names when you created the table, you now need to use them always:
DELETE FROM public."Department"
WHERE "DEPT_CODE" = 'CIS';
^ ^
| column name | string constant
It's strongly recommended to avoid those dreaded quoted identifiers completely. So never use double quotes when you create tables, then you never need to use them when working with the tables

Related

Select column name containing forward slash in PostgreSQL

I am new to PostgreSQL and trying the below two scenarios:
Select a column name which already has a forward slash(/) in database (Road/Highway)
Using case on same column along with index
Select Road/Highway,
case
when index(upcase(Road/Highway), 'xturn') > 0 then 2
else 0
end as preferred_road
from abc_data;
But I am getting syntax error near index and for slash it is only taking Road.
Generally / means "division", so your column name is non-standard, much like working with keyword column names, column names with special characters must be quoted with double quotes. Use "Road/Highway" when referring to the column.

postgres error: column doesn't exist error in Postgesql 11.6

I am trying to run an update command on postgresql 11.6 by below syntax
update "YP_SUPPLIERS" set "YP_SUPPLIERS.supplierName" = "update" where "YP_SUPPLIERS.supplierID" = da68e9d0-1100-43e2-0011-db8fbe654321;
I am getting this below error
ERROR: column "YP_SUPPLIERS.supplierID" does not exist
LINE 1: ... set "YP_SUPPLIERS.supplierName" = "update" where "YP_SUPPLI...
tired different combinations by only giving the column name , removing the quotes but nothing seems to be working.
Could any one suggest me a right way to do it.
You need to quote each element separately, and the table does not need to be repeated for the target column. String constants need to be enclosed in single quotes (') in SQL. Double quotes are only for identifiers.
update "YP_SUPPLIERS"
set "supplierName" = 'update' --<< single quotes for constant values
-- ^ no table name here
where "YP_SUPPLIERS"."supplierID" = 'da68e9d0-1100-43e2-0011-db8fbe654321';
-- ^ schema and table name must be quoted separately

search string and insert value in table

i have table having below records.The below product description are given by user in textbox in
front end(asp.net).The product description will come with single quotes or double quotes.i want to insert in the table.so how can we check whether single quotes or double quotes are exists in the
the input and insert value in the table.please help.
String s=Textbook
CREATE TABLE Product_Details(Product_Description varchar(50))
Required Output
Product_Description
-------------------
STORE('COVERED)
STEEL("ROOFED)
Insert statement will be differ for above two string?.
I'm not sure what exactly what you are looking for. You should be able to store text with quotes or double quotes without any trouble (note, I'm testing on Postgresql 9.4, don't have 9.2).
The problem is sometimes creating the text with the single quotes. In those cases it is common to have two single quotes like this insert into product_details values ('STORE(''COVERED)') The double quotes (") should not be a problem. You can use the syntax E'STORE(\'COVERED)' instead of the two quotes. Sometimes more readable.
If you just want to check if there are ' or "" in the input, this check is convenient:
select length(replace(product_description,'''',''))!=length(product_description),
length(replace(product_description,'"',''))!=length(product_description)
which return true/false columns telling if single-quote exists in string in first column and double-quote in the latter.
To delete the quotes in string you can do:
select replace(replace(product_description,'"',''),'''','')
Best regards,
Bjarni

Timescaledb - How to display chunks of a hypertable in a specific schema

I have a table named conditions on a schema named test. I created a hypertable and inserted hundreds of rows.
When I run select show_chunks(), it works and displays chunks but I cannot use the table name as parameter as suggested in the manual. This does not work:
SELECT show_chunks("test"."conditions");
How can I fix this?
Ps: I want to query the chunk itself by its name? How can I do this?
The show_chunks expects a regclass, which depending on your current search path means you need to schema qualify the table.
The following should work:
SELECT public.show_chunks('test.conditions');
The double quotes are only necessary if your table is a delimited identifier, for example if your tablename contains a space, you would need to add the double quotes for the identifier. You will still need to wrap it in single quotes though:
SELECT public.show_chunks('test."equipment conditions"');
SELECT public.show_chunks('"test schema"."equipment conditions"');
For more information about identifier quoting:
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Edit: Addressing the PS:
I want to query the chunk itself by its name? How can I do this?
feike=# SELECT public.show_chunks('test.conditions');
show_chunks
--------------------------------------------
_timescaledb_internal._hyper_28_1176_chunk
_timescaledb_internal._hyper_28_1177_chunk
[...]
SELECT * FROM _timescaledb_internal._hyper_28_1176_chunk;

Omitting the double quote to do query on PostgreSQL

Simple question, is there any way to omit the double quote in PostgreSQL?
Here is an example, giving select * from A;, I will retrieve ERROR: relation "a" does not exist, and I would have to give select * from "A"; to get the real result.
Is there any way not to do the second and instead do the first on PostgreSQL?
Your problem with this query started when you created your table. When you create your table, don't use quotes.
Use this:
CREATE TABLE a ( ... );
Not this:
CREATE TABLE "A" ( ... );
The latter will make it so that you always have to quote it later. The former makes it a normal name and you can use SELECT * FROM a; or SELECT * FROM A;
If you can't just recreate your table, use the ALTER TABLE syntax:
ALTER TABLE "A" RENAME TO a;
double quotes are required if you include capital letters in your table name in postgres
to avoid the requirements name your table "a"
Postgresql has some particular behaviour in regard to quoting and case sentivity: it folds every non-quoted identifier to lower case (also at creation time) and then works case-sensitively.
Double quotes in identifiers are only needed when the identifier (table name, column name, etc) was defined (at schema creation time) with uppercase letters (some or all) and between double quotes.
In that case (which I advice against), when you use that identifier, you must type it in the same way: case sensitively (type upper/lower case letter exactly as defined) and between double quotes.
In other cases, you can use non-quoted identifiers and work always case-insensitively.
Don't use upper case letter in your table name or it's column name, if you are using such thing then the postgres will required double quote for accessing it.
Please see the detailed description of what is happening here.
The PostgreSQL server table names are case-sensitive, but forced to be lower-case by default: when you type CREATE TABLE AAA, it will become CREATE TABLE aaa before the query execution.
Double-quoted names keep their case as it was, so after CREATE TABLE "AaA" you get the table AaA and have to write it double-quoted again and again.
Have no idea why did they do so :)