For adding a column (attribute) to an existing table, we use the MODIFY
statement in MySQL.
For example, suppose that I have a table named student. The student table contains 3 columns (attributes) named name, id and cgpa. Now I want to add one more column named phone_number there.
Then the MySQL command would be:
ALTER TABLE student ADD phone_number VARCHAR(12);
Now let me explain each keyword. As I want to alter a table that is already been created, I used the ALTER
keyword. Then the question comes which thing I want to alter; is it a table or database or anything else? The answer is I want to alter a particular table.
Therefore I used ALTER TABLE
. A database can have multiple tables in it. I need to spcify the exact table where I want to make the alteration. Therefore, I stated the table name there. Then comes the part exactly which type of alteration I want to apply.
As I want to add a new column, I have stated that in my command. Then I stated my new attribute (column) name and the data type with it.
After finishing the statement, I used a semicolon to specify that the statement is finished.
That is how the query command works!
📹 If you are comfortable with a step by step video tutorial, then I have already created one video for all of you!
If you want to follow me then you can follow me on Twitter and GitHub.
Also, make sure to endorse me on relevant skills in LinkedIn.
If you like to watch programming related content regularly, then check out my YouTube channel.
Have a good day! 😊
Cover: Photo by Caspar Camille Rubin on Unsplash
Top comments (0)