How to create unique constraints on AgensGraph - agens-graph

I want to use "id" property as primary key on label.
agens=# create vlabel v;
CREATE VLABEL
agens=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
agens=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
But, I don't know grammar for create constraint.
How to create unique constraints on AgensGraph.

Use unique property index on AgensGraph.
agens=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
agens=# create unique property index on v(id);
CREATE PROPERTY INDEX
agens=# create (:v{id:1});
ERROR: duplicate key value violates unique constraint "v_id_idx"
DETAIL: Key ((properties.'id'::text))=(1) already exists.

Related

Why does violation of PRIMARY KEY constraint return error code 2627 not 2601 in SQL Server?

I am confused for a while since the Document point out:
When you create a PRIMARY KEY constraint, a unique clustered index on
the column or columns is automatically created if a clustered index on
the table does not already exist and you do not specify a unique
nonclustered index. The primary key column cannot allow NULL values.
I have a table in SQL server with a PRIMARY KEY constraint. According to the above point,a unique clustered index on the column or columns is automatically created since i did't create any clustered in the table.
I learned 2601 Cannot insert duplicate key row in object '%.*ls' with unique index '%.*ls' from Database Engine Errors.
My question is that why SQL server return error code 2627 and not 2601 when I try to insert duplicate value in primary key column into my table which have a unique clustered index on primary key? Is it because 2627 has a higher priority than 2601 or what?
Can someone please give me some advice or help? Thanks.
A Primary Key, at least on SQL Server, is a type of Constraint. As a result when you create a Primary Key it is both a (unique) Index and a Constraint. Both error 2627 and 2601 have the same severity, so it appears that SQL Server will return the higher error code (as both a unique index and constraint were violated).
From testing, you'll only get the error 2601 is the column has a unique index that is violated, but does not have a constraint. Most likely, therefore, you'll see this on a conditional unique index.
Take the below examples:
USE Sandbox;
GO
--First sample table with primary key (Clustered)
CREATE TABLE dbo.TestTable1 (ID int PRIMARY KEY);
GO
--inserts fine
INSERT INTO dbo.TestTable1
VALUES(1);
GO
--Errors with code 2627
INSERT INTO dbo.TestTable1
VALUES(1);
GO
--Create second sample table, with unique Constraint
CREATE TABLE dbo.TestTable2(ID int,
CONSTRAINT U_ID UNIQUE(ID));
GO
--Inserts fine
INSERT INTO dbo.TestTable2
VALUES(1);
GO
--Errors with code 2627
INSERT INTO dbo.TestTable2
VALUES(1);
GO
--Create third sample table
CREATE TABLE dbo.TestTable3(ID int);
--Create unique index, without Constraint
CREATE UNIQUE INDEX ID_UX ON dbo.TestTable3(ID);
GO
--Inserts fine
INSERT INTO dbo.TestTable3
VALUES(1);
GO
--Errors with code 2601
INSERT INTO dbo.TestTable3
VALUES(1);
GO
--Clean up
DROP TABLE dbo.TestTable1
DROP TABLE dbo.TestTable2
DROP TABLE dbo.TestTable3
Note that only the last insert fails with error 2601; the other 2 fail with 2627.

SQL Server - Unique index vs Unique constraint - Re. Duplicate values

