February 19, 2010 at 3:34 pm
Hi,
here is my query that I wrote
create table Login
(LoginID int identity(5,5) Not null Primary Key,
Username varchar(15) not null,
Password text Null default 'badgerbadger',
DataAdded datetime Not null default getdate(),
DataUpdated datetime Null default getdate() ,
UNIQUE(Username,password, DataAdded))
I want to add composite unique key over three attributes i.e. username, password, dateadded. But Some reason I dont know this query isnt executing when I run this in SQL server. can any one help me in solving this issue.
I have a question tooo can we add a unique key over Text datatype?:exclamation:
Thanks,
Ravi Shankar
February 19, 2010 at 5:51 pm
No this is not allowed on a Text Data Type.
Try it...
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
February 20, 2010 at 12:42 am
See CREATE INDEX (Transact-SQL) for details. Here is the extract relevant to your current question:
You should probably also try to move away from using the text data type - it is deprecated in favour of the MAX data types. See ntext, text and image (Transact-SQL). Again, an extract from that Books Online entry:
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
February 20, 2010 at 12:49 am
It also occurs to me that you might be using 'text' from some past familiarity with Access. In SQL Server, text is one of the old large-object types, able to store up to 2GB of data. For a column like 'password' you probably need to use varchar(n) where n is the maximum number of characters to be stored. The maximum value of n for ordinary varchar is 8000, but varchar(max) can store up to 2GB of character data. If you work with unicode data, the equivalents are nvarchar(n) and nvarchar(max). See cha and varchar for full details.
If you change the definition of your password column to something like varchar(20), it will happily take part in a unique index. Whether that is a good choice for a unique index is another question...
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
February 20, 2010 at 9:24 am
I apologize for not taking the time as Paul did to fully explain the implications concerning the use of a Text Field.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
February 20, 2010 at 11:39 pm
Welsh Corgi (2/20/2010)
I apologize for not taking the time as Paul did to fully explain the implications concerning the use of a Text Field.
Nothing to apologize for - what you posted was correct, I just had a little more time to kill than you 😉
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
February 21, 2010 at 8:23 pm
Speaking of killing things... I can't believe that anyone would put a default on a password column unless it's meant to daze and confuse. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
February 21, 2010 at 8:54 pm
If you define an Index on an int column it tasks 4 bytes of storage.
If you define an Index on a CHAR(30) it would take 30 bytes of Storage.
This has an impact on how many pages SQL Server has to search to return the record.
The choice of the datatype used for an Index impacts on performance.
This is a very simplified explanation, however this subject is explained greater Detail in various Microsoft Publications, Forums , Blogs and Articles as well as the Microsoft Press Book for the SQL Server Implementation & Maintenance MCTS Exam.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
February 21, 2010 at 9:53 pm
Jeff Moden (2/21/2010)
Speaking of killing things... I can't believe that anyone would put a default on a password column unless it's meant to daze and confuse. 😉
The original posted query has many fine features - that is but one 😀
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply