I am using PgAdmin 4 with PostgreSQL 12. Below is a simple tabledefinition vocabulary and a view vocabulary_input. When I use the SELECT - statement from the vocabulary_input in the Query-tool screen, I can update and add rows. However, when I choose "View / Edit data -- all rows" from vocabulary_input the view is locked. Why is that?
CREATE TABLE public.vocabulary
(
entry text COLLATE pg_catalog."default" NOT NULL,
description text COLLATE pg_catalog."default",
reference text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT vocabulary_pkey PRIMARY KEY (entry, reference)
);
CREATE OR REPLACE VIEW public.vocabulary_input
AS
SELECT vocabulary.entry,
vocabulary.description,
vocabulary.reference
FROM vocabulary
ORDER BY vocabulary.entry, vocabulary.reference;
I have posted this question at the developers site of pgadmin with the question if this is a bug or "expected behavior".
This is the answer:
"I am rejecting this RM, as in the future we will get rid of View/Edit data. We will enhance the query tool further, given that we can edit in place in the query tool."
So, my conclusion is that's is an interface issue and has nothing to do with SQL.
See: https://redmine.postgresql.org/issues/5532
Related
I have a table to hold phrases with their translation and pronunciation.
I'm not interested in providing a translation (that's why I removed NOT NULL on the translation column) but I want to provide a pronunciation for all inserts.
CREATE TABLE phrase (
id SERIAL UNIQUE PRIMARY KEY,
phrase TEXT NOT NULL,
translation TEXT,
pronunciation TEXT,
created timestamptz NOT NULL DEFAULT now(),
modified timestamptz,
);
However other users may want to add a translation but not a pronunciation or they want to fill both columns with data. It's up to them.
However I want at least one of the columns to be provided.
"No matter what you provide user, a translation or a pronunciation, at least provide one of the two. If both are empty, then no inserts. If one of them is filled, then perform insert."
I can do this checking and validation on the server-side with PHP but I was thinking if it would be better to check on the database-side as well. I read something about this:
constraint chk_fields check (translation is not null or pronunciation is not null)
but I have no idea where to add it in the CREATE TABLE statement. I can re-create the whole table, that's not an issue.
Just add it as another "list item" if you want to call it like that.
CREATE TABLE phrase (
id SERIAL UNIQUE PRIMARY KEY,
phrase TEXT NOT NULL,
translation TEXT,
pronunciation TEXT,
created timestamptz NOT NULL DEFAULT now(),
modified timestamptz,
constraint chk_fields check (translation is not null or pronunciation is not null)
);
You can also use an ALTER TABLE statement to add the constraint to the existing table.
ALTER TABLE phrase
ADD CONSTRAINT chk_fields
CHECK (translation IS NOT NULL
OR pronunciation IS NOT NULL);
(The data must already meet the requirements.)
I'm new to Postgres and can't seem to edit the data in the table. The test box pops up but doesn't allow me to change the text. This initial table didn't have any PK or SERIAL. So I added them and my table definition is now this:
CREATE TABLE public.weather
(
city character varying(80) COLLATE pg_catalog."default",
temp_lo integer,
temp_hi integer,
prcp real,
date date,
id integer NOT NULL DEFAULT nextval('weather_id_seq'::regclass),
CONSTRAINT weather_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.weather
OWNER to postgres;
It's probably very simple
Right-click on your table, select View Data/View All Rows (or one of the variants). That window will let you edit the data. Then press F6 to save changes (with thanks to leverglowh for pointing that out).
With pgadmin 4.6 the icon has changed (see screenshot):
a zoom to the icon:
You can also use the shortcut F6.
Right-click on your table, select View/Edit Data -> All Rows:
Then there is an icon in the top bar, looking like a table with an arrow pointing down (the icon on the right of the screenshot below):
The icon is gray but works nevertheless.
There is an image like Database with Save icon to save the edited data in the table
The above solutions all work for the specific case of the question, but I found that they did not work for me.
The reason is that my table did not have a primary key. As said on pgAdmin's official page:
To modify the content of a table, each row in the table must be
uniquely identifiable. If the table definition does not include an OID
or a primary key, the displayed data is read only.
I had believed that including:
CREATE TABLE IF NOT EXISTS public.mytable(
id INT GENERATED ALWAYS AS IDENTITY, [...]);
would automatically assign id as the primary key. I was wrong and thus could not edit "mytable" in pgAdmin.
Once I entered:
ALTER TABLE public.mytable
ADD CONSTRAINT mytable_pkey PRIMARY KEY (id);
The problem was solved.
On a quite simple table
CREATE TABLE collectionmaster
(
id character varying(32) NOT NULL,
version integer,
publisherid character varying(12),
title character varying(1024),
subtitle character varying(1024),
type character varying(255) NOT NULL,
CONSTRAINT collectionmaster_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
CREATE INDEX idx_coll_title
ON collectionmaster
USING btree
(title COLLATE pg_catalog."default");
I tried to add a unique check either via unique index
CREATE UNIQUE INDEX idx_uni_coll_title
ON collectionmaster
USING btree
(publisherid COLLATE pg_catalog."default", title COLLATE pg_catalog."default", (COALESCE(subtitle, 'no_sub'::character varying)) COLLATE pg_catalog."default");
or via unique constraint
ALTER TABLE collectionmaster
ADD CONSTRAINT uni_coll_title UNIQUE(publisherid, title, subtitle);
to intercept accidentally multiple creation by a java service (spring-jpa on hibernate) executed in several threads. But strangely neither of index and constraint works as expected. Eight records are added by eight threads which are not unique according to either constraint or index in separate transaction, but no unique violations are thrown, and all records are inserted into the table. And none of the values are null, as was the problem in other questions here.
The constaint was not deferred, index as constraint where valid.
Postgres version is 9.2.
I am quite clueless here.
EDIT: These are the results of a query in this table, extracted from pgAdmin (hard to format it nicer here):
"id";"version";"publisherid";"title";"subtitle";"type"
"53b3df625baf40bf885b48daa366fbc8";1;"5109138";"Titel";"Untertitel";"set"
"2673ef9a33f84289995d6f7288f07b46";1;"5109138";"Titel";"Untertitel";"set"
"ef7c385205034fdc89fe39e3eca48408";1;"5109138";"Titel";"Untertitel";"set"
"527922f2f3464f91826dbae2e2b67caf";1;"5109138";"Titel";"Untertitel";"set"
"794638a725324319852d10b828257df7";1;"5109138";"Titel";"Untertitel";"set"
"dbe2201058974d63a2107131f0080233";1;"5109138";"Titel";"Untertitel";"set"
"cbb77c7c1adb415db006853a6f6244ac";1;"5109138";"Titel";"Untertitel";"set"
"0b6606fe015040fbbc85444361ab414c";1;"5109138";"Titel";"Untertitel";"set"
Even on these I can execute
insert into collectionmaster(id, version, publisherid, title, subtitle, type) values('aaa',1,'5109138','Titel','Untertitel','set')
without getting a constraint violation. I can't believe it, too, but this is the problem...
Make sure your spring-jpa config spring.jpa.hibernate.ddl-auto is not set to create-drop.
That way, hibernate will always drop & re-create your whole schema, which won't contain your custom unique constraints, nor your indexes.
I have the following problem. I'm using SQLite3 to store some code table information.
There is a text file that contains all the rows I need. I've trimmed it down to one to make things easier.
The codetbls.txt file contains the one row I want insert into the table codetbls.
Using notepad++ to view the file contents shows the following:
codetbls.txt (Encoding: UTF-8)
1A|Frequency|Fréquence
I've created the following table:
create table codetbls (
id char(2) COLLATE NOCASE PRIMARY KEY NOT NULL,
name_eng varchar(50) COLLATE NOCASE,
name_fr varchar(50) COLLATE NOCASE
);
I then execute the following:
.read codetbls.txt codetbls
Now, when I run a select, I see the following:
select * from codetbls;
id name_eng name_fr
--+---------+----------
1A|Frequency|Fr├®quence
I don't understand why it doesn't show properly.
If I execute an insert statement with 'é' using the shell prompt, it shows up correctly. However using the .read command doesn't seem to work.
Based on other suggestions, I have tried the following:
- changed datatype to 'text'
- changed character encoding to UTF-8 without BOM
I don't know why it doesn't show. Any help?
I have a function that includes:
SELECT #pString = CAST(#pString AS VARCHAR(255)) COLLATE SQL_Latin1_General_Cp1251_CS_AS
This is useful, for example, to remove accents in french; for example:
UPPER(CAST('Éléctricité' AS VARCHAR(255)) COLLATE SQL_Latin1_General_Cp1251_CS_AS)
gives ELECTRICITE.
But using COLLATE makes the function non-deterministic and therefore I cannot use it as a computed persisted value in a column.
Q1. Is there another (quick and easy) way to remove accents like this, with a deterministic function?
Q2. (Bonus Question) The reason I do this computed persisted column is to search. For example the user may enter the customer's last name as either 'Gagne' or 'Gagné' or 'GAGNE' or 'GAGNÉ' and the app will find it using the persisted computed column. Is there a better way to do this?
EDIT: Using SQL Server 2012 and SQL-Azure.
You will find that it is in fact deterministic, it just has different behavior depending on the character you're trying to collate.
Check the page for Windows 1251 encoding for behavior on accepted characters, and unacceptable characters.
Here is a collation chart for Cyrillic_General_CI_AI. This is codepage 1251 Case Insensitive and Accent Insensitive. This will show you the mappings for all acceptable characters within this collation.
As for the search question, as Keith said, I would investigate putting a full text index on the column you are going to be searching on.
The best answer I got was from Sebastian Sajaroff. I used his example to fix the issue. He suggested a VIEW with a UNIQUE INDEX. This gives a good idea of the solution:
create table Test(Id int primary key, Name varchar(20))
create view TestCIAI with schemabinding as
select ID, Name collate SQL_Latin1_General_CP1_CI_AI as NameCIAI from Test
create unique clustered index ix_Unique on TestCIAI (Id)
create unique nonclustered index ix_DistinctNames on TestCIAI (NameCIAI)
insert into Test values (1, 'Sébastien')
--Insertion 2 will fail because of the unique nonclustered indexed on the view
--(which is case-insensitive, accent-insensitive)
insert into Test values (2, 'Sebastien')