Skip to content

Instantly share code, notes, and snippets.

@annejohnson
annejohnson / conditional_drop_pk.sql
Last active March 9, 2023 00:59
Conditionally drop a primary key from a table in MySQL (v. 5.6)
-- Temporarily change delimiter to $$
DELIMITER $$
-- Drop procedure if it exists
DROP PROCEDURE IF EXISTS drop_pk_if_exists $$
-- Begin create procedure
CREATE PROCEDURE drop_pk_if_exists () BEGIN
-- If my_table has a primary key
IF EXISTS(select * from `information_schema`.`table_constraints`
where `constraint_schema` = database()
and `constraint_type` = "PRIMARY KEY"