Create a hash index results in error #1071 on Aria - indexing

I get an #1071 error on an Aria table with this:
ALTER TABLE `code`
ADD UNIQUE `ix_code_url_sha` (`url_sha`(64)) USING HASH;
The column is a sha() of a URL column.
How do I create a unique hash index on an Aria-based SHA-1 column?

You'll need a generated column and an index on it.
So from:
create table code (url varchar(160))
create the generated column and index:
alter table code add url_sha varbinary(32) as (unhex(sha2(url,256))) stored unique

Related

Alter a table which has non-unique non-clustered index to add a column

I have a table named Person which already have few non-unique non-clustered index and one clustered index (based on primary key).
I have to write two queries:
I need to alter the table to add column birthplace. Do I need to check any index while writing the alter table person add birthplace varchar(128) not null?
My next requirement is I have another column named cityCode narvar(10). This I need to include in my existing non-unique, non-clustered index. How should I write my script? should I drop my index and recreate it?
Please suggest. I am able to find some related questions, but not clear about non-unique non-clustered index.
alter table person add birthplace varchar(128) not null
Be aware when you specify not null without default value you will get error. It's recommended to add default value. But in case you can not consider any, then add column as nullable then update it later. At the end make it NOT NULL using below code:
alter table person Alter column birthplace varchar(128) not null
And if you want to involve new column to an existing index then use CREATE INDEX with DROP_EXISTING option. The performance is more efficient. To do so,
generate a create index script then modify script by adding new column to the column list of index and then add this at the end of CREATE INDEX statement.
WITH (DROP_EXISTING = ON)

SQL check constraint on column

I want to implement a constraint on a column to ensure that one particular value is only stored once in the column.
For example in a column values, I would like to have a constraint such that the value 0 can only be stored once.
How can I implement this in postgres?
Create partial unique index:
create table test(col int);
create unique index on test (col) where col = 0;

Assigning an index to a constraint

I have a script written for Oracle databases that I'm converting to work in SQL Server and I have two questions about a specific section of code.
In Oracle script I have this code:
CREATE UNIQUE INDEX "PK_PORTALROLES" ON "PORTAL_ROLE" ("ROLE_NAME");
ALTER TABLE "PORTAL_ROLE" ADD CONSTRAINT "PK_PORTALROLES"
PRIMARY KEY ("ROLE_NAME") USING INDEX ENABLE;
Question (1)
From the code above what is the USING INDEX command in the ALTER TABLE line doing? Is it assigning the UNIQUE INDEX created on the first line to the newly created CONSTRAINT or is the CONSTRAINT getting a new UNIQUE INDEX to use when it is created?
Question (2)
To duplicate this in SQL Server, I commented out the CREATE UNIQUE INDEX line like this:
--CREATE UNIQUE INDEX "PK_PORTALROLES" ON "PORTAL_ROLE" ("ROLE_NAME");
And then replaced the ALTER TABLE line with this:
ALTER TABLE "PORTAL_ROLE" ADD CONSTRAINT "PK_PORTALROLES" PRIMARY KEY ("ROLE_NAME");
I understand that when a PRIMARY KEY CONSTRAINT is created in SQL Server a UNIQUE INDEX is automatically made. So is the one line of SQL Server code directly above doing the same thing as the two lines of Oracle code above?
EDIT
One final question. Is there a way in Oracle and SQL Server to assign an existing INDEX to a CONSTRAINT?
In Oracle
If there are more than one indexes on the column on which you want to add PK constraint, we can selectively choose the index to be assoicated with the PK using “USING INDEX“. This clause can be used while:
Adding the PK constraint for the first time (using “ALTER TABLE” command).
CREATE TABLE tbl_test ( col_1 NUMBER,
col_2 NUMBER,
col_3 NUMBER);
CREATE INDEX idx_col_1_2 ON tbl_test(col_1, col_2);
CREATE INDEX idx_col_1_3 ON tbl_test(col_1, col_3);
CREATE UNIQUE INDEX idx_col_1 ON tbl_test(col_1);
-- Forcing oracle to use the unique index "IDX_COL_1"
ALTER TABLE tbl_test ADD CONSTRAINT tbl_test_pk PRIMARY KEY(col_1)
USING INDEX idx_col_1;
SELECT constraint_name, constraint_type, index_name
FROM user_constraints
WHERE table_name = 'TBL_TEST';
-- CONSTRAINT_NAME | CONSTRAINT_TYPE | INDEX_NAME
-- TBL_TEST_PK | P | IDX_COL_1
What if? If you don't use the USING INDEX clause
ALTER TABLE tbl_test ADD CONSTRAINT tbl_test_pk PRIMARY KEY(col_1);
-- Although an unique index exists, oracle has picked up the first index
SELECT constraint_name, constraint_type, index_name
FROM user_constraints
WHERE table_name = 'TBL_TEST';
-- CONSTRAINT_NAME | CONSTRAINT_TYPE | INDEX_NAME
-- TBL_TEST_PK | P | IDX_COL_1_2
That shows The index associated with the PK constraint needn’t be unique.
But in SQLServer implicitly CLUSTERED INDEX will be created when primary key defined on any column
So to your last question. In Oracle you can assign index which we created can be assigned to constraints which we will create in future.
In SQLServer i guess it is not possible.
In Oracle, you can do
alter table PORTAL_ROLE add constraint pk_portalroles primary key('ROLE_NAME');
And it will create unique index for you. But when you drop or disable(!) constraint, it will drop that index. To avoid that (or to give index some custom name, or to use non-unique index, etc), it is usually done in 2 steps as in your code (as recommended by ORACLE).
Q1: constraint will be using existing index.
Q2: Yes, it will be enough for SQL Server.
When you specify a unique constraint on one or more columns, Oracle implicitly creates an index on the unique key. If you are defining uniqueness for purposes of query performance, then Oracle recommends that you instead create the unique index explicitly using a CREATE UNIQUE INDEX statement. You can also use the CREATE UNIQUE INDEX statement to create a unique function-based index that defines a conditional unique constraint.

