0

I wasn't sure if this is something that can be done in a CHECK constraint or if this requires a Stored Procedure for validating during insert, but can a constraint be created to allow only X amount of duplicates in a column, where X > 1?

Something along the lines of:

CONSTRAINT limiter_chk CHECK (SELECT COUNT(Id) AS Ct FROM Mytable WHERE Id = Id AND Count < 50)

I'm aware the the above isn't valid, but just trying to show the type of constraint I'm describing.

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
  • 1
    Currently MySQL [parses but ignores](http://stackoverflow.com/q/2115497/3404097) CHECK constraints. Most DBMSs allow [only columns of a table in a condition in a CHECK constraint](http://stackoverflow.com/q/6195881/3404097). – philipxy Mar 17 '16 at 00:13
  • Recently learned that MySQL ignores the CHECK constraints (shortly after after I posted this) That's unfortunate. – JNYRanger Mar 17 '16 at 13:15

1 Answers1

1

Business logic should be in the application, not the database.

However, you probably could write a TRIGGER to do what you are asking for.

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • I had a feeling this was the answer, but couldn't find anything definitive online. Thanks for the confirmation. – JNYRanger Mar 17 '16 at 13:14