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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 CREATE TABLE with Check

Author  Topic 

idkase
Starting Member

6 Posts

Posted - 2014-04-07 : 04:34:33
Hi, I'm new to SQL and I'm creating my own database just to practice.

I'm trying to create a table with a restriction, and this is to not to allow to create a username with special characters.

I tried to do it this way:

CREATE TABLE Usuarios(
id int identity(1,1),
username varchar(20) check (USERNAME LIKE '%[^A-z]%'),
pwd varchar(40),
CONSTRAINT PK_USUARIOS PRIMARY KEY(id, username)
)

And also with between 'A' and 'z', but none of those seems to work, because if I try:


INSERT USUARIOS (USERNAME, PWD)
VALUES ('Ç=87', 'FASDF')

It allows me to insert it without any problems.

Help?

Thx, idkase.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-04-07 : 05:16:56
You should use NOT LIKE instead of LIKE


Too old to Rock'n'Roll too young to die.
Go to Top of Page

idkase
Starting Member

6 Posts

Posted - 2014-04-07 : 06:26:40
It worked, thanks!
Go to Top of Page
   

- Advertisement -