@UniqueConstraint annotation in Java

I have a Java bean. Now, I want to be sure that the field should be unique. I am using the following code: @UniqueConstraint(columnNames={“username”}) public String username; But I’m getting some error: @UniqueConstraint is dissallowed for this location What’s the proper way to use unique constraints? Note: I am using play framework. 12 Answers 12

ERROR: there is no unique constraint matching given keys for referenced table “bar”

Trying to create this example table structure in Postgres 9.1: CREATE TABLE foo ( name VARCHAR(256) PRIMARY KEY ); CREATE TABLE bar ( pkey SERIAL PRIMARY KEY, foo_fk VARCHAR(256) NOT NULL REFERENCES foo(name), name VARCHAR(256) NOT NULL, UNIQUE (foo_fk,name) ); CREATE TABLE baz( pkey SERIAL PRIMARY KEY, bar_fk VARCHAR(256) NOT NULL REFERENCES bar(name), name VARCHAR(256) … Read more

Can I add a UNIQUE constraint to a PostgreSQL table, after it’s already created?

I have the following table: tickername | tickerbbname | tickertype ————+—————+———— USDZAR | USDZAR Curncy | C EURCZK | EURCZK Curncy | C EURPLN | EURPLN Curncy | C USDBRL | USDBRL Curncy | C USDTRY | USDTRY Curncy | C EURHUF | EURHUF Curncy | C USDRUB | USDRUB Curncy | C I don’t … Read more

How do I ALTER a PostgreSQL table and make a column unique?

I have a table in PostgreSQL where the schema looks like this: CREATE TABLE “foo_table” ( “id” serial NOT NULL PRIMARY KEY, “permalink” varchar(200) NOT NULL, “text” varchar(512) NOT NULL, “timestamp” timestamp with time zone NOT NULL ) Now I want to make the permalink unique across the table by ALTER-ing the table. 3 Answers … Read more

How do I specify unique constraint for multiple columns in MySQL?

I have a table: table votes ( id, user, email, address, primary key(id), ); Now I want to make the columns user, email, address unique (together). How do I do this in MySql? Of course the example is just… an example. So please don’t worry about the semantics. 14 s 14 To add a unique … Read more