primary keys

Posted on 2005-04-28
Last Modified: 2012-05-05
do primary keys have an autonumber funcionality? if not, how do you handle unique id's?

Question by:epmo
    LVL 25

    Expert Comment

    Primary Keys aren't autonumber by definition.  In fact, many primary keys are strings or multiple columns.  However, most databases do have a sequnece that you can use to autonumber a column.  That with a trigger could make it automatic.  What DB?
    LVL 25

    Expert Comment

    Sorry....obvously SQL Server.  You can also use an identity:

    create table #a (id int identity, j int)

    and this will generate a squential number on insert.

    Author Comment


    thanks jrb1, do i need to insert any values into it when inserting records?

    Author Comment

    i mean, we get an error when inserting records on the primary key - Cannot insert the value NULL into column ..
    LVL 25

    Accepted Solution

    You shouldn't....

    An example is this create table...

    CREATE TABLE products
    product_id int IDENTITY(10,2),
    product_name varchar(50)

    and this insert statement:

    INSERT INTO products

    The "10" in the identity statement causes this insert statement to put 10 into the product_id column.
    LVL 8

    Expert Comment

    You cannot insert nulls into a Primary key column - if the primary key column is also a identity column - this will autonumber the column.


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now