Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are SQL Server Database Ids always positive?

Tags:

sql

sql-server

Are SQL Server database Ids always positive?

As in the dbid from sysdatabases.

SELECT name, dbid 
  FROM master.dbo.sysdatabases

This question has nothing to do with identity columns or primary keys.

like image 946
Robert Wilkinson Avatar asked Feb 12 '10 17:02

Robert Wilkinson


People also ask

Do database IDS start at 0 or 1?

if you are populating a code object from a database record, the object will initialize with an "ID" property of 0. Then if the populating is successful it will be something other than the default of 0.

Should I always use ID as primary key?

Every table (except for the rare conditions) should have a PRIMARY KEY , that is a value or a set of values that uniquely identify a row. See here for discussion why. IDENTITY is a property of a column in SQL Server which means that the column will be filled automatically with incrementing values.

What is ID field in database?

An identity column is a column (also known as a field) in a database table that is made up of values generated by the database. This is much like an AutoNumber field in Microsoft Access or a sequence in Oracle.


2 Answers

Edited, since you changed the question.

In this query: SELECT name, dbid from master.dbo.sysdatabases The value of dbid will ALWAYS be positive, since it is defined as a 1,1 identity.

like image 64
Erich Avatar answered Sep 23 '22 12:09

Erich


They don't have to be, but the general practice is to make them. You can easily create a Primary Key that is an indentity with a negative Identity Increment. You would also not make it an identity and give it a negative value as well.

like image 41
Keith Adler Avatar answered Sep 19 '22 12:09

Keith Adler