faqts : Computers : Databases : Microsoft SQL Server

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 5 people (60%) answered Yes
Recently 3 of 5 people (60%) answered Yes

Entry

Database:Microsoft SQLserver:SQL:Table:Operation:Create:How create table with foreign key, manually?

Dec 22nd, 2003 13:09
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 22 December 2003 - 10:07 pm -------------------

Database:Microsoft SQLserver:SQL:Table:Operation:Create:How create 
table with foreign key, manually?

---

For example, you want to create
two tables.

One table with person information
and a primary key
and another table with email information
and a secundary key.

---

Steps: Overview:

 1. -Possibly design your tables

      Table 1:

      ---

      Table 'Person'
      ----------------------------------------------------
      NAME                     PERSONKEY (=PRIMARY KEY)
      ----------------------------------------------------
      john doe                 1
      vanessa beau             2
      ----------------------------------------------------


      Table 'Email'
      -----------------------------------------------------
      EMAILADDRESS             PERSONKEY (=FOREIGN KEY)
      -----------------------------------------------------
      john.doe@test.com        1
      j.d@aol.com              1
      johnny6@yahoo.com        1
      vanessa@vendor.com       2
      vanessa.beau@yahoo.com   2
      v.b.@aol.com             2
      -----------------------------------------------------

 2. -Create your table with a primary key

 3. -Create your table with a foreign key

     Steps: Overview:

      1. -Open Microsoft SQL server 'Enterprise Manager'

      2. -Expand 'Microsoft SQL Servers' in the left pane
          (by clicking on the '+')

      3. -Expand 'SQL Server Group' in the left pane
          (by clicking on the '+')

      4. -Expand the server of your choice
          (e.g. your (local) (Windows NT) server
          (by clicking on the '+')

      5. -Expand 'Databases' (click on the '+')

      6. -Expand the database in which you want to
         add a table
         (e.g. click on the '+' of 'mydatabase')

      7. -Right click on 'Tables' in the list

      8. -Select from list 'Design table'

      9. -Possibly change some information:

          1. Fill in your data for the first column

             1. Click on the column below 'Column Name'
                and fill in a name for the table

                e.g.

                 emailaddress

             2. Choose the type from the list:

                e.g.

                 varchar

             3. Length

                e.g.

                 50

          2. Fill in your data for the second column

             1. Click on the column below 'Column Name',
                go to the next row,
                and fill in a name for the table

                e.g.

                 PersonKey

             2. Choose the type from the list:

                e.g.

                 int

             3. Length

                e.g. leave the default:

                 4

             4. Now right click on this row

                1. -select from list 'Indexes/Keys'

                2. -select tab 'Relationships'

                3. -click button 'New'

                4. -select your primary key

                    1. -in the box 'Primary key table'
                        select your primary table

                         e.g. 'Person'

                    2. -Select which column is the primary
                        key column

                        1. click in the text field below
                           'Primary key table'.

                        2. -select from list 'PersonKey'

                5. -select your foreign key

                    1. -in the box 'Foreign key table'
                        select your foreign table

                         e.g. 'Email'

                    2. -Select which column is the foreign
                        key column

                        1. click in the text field below
                           'Foreign key table'.

                        2. -select from list 'PersonKey'

                6. -click button 'Close'

     10. -Important: expand this windows in order to see the
          menu (otherwise this menu is not shown) by clicking
          in the right corner on the window expand button
          [] (as usual in a Microsoft Windows window).
          So maximize the window.

     11. -Save this table

          1. Answer 'Yes' when you are asked if you want to save this
             tables

     12. -Exit the program (e.g. click on the 'x' in the right corner
          of the window)

---
---

Internet: see also:

---

Database: Microsoft SQL server: SQL: Operation: Overview:Can you give 
an overview of SQL operations?
http://www.faqts.com/knowledge_base/view.phtml/aid/27560/fid/147

----------------------------------------------------------------------