• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

primary keys

do primary keys have an autonumber funcionality? if not, how do you handle unique id's?

tks.
0
epmo
Asked:
epmo
  • 3
  • 2
1 Solution
 
jrb1Commented:
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?
0
 
jrb1Commented:
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.
0
 
epmoAuthor Commented:

thanks jrb1, do i need to insert any values into it when inserting records?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
epmoAuthor Commented:
i mean, we get an error when inserting records on the primary key - Cannot insert the value NULL into column ..
0
 
jrb1Commented:
You shouldn't....

http://www.databasejournal.com/features/mssql/article.php/3307541

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
      (product_name)
VALUES
      ('computer')

The "10" in the identity statement causes this insert statement to put 10 into the product_id column.
0
 
JulianvaCommented:
You cannot insert nulls into a Primary key column - if the primary key column is also a identity column - this will autonumber the column.


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now