Create Table

  • kapil_kk (4/26/2013)


    Mick Gillatt (4/26/2013)


    ... I also didn't find that information...

    Thanks Kapil. I wasn't criticising your explanation of the answer, I only asked my question because I couldn't find the info where anywhere in BOL and that's frustrating. I suppose most people would think it natural that both columns should have the same length, although I was wondering what would be so bad about a foreign key column that is defined as a varchar type with a longer length than the column it references.

  • Nice question. Thanks.

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • thanks for the question

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Thanks for an easy one, Kapil!

  • Nice one for a Friday.

    Thanks!

    Not all gray hairs are Dinosaurs!

  • Thanks for nice question.

    ---Statement 1

    CREATE TABLE #table1

    (

    ID int PRIMARY KEY,

    Name varchar(20)

    CONSTRAINT UN_Name UNIQUE(Name)

    );

    CREATE TABLE #table2

    (

    ID int PRIMARY KEY,

    PersonName varchar(30) REFERENCES #table1(Name)

    );

    go

    select * from #table1

    select * from #table2

    drop table #table1

    The above code will not throw error. Because the length of the data type for column PersonName in second table is greater than the length of first table column Name

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • malleswarareddy_m (4/26/2013)


    Thanks for nice question.

    ---Statement 1

    CREATE TABLE #table1

    (

    ID int PRIMARY KEY,

    Name varchar(20)

    CONSTRAINT UN_Name UNIQUE(Name)

    );

    CREATE TABLE #table2

    (

    ID int PRIMARY KEY,

    PersonName varchar(30) REFERENCES #table1(Name)

    );

    go

    select * from #table1

    select * from #table2

    drop table #table1

    The above code will not throw error. Because the length of the data type for column PersonName in second table is greater than the length of first table column Name

    Well, I think that the reason that your script runs is not because of the wider foreign key column. I think it's because you used temporary tables and they don't enforce foreign key constraints.

  • Yes you are right.I have verified with temp table.

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • malleswarareddy_m (4/26/2013)


    Thanks for nice question.

    ---Statement 1

    CREATE TABLE #table1

    (

    ID int PRIMARY KEY,

    Name varchar(20)

    CONSTRAINT UN_Name UNIQUE(Name)

    );

    CREATE TABLE #table2

    (

    ID int PRIMARY KEY,

    PersonName varchar(30) REFERENCES #table1(Name)

    );

    go

    select * from #table1

    select * from #table2

    drop table #table1

    The above code will not throw error. Because the length of the data type for column PersonName in second table is greater than the length of first table column Name

    +1 🙂

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Nice question...

  • Very good question.

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

  • Good Question. Thanks.

  • nice and easy question....

    Manik
    You cannot get to the top by sitting on your bottom.

  • That was an easy question without any complexities. Thanks. 🙂

Viewing 14 posts - 16 through 29 (of 29 total)

You must be logged in to reply to this topic. Login to reply