A unique index ensures that the values in the index key columns are unique.
A unique constraint guarantees that no duplicate values can be inserted into the column(s) on which the constraint is created. When a unique constraint is created a corresponding unique index is automatically created on the column(s).
Qusetions:
Can duplicate values be inserted if we have a unique index on a column and no unique constraint?
What about existing duplicates in any on the column - will it allow to create unique index or unique constraint?
Can duplicate values be inserted if we have a unique index on a column
and no unique constraint?
Generally, duplicate values cannot be inserted and an error is raised when a unique index exists on the column. The exceptions are:
Index was created with the IGNORE_DUP_KEY option. No error is raised and the insert is ignored.
The non-clustered index is filtered such that the duplicate value does not satisfy the index WHERE clause. The row is inserted but not reflected in the non-clustered index.
What about existing duplicates in any on the column - will it allow to
create unique index or unique constraint?
No, with the exception of the filtered index mentioned above.
Can duplicate values be inserted if we have a unique index on a column and no unique constraint?
No, the values of the columns within the index must create a unique set of data within that index.
What about existing duplicates in any on the column - will it allow to create unique index or unique constraint?
No, you cannot create a Unique Index on a table that has duplicate values.
This easiest way to have found this out would be to try (I suggest for things like that doing so, it's a great way of learning):
CREATE TABLE dbo.SomeTable (SomeInt int, AnotherInt int);
GO
INSERT INTO dbo.SomeTable (SomeInt,
AnotherInt)
VALUES (1,1),
(1,2),
(2,1);
GO
--Create a unique index on a column with duplicate values
CREATE UNIQUE INDEX UQ_SomeInt ON dbo.SomeTable(SomeInt); --fails
GO
--Create a unique index on the 2 columns, as they are unique
CREATE UNIQUE INDEX UQ_Some_AnotherInt ON dbo.SomeTable(SomeInt, AnotherInt); --Succeeds
GO
--Try to insert a duplicate value
INSERT INTO dbo.SomeTable (SomeInt,
AnotherInt)
VALUES(2,1); --fails
GO
SELECT *
FROM dbo.SomeTable
GO
DROP TABLE dbo.SomeTable;
One potentially unintuitive scenario that confused me at first: postgres does not treat NULL values as equal. If your table looked like this:
+-------+-------+-------+
|id |a |b |
+-------+-------+-------+
|1 |0 |NULL |
|2 |0 |NULL |
+-------+-------+-------+
You could still add a unique index on columns a and b. According to Postgres, row with id 1 and row with id 2 have the same value for column a, but different values for column b

How to drop "GRAPH" on AgensGraph?

I tried to drop graph. An error occurred when dropping graph.
# drop graph graph;
ERROR: cannot drop graph graph because other objects depend on it
DETAIL: sequence graph.ag_label_seq depends on schema graph
label ag_vertex depends on schema graph
label ag_edge depends on schema graph
HINT: Use DROP ... CASCADE to drop the dependent objects too.
How to drop "GRAPH"?
When dropping graph, all data & schema must be drop before.
For convenience, optional "CASCADE" clause is usable.
# drop graph graph cascade;
NOTICE: drop cascades to 3 other objects
DETAIL: drop cascades to sequence graph.ag_label_seq
drop cascades to label ag_vertex
drop cascades to label ag_edge
DROP GRAPH

how to define full text searching on table with two keys?

I have a table with 2 columns as primary key like below.
create table table1(key1 int NOT NULL,key2 int NOT NULL,content NVARCHAR(MAX), primary key(key1,key2))
I have created index on table with this query
CREATE unique INDEX index1 ON table1 (key1,key2);
and with this query, I create full-text searching
create fulltext index on table1 (content ) key index index1;
but I get this error because index must be single-column
'index1' is not a valid index to enforce a full-text search key. A full-text search key must be a unique, non-nullable, single-column index which is not offline, is not defined on a non-deterministic or imprecise nonpersisted computed column, does not have a filter, and has maximum size of 900 bytes. Choose another index for the full-text key.
and with single Column indexing, when I insert a new row I get a duplicate error.
what should I do?
I am using SQL Server and EF orm
Update
i solve this problem by creating a computed column that return unique data
ALTER TABLE Table1 ADD indexKey AS cast(key1 as float) + cast((cast(key2 as float)/power(10,len(key2))) as float) PERSISTED not null
and i create my index on this column and it work pretty fine.

Can't delete primary key on table with FULLTEXT index

I have a table with a primary key that is an int data type. I want to drop this column (as it is unused, and I fear this column may reach the maximum limit of the int data type, so we may as well drop it.).
First, I could not drop I tried to first drop the constraint with:
ALTER TABLE dbo.MyTable DROP CONSTRAINT PK_MyTableID
I'm getting the error:
Cannot drop index 'PK_MyTableID' because it enforces the full-text key for table or indexed view 'MyTable'.
I don't understand this error, because the primary key is an int, and I don't think this table has a FULLTEXT index, but if it does, I don't need it.
EDIT:
I was able to drop the column after deleting the FULLTEXT index:
DROP FULLTEXT INDEX ON dbo.MyTable
I believe there is a full text index on the table. A full text index requires you to have unique key:
From MSDN: KEY INDEX index_name
Is the name of the unique key index on table_name. The KEY INDEX must be a unique, single-key, non-nullable column. Select the smallest unique key index for the full-text unique key. For the best performance, we recommend an integer data type for the full-text key.
You can check for a tables full text indexes using:
SELECT object_id, property_list_id, stoplist_id FROM sys.fulltext_indexes
where object_id = object_id('myTable');

Resources