Results 1 to 5 of 5

Thread: [RESOLVED] Refer to same Id on table

  1. #1

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Resolved [RESOLVED] Refer to same Id on table

    Hello all,

    I just have a quick question on sql, something that I thought was strange but it does exist.

    I need to create a table which contains a filed that needs to refer to some other record on the same table based on the unique id.

    I.e

    EmployeeID [primary key, identity]
    Employee_Name
    WeeklyHours
    Salary
    ManagerID (this column with need to refer to the employeeId field but can be null)

    How should I proceed on creating the managerId field?

    Thanks in advance,
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Refer to same Id on table

    The ManagerID field should be the same kind of data type as the EmployeeID field, but without the Primary Key and Identity properties set.

    In terms of using it for queries etc, that is a little more complex. There is an example in the "SQL" section of the FAQs, but that is for SQL Server so may need tweaking to whatever database system you are using.

  3. #3

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Refer to same Id on table

    Thanks for the reply,

    Yup I am using SQL server. My only concern with that idea is that say, if a new record is entered, someone could accidentally insert a managerid that can't be referenced.

    Is possible if you could point me out to which FAQ as there is quite a few?

    Thanks again,
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Refer to same Id on table

    someone could accidentally insert a managerid that can't be referenced
    You just need to set it up as a foreign key and tell it to enforce integrity. If someone tries to enter a manager id that doesn't exist as an employee id it will throw an error.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Refer to same Id on table

    Quote Originally Posted by Greyskull View Post
    Is possible if you could point me out to which FAQ as there is quite a few?
    The last article in the "SQL" section of our Database Development FAQs/Tutorials

Posting Permissions

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



Click Here to Expand Forum to Full Width