Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

mysql - is it possible to allow delete query only if there is primary key in where clause?

My use-case is to delete a row from the table only with the unique key (or Primary key) to assure that the right data is getting deleted.

I want to prevent delete queries if there are fields other than the Primary key or unique key.

Let's say 'id' is the auto-incremented primary key in my table than,

delete from my_table where id = 10

This should work but,

delete from my_table where first_name = "John"

this should be blocked for all users of MySQL. can we achieve this just with MySQL? I know, I can add multiple checks with my backend language in my logic part but I want to check if it is possible to handle on MySQL part?

question from:https://stackoverflow.com/questions/65859645/is-it-possible-to-allow-delete-query-only-if-there-is-primary-key-in-where-claus

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your approach is flawed because I can trick MySQL into thinking that I am using a primary key like so -- DELETE FROM table WHERE id <> 0;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...