SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Adding Field With Data?

    Hi Guys,

    I have a question for you which is playing on my mind, i'm trying to add a field to a table which already has data in but i want the new fields to place data in each row for each new field that i add. So for example say my table looked like this:

    Code:
    Fields:
    FirstName, LastName, Username
    
    Rows:
    Jerry, Thomas, Jtom2
    Jenna, Fish, jfish4
    Darren, Jackson, Dazzbo33
    Chris, Hunt, Hunty93
    How do i add a new field to the table which will automatically add a default value of '-'. So say i wanted to add a new field called EmailAddress the rows would now look like

    Code:
    Fields:
    FirstName, LastName, Username, EmailAddress
    
    Rows:
    Jerry, Thomas, Jtom2, -
    Jenna, Fish, jfish4, -
    Darren, Jackson, Dazzbo33, -
    Chris, Hunt, Hunty93, -
    How do i do this please?

    Thanks in advance!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ideally, you'd want to have a NULL default I'd say. If you need a hyphen when you display the data, do it then.

    Code MySQL:
    ALTER TABLE
        schema.table 
    ADD COLUMN
        EmailAddress VARCHAR(255) DEFAULT null 
    AFTER
        Username;

    Something like that.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Ideally, you'd want to have a NULL default I'd say. If you need a hyphen when you display the data, do it then.

    Code MySQL:
    ALTER TABLE
        schema.table 
    ADD COLUMN
        EmailAddress VARCHAR(255) DEFAULT null 
    AFTER
        Username;

    Something like that.
    Hi Anthony,

    Thanks for the reply, I know how to add a new field to a table but the problem im facing is i dont know once i have added the new field to the table how to place data in the new field for exisiting data already in the table. For example my table has over 1,000,000 records, how do i add '-' into the new field i just added to the table to the exisiting records?

    Sorry my mind is blank, been doing SQL 12hrs none stop now without sleep and its about time i really need to get some sleep, taking half an hour to find simple problems such as missing commas tells me i need to go to sleep. 08:33 here in Manchester (UK) so my bed is calling very soon.

    Thanks

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In which case, my previous point still stands. Don't use a hyphen, use NULL. If you need to display a hyphen when you render the results, do it then; not now.

    You're going to be (potentially) storing a million hyphens for no benefit what so ever.

    Unless of course you *know* why you need to store a million hyphens?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •