MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What the difference between unique and primary key?

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
issac1029
Intermediate


Joined: 10 Dec 2005
Posts: 159
Topics: 75

PostPosted: Thu Mar 16, 2006 12:02 am    Post subject: What the difference between unique and primary key? Reply with quote

Sad
1. as the subject

2.about implicity created index:It was said in <sql reference>,my understanding as follow:If I define a column with a constraint of primary key or unique,implicity created index would be generated automaticly,and I do not have to deine unique index for that column. Am I right about that?
Back to top
View user's profile Send private message Send e-mail MSN Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Mar 16, 2006 6:05 am    Post subject: Reply with quote

issac1029,

Please search before posting. Check this link

http://www.mvsforums.com/helpboards/viewtopic.php?p=25345#25345

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
issac1029
Intermediate


Joined: 10 Dec 2005
Posts: 159
Topics: 75

PostPosted: Thu Mar 16, 2006 9:15 am    Post subject: Reply with quote

It refers to unique index.But I mean the constraint of a column,

for example: create table
(aaaa primary key
bbbb unique)
Back to top
View user's profile Send private message Send e-mail MSN Messenger
astro
Beginner


Joined: 05 Oct 2005
Posts: 31
Topics: 7

PostPosted: Thu Mar 16, 2006 9:57 am    Post subject: Reply with quote

Isaac,
Difference between Primary & Unique key
-------------------------------------------------
1. Primary Key can not contain null values whereas unique key can have one null.If it is created from multiple columns, then only one column of unique key can have null value.
2. There can be only one Primary key for a table whereas you can have more than one unique index in a table to satisfy your performance criteria.
3. If you have already created a unique index with not null constraint and if you later create a primary key with those columns of unique index, DB2 will not create a new index for this primary key it will use the existing unique index itself
4. If there is no unique index created already and if you are creating primary key then DB2 will create a unique index for this primary key by default.

Hope all your questions are answered.
Back to top
View user's profile Send private message
Manas Biswal
Intermediate


Joined: 29 Nov 2002
Posts: 382
Topics: 27
Location: Chennai, India

PostPosted: Thu Mar 16, 2006 10:01 am    Post subject: Reply with quote

issac1029,

You are confusing between DB2 UDB for LUW(Linux, Unix, Win) and DB2 for Z/OS. In DB2 UDB, indexes are implicitly created for primary key and unique constraints. But in DB2 for Z/OS (at least uptill v7), you have to explicitly create unqiue indexes for all types of unique constraints (including primary key) after the table is created. Otherwise, the table will remain unusable.

Regards,
Manas
_________________
There is no path to peace. Peace is the path.
- Mahatma Gandhi (1869-1948)
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Manas Biswal
Intermediate


Joined: 29 Nov 2002
Posts: 382
Topics: 27
Location: Chennai, India

PostPosted: Thu Mar 16, 2006 10:13 am    Post subject: Reply with quote

astro,

Quote:

If there is no unique index created already and if you are creating primary key then DB2 will create a unique index for this primary key by default.


Are you sure of this statement for "DB2 on Z/OS". I am really not sure if that is an accurate statement. You can specify a primary key while creating the table. In that case, you have to define an unique index for the column(s) of the primary key explicitly after the table create otherwise the table remains unusable.
If you add a primary key constraint for a table after the table create (using ALTER table), then a unique index must already exists for the key columns before you can add the constraint.
Either way, the index has to be explicitly defined.

Regards,
Manas
_________________
There is no path to peace. Peace is the path.
- Mahatma Gandhi (1869-1948)
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
astro
Beginner


Joined: 05 Oct 2005
Posts: 31
Topics: 7

PostPosted: Thu Mar 16, 2006 11:15 am    Post subject: Reply with quote

Manas,
My reply is for UDB not for "DB2 on Z/OS".
Back to top
View user's profile Send private message
issac1029
Intermediate


Joined: 10 Dec 2005
Posts: 159
Topics: 75

PostPosted: Thu Mar 16, 2006 9:33 pm    Post subject: Reply with quote

Thanks,Manas Biswal and astro:

your reply are really helpful!My document is not only for z/os,so it's probably a luw statement.

By the way ,I saw that 'when a primary key defined in a table, a implicitly unique index will be created if create table is processed by
Quote:

schema processor

,otherwise unique index should be manually be created.What is 'schema processor'?Is it for z/os?
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Manas Biswal
Intermediate


Joined: 29 Nov 2002
Posts: 382
Topics: 27
Location: Chennai, India

PostPosted: Fri Mar 17, 2006 10:25 am    Post subject: Reply with quote

issac1029,

Yes. Schema Processor is sort of a wrapper around the SQL used on DB2 for Z/OS to make it more compatible with the SQL used on other DB2 platforms. It has been there for a long time, but nobody much uses it. If you run your DDLs under the schema processor, it will behave similar to DB2 on Z/OS for a lot of cases like implicit qualification of all database objects, implicit creation of indexes etc.

The schema processor program is DSNHSP. You can find a sample job somewhere in the SDSNSAMP lib. I don't remember the member name. Will let you know if I find it.

HTH...Regards,
Manas
_________________
There is no path to peace. Peace is the path.
- Mahatma Gandhi (1869-1948)
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
issac1029
Intermediate


Joined: 10 Dec 2005
Posts: 159
Topics: 75

PostPosted: Fri Mar 17, 2006 8:46 pm    Post subject: Reply with quote

Manas Biswal,

I've found that,it's DSNTEJ1S .Thanks!
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group