Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 derived fields/foreign keys

Author  Topic 

newbie06
Starting Member

1 Post

Posted - 2006-02-16 : 06:43:21
firstly, how do u write a derived field in sql table, e.g you are following a logical design and you are calculating a score for a match table.

secondly, if a foreign key is being used as a primary key in another table, do u have to set it to NOT NULL ?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-02-16 : 07:48:46
>>how do u write a derived field in sql table
see below

>>if a foreign key is being used as a primary key in another table, do u have to set it to NOT NULL ?
no - see below

Here are 3 simple examples. A computed column, a derivedResultSet column and a nullable foreign key.

use pubs
set nocount on

--create a table with a derived column
create table junk (i int primary key, derivedCol as i+1)
go
insert junk (i) values (1)
select * from junk

go
--create a table with a nullable foreign key
create table junk2 (rowid int identity(1,1), i int null references junk(i))
go
insert junk2 (i) values (null)
insert junk2 (i) values (1)
go

select i, derivedCol, derivedCol-i as derivedResultSetColumn from junk
select rowid, i from junk2
go
drop table junk2
drop table junk


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -