Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views2 pages

Creating and Working With Tables

This article explains how to create tblPerson and tblGender tables in SQL Server and establish primary key and foreign key constraints. It provides step-by-step instructions for both graphical and query methods to create tables and add foreign key references. Foreign keys are emphasized as essential for maintaining database integrity by ensuring valid data relationships between tables.

Uploaded by

ms9315904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Creating and Working With Tables

This article explains how to create tblPerson and tblGender tables in SQL Server and establish primary key and foreign key constraints. It provides step-by-step instructions for both graphical and query methods to create tables and add foreign key references. Foreign keys are emphasized as essential for maintaining database integrity by ensuring valid data relationships between tables.

Uploaded by

ms9315904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Creating and Working with tables - Part 3

The aim of this article is to create tblPerson and tblGender tables and establish primary key
and foreign key constraints. In SQL Server, tables can be created graphically using SQL
Server Management Studio (SSMS) or using a query.

To create tblPerson table, graphically, using SQL Server Management Studio


1. Right click on Tables folder in Object explorer window
2. Select New Table
3. Fill Column Name, Data Type and Allow Nulls, as shown below and save the table as
tblPerson.

The following statement creates tblGender table, with ID and Gender columns. The
following statement creates tblGender table, with ID and Gender columns. ID column, is
the primary key column. The primary key is used to uniquely identify each row in a table.
Primary key does not allow nulls.
Create Table tblGender
(ID int Not Null Primary Key,
Gender nvarchar(50))
In tblPerson table, GenderID is the foreign key referencing ID column in tblGender table.
Foreign key references can be added graphically using SSMS or using a query.

To graphically add a foreign key reference


1. Right click tblPerson table and select Design
2. In the table design window, right click on GenderId column and select Relationships
3. In the Foreign Key Relationships window, click Add button
4. Now expand, in Tables and Column Specification row, by clicking the, + sign
5. Click on the elipses button, that is present in Tables and Column Specification row
6. From the Primary Key Table, dropdownlist, select tblGender
7. Click on the row below, and select ID column
8. From the column on the right hand side, select GenderId
9. Click OK and then click close.
10. Finally save the table.

To add a foreign key reference using a query


Alter table tblPerson
add constraint tblPerson_GenderId_FK FOREIGN
KEY (GenderId) references tblGender(ID)

The general formula is here


Alter table ForeignKeyTable add constraint ForeignKeyTable_ForiegnKeyColumn_FK
FOREIGN KEY (ForiegnKeyColumn) references PrimaryKeyTable
(PrimaryKeyColumn)
Foreign keys are used to enforce database integrity. In layman's terms, A foreign key in
one table points to a primary key in another table. The foreign key constraint prevents
invalid data form being inserted into the foreign key column. The values that you enter into
the foreign key column, has to be one of the values contained in the table it points to.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

You might also like