SQL Unique Constraint on Subset of data in table [duplicate] - sql-server

This question already has answers here:
conditional unique constraint
(7 answers)
SQL can I have a "conditionally unique" constraint on a table?
(5 answers)
Partial value constraint of sql server table
(1 answer)
Closed 11 months ago.
I have a SQL Server table tbl_Submissions which contains a number of records for any given CaseID. Each Submission has an AuthorisedStatus field which can be "Authorised", "Rejected", "Rescinded". There could be multiple rejected or rescinded submissions for a CaseID, but there can only ever be one authorised record.
Is it possible to add a constraint or index to enforce this?

It could be implemeneted using filtered index:
CREATE UNIQUE INDEX udx ON tbl_Sumbissions(CaseID)
WHERE AuthorisedStatus = 'Authorised'

Related

How do I create a table that will accept members to register their shops numbers as alpa-numeric as end-user [duplicate]

This question already has answers here:
How to generate unique incremental employee code(pattern: E0001, E0002, E0003, ... ) in the INSERT stored procedure in SQL Server?
(4 answers)
Closed 8 months ago.
I am building this project with SQL Server.
I need to create a Shop table, this shop (numbers) will include alphanumeric as the shop's identification number.
How do I create a table with alphanumeric primary key instead of using INT as data type? And will this table still increment like when using INT for the primary key?
As for the data type, you can use "nvarchar" to be able to deal with letters and numbers, for increment you have to do it manually (either in code or in a function in the database)
You can use guid and below is how you define your primary column.
CREATE TABLE table_name
(
id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY
);
Read more on guid here

Using alphanumeric instead of INT in my primary key table. The table ID is going to be including A1, A2 or B1,C1 [duplicate]

This question already has answers here:
How to generate unique incremental employee code(pattern: E0001, E0002, E0003, ... ) in the INSERT stored procedure in SQL Server?
(4 answers)
Closed 7 months ago.
I am building this project with SQL Server.
I need to create a Shop table, this shop (numbers) will include alphanumeric as the shop's identification number.
How do I create a table with alphanumeric primary key instead of using INT as data type? And will this table still increment like when using INT for the primary key?
As for the data type, you can use "nvarchar" to be able to deal with letters and numbers, for increment you have to do it manually (either in code or in a function in the database)
You can use guid and below is how you define your primary column.
CREATE TABLE table_name
(
id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY
);
Read more on guid here

Error adding unique constraint: Column in table is of a type that is invalid for use as a key column in an index [duplicate]

This question already has answers here:
SQL Server 2005 How Create a Unique Constraint?
(10 answers)
Closed 7 years ago.
If I already have a primary key on the column Id and I have a column Name on which I want to add a Unique constraint, how can I do that in SQL Server?
I tried the following:
ALTER TABLE dbo.Hotels ADD CONSTRAINT
UNQ_NAME UNIQUE NONCLUSTERED
(
Name
)
Error:
Msg 1919, Level 16, State 1, Line 1
Column 'Name' in table 'Hotels' is of a type that is invalid for use as a key column in an index.
Name is represented as:
Name nvarchar(MAX) Allow Nulls
The syntax for this is:
ALTER TABLE <tablename> ADD CONSTRAINT
<constraintname> UNIQUE NONCLUSTERED
(
<columnname>
)

How to alter column with primary key and identity [duplicate]

This question already has answers here:
SQL Server add auto increment primary key to existing table
(16 answers)
Closed 8 years ago.
How to alter column in the existing table for the primary key and identity. I tried below query but showing Incorrect syntax near the keyword 'identity'.
alter table IAM_Software_Licence_Master
alter column SoftwareLicId int identity(1,1) primary key
How can I achieve this..
Its difficult to say what the issues are with changing this on your end as I do not know the table structure or whether or not this is used as a foreign Key. But the below link should give you everything you need:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/04d69ee6-d4f5-4f8f-a115-d89f7bcbc032/how-to-alter-column-to-identity11

Remove auto increment By Script SQL Server [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL Server how to drop identity from a column
How I alter a Table by script to remove autoindenty of a column?
Add new NON-autoincrementing column of the same type with default constraint to your identity column
drop default constraint
drop identity column
rename new column to old one with sp_rename

Resources