Error: Missing Right Parenthesis when Creating Index on Oracle

I am trying to create a table with an index but it's causing an ORA-00907 error which says I am missing a right parentheses. Here is my example sql that causes the error.
create table example
(
id number(12, 0) not null using index (create index example_idx on example(id))
);
Perhaps it is because of the not null keywords but I don't understand why it asks a right parenthesis.
create table and create index are separate statements, you can't mix them like that (although you can have an index implicitly or explicitly created to back up a unique or primary key constraint you define in-line).
You need to do this in two steps, as two separate statements:
create table example (id number(12, 0) not null);
create index example_idx on example(id);
The example you show from here:
CREATE TABLE a (
a1 INT PRIMARY KEY USING INDEX (create index ai on a (a1)));
is in a section titled 'Specifying the Index Associated with a Constraint', and
is creating the index as part of a primary key constraint. The using index clause is described here.
In your code you are created a not-null constraint, which is not backed by an index, so that clause is not valid here. You can only use this method of creating an index if it is to back a unique or primary key, as the link you provided says.

SQL: Create new column with default, unique value

I have added a new column, called Ordinal, to a table called Activity. The problem is that I gave it a UNIQUE constraint, set it to allow NULL (though this I won't want in the end.. I just needed to set it to that to get a little farther with the script), and did not give it a default value. I'm now running a RedGate SQL Compare script that was generated by comparing this table to a version of the Activity table that does not have the column. But I'm getting the following error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'iwt.Activity' and the index name 'IX_Activity'. The duplicate key value is (1).
So based on my research, it's trying to create a unique key constraint on the Ordinal column, but NULL is not unique. So my next step was to give it a unique value of 1 just to let the script pass. But 1 isn't going to be unique either. So, finally, my question:
Preferably in SQL Server Management Studio, how do I set a column as having a unique default value? Isn't that what I would need to create this constraint?
Thanks.
try this:
NULL will be the first constraint when you create the column.
UNIQUE will be as add constraint, you should add the second constraint.
they can run on this order with no problem (tested):
--first constraint
alter table Table_Name
add Column_Name int null
--second constraint
alter table Table_Name
add constraint Constraint_Name unique (Column_Name)
In my example :
PaymentGatewayHash is column
Cart is a table
--first query
alter table Cart
add PaymentGatewayHash NVARCHAR(20) null
--second query
CREATE UNIQUE INDEX PaymentGatewayHashUnique
ON Cart (PaymentGatewayHash)
WHERE PaymentGatewayHash IS NOT NULL
I just tested that :D