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?

4 of 7 people (57%) answered Yes
Recently 4 of 7 people (57%) answered Yes

Entry

Database:MicrosoftSQLserver:SQL:Table:Operation:Create:How create table with foreign key, using SQL?

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


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

Database:MicrosoftSQLserver:SQL:Table:Operation:Create:How create 
table with foreign key, using SQL?

---

For example, you want to create a table
with emailaddresses of persons.

---

Steps: Overview:

 1. -Design your tables

      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 tables

     2. Creating this table in SQL

        ---

      CREATE TABLE Email (
       emailaddress VARCHAR( 50 ) NOT NULL,
       PersonKey INT NOT NULL,
       FOREIGN KEY ( PersonKey ) REFERENCES Person ( PersonKey )
      );

        ---

---
---

More in general:

---

 CREATE TABLE <your tablename> (
  <your columnname1> <Datatype1> [NOT NULL],
  <your columnname2> <Datatype2> [NOT NULL],
  ...
  <Columnnamelast> <Datatypelast> [NOT NULL],
  [FOREIGN KEY ( <your columnnameJ> ) REFERENCES <your columnnameK> ( 
<your columnnameJ> )]
 );

---
---

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

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