Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. to set foreign key or not to set?

to set foreign key or not to set?

Scheduled Pinned Locked Moved Database
comperformancequestionannouncement
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I want to ask about indexing. I have employee table and department table with employee_id and department_id primary keys. Also I have department_id in the employee table. Do I set the employee.department_id as a foreign key for a better performance? Thanks, Jassim

    Technology News @ www.JassimRahma.com

    _ 1 Reply Last reply
    0
    • J Jassim Rahma

      Hi, I want to ask about indexing. I have employee table and department table with employee_id and department_id primary keys. Also I have department_id in the employee table. Do I set the employee.department_id as a foreign key for a better performance? Thanks, Jassim

      Technology News @ www.JassimRahma.com

      _ Offline
      _ Offline
      _AK_
      wrote on last edited by
      #2

      Creating a foreign key is required for refrential integrity and is normally a good practice. It is also true that a foreign key has a say inperformance. To make it better what you can do is to create index on the foreign keys.

      .AK.

      J 1 Reply Last reply
      0
      • _ _AK_

        Creating a foreign key is required for refrential integrity and is normally a good practice. It is also true that a foreign key has a say inperformance. To make it better what you can do is to create index on the foreign keys.

        .AK.

        J Offline
        J Offline
        Jassim Rahma
        wrote on last edited by
        #3

        I did indexed it but do I still need (or is it better) to declare it as foreign key?

        Technology News @ www.JassimRahma.com

        _ 1 Reply Last reply
        0
        • J Jassim Rahma

          I did indexed it but do I still need (or is it better) to declare it as foreign key?

          Technology News @ www.JassimRahma.com

          _ Offline
          _ Offline
          _AK_
          wrote on last edited by
          #4

          As I mentioned creating a foreign key constraint is required for refrential integrety and normally is a good practice. It do have an impact on performance which can be reduced by creating an index. If you craete the index on foreign key it will reduce the lookups.

          .AK.

          J 1 Reply Last reply
          0
          • _ _AK_

            As I mentioned creating a foreign key constraint is required for refrential integrety and normally is a good practice. It do have an impact on performance which can be reduced by creating an index. If you craete the index on foreign key it will reduce the lookups.

            .AK.

            J Offline
            J Offline
            Jassim Rahma
            wrote on last edited by
            #5

            so declaring it as foreign key is a good practice and will have an impact on the performance but after declaring it do you still recommend to index it or declaring is enough?

            Technology News @ www.JassimRahma.com

            _ 1 Reply Last reply
            0
            • J Jassim Rahma

              so declaring it as foreign key is a good practice and will have an impact on the performance but after declaring it do you still recommend to index it or declaring is enough?

              Technology News @ www.JassimRahma.com

              _ Offline
              _ Offline
              _AK_
              wrote on last edited by
              #6

              Creating a foreign key is not mandatory but is advised to create. Here[^] is a link which tells about the importance of it. Now here[^] is another article which tells about refrential integrity and performance degradation which might give you a bit more idea about this.

              .AK.

              J 1 Reply Last reply
              0
              • _ _AK_

                Creating a foreign key is not mandatory but is advised to create. Here[^] is a link which tells about the importance of it. Now here[^] is another article which tells about refrential integrity and performance degradation which might give you a bit more idea about this.

                .AK.

                J Offline
                J Offline
                Jassim Rahma
                wrote on last edited by
                #7

                Great Thanks

                Technology News @ www.JassimRahma.com

                _ 1 Reply Last reply
                0
                • J Jassim Rahma

                  Great Thanks

                  Technology News @ www.JassimRahma.com

                  _ Offline
                  _ Offline
                  _AK_
                  wrote on last edited by
                  #8

                  You are welcome.

                  .AK.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups