I need to add a unique field index to an existing table. I made this row:
ALTER TABLE auth_user ADD UNIQUE INDEX (email);
The table and the field are already exist.
The error is:
Query Error: near "UNIQUE": syntax error Unable to execute statement
What am I missed? Did it have any specific requirements for SQLite3?
CREATE UNIQUE INDEX IF NOT EXISTS MyUniqueIndexName ON auth_user (email)
Also, read the official manual:
http://www.sqlite.org/lang_createindex.html
Related
I want to add a unique index in my DB, but some data has already been duplicated.
I am trying out this using a test table before applying to the actual one.
It seems that if there are duplicated rows then we wouldn't be able to add the unique constraint.
I want to add the unique constraint and do not care which row gets deleted.
When I run the following
ALTER TABLE test_user ADD CONSTRAINT test_constraint UNIQUE (personid);
I am getting this error
ERROR: could not create unique index "test_constraint"
DETAIL: Key (personid)=(1) is duplicated.
SQL state: 23505
What would be the best way to achieve this? I am using Postgres as the DB
Create a copy of the table eliminating duplicates:
CREATE TABLE test_user_new (LIKE test_user);
INSERT INTO test_user_new
SELECT DISTINCT ON (id) *
FROM test_user;
ALTER TABLE test_user_new PRIMARY KEY (personid);
Then replace the original table:
DROP TABLE test_user;
ALTER TABLE test_user_new RENAME TO test_user;
I have an index badName on table tableName inside schemaName, created like so:
CREATE UNIQUE INDEX badName ON schemaName.tableName USING btree;
Now I want to rename the index to goodName. This is my attempt at it:
ALTER INDEX schemaName.tableName.badName RENAME TO goodName;
Which results in:
Error [0A000] cross-database references are not implemented
I am using postgresql database, but want to use native SQL query.
You need to specify (only) the index name, not the table name:
ALTER INDEX schemaname.badname RENAME TO goodname;
I'm trying to insert a row into a Sybase ASE table, and I get a error saying,
Attempt to insert duplicate key row in object 'Employee' with unique index 'Employee_uk'
When I opened the DDL for the Employee table I don't see a unique constraint in it. So some other table's DDL is created with a unique index pointing to this Employee table.
How can I find which table is created with this unique index?
Cheers!!
You can use the command:
sp_help Employee
to obtain all the information about the table.
This command can be used to get information on any database object.
Regards
I'm having an issue trying to re-enable a unique constraint. I try using this command:
alter table TESTSCHEMA_1.TEST_TABLE1 enable constraint TEST_UNIQUE_CONSTRAINT1;
The issue is that i have multiple schemas (say: TESTSCHEMA_1 to _5), and they all have tables with the same name TEST_TABLE1 which also have a Unique constraint with the same name TEST_UNIQUE_CONSTRAINT1.
As a result I get this error:
ORA-02299: cannot validate (TESTSCHEMA_1.TEST_UNIQUE_CONSTRAINT1) - duplicate keys found
How can I indicate specifically the schema where is the constraint i want to enable? I've tried using TESTSCHEMA_1.TEST_UNIQUE_CONSTRAINT1, but it throws a syntax error (Non-properly ended sql command)
check uniqueness of your data.
select unique_column_in_test_table1, count(unique_column_in_test_table1) from TESTSCHEMA_1.TEST_TABLE1
group by unique_column_in_test_table1
having count(unique_column_in_test_table1) > 1
if any rows return by this query you have to handle/correct it to be unique.
In PG:
I made a user table that includes unique emails, but later decided that emails should not be unique. I pushed changes to make my email field non-unique (I use an ORM, so I don't actually have the exact SQL that took place), but PG still won't let me use duplicate email addresses.
I checked the index and it's not unique, but there's a constraint keeping me from having duplicate email addresses. However I'm having trouble dropping this constraint. What am I doing wrong?
SQL> ALTER TABLE "users" DROP CONSTRAINT "unique_users_email"
PGError: ERROR: constraint "unique_users_email" of relation "users" does not exist
SQL> UPDATE users SET email = 'test#test.com'
PGError: ERROR: duplicate key value violates unique constraint "unique_users_email"
DETAIL: Key (email)=(test#test.com) already exists.
I bet that "unique_users_email" is actually the name of a unique index rather than a constraint. Try:
DROP INDEX "unique_users_email";
Recent versions of psql should tell you the difference between a unique index and a unique constraint when looking at the \d description of a table.