I have a CustomerID column and an EffectiveDate column in a table.
I need the combination of these two to be unique.
However, I already have a primary key on an auto-numbered integer column.
What is a good way to accomplish my goal?
Thanks
Simply add a unique constraint:
Alter Table TableName
Add Constraint UC_TableName_Col1Col2 Unique ( Col1, Col2 )
SQL Server creates a unique index when you create a unique constraint. If there is already a clustered index, then the above will create that index as nonclustered. However, you can be explicit like so:
Alter Table TableName
Add Constraint UC_TableName_Col1Col2 Unique Nonclustered ( Col1, Col2 )
CREATE UNIQUE INDEX Some_Index_Name ON My_Table (CustomerID, EffectiveDate)
Try creating a UNIQUE index on the two columns.
CREATE TABLE Example
(Col1 int NOT NULL,
Col2 int NOT NULL,
UNIQUE (Col1, Col2)
)
Example taken from this thread.
CREATE TABLE MyTable
(
<columns here>
CONSTRAINT U_ConstraintName UNIQUE (CustomerID, EffectiveDate)
)
Related
I'm trying to combine two tables together with a third that is comprised of a single column that is a combination of the two tables primary key column. I've considered using substring to separate the column into parts that can be compared to either table's key column but I might be making it more difficult.
The problem here is your design; you need to fix it. You are storing a delimited value in your column, combinedcode, in your junction table.
What you should be doing is storing the 2 values in separate columns, and then creating your foreign keys on those values. This would look something like this:
CREATE TABLE dbo.Table1 (SomeID varchar(10) NOT NULL,
SomeValue varchar(20));
ALTER TABLE dbo.Table1 ADD CONSTRAINT PK_Table1 PRIMARY KEY (SomeID);
GO
CREATE TABLE dbo.Table2 (OtherID varchar(10) NOT NULL,
OtherValue varchar(20));
ALTER TABLE dbo.Table2 ADD CONSTRAINT PK_Table2 PRIMARY KEY (OtherID);
GO
CREATE TABLE dbo.JunctionTable (SomeID varchar(10) NOT NULL,
OtherID varchar(10) NOT NULL);
ALTER TABLE dbo.JunctionTable ADD CONSTRAINT FK_JunctionTable1 FOREIGN KEY (SomeID) REFERENCES dbo.Table1(SomeID);
ALTER TABLE dbo.JunctionTable ADD CONSTRAINT FK_JunctionTable2 FOREIGN KEY (OtherID) REFERENCES dbo.Table2(OtherID);
Depending on your design, you may want to make it so that the value in the junction table are are unique:
ALTER TABLE dbo.JunctionTable ADD CONSTRAINT PK_JunctionTable PRIMARY KEY (SomeID,OtherID);
Then, to do your JOINs it would be as simple as:
SELECT {Your Columns}
FROM dbo.Table1 T1
JOIN dbo.JunctionTable JT ON T1.SomeID = JT.SomeID
JOIN dbo.Table2 T2 ON JT.OtherID = T2.OtherID;
You mase use join with LIKE operator.
Something like this for the first table:
combinedcode LIKE '%' + purchasecode
And that for the second
combinedcode LIKE receiptcode + '%'
Although this code may not be good in performance. If you can you should change your data model
I have a table which has a father_id column and a serial_number column,
I want to create a unique constraint on the serial number by the father id.
Meaning rows that have the same father_id can't have the same serial_number, but rows that have a different father_id can have the same serial_number.
To rephrase the requirement, you want the combination of father_id and serial_number to be unique. Once the requirement is phrased like that, it's easier to translate to SQL:
ALTER TABLE mytable
ADD CONSTRAINT mytable_unq UNIQUE (father_id, serial_number);
This looks like a compound unique key. Here is one way to do it with a unique index:
create unique index myindex on mytable(father_id, serial_number);
Or in a create table statement, with a unique constraint:
create table mytable (
father_id int,
serial_number int,
constraint myconstraint unique (father_id, serial_number)
);
I have two tables, one with columns accountid, category, code, name, desc and the other with columns id, accountid, code, reason, desc, active.
Is there any way I can create a constraint such that code in the second table contains only values of code that are in first table with condition where category is somevalue?
One method is a little trick. First, create a unique index on (category, code) in the first table:
create unique index unq_table1_category_code on table1(category, code)
Then create a computed column in table2:
alter table table2 add category as ('somevalue') persisted;
Then create the foreign key relationship:
alter table table2 add constraint fk_somevalue_code
foreign key (category, code) references table1(category, code);
ALTER TABLE table2 WITH CHECK
ADD CONSTRAINT CK_table2category CHECK (category = 'somevalue'),
CONSTRAINT FK_table2code FOREIGN KEY (code) REFERENCES table1 (code);
I was curious if it was possible to take data from a table, and duplicate it but assign a new primary key
for example, I wish to take data that has a column "question_id" which acts as the unique key for the table, and copy all of the data from the table with that question_id to the same table but with a new question_id.
any thoughts as to if this is possible using SQL?
my database is an ingres database
thanks in advance
Sure, something like this should work:
INSERT INTO YourTable (Question_Id, OtherField,...)
SELECT SomeNewQuestionId, OtherField,...
FROM YourTable
WHERE Question_Id = SomeQuestionId
Just replace SomeQuestionId and SomeNewQuestionId with the appropriate values.
It's a simple select query.
insert into mytable
(field2, field3, etc)
select field2, field3, etc
from mytable
where whatever.
This assumes that neither fields 2 nor 3 are the primary key, and that you have an autoincrement table.
Fast forward two years.... :)
I think this is the best and simplest way to do this. Inserting a row of data from the same table with a primary key will result in error because primary keys are unique for each row: Let question_id = 100.
INSERT INTO MyTable SELECT * FROM MyTable Where question_id=100;
In PostgreSQL:
ERROR: duplicate key value violates unique constraint "MyTable_pkey"
DETAIL: Key (question_id)=(100) already exists.
It is very simple to avoid the duplicate key value:
INSERT INTO MyTable SELECT (SELECT MAX(question_id)+1),Column1,Column2,etc FROM MyTable Where question_id=100;
By using MAX(question_id)+1, you are incrementing the maximum value of MyTable's question_id primary key, then add/copy the data to a new row with a unique question_id value.
Is it possible to have absolute value as a constraint in sql 2008. Something like this:
ALTER TABLE [dbo].[myTable] WITH NOCHECK ADD
CONSTRAINT [IX_Blah] UNIQUE NONCLUSTERED
(
ABS([ID_1]),
[ID_2]
) ON [PRIMARY]
In my table ID_1 can be negative. So I need make sure I don't have records with
ID_1 ID_2
1 1
-1 1
They should be considered the same and not be allowed.
Thank you.
I'm assuming you've tried it and got an error.
No, unique constraints cannot include formulas. You can however, have a unique constraint on a computed column:
ALTER TABLE myTable
ADD ID_3 AS ABS(ID_1)
ALTER TABLE [myTable]ADD
CONSTRAINT [IX_Blah] UNIQUE NONCLUSTERED
(
[ID_3],
[ID_2]
)