How to alter a column by adding UNIQUE - postgresql-9.1

I would like to alter a column without the addition of constraint:
My column definition:
...
name character varying(64) not nul,
...
What I want:
...
name character varying(64) unique not nul,
...
I tried:
alter table T add unique(name);
but a constraint with index is suggested.

alter table T add constraint unique_name unique (name);
See the examples in the manual: http://www.postgresql.org/docs/current/static/sql-altertable.html
Or as part of the table definition:
create table t
(
...,
name character varying(64) not null,
constraint unique_name unique (name)
);
Or simply as a unique index:
create unique index unique_name on t (name);

The docs suggest:
alter table T add unique using index I
http://www.postgresql.org/docs/9.1/static/sql-altertable.html
This would assume you've created the unique index I beforehand.

Related

Multiple SQLite unique columns as one

Given the following example:
CREATE TABLE shapes(
shape_id INTEGER PRIMARY KEY,
background_color TEXT,
foreground_color TEXT,
UNIQUE(background_color,foreground_color)
);
background_color AND foreground_color need to be unique to insert but I don't want that. consider the row exists:
black|blue
and I try to insert:
white|blue
it will insert but if I try to insert another row containing:
black|blue
it will ignore the insert.
Will a primary key of the two cols accomplish this? If so, do I need to also use INSERT OR IGNORE?
First remove the UNIQUE constraint from the definition of the table.
What you can do is create a unique index not on the columns but on the min and max values of the 2 columns:
CREATE UNIQUE INDEX idx_shapes_colors ON shapes(
MIN(background_color, foreground_color),
MAX(background_color, foreground_color)
);
Then, when you try to insert the same combination of colors, like:
INSERT INTO shapes (background_color, foreground_color) VALUES ('black', 'blue');
INSERT INTO shapes (background_color, foreground_color) VALUES ('blue', 'black');
the 2nd statement will fail with an error message:
UNIQUE constraint failed: index 'idx_shapes_colors'
or, if you use INSERT OR IGNORE:
INSERT OR IGNORE INTO shapes (background_color, foreground_color) VALUES ('blue', 'black');
there will be no error but the statement will not insert the row.
See the demo.

Will AutoIncrement work with Check constraints?

The question is simple:
In SQLite, if I choose to AutoIncrement a primary key of type NUMERIC which has a check constraint like CHECK(LENGTH(ID) == 10), will it work correctly inserting the first value as 0000000001 and so on?
No, that does not work. Adding a check does not magically also add a way of fullfilling the check to insert the data.
See this SQLFiddle.
If you want to restrict the value of an autoincrement column like that, you need to seed the internal sequence table. (There are other ways.)
create table foo (
foo_id integer primary key autoincrement,
other_columns char(1) default 'x',
check (length(foo_id) = 10 )
);
insert into sqlite_sequence values ('foo', 999999999);
Application code is allowed to modify the sqlite_sequence table, to
add new rows, to delete rows, or to modify existing rows.
Source
insert into foo (other_columns) values ('a');
select * from foo;
1000000000|a
Trying to insert 11 digits makes the CHECK constraint fail.
insert into foo values (12345678901, 'a');
Error: CHECK constraint failed: foo
One alternative is to insert a "fake" row with the first valid id number immediately after creating the table. Then delete it.
create table foo(...);
insert into foo values (1000000000, 'a');
delete from foo;
Now you can insert normally.
insert into foo (other_columns) values ('b');
select * from foo;
1000000001|b
In fact the ID's length is 1, so it doesn't work.

INSERT OR IGNORE INTO doesn't work

I have a table created as:
create table association (_id integer unique primary key autoincrement , id_rules integer, id_places integer)";
To avoid replication of entry, I use the statement INSERT OR IGNOR, but it doesn't work. For example,
value (id_rules , id_places) = ("11","1") alredy in table, but using:
INSERT OR IGNORE INTO association (id_rules , id_places) VALUES ("11","1")
a new row is created.
Please, do anyone Know hwere is my mistake?
INSERT OR IGNORE will ignore any rows that would violate a UNIQUE constraint.
The only such constraint is on the _id column, which you did not specify.
If you want to prevent duplicates in those two columns, you have to add a constraint for them to the table definition:
CREATE TABLE association (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
id_rules INTEGER,
id_places INTEGER,
UNIQUE (id_rules, id_places)
);

How to check Duplicate items in sqlite phonegap

I am creating a table in sqlite .Then i insert data in table.Take example I have table A
Having column ID Name.I don't need any duplicate name .If i have already same name .I don't want to insert that name in my table .Mean i don't need duplicate items? How to do that?
Thanks
You can the specify the ID as "PRIMARY KEY" and Name as "UNIQUE". Setting ID as primary key this link helps. If the data base is already there and you want to you only distinct values you follow thislink
You can specify the column to be a "key" or declare the column to be "unique"
Here is an example:
CREATE TABLE myTable (
id INTEGER PRIMARY KEY,
num FLOAT NOT NULL,
cost FLOAT NOT NULL,
paid FLOAT NOT NULL,
date DATE NOT NULL,
UNIQUE(num));

Sqlite create unique pair of columns

I want to create a table with two columns: user_id, image_id.
I don't want user_id or image_id to be unique, but I also want to protect my table from duplicate pairs of same user_id and image_id. Can I do that?
Add a separate constraint for both columns:
CREATE TABLE MyTable(
user_id INTEGER,
image_id INTEGER,
[...],
UNIQUE(user_id, image_id)
)

Resources