Jump to content

Add Column to Table


scott100

Recommended Posts

Using MySQL, is it possible to add another column onto a table (table already has information stored in it)?For example, say i have this:id name addressAnd i need to add another column named postcodeid name address postcodeIs that possible?

Link to comment
Share on other sites

You won't be able to set the new column to NOT NULL unless you set a default value because if the table already has rows trying to ad a new column with NOT NULL and no default value will try and put in NULL for all the existing records causing hte ALTER to fail.

Link to comment
Share on other sites

You won't be able to set the new column to NOT NULL unless you set a default value because if the table already has rows trying to ad a new column with NOT NULL and no default value will try and put in NULL for all the existing records causing hte ALTER to fail.

@ pulpfiction, thanks i got it to insert, this will be very usefull in future :) @ aspneyguy, what? :) can you explain more please, i didn't understand :)
Link to comment
Share on other sites

okay if you want to insert a new column and you want that column to NOT allow NULL you must set a default value for that column. Meaning when you add a record and you don't specify a value for that column it will use the default value you set.You must do this if you have existing records because you cannot add a column (it is applied to the existing records too) that doesn't allow NULL (without a default value) because when you execute the statement to add the column it will try to fill the existing records with NULL for the new column which breaks the NOT NULL constraint.Sorry if I am not clear, I am having a hard time explaining it. As long as you were able to do what you needed to do. If you run into this problem you will understand what I mean. It is just hard to explain.

Link to comment
Share on other sites

If you run into this problem you will understand what I mean. It is just hard to explain.
Thanks for the heads up, if i am using NOT NULL and the table throws a wobbler then i will remember this post :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...