I need to modify the following constraint so that it doesnt apply to negative AMOUNTS. Is that possible? I've played around with the syntax in the SSMS for a while now and it hasn't likeed anything I've come up with.
ALTER TABLE Payments
ADD CONSTRAINT Unique_Payment_2021 UNIQUE (InvoiceID, Amount, TransDate, TransTime);
FYI, this is happening on a 2012 SQL server.
thanks
Harry
A unique constraint is implemented using a unique index. You can get the same functionality using a filtered index:
create unique index unq_payment_2021 on
payments(InvoiceID, Amount, TransDate, TransTime)
where amount >= 0;
One slight difference is the error message that you get when the condition is violated. In one case, it refers to a unique constraint and in another to a unique index.
Here is a db<>fiddle.
Related
I have a customer table and an order table.
An index should be added that will not allow duplication of customer order number according to the customer
You can create a duplicate order number as long as it is for 2 or more different customers
How can such a thing be created?
Does it eliminate Identity specification ??
You can use Unique Constraint as allmhuran mentioned, also you can Create unique index on multiple columns to do that:
CREATE NONCLUSTERED UNIQUE INDEX IX_Order_Customer ON Order (CustomerId, OrderNo)
This sounds like it might be homework.
Your answer will look something like alter table order add constraint UniqueOrderPerCustomer unique(customer, orderNumber). Study Creating Unique Constraints
I have a table in Oracle 11g such as with below columns.
COL1_STATUS
COL2_ID
COL3_TYPE
COL4_DATE
I want to create a UNIQUE constraint combining all 4 columns but only when COL1_STATUS = 10
How can I do that? Table is already created so I am looking for only ALTER command.
Also, I have searched and found similar question where it is suggested to use unique index but I want to achieve this by constraint.
Conditional unique constraint with multiple fields in oracle db
Thanks in advance.
A unique index and a constraint are essentially the same thing. A unique constraint is implement using a unique index. So this really should do what you want:
create unique index idx_table_4 on
table(case when status = 10 then id end,
case when status = 10 then type end,
case when status = 10 then date end);
In fact, this is how the documentation recommends implementing a unique constraint:
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. See "Using a Function-based Index to
Define Conditional Uniqueness: Example" for more information.
I'm trying to load the below table using SQL. In my Ldirectory table I have the combination of SY and LIDENTIFIER as primary key and I'm trying to run this query
insert into S_User.LDIRECTORY (SY,LIDENTIFIER,ONAME,TELNUMBER)
select 2013,D.CODE, D.NAME, D_YEAR.PHONE_NUMBER
from WHS.D WHS.D_YEAR
where WHS.D.D_KEY=WHS.D_YEAR.D_KEY
and the error that I'm receiving is:
SQL Error ORA-00001 unique constraint violated
Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
Action: Either remove the unique restriction or do not insert the key.
How can I solve this issue? I think during the insertion its treating the SY column as primary key itself but I have just one same value for that column.
The error message should indicate the name of the unique constraint that was violated. Assuming the constraint in question is the primary key rather than some other unique constraint defined on the table and assuming that the primary key constraint is a composite constraint defined on the combination of SY and LIDENTIFIER as you indicate, that implies that your query is returning duplicate rows.
The query you are running returns a hard-coded value of 2013 for SY. So you would expect the constraint to be violated if there are any two rows in the result where D.CODE are the same. Are you certain that you expect D.CODE to be unique across the entire result set? It's hard to guess based on the names of the objects (no idea what D might represent, no idea why D_YEAR would have a PHONE_NUMBER column, etc) but I would tend to guess that one row in D might map to multiple rows in D_YEAR in which case there would be multiple rows in the result that had the same D.CODE value thus violating the constraint.
If you are convinced that the query should not return any two rows with the same D.CODE value, you could use DML error logging to write the rows that violate the constraint to an error table so that you could analyze the problem.
I am trying to add a new unique index on one of my database tables in SQL Server 2008. This is an existing table and the column where I want the unique index already has some duplicate values.
Can I set up a unique index for that column? If so, how?
You can't set this column up with a UNIQUE index if the table already has duplicate values, unless you remove the records containing the duplicate values for that column. This goes to the definition of UNIQUE.
First you are gonna need to delete the duplicate values on your column and then you can create a unique index on it. So lets assume your table has 2 columns, id and column1. To delete duplicate values you need to choose one, it can be random or with some order. So it would be like this:
WITH CTE AS
(
SELECT *, ROW_NUMBER() OVER(PARTITION BY column1 ORDER BY Id) Corr
FROM YourTable
)
DELETE FROM CTE
WHERE Corr > 1
CREATE UNIQUE INDEX I_Unique ON YourTable(Column1)
No as the name suggest, Unique Index which says key has to be unique. So you cant
See this
If the column already has duplicate values then I would recommend you create a unique composite key instead.
e.g.
So, to handle that issue with this table design, you need to create a unique constraint on the table CustomerID/ProductID columns:
create unique index cust_products_unique on CustomerProducts (CustomerID, ProductID)
So that in essence a combination of fields ensures that the index is unique.
Regards
May not have been true in SQL Server 2008, however you can use Management Studio to do this in later versions such as 2014.
Right click your table
Choose Design
Expand "Identity Specification" and set (is Identity) to Yes
Save
I am attempting to replace all records for a give day in a certain table. The table has a composite primary key comprised of 7 fields. One such field is date.
I have deleted all records which have a date value of 2/8/2010. When I try to then insert records into the table for 2/8/2010, I get a primary key violation. The records I am attempting to insert are only for 2/8/2010.
Since date is a component of the PK, shouldn't there be no way to violate the constraint as long as the date I'm inserting is not already in the table?
Thanks in advance.
You could have duplicates in the data you are inserting.
Also, it is a very, very, very poor practice to have a primary key that consists of 7 fields. The proper way to handle this is to havea surrogate identity key and a unique index on the seven fields. Joining to child tables on 7 feilds is a guarantee of poor performance and updating records when they have child records becomes a nightmare and can completely lock up your system. A primary key should be unique adnit should NEVER change.
Do all the rows have only a date component in that field (i.e. the time is always set to midnight: 00:00:00)? If not, you'll need to delete the rows >= 2/8/2010 and < 2/9/2010.
Also, are you sure you're not accidentally trying to insert two records with the same date (and same values in the other 6 PK fields)?
Perhaps there's something going on here you're not aware of. When you insert a row and get a primary key violation, try doing a SELECT with the appropriate key values from the row which could not be inserted (after doing a ROLLBACK, of course) and see what you get. Or perhaps there's a trigger on the table into which you're inserting data that is inserting rows into another table which uses the same primary key but was not cleaned out.
You might try the following SELECT to see what turns up:
SELECT *
FROM YOUR_TABLE
WHERE DATE > 2/7/2010 AND
DATE < 2/9/2010;
(Not sure about the proper format for a date constant in SQL Server as I haven't used it in a few years, but I'm sure you get the idea). See what you get.
Good luck.