Jump to content
jimfog

primary keys

Recommended Posts

I have a table where a column stores the output of a uniqid-meaning a random number that every time the function is called, it is different. I have set this to be the primary key of the table. Do I need to set it AUTO INCREMENT also? Or do you think maybe is better to create a separate column named primary and set this to auto increment?

Share this post


Link to post
Share on other sites

what does uniqid refers? if it is mean to be id to identify something uniqly then you dont need to set seperate column for ids. purpose should be clear though for each field. if you dont set auto increment in case it encounters same id in database it will throw error. if you don auto increment it and it encounters same id it will incerement by 1 from the largest id it have in the database.

Share this post


Link to post
Share on other sites

As a sidenote and in relation to primary keys...if I set something as auto increment does that means THAT automatically gets to become a primary key. The above question does not relate in anyway with the uniqid example I mention above.

Share this post


Link to post
Share on other sites

Autoincrement can only apply to a column that is a key, but it doesn't need to be the primary key.

Share this post


Link to post
Share on other sites

Are there other keys also besides primary ones...to what you are referring to? Can you be specific?

Share this post


Link to post
Share on other sites

got it

Share this post


Link to post
Share on other sites

I found in the web this line of code which I am having difficulty in finding what exactly the constraint keyword does plus the value next to it: CONSTRAINT tb_pk PRIMARY KEY (ProviderID),I ran the above query(and as guess) it a column named ProviderID which is going to be also the primary key.The words though "CONSTRAINT tb_pk" seem to have any effect at all. Sorry if I am putting this question here-If I have to, I will create a new topic. It seems CONSTRAINED is used to give a name to a primary key, but why we would want to do that?

Edited by jimfog

Share this post


Link to post
Share on other sites

yes...but if I do not set a name using CONSTRAINT, the indexes still get name, the name of the column. What it the point of giving them a name of our choice-such as tb_pk in the example above?That is one thing,the other, what is the reason giving name to a primary key(again look at the example above). The name that the primary key will take...will be PRIMARY.

Share this post


Link to post
Share on other sites

If you have an index that spans multiple columns it is easier to refer to it with a short name. It's just for convenience.

Share this post


Link to post
Share on other sites

Is it going to help when performing queries?

Share this post


Link to post
Share on other sites

you can check in "information_scheme" database for "table_constraints" how the keys are represented. the name used to identify keys uniquely in table scheme.

Is it going to help when performing queries?
it is more of need than performance. keys will work quite when you use multiple column for one keys.as example sometime you wants to use two column combination as unique. means column1 column2A BA AB AB A if you have unique constraint on column1 and column B the above values will be considered unique in each case(combination of both coulmn) Edited by birbal

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...