I have a table called “Users” it contain some fields such as Id, ParentId, Name.
What I want is to alter this table to add new self join relation so that ParentId link to Id, but ParentId is nullable.
I want to write this alter sql statment in mysql without dropping the table.
I have a table called Users it contain some fields such as Id, ParentId,
Share
Do you mean that you want to add a foreign key constraint? If so, see the FOREIGN KEY Constraints docs. Here’s an example, starting with creating a simple table:
Add an index for the
parent_idcolumn:Add the foreign key constraint:
Show the new table structure: