SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How can I mention primary key to specific column?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

grandhi
Starting Member

31 Posts

Posted - 04/30/2013 :  07:14:39  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 04/30/2013 :  07:20:21  Show Profile  Reply with Quote
use statement like

ALTER TABLE loginDetails ADD CONSTRAINT PK_loginDetails PRIMARY KEY (userId)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

grandhi
Starting Member

31 Posts

Posted - 04/30/2013 :  07:27:16  Show Profile  Reply with Quote
@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.

Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1050 Posts

Posted - 04/30/2013 :  07:29:16  Show Profile  Reply with Quote
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 allowed

Since, "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" constraint

CREATE TABLE loginDetails
(
UserId varchar(150) unique, -- this will maintain the uniqness but allow NULL too.
Password varchar(100),
Category varchar(50)
)

Third version
CREATE TABLE loginDetails
(
UserId varchar(150) Unique NOT NULL, -- this will maintain the uniqness but will NOT allow null.
Password varchar(100),
Category varchar(50)
)

Cheers
MIK

Edited by - MIK_2008 on 04/30/2013 07:31:16
Go to Top of Page

grandhi
Starting Member

31 Posts

Posted - 04/30/2013 :  08:09:31  Show Profile  Reply with Quote
Thank you....
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000