> What SQL data type would I use for a table that has a column for Mr. or Mrs.?

What SQL data type would I use for a table that has a column for Mr. or Mrs.?

Posted at: 2014-12-18 
Personally, I'd create a table for the enumeration and link to that. So, create something like a "Title" table, containing the Mr and Mrs records (and possibly more, later), with corresponding IDs. Then in your current table, add a TitleID column, setting up a relation between the TitleID column and the Title.ID column.

I would use a string. However, theoretically, if you wanted to, you could set it to be an integer, set it to either 1 or 2 (or 3 for Ms.), via your registration form or whatever, and have your program parse 1 as Mr., 2 as Mrs., 3 as Ms., etc.

Just plaintext would work fine though.

For the Title table,

CREATE TABLE [dbo].[Title](

[ID] [int] NOT NULL,

[Description] [nvarchar](50) NULL,

CONSTRAINT [PK_Title] PRIMARY KEY CLUSTERED

(

[ID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

For a table called Employee that uses the Title as a foreign key

CREATE TABLE [dbo].[Employee](

[ID] [nchar](10) NOT NULL,

[Title] [int] NULL,

[Name] [nvarchar](50) NULL,

CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED

(

[ID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Employee] WITH CHECK ADD CONSTRAINT [FK_Employee_Title] FOREIGN KEY([Title])

REFERENCES [dbo].[Title] ([ID])

GO

ALTER TABLE [dbo].[Employee] CHECK CONSTRAINT [FK_Employee_Title]

GO

The titles, such as Mr. Mrs, Miss, Ms would be in the title.Description field.

This query would show data from both tables using an inner join

SELECT Title.Description as Title, Employee.Name

FROM Employee INNER JOIN

Title ON Employee.Title = Title.ID