-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_category_issue.sql
More file actions
55 lines (45 loc) · 1.75 KB
/
fix_category_issue.sql
File metadata and controls
55 lines (45 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- Fix Product table structure - add Category column and fix foreign key
USE osms_db;
-- Disable foreign key checks
SET foreign_key_checks = 0;
-- 1. Drop any existing foreign key constraints on the Product table
SELECT CONCAT('ALTER TABLE Product DROP FOREIGN KEY ', CONSTRAINT_NAME, ';')
INTO @dropConstraintSQL
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = 'osms_db'
AND TABLE_NAME = 'Product'
AND COLUMN_NAME = 'SupplierId'
AND REFERENCED_TABLE_NAME = 'Suppliers'
AND CONSTRAINT_NAME IS NOT NULL
LIMIT 1;
-- Execute the drop constraint command if a constraint was found
SET @preparedStmt = IF(@dropConstraintSQL IS NOT NULL, @dropConstraintSQL, 'SELECT "No constraint found"');
PREPARE stmt FROM @preparedStmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 2. Add the Category column if it doesn't exist
SET @checkCategory = (
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'osms_db'
AND TABLE_NAME = 'Product'
AND COLUMN_NAME = 'Category'
);
SET @addCategorySQL = IF(@checkCategory = 0,
'ALTER TABLE Product ADD COLUMN Category VARCHAR(100) NULL',
'SELECT "Category column already exists"');
PREPARE stmt FROM @addCategorySQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 3. Make SupplierId nullable if it isn't already
ALTER TABLE Product MODIFY COLUMN SupplierId INT NULL;
-- 4. Add back the foreign key constraint with a new name
ALTER TABLE Product ADD CONSTRAINT fk_product_supplier_new
FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId)
ON DELETE SET NULL
ON UPDATE CASCADE;
-- Re-enable foreign key checks
SET foreign_key_checks = 1;
-- Show the updated Product table structure
DESCRIBE Product;
-- Done
SELECT 'Product table structure fixed successfully' AS Message;