Lọ mọ cả ngày tìm ra lý do tại sao module reset password lại không hoạt động. Hóa ra cột chứa password có kiểu CHAR(42) trong khi password chỉ dài có 40 kí tự. Công việc giảm độ dài của field này cũng lắm nhiêu khê vì không thể làm đơn giản kiểu
ALTER TABLE users MODIFY password CHAR(40);
vì sẽ có lỗi 01441 ngay lập tức khi mà bảng đã có dữ liệu.
Vậy phải làm thế nào:
+ Thêm cột mới vào ngay sau cột cũ
+ Trao đổi dữ liệu 2 cột
+ Drop cột cũ
Tuy nhiên MySQL is more advanced than Oracle trong cái vụ ADD BEFORE/AFTER này nhiều. Oracle thì phải làm như sau:
/** Add a new column with desired width */
ALTER TABLE users ADD (password2 char(40));
/** Trimming trailing whitespace and swap */
UPDATE users SET password2 = TRIM(TRAILING ' ' FROM 'password');
COMMIT;
/** Drop old column */
ALTER TABLE users DROP COLUMN user_password;
/** Change to old name */
ALTER TABLE users RENAME COLUMN password2 TO password;
/** Prepare for the temporary table */
ALTER TABLE users RENAME TO users2;
/** Reserve the order of the field so not insert/update query will be affected */
CREATE TABLE users NOLOGGING /* unrecoverable */
AS
SELECT user_id, username, password,
email, lastname, firstname, company, reg_dtime,
user_act_code, status, news_subscriber, duration_type_id,
salutation_id, referrer_id, another_referrer, language_id, gender_id
FROM users2;
/** GC */
DROP TABLE users2;
I know this is an Old question. but…it may help someone.
Oracle 10g Doc says
“You can increase the length of an existing column, or decrease it, if all existing data satisfies the new length. You can change a column from byte semantics to CHAR semantics or vice versa. You must set the initialization parameter BLANK_TRIMMING=TRUE to decrease the length of a non-empty CHAR column.”