How can I make an int column unique - on an existing table with data in - but allow multiple nulls - in SSMS.
There are many records in the table now - and they all have NULL in this column now.
I have seen ways to do this using a unique filtered index in TSQL - and I can see ways in the UI to do it without allowing NULLS.
Is this just not possible using the SSMS GUI?
As an aside what's the best way to do this just using pure TSQL? A unique filtered index?
With code:
create unique nonclustered index uixf_mycol
on dbo.t (col)
where col is not null;
In SSMS:
create a new nonclustered index by right clicking the index folder from
the expanded table in object explorer
add the column
check the unique checkbox
add the where clause in the filter panel.
You can create UNIQUE constraint on the column.
CREATE TABLE Test
(
P_Id int,
CONSTRAINT uc_ID UNIQUE (P_Id)
)
Related
I need to add a new column and make it the primary key in SQL Server; the table in question does not yet have a unique key column.
Here is the sample table http://www.sqlfiddle.com/#!18/8a161/1/0
My goal is simply to have a column ID and insert values from 1 to 1160 (total number of records in this table) and make that the primary key. Also is there a way to automatically add the numbers from 1-1160 without adding each record one by one since there are 1000+ rows in this table?
Thank you!
Simply alter the table and add the column with the appropriate characteristics.
alter table x add id smallint identity(1,1) not null primary key;
Thrown together quickly and you probably should get in the habit of naming constraints. fiddle. I will note that you may or may not want to use an identity column - think carefully about your actual goal and how you want to continue using this table after the alteration.
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.
In SQL Server, I have created a view that contains two columns. a normal column and a calculated hash column. I need to create a unique constraint on these two columns. Trying to add a constraint or index causes an error because of the GetHash UDF.
CREATE VIEW HashView
WITH SCHEMABINDING
AS
SELECT p.ItemId, [dbo].[GetHash](p.Id) as PriceHash from dbo.price p
Is there a simple way to solve this or do I need to resort to using a trigger?
SQL Server is not willing to trust you that your CLR function is deterministic. And so only allows CLR computed columns to be indexed if persisted.
see: https://learn.microsoft.com/en-us/sql/relational-databases/indexes/indexes-on-computed-columns
make your view like this,
CREATE VIEW HashView
WITH SCHEMABINDING
AS
SELECT p.ItemId,p.Id, [dbo].[GetHash](p.Id) as PriceHash from dbo.price p
then create unique composite constraint on itemid and id as usual
CREATE UNIQUE INDEX IX_HashView ON dbo.HashView(itemid,id)
GO
I am using SQL Server 2012 and need to add a column with a unique primary key. I am about to load several hundred thousand records BULK and just discovered repetition in the field I was going to use. Have seen SEQUENCE and GUID. Need some guidance on the best choice and how to go about setting this up so that the key field is populated during the bulk load.
When you create your table in which you want to insert information create an IDENTITY column. That will serve as an auto-populating column with a unique number for each record.
Here is a link that might help you.
If you have already created your table just change this query to what suits to your table name and run it in order to add the new column you requested.
ALTER TABLE mytable
ADD COLUMN unique_id IDENTITY (1,1)
Just a slight update on what’s already posted that includes details for adding primary key constraint
alter table database.schema.table_t
add ID_column int identity(1,1)
primary key (ID_column)
If you already set the primary key on this table just go and remove it before you execute this statement.
What I'm trying to do is make a copy of a table using a SELECT INTO statement.
After the table is created I want to duplicate the indexes as well.
So the code I'm using is as follows:
SELECT * INTO TableCopy FROM Table
Then:
ALTER TABLE TableCopy ADD CONSTRAINT pkGUID PRIMARY KEY ([GUID])
CREATE INDEX ixIndexName ON TableCopy (CountryCode)
When I execute the index SQL, I get an error that the indexes already exist in the catalog. I didn't think index names had to be unique, I thought they could be duplicated across different tables.
And lo and behold if I create the indexes through management studio, it accepts the index names.
What am I missing here?
Thanks.
I didn't think index names had to be unique, I thought they could be duplicated across different tables.
No. They do have to be unique within a table/view.
When you execute within SSMS, it drops the existing index and creates a new one.
From CREATE INDEX (Transact-SQL) on MSDN:
index_name - Is the name of the index. Index names must be unique within a table or view but do not have to be unique within a database.
(emphasis mine)
However, pkGUID is not an index - it is a constraint, and these do have to be unique within a database.