I'm running Postgres 8.4.13, and trying to add a constraint to an existing table. According to the docs, this should be possible:
alter table indexed_friends add constraint no_duplicate_user_friends unique (user, friend);
Yet when I run this I get the following error:
ERROR: syntax error at or near "user"
I'm confused because I'm following an unique constraint example listed in the documentation almost exactly. I can provide the table schema, but since it's complaining about a syntax error, I'm not sure that's necessary.
The syntax for creating a unique constraint using an ALTER TABLE statement in PostgreSQL is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); table_name.
The error message “syntax error at or near 'grant'” is one of the most common PostgreSQL database errors. However, it can easily be identified and resolved. To understand this issue, you need to know that SQL distinguishes between reserved and non-reserved key word tokens.
A check constraint is the most generic constraint type. It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression. For instance, to require positive product prices, you could use: CREATE TABLE products ( product_no integer, name text, price numeric CHECK (price > 0) );
What is a primary key in PostgreSQL? In PostgreSQL, a primary key is a single field or combination of fields that uniquely defines a record. None of the fields that are part of the primary key can contain a NULL value. A table can have only one primary key.
Ahhh... The word user
is a reserved word in Postgres.
Surrounding it in quotes:
alter table indexed_friends add constraint no_duplicate_user_friends unique ("user", friend);
worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With