Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index: Avoid duplicates in table when Status = 'S'

Is there a way of avoid duplicate rows only when status = 'S'? Example:

table showing what I need to do.

I tried to create an index "unique" but when status = 'N' it should allow duplicate, then it did not work correctly.

Any sugestions to avoid duplicates in SQL?

like image 514
Ian Grinkraut Avatar asked Jun 13 '19 20:06

Ian Grinkraut


People also ask

How do you avoid duplicates in a table?

Set a field's Indexed property to Yes (No duplicates) Select the field that you want to make sure has unique values. In the Field Properties pane at the bottom of the table design view, on the General tab, set the Indexed property to Yes (No duplicates). Save the changes to your table.

How do I ignore duplicate records in SQL while selecting query?

If you want the query to return only unique rows, use the keyword DISTINCT after SELECT . DISTINCT can be used to fetch unique rows from one or more columns. You need to list the columns after the DISTINCT keyword.

How do you avoid including duplicate values in a query result?

The go to solution for removing duplicate rows from your result sets is to include the distinct keyword in your select statement. It tells the query engine to remove duplicates to produce a result set in which every row is unique.

How do I restrict duplicate entries in MySQL?

Note − Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.


1 Answers

Yes, you could use filtered index:

CREATE UNIQUE INDEX UIX_name ON tab(fk_client) WHERE status = 'S';
like image 125
Lukasz Szozda Avatar answered Oct 25 '22 17:10

Lukasz Szozda