SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,082
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)

    Default value, cast another column

    I want to create a new column that has a default value of the primary key in a varchar(10) format. Hows this done?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,021
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    it's not done -- you cannot assign a default value of another column

    why not just convert the value when you run a SELECT?

    or, if you're gonna do it relentlessly on every SELECT, why not set up a view which has the extra column incorporated?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    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)
    You're duplicating data though, why would the pk value not suffice?
    @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.

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,082
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Our large dataset needs compared regularly against two seperate databases, One stores the pk as a double, the other as a string (wonderful right?) Our local copy of the dataset is only a small portion of the whole system so storing both types of primary keys for joining to both seemed useful.

  5. #5
    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)
    Ah, I see. A view, as R937 states, sounds like it could be useful here then.
    @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.

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,082
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Ah nice. Had not seen or heard of views before now. Thanks for that pointer.


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
  •