1

I have a table column that holds a hashed token generated when people use the 'forgotten password' link on a login system (it is generated when they enter their email to be sent a link). This token is then used as a url parameter included in the email link that is sent to them.

I am going to set a time limit on this link so that it expires every two hours, and the fields in this column will be deleted with a cron job.

How do I delete all of the fields in a column, but WITHOUT deleting the column itself?

There is plenty of info on how to delete a MYSQL table column e.g. ALTER TABLE mytable DROP COLUMN mycolumn but I can't seem to find any info on how to remove all of the column fields, but keep the column itself (which will be populated with NULL fields unless they have requested a password reset link)?

pjk_ok
  • 618
  • 7
  • 35
  • 90

2 Answers2

1

Instead of deleting, in your case you should think of updating. An update query like this, would work in your case:

UPDATE mytable 
SET 
    mycolumn = null;
0

You can use UPDATE to achieve your desired outcome:

UPDATE your_table
SET    your_column = null
WHERE  your_column is not null;
codie-fz
  • 116
  • 6
  • Although it's correct, according to this: https://stackoverflow.com/questions/35364214/does-mysql-overwrite-a-column-of-same-value-on-update, the extra where clause is not needed. – Christos Panagiotakopoulos May 24 '21 at 22:23