Click here to Skip to main content
15,668,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a scenario where i should restrict the data to be inserted in table where one column with the same value and other column with null . for ex :

I have the below table, i want to restrict the 3rd row to be inserted where Name column with the value A which exists already and Salary as Null. I want to write a constraint for the same in Database. Can some once pls help

Id Name age salary
1 A 30 NULL
2 B 32 5000
3 A 30 NULL

What I have tried:

Thorugh code we can restrict the data to inserted with the values but through DB we would need it
Updated 29-Apr-21 23:42pm

1 solution

It sounds like you need to set up a unique constraint. See Create Unique Constraints - SQL Server | Microsoft Docs[^] for SQL Server or Oracle Unique Constraint: Ensures Unique Data Contained in a Column[^] for Oracle
Share this answer
CHill60 30-Apr-21 7:07am    
Should have added - don't forget to handle the exception that is raised when you try to add a duplicate row. Can't give more detail than that without knowing what language you use and how you insert the records

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900