0.1 Creating and Working With Tables
0.1 Creating and Working With Tables
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.
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.
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.