Question:
SQL VARCHAR datatype, can I specify a min size?
sgrmnl
2006-11-21 13:28:52 UTC
VARCHAR specifies max number of characters,
but I'm trying to specify an attribute (username) that requires a minimum and a maximum number of characters.... how do I do this?

Thanks.
Six answers:
nljth123
2006-11-21 13:46:04 UTC
You cannot specify a minimum number of characters in SQL. You can only specify whether the field allows nulls or a max number of charactes. You would need to enforce character limits in your programming - either on the server-side or by using client-side validation to ensure a username is greater than 1 character. I would recommend doing both.
susanta_nj
2006-11-21 23:08:19 UTC
You cannot specify the minimum number of characters in SQL VARCHAR datatype.



However, there are other ways of handling this in database level. Like in Oracle or DB2, you can specify CHECK constraint in CREATE TABLE statement.



For e.g., In Oracle,



CREATE TABLE A ( B VARCHAR2(250) NOT NULL

CHECK (length(b) > 6)

)



Now, the following INSERT SQL will generate an exception:



insert into A values ('xy')





Also, if your database support Triggers (and most of them support), then you can add trigger to check the length of the String inserted and throw an Exception when the condition is not met.
comn8u
2006-11-21 21:49:36 UTC
You would do that in the code on your page. What language are you using? SQL, I believe only allows you to specify the maximum value that you want it to hold.



In ASP.NET, you can click on a textfield and place a minimum/maximum constraints. You can also validate the textbox with javascript to count the number of characters in it.
chemicalimbalance000
2006-11-22 04:46:23 UTC
You can't put that constraint in the datatype itself (although I believe SQL Server and someothers will let you do a NOT NULL).



You should probably do that validation on the Form or WebPage itself before the data makes it to the database.
Sandeep
2006-11-22 06:36:06 UTC
you ca'nt specfy the minimum length, you will have to check for minimum characters at stored procedure level or the program.
rachelle105210
2006-11-21 21:33:01 UTC
answering....


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...