LibreOffice Error "1: Unexpected token: UNQUE in statement" - libreoffice

I ran this SQL command
CREATE TABLE TEST(
KEY char(10) UNIQUE NOT NULL,
PRIMARY KEY(KEY)
);
and it gave me this error:
2: Unexpected token: UNIQUE in statement [CREATE TABLE TEST(
KEY char(10) UNIQUE]
I'm just trying to create a simple table from SQL command.
This is the error I got, which it seems to be strange, because this would run perfectly on MS access.
Anyway to fix this?
//EDIT
Due to "Key" is restricted word in SQL, I gave it another try.
CREATE TABLE TEST(
MLP char(10) UNIQUE NOT NULL,
PRIMARY KEY(MLP)
);
However, it seems to got broken again.
5: Unexpected token: UNIQUE in statement [CREATE TABLE TEST(
MLP char(10) UNIQUE]

I'm not sure about this, but try removing the "unique" parameter. Because if you're defining MLP as the key you're implying that it is unique, but SQL takes "KEY" and "UNIQUE" as incompatibles.

KEY is a reserved word in SQL. Choose another name for your column and it should work fine.
The official tutorial uses the following syntax:
CREATE TABLE TEST(
MLP CHAR(10) NOT NULL PRIMARY KEY
);

try restarting the SQL server, because the exact code you provided with works perfectly fine when i try it. Also, delete any sort of coloumn with the same name as TEST in your database

Related

syntax error in my postgres statement for alter sequence

UPDATE:
using postgres 14,
I just get error :Query 1 ERROR: ERROR: syntax error at or near "ok"
This is my database:
-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS id_seq;
-- Table Definition
CREATE TABLE "public"."ok" (
"id" int8 NOT NULL DEFAULT nextval('id_seq'::regclass),
PRIMARY KEY ("id")
);
And I want to modify the sequence:
ALTER SEQUENCE ok_id_seq RESTART;
I keep getting errors at ok_id_seq.
I tried id_seq only
Tried quotes everywhere.
Ok, I now learnt that sequences are not table specific. I was trying to do alter table alter sequence.
I also used SELECT * FROM information_schema.sequences; to view all available sequences.
I don't know what happened so I recreated the table, checked for the sequence name.
Then was able to alter it with restart

Postgres: Can I bypass the error "cannot insert into generated column" using a PostgreSQL INSTEAD OF INSERT rule?

I know this isn't pretty but it would be helpful to bypass the error for insert into a generated column in Postgres. Let's say, we have a table like so:
create table testing (
id int primary key,
fullname_enc bytea,
fullname text generated always as (pgp_sym_decrypt(fullname_enc, 'key')) stored
);
A query like the following returns the expected error: ERROR: cannot insert into column "fullname" DETAIL: Column "fullname" is a generated column.
insert into testing(id, fullname) values (3, 'John Doe');
I want to create a rule on this table on INSERTs like:
create rule encrypter as on insert to testing DO INSTEAD insert into testing (id, fullname_enc) values (new.id, pgp_sym_encrypt(new.fullname, 'key'));
Since we rewrite the query, I was naively thinking if this would not result in the error from the engine but it still does. Any idea how this could be achieved?
The reason for asking this is migration to PostgreSQL 12.
This cannot be achieved, and if it could be achieved somehow, that would be a bug that needs to be fixed. Otherwise, restoring from a dump would change the values.
I think that what you need is a BEFORE trigger that sets fullname.
I hope that this is a mock example and not something that is intended to improve security.

PostgreSQL - CREATE TABLE with int4range constraint

I want to use a range constraint in creating a table, as it seems more elegant than what I'm currently doing:
DROP TABLE IF EXISTS "coa_sandbox"."account_list";
CREATE TABLE "coa_sandbox"."account_list" (
id serial unique not null,
account_number int unique not null CONSTRAINT within_range CHECK (account_number >= 10000 AND account_number <= 99999),
account_name text unique not null
) WITH (oids = false);
Maybe it's silly, but I think using int4range seems to be a more professional approach.
I've been using Erwin Brandstetter's solution to this problem [https://stackoverflow.com/a/35028185] as inspiration, without success. I re-created my table above, without the constraint, then tried altering the table. I started with:
ALTER TABLE "coa_sandbox"."account_list" ADD CONSTRAINT within_range
CHECK ("account_number" = ANY ('{9999,99999}'::int4range[]));
... with this result:
Error in query: ERROR: malformed range literal: "9999"
DETAIL: Missing left parenthesis or bracket.
Note that I'm using Adminer 4.7.8 to perform these queries.
I've continued trying with different combinations of brackets, curly braces & parenthesis, using the error details and my intermediate level of knowledge regarding these items in Perl as influence.
I got nuthin'. I've been searching the PostgreSQL docs for details on syntax, and if the solution is evident then I'm just not seeing it.
Will someone please set me straight on this? If my initial working solution is the best approach, please tell me. If I'm heading in the right direction but missing the correct details, please tell me what I'm missing.
Thank you.
Finally figured it out. The CONTAINED IN (<#) operator is what I was looking for.
The statement I've wanted is:
DROP TABLE IF EXISTS "coa_sandbox"."account_list";
CREATE TABLE "coa_sandbox"."account_list" (
id serial unique not null,
account_number int unique not null CONSTRAINT within_range
CHECK ("account_number" <# int4range(10000,100000)),
account_name text unique not null
) WITH (oids = false);

creating a table with column names having brackets

I tried to create a table with postgresql query:
CREATE TABLE customer_account(ID_account integer primary key, customer_name (lastname) text);
but it gives an error message:
ERROR: syntax error at or near "("
LINE 5: customer_name (lastname) text,
Probably the problem comes from the bracket, and I already tried like
CREATE TABLE customer_account("ID_account" primary key, "customer_name (lastname)" text);
But it also gave me a similar error message.
How to correct the query? I really need to use bracket.
Using " will work, but you are missing the data type for your primary key:
CREATE TABLE customer_account
(
"ID_account" integer primary key,
--^ here
"customer_name (lastname)" text
);
Online example
But I strongly suggest you do not use quoted identifiers.
They will give you much more trouble in the long run then they are worth it.
I would recommend to use something like this:
CREATE TABLE customer_account
(
account_id integer primary key,
customer_lastname text
);
("ID" as a prefix sounds quite strange in English)

Postgresql User Defined Type in primary key constraint

I'm using postgresql 9.1 on Ubuntu 12.04. I have a user defined type as one column of a table. When I create a primary key constraint I get a syntax error.
Here is a sample sql script I'm using with psql to create the table:
CREATE TYPE my_type AS
(
field1 integer,
field2 integer
);
CREATE TABLE my_table
(
my_data my_type,
other_data integer
);
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I get this error:
ERROR: syntax error at or near "."
LINE 1: ...ble ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I've tried using (my_data).field1 but also get a syntax error.
If I just use my_data in the constraint there is no error:
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data);
But I would like to use just one field as part of the constraint.
Thanks for any ideas.
I found this question using google and I'm pretty sure is dead but I still want to answer it cause someone else might see it.
Short answer is you have to use the field's name:
ALTER TABLE "public"."carga" ADD CONSTRAINT "pk_my_table" PRIMARY KEY ("field1");