From 11da087c64f7c3773095e22bd739bd5f089c9d67 Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Tue, 24 Jun 2025 08:06:35 -0700 Subject: [PATCH 1/9] Create example-sql-statements-that-generate-error-message.sql --- ...statements-that-generate-error-message.sql | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql new file mode 100644 index 00000000..aa7b2d49 --- /dev/null +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql @@ -0,0 +1,54 @@ +//Correct syntax +ALTER TABLE specification +ADD CONSTRAINT specification_program_id_fkey +FOREIGN KEY (program_id) +REFERENCES program (id); + +//Incorrect syntax; jumbled syntax +ALTER TABLE specification +ADD FOREIGN KEY specification_program_id_fkey +CONSTRAINT(program_id) +REFERENCES program(id); + +//Incorrect syntax; wrong punctuation +ALTER TABLE specification ( +ADD CONSTRAINT + specification_program_id_fkey + FOREIGN KEY(program_id) + REFERENCES program (id) +); + +//Incorrect syntax; a reserved word +ALTER TABLE specification +ADD CONSTRAINT foreign +FOREIGN KEY (program_id) +REFERENCES program (id); + +//Incorrect syntax, misspelled keyword REFERENCES +ALTER TABLE specification +ADD CONSTRAINT specification_program_id_fkey +FOREIGN KEY(program_id) +REFERENCE program (id); + +//Incorrect syntax, missing closing parenthesis +ALTER TABLE specification +ADD CONSTRAINT specification_program_id_fkey +FOREIGN KEY(program_id +REFERENCES program (id); + +//Incorrect syntax; quotes +ALTER TABLE 'specification' +ADD CONSTRAINT specification_program_id_fkey +FOREIGN KEY('program_id') +REFERENCES program ('id'); + +//Correct syntax; backticks +ALTER TABLE `specification` +ADD CONSTRAINT specification_program_id_fkey +FOREIGN KEY(`program_id`) +REFERENCES program (`id`); + +//Incorrect syntax, missing required elements +ALTER TABLE specification +ADD CONSTRAINT specification_program_id_fkey +FOREIGN KEY(program_id); From 3c3bda235ae1e1821485d301be6e9f4faa475015 Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Tue, 24 Jun 2025 08:10:59 -0700 Subject: [PATCH 2/9] Create list-constraint.sql --- .../list-constraint.sql | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql new file mode 100644 index 00000000..4111553b --- /dev/null +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql @@ -0,0 +1,7 @@ +SELECT + TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME +FROM + INFORMATION_SCHEMA.KEY_COLUMN_USAGE +WHERE + REFERENCED_TABLE_SCHEMA = (SELECT DATABASE()) AND + REFERENCED_TABLE_NAME = 'PROGRAM' \G From cb1051f85887264fcf38c5a77b11c8bf095ca432 Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Tue, 24 Jun 2025 08:11:42 -0700 Subject: [PATCH 3/9] Create drop-constraint.sql --- .../drop-constraint.sql | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql new file mode 100644 index 00000000..4a037713 --- /dev/null +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql @@ -0,0 +1,2 @@ +ALTER TABLE specification +DROP FOREIGN KEY specification_program_id_fkey; From 4f8342560b18fdd5380c9975feb8d80f7500dc1d Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Tue, 24 Jun 2025 08:33:34 -0700 Subject: [PATCH 4/9] Update example-sql-statements-that-generate-error-message.sql --- .../example-sql-statements-that-generate-error-message.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql index aa7b2d49..703bb594 100644 --- a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql @@ -37,10 +37,10 @@ FOREIGN KEY(program_id REFERENCES program (id); //Incorrect syntax; quotes -ALTER TABLE 'specification' +ALTER TABLE "specification" ADD CONSTRAINT specification_program_id_fkey -FOREIGN KEY('program_id') -REFERENCES program ('id'); +FOREIGN KEY("program_id") +REFERENCES program ("id"); //Correct syntax; backticks ALTER TABLE `specification` From 90f0c3719b2c2b744930212f79a7c278ab527084 Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Thu, 26 Jun 2025 07:09:07 -0700 Subject: [PATCH 5/9] Update list-constraint.sql --- .../list-constraint.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql index 4111553b..1b39c18b 100644 --- a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/list-constraint.sql @@ -1,5 +1,6 @@ +# List the FK constraints that reference the PROGRAM table SELECT - TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME + TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE From 33bc33a98bfbe24f976baf5fea71243f058a8c91 Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Thu, 26 Jun 2025 07:09:41 -0700 Subject: [PATCH 6/9] Update drop-constraint.sql --- .../drop-constraint.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql index 4a037713..be0915c1 100644 --- a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql @@ -1,2 +1,3 @@ +-- Drop the constraint so that we can add it back ALTER TABLE specification DROP FOREIGN KEY specification_program_id_fkey; From 9b5c9f1d11c4f729d79d4b6a3bc2ad2bf7d3c5cd Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Thu, 26 Jun 2025 07:10:49 -0700 Subject: [PATCH 7/9] Update example-sql-statements-that-generate-error-message.sql --- ...-statements-that-generate-error-message.sql | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql index 703bb594..6159ae09 100644 --- a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql @@ -1,16 +1,16 @@ -//Correct syntax +# Correct syntax ALTER TABLE specification ADD CONSTRAINT specification_program_id_fkey FOREIGN KEY (program_id) REFERENCES program (id); -//Incorrect syntax; jumbled syntax +# Incorrect syntax; jumbled syntax ALTER TABLE specification ADD FOREIGN KEY specification_program_id_fkey CONSTRAINT(program_id) REFERENCES program(id); -//Incorrect syntax; wrong punctuation +# Incorrect syntax; wrong punctuation ALTER TABLE specification ( ADD CONSTRAINT specification_program_id_fkey @@ -18,37 +18,37 @@ ADD CONSTRAINT REFERENCES program (id) ); -//Incorrect syntax; a reserved word +# Incorrect syntax; a reserved word ALTER TABLE specification ADD CONSTRAINT foreign FOREIGN KEY (program_id) REFERENCES program (id); -//Incorrect syntax, misspelled keyword REFERENCES +# Incorrect syntax, misspelled keyword REFERENCES ALTER TABLE specification ADD CONSTRAINT specification_program_id_fkey FOREIGN KEY(program_id) REFERENCE program (id); -//Incorrect syntax, missing closing parenthesis +# Incorrect syntax, missing closing parenthesis ALTER TABLE specification ADD CONSTRAINT specification_program_id_fkey FOREIGN KEY(program_id REFERENCES program (id); -//Incorrect syntax; quotes +# Incorrect syntax; quotes ALTER TABLE "specification" ADD CONSTRAINT specification_program_id_fkey FOREIGN KEY("program_id") REFERENCES program ("id"); -//Correct syntax; backticks +# Correct syntax; backticks ALTER TABLE `specification` ADD CONSTRAINT specification_program_id_fkey FOREIGN KEY(`program_id`) REFERENCES program (`id`); -//Incorrect syntax, missing required elements +# Incorrect syntax, missing required elements ALTER TABLE specification ADD CONSTRAINT specification_program_id_fkey FOREIGN KEY(program_id); From 00b21e341546269c2f39c28ca417c29e76712d34 Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Mon, 30 Jun 2025 11:59:33 -0700 Subject: [PATCH 8/9] Update drop-constraint.sql --- .../drop-constraint.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql index be0915c1..f5600118 100644 --- a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/drop-constraint.sql @@ -1,3 +1,3 @@ -- Drop the constraint so that we can add it back -ALTER TABLE specification +ALTER TABLE Specification DROP FOREIGN KEY specification_program_id_fkey; From 1ae78f7342e6d90d5f6aa6f2c7b4ee59273e730f Mon Sep 17 00:00:00 2001 From: Deepak Vohra Date: Mon, 30 Jun 2025 12:03:03 -0700 Subject: [PATCH 9/9] Update example-sql-statements-that-generate-error-message.sql --- ...statements-that-generate-error-message.sql | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql index 6159ae09..87fb6743 100644 --- a/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql +++ b/sql-queries-11/fix-error-1064-when-using-ALTER-TABLE-ADD CONSTRAINT-in-MySQL/example-sql-statements-that-generate-error-message.sql @@ -1,54 +1,54 @@ # Correct syntax -ALTER TABLE specification +ALTER TABLE Specification ADD CONSTRAINT specification_program_id_fkey FOREIGN KEY (program_id) -REFERENCES program (id); +REFERENCES Program (id); # Incorrect syntax; jumbled syntax -ALTER TABLE specification +ALTER TABLE Specification ADD FOREIGN KEY specification_program_id_fkey -CONSTRAINT(program_id) -REFERENCES program(id); +CONSTRAINT (program_id) +REFERENCES Program (id); # Incorrect syntax; wrong punctuation -ALTER TABLE specification ( +ALTER TABLE Specification ( ADD CONSTRAINT specification_program_id_fkey - FOREIGN KEY(program_id) - REFERENCES program (id) + FOREIGN KEY (program_id) + REFERENCES Program (id) ); # Incorrect syntax; a reserved word -ALTER TABLE specification +ALTER TABLE Specification ADD CONSTRAINT foreign FOREIGN KEY (program_id) -REFERENCES program (id); +REFERENCES Program (id); # Incorrect syntax, misspelled keyword REFERENCES -ALTER TABLE specification +ALTER TABLE Specification ADD CONSTRAINT specification_program_id_fkey -FOREIGN KEY(program_id) -REFERENCE program (id); +FOREIGN KEY (program_id) +REFERENCE Program (id); # Incorrect syntax, missing closing parenthesis -ALTER TABLE specification +ALTER TABLE Specification ADD CONSTRAINT specification_program_id_fkey -FOREIGN KEY(program_id -REFERENCES program (id); +FOREIGN KEY (program_id +REFERENCES Program (id); # Incorrect syntax; quotes -ALTER TABLE "specification" +ALTER TABLE "Specification" ADD CONSTRAINT specification_program_id_fkey -FOREIGN KEY("program_id") -REFERENCES program ("id"); +FOREIGN KEY ("program_id") +REFERENCES Program ("id"); # Correct syntax; backticks -ALTER TABLE `specification` +ALTER TABLE `Specification` ADD CONSTRAINT specification_program_id_fkey -FOREIGN KEY(`program_id`) -REFERENCES program (`id`); +FOREIGN KEY (`program_id`) +REFERENCES Program (`id`); # Incorrect syntax, missing required elements -ALTER TABLE specification +ALTER TABLE Specification ADD CONSTRAINT specification_program_id_fkey -FOREIGN KEY(program_id); +FOREIGN KEY (program_id);