January 25, 2010 at 12:49 pm
Consider my table as follow,
declare @tUsrAcct table
(
TrnxID int identity(1,1) not null,
UsrID varchar(30) not null,
FNme varchar(50) not null,
LNme varchar(50) not null,
Remarks varchar(50) not null
)
--My 1st statement as follow,
insert into @tUsrAcct(UsrID,FNme,LNme,Remarks) values('wolverine1','James','Howlet','')
--My 2nd statement as follow,
insert into @tUsrAcct(UsrID,FNme,LNme,Remarks) values('wolverine2','James','Howlet','NA')
I need to know, which T-SQL is the best practice? 1st statement or 2nd statment? Hopefully somebody will give an answer with explanation.
January 25, 2010 at 1:12 pm
I don't think there will be a "best practice" answer...
It's more like one for the "it depends" category... But I might be wrong.
I, personally would use the first option since it makes it a little easier to visually identify empty remarks. Using special characters might also force you to take care of several issues (for instance leading blanks, upper/lower/mixed case, "misuse" like SSMS OpenTable and delete a remark in table grid).
But if it's common practice for other tables/apps to identify empty rows with NA I'd use NA as well. So: It depends 🙂
You've already done the major step to not allow NULL values in the first place. But this might start a discussion whether this is best practice or not, as well. 😉
January 25, 2010 at 1:18 pm
My honest opinion...
If a column is optional and it's valid and allowed for there not to be an entry in that column, make it nullable and let null stand for 'not there'
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 25, 2010 at 3:00 pm
My reason to make it Remarks is Not Null because it'll allow me to index this column
if me make it this Remarks allow Null, this column can't be indexing
correct me if im wrong
so, me assume 1st statement is ok,
insert into @tUsrAcct(UsrID,FNme,LNme,Remarks) values('wolverine1','James','Howlet','')
and 2nd statement is ok also.
insert into @tUsrAcct(UsrID,FNme,LNme,Remarks) values('wolverine2','James','Howlet','NA')
January 25, 2010 at 3:07 pm
I agree with Gail, and, correct me if I'm wrong, but I do believe SQL 2005 can index nullable columns.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
January 26, 2010 at 1:54 am
miss.delinda (1/25/2010)
My reason to make it Remarks is Not Null because it'll allow me to index this columnif me make it this Remarks allow Null, this column can't be indexing
Create table #temp (
TrnxID int identity(1,1) not null,
UsrID varchar(30) not null,
FNme varchar(50) not null,
LNme varchar(50) not null,
Remarks varchar(50) null
)
Create Index idx_Temp_REmarks ON #Temp (Remarks)
Command(s) completed successfully.
I changed to a temp table, because table variables cannot be indexed at all, regardless of data type and nullability.
The only restrictions on an index key is that it cannot exceed 900 bytes or 16 columns and that it cannot contain the LOB data types (varchar(max), etc)
Of course, the usefulness of indexing a remarks column is another question entirely. How often do you search the table only by remarks.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 26, 2010 at 9:21 am
tq for all the idea.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply