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.
Author |
Topic |
grandhi
Starting Member
31 Posts |
Posted - 2013-04-30 : 07:14:39
|
Hi All, I want to mention primary key to specific column.for that purpose,using Google help I wrote the following query.but it allowing duplicate values. CREATE TABLE loginDetails( UserId varchar(150) NOT NULL, Password varchar(100), Category varchar(50)) Ho can I set primary key to column UserId.Can anyone help me?Thank you. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-30 : 07:20:21
|
use statement likeALTER TABLE loginDetails ADD CONSTRAINT PK_loginDetails PRIMARY KEY (userId) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
grandhi
Starting Member
31 Posts |
Posted - 2013-04-30 : 07:27:16
|
@visakh16 Thank you for you answer.I got it how can I create Primary Key at the table creation time.I found it through Google.I have a doubt,about my code.UserId varchar(150) NOT NULL In the above statement,UserId specifies columnName and Varchar(150) specifies dataType.But Not Null specifies what ?can You explain.Really I don't know anything about database.That's why I am asking you.Thank you. |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-04-30 : 07:29:16
|
If you're requirements are to impose uniqueness and along with "not null" and referential integrity then use CREATE TABLE loginDetails( UserId varchar(150) Primary Key, Password varchar(100), Category varchar(50))INSERT INTO loginDetails values ('x','y','z')INSERT INTO loginDetails values ('x','y','z') -- will not be allowedSince, "Primary Key" by default impose "not Null" check and also uniquness on the column. Along that, can also be used for referential integrity purposes. On the other hand if you're only looking for it to be unique but allow Null values then use "Unique" constraintCREATE TABLE loginDetails( UserId varchar(150) unique, -- this will maintain the uniqness but allow NULL too. Password varchar(100), Category varchar(50))Third versionCREATE TABLE loginDetails( UserId varchar(150) Unique NOT NULL, -- this will maintain the uniqness but will NOT allow null. Password varchar(100), Category varchar(50))CheersMIK |
|
|
grandhi
Starting Member
31 Posts |
Posted - 2013-04-30 : 08:09:31
|
Thank you.... |
|
|
|
|
|
|
|