Home All Groups Group Topic Archive Search About
Author
5 Sep 2006 12:45 AM
James
I have a table that has 3 columns: firstname, middlename and lastname.
The concatanation of the 3 columns will always be unique.
I would like to create a fourth column called fullname that would have
[firstname]+ ' ' + [middlename] + ' ' + [lastname] as a formula and make this
the primary key.
However the middlename column can, and often does, have Null values. Becuase
the middlename column can hae null values I am having trouble using this as
part of my primary key field. If 2 people have the same first and last name
and middle name will always be included for one of the names to ensure unique
values.
Any suggestions are appreciated. Thanks

Author
5 Sep 2006 1:21 AM
Hari Prasad
Hi,

Create a Unique key based on First, Middle and Last names. Unique key allows
one null for this combination.

THanks
Hari
SQL Server MVP

Show quote
"James" <Ja***@discussions.microsoft.com> wrote in message
news:D1868B34-5A9E-40F3-A2CC-758B933F33B5@microsoft.com...
>I have a table that has 3 columns: firstname, middlename and lastname.
> The concatanation of the 3 columns will always be unique.
> I would like to create a fourth column called fullname that would have
> [firstname]+ ' ' + [middlename] + ' ' + [lastname] as a formula and make
> this
> the primary key.
> However the middlename column can, and often does, have Null values.
> Becuase
> the middlename column can hae null values I am having trouble using this
> as
> part of my primary key field. If 2 people have the same first and last
> name
> and middle name will always be included for one of the names to ensure
> unique
> values.
> Any suggestions are appreciated. Thanks
Author
5 Sep 2006 5:05 AM
Michael C
Show quote
"James" <Ja***@discussions.microsoft.com> wrote in message
news:D1868B34-5A9E-40F3-A2CC-758B933F33B5@microsoft.com...
>I have a table that has 3 columns: firstname, middlename and lastname.
> The concatanation of the 3 columns will always be unique.
> I would like to create a fourth column called fullname that would have
> [firstname]+ ' ' + [middlename] + ' ' + [lastname] as a formula and make
> this
> the primary key.
> However the middlename column can, and often does, have Null values.
> Becuase
> the middlename column can hae null values I am having trouble using this
> as
> part of my primary key field. If 2 people have the same first and last
> name
> and middle name will always be included for one of the names to ensure
> unique
> values.
> Any suggestions are appreciated. Thanks

Create a field called ID as Guid or int and make that the primary key. Even
though some professor somewhere will tell you this is incorrect most
programmers use this method.

Michael
Author
5 Sep 2006 5:43 AM
Arto Viitanen
James wrote:
> I have a table that has 3 columns: firstname, middlename and lastname.
> The concatanation of the 3 columns will always be unique.
> I would like to create a fourth column called fullname that would have
> [firstname]+ ' ' + [middlename] + ' ' + [lastname] as a formula and make this
> the primary key.
> However the middlename column can, and often does, have Null values. Becuase
> the middlename column can hae null values I am having trouble using this as
> part of my primary key field. If 2 people have the same first and last name
> and middle name will always be included for one of the names to ensure unique
> values.
> Any suggestions are appreciated. Thanks

You can have more than one attribute as a primary key, so make primary
key as firstname, middlename and lastname. You can evaluate fullname  as

[firstname] + ' ' + coalesce([middlename], ' ') + ' ' + [lastname]

Make either a view that contains the fullname, or add the fullname to
the original table and make a  trigger that evaluates new value each
time some of the firstname, middlename or lastname is added or changed.

--
Arto Viitanen, CSC Ltd.
Espoo, Finland
Author
5 Sep 2006 12:30 PM
Hilarion
> [firstname] + ' ' + coalesce([middlename], ' ') + ' ' + [lastname]


This adds extra spaces when "middlename" is NULL, eg:

'John' + ' ' + COALESCE( NULL, ' ' ) + ' ' + 'Doe'

will give:

'John   Doe'

Try this:

[firstname] + ' ' + ISNULL( [middlename] + ' ', '' ) + [lastname]

It should give 'John Doe' (but still 'John Kevin Doe' if the
"middlename" is "Kevin").


--
Kamil 'Hilarion' Nowicki
Author
5 Sep 2006 6:30 AM
David Portas
James wrote:
> I have a table that has 3 columns: firstname, middlename and lastname.
> The concatanation of the 3 columns will always be unique.
> I would like to create a fourth column called fullname that would have
> [firstname]+ ' ' + [middlename] + ' ' + [lastname] as a formula and make this
> the primary key.
> However the middlename column can, and often does, have Null values. Becuase
> the middlename column can hae null values I am having trouble using this as
> part of my primary key field. If 2 people have the same first and last name
> and middle name will always be included for one of the names to ensure unique
> values.
> Any suggestions are appreciated. Thanks

James wrote:

> However the middlename column can, and often does, have Null values.

Why? No-one has a null for a middle name. What does the uniqueness of
these three columns mean to you if the middle name is "missing"
(unknown?) for some reason?

ALTER TABLE tbl
ALTER COLUMN middlename VARCHAR(20) NOT NULL;

ALTER TABLE tbl ADD CONSTRAINT pk_tbl
PRIMARY KEY (firstname, middlename, lastname);


> If 2 people have the same first and last name
> and middle name will always be included for one of the names to ensure unique
> values.

And what if two people have the same middle name as well as first and
last name?

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
Author
6 Sep 2006 5:31 AM
Arto Viitanen
David Portas wrote:
>
> Why? No-one has a null for a middle name. What does the uniqueness of
> these three columns mean to you if the middle name is "missing"
> (unknown?) for some reason?

Why mother does, if you interpret NULL as "data does not exist".

--
Arto Viitanen,  CSC Ltd
Espoo, Finland
Author
6 Sep 2006 2:29 PM
Anith Sen
>> Why mother does, if you interpret NULL as "data does not exist".

That is the problem with NULLs -- subjective interpretation. You interpret
NULLs in one way, your neighbor does in another and a bystander does it
differently.

So when using NULLs, how can you formalize your facts for logical
representation and how does a formal DBMS system infer new facts from
existing facts?

--
Anith
Author
7 Sep 2006 5:26 AM
Michael C
"Anith Sen" <an***@bizdatasolutions.com> wrote in message
news:%23bnecDc0GHA.3716@TK2MSFTNGP03.phx.gbl...
> That is the problem with NULLs -- subjective interpretation. You interpret
> NULLs in one way, your neighbor does in another and a bystander does it
> differently.

The way in which the DBs author interprets it would be the correct
interpretation and all other's should follow. Naturally the author would
have documented this :-)

Michael
Author
6 Sep 2006 9:31 PM
Hugo Kornelis
On Wed, 06 Sep 2006 08:31:41 +0300, Arto Viitanen wrote:

>David Portas wrote:
>>
>> Why? No-one has a null for a middle name. What does the uniqueness of
>> these three columns mean to you if the middle name is "missing"
>> (unknown?) for some reason?
>
>Why mother does, if you interpret NULL as "data does not exist".

Hi Arto,

Since NULL is defined as a marker for missing data, you'd be using it
incorrect. Since you know that mother has no middle name, the data is
not missing, it's an empty string. You'd have to use NULL to store my
middle name, since (as far as I know) I never disclosed anywhere on the
internet whether or not I have one - so you can't know and hance can't
enter anything, not even an empty string.

Of course, you can choose to use NULL to represent the (known) absence
of a middle name. Just as you can choose to use 3 to represent the
number 75 and 75 to represent the number 3. And as long as all your
front-end code is built around this non-standard interpretation of 3 and
75, you won't run into trouble - but you will find that you can't use
most of SQL Server's builtin functions and expressions, since they don't
share your non-standard interpretation of 3 and 75.

--
Hugo Kornelis, SQL Server MVP
Author
5 Sep 2006 7:16 AM
Arjan de Haan
"James" <Ja***@discussions.microsoft.com> wrote in message
news:D1868B34-5A9E-40F3-A2CC-758B933F33B5@microsoft.com...
>I have a table that has 3 columns: firstname, middlename and lastname.
> The concatanation of the 3 columns will always be unique.

Are you sure about this? I have 2 friends of mine which have exactly the same name, from
first to last.

> I would like to create a fourth column called fullname that would have
> [firstname]+ ' ' + [middlename] + ' ' + [lastname] as a formula and make this
> the primary key.
> However the middlename column can, and often does, have Null values. Becuase
> the middlename column can hae null values I am having trouble using this as
> part of my primary key field. If 2 people have the same first and last name
> and middle name will always be included for one of the names to ensure unique
> values.

Most DBA's will tell you that this is bad practice. Never let business data (broad term,
OK) be the PK for a table, for at least 2 reasons:
1) to uniquely identify a person from this table, you would need to include all fields (3
in your case) into referencing tables, and
2) business data has a tendency to change over time. What if someone mistyped a person's
name and later on this is corrected? If something changes in the PK, all your related
tables need to be updated too!

It is much easier to create a new field and make it an IDENTITY and Primary Key. This way
SQL Server ensures that each key will be unique. Next, if you are sure your rule that all
3 fields will be unique will hold all the times, create a unique index on those 3 fields.
Now, when referencing a person from your table, all you need to do is include this
single-field PK into the table. It's reference will still be valid even if the persons
name would change...

Hope this helps.
Success!

....Arjan...

AddThis Social Bookmark Button