creating a table with column names having brackets - postgresql

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)

Related

PostgreSQL: column does not exists

Right now I'm trying to create a filter that would give me every result from start of the month. The query looks like this:
cur.execute('SELECT SUM(money_amount) '
f'FROM expense WHERE created >= "{first_day_of_month}"'
But I'm getting such error: psycopg2.errors.UndefinedColumn: column "2022-08-01" does not exist
my createtable.sql:
CREATE TABLE budget(
codename varchar(255) PRIMARY KEY,
daily_expense INTEGER );
CREATE TABLE category(
codename VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
is_basic_expense BOOLEAN,
aliases TEXT );
CREATE TABLE expense(
id SERIAL PRIMARY KEY,
money_amount INTEGER,
created DATE,
category_codename VARCHAR(255),
raw_text TEXT,
FOREIGN KEY(category_codename) REFERENCES category(codename) );
What is wrong and why the column does not exist, when it is?
This is probably the most common reason to get a "column does not exist" error: using double quotes. In PostgreSQL, double quotes aren't used for strings, but rather for identifiers. For example, if your column name had a space in it, you wouldn't be able to write WHERE the date > '2022-08-01', but you would be able to write WHERE "the date" > '2022-08-01'. Using double quotes around a string or stringy thing like a date gets interpreted as an attempt to use an identifier, and since you're using it where a value should be it will usually be interpreted as trying to identify a column in particular. I make this mistake at least once a week. Instead, use single quotes or placeholders.

42601 ERROR: syntax error at or near "NULLS" using unique NULLS NOT DISTINCT on PostgreSQL 14.4 Ubuntu

I have a situation where I want my composite field null check to be UNIQUE. So I'm using the UNIQUE NULLS NOT DISTINCT syntax as follows:
create table codes
(
id serial primary key,
code text not null,
sub_code text null,
unique nulls not distinct (code,sub_code)
);
In this example the code can be entered again if it has a sub_code but only one version of codes can exist without a sub_code
It looks like PostgreSQL is rejecting the word nulls. Any ideas why? Is there a configuration to turn this syntax on?
You have to use the CONSTRAINT keyword like this:
create table codes
(
id serial primary key,
code text not null,
sub_code text null,
CONSTRAINT uq_sub_code
UNIQUE NULLS NOT DISTINCT (sub_code)
);

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");

What's wrong with my CREATE TABLE command?

I'm following the instructions from this document. My exact version is 8.4.4.
This is what I try to do
CREATE TABLE testInfo (
testNo integer PRIMARY KEY,
product varchar(15),
firmware varchar(15),
startDate date,
eta date
);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "testinfo_pkey" for table "testinfo"
It totally ignores my PRIMARY KEY constraint. I don't see whay this isn't essentially the same as the example in the docs.
CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric
)
I'm sure the obvious is staring me right in the face. Nevertheless I would appreciate any help offered.
Update: I just tried the example from the documentation, returns the same message. So may I conclude the documentation is in error, or that 8.4.4 is buggy?
I'm no Postgresql expert, but it appears the message is simply to inform you that an INDEX is being created to assist in the implementation of the PRIMARY KEY that you defined.
It's not ignoring your primary key, it's telling you the mechanism it will use to enforce it. This message can be disabled with client_min_messages (warning).

Better way of primary key constraint handling?

i have this sqlite table:
CREATE TABLE frames (videomd5 TEXT, framemd5 TEXT, type TEXT, PRIMARY KEY (videomd5, framemd5))
As you can see, the table has a combined PRIMARY KEY because it is allowed that one of the fields has the same values but never both at once.
Currently I'm performing a check like this
SELECT framemd5 FROM frames WHERE framemd5='$digest' AND videomd5='$videomd5'
before adding something to the table to avoid a PRIMARY KEY CONTRAINTs but i feel there is a better way to handle it. Should i fire the INSERT without checking first and handel the CONSTRAINT afterwards? If so, how is this best done in perl?
Thank you
Yes you can use the "OR IGNORE" in your insert, eg:
sqlite> create table bla (id INT PRIMARY KEY);
sqlite> insert into bla values (1);
sqlite> insert into bla values (1);
Error: column id is not unique
sqlite> insert or ignore into bla values (1);
sqlite>
Refer to the official doc for the details:
http://www.sqlite.org/syntaxdiagrams.html#insert-stmt
As you said yourself - there are two ways, do some benchmarks or select the one which matches your coding style.
I prefer first approach, though, than handling primary key violation exceptions