Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative database IDs in SQL Server

Tags:

sql-server

Can database Ids be negative in SQL Server?

I know that you can have a total 32767 (for both 32 bit and 64 bit) (Maximum Capacity SQL Server)

I know that the first few database Ids are reserved for master, model, msdb and tempdb and 32767 is the hidden system resource database.

I have never seen a negative database Id but I would like to confirm that it is not possible.

like image 713
Robert Wilkinson Avatar asked Sep 13 '12 16:09

Robert Wilkinson


People also ask

Can SQL Id be negative?

Negatives: The reason for not using negatives for IDs is that negative numbers are not portable. The binary representation of a decimal value depends on the underlying numerical architecture, and this effects the way a negative decimal value will be presented in non-negative, streamed format (e.g., hex, base36, etc.).

What is negative blocking in SQL Server?

Negative Blocking Session Ids (-5 = Latch ANY TASK RELEASOR) ‎Jan 21 2022 04:17 PM. SQL Server may report a blocking session id as a negative integer value. SQL Server uses negative sessions ids to indicate special conditions.

Can primary key have negative values?

No there's nothing inherently wrong with negative values in a primary key.

How do I stop negative values in SQL?

First, change your query: UPDATE tblProducts SET qty = qty - @quantity where pName = @name AND qty >= @quantity; SELECT @@ROWCOUNT; Then, instead of using ExecuteNonQuery use ExecuteScalar and check if the number of records modified is 0 it means that the @quantity is bigger than the value of qty .


1 Answers

In short, they can be, but they almost never are.

In the master DB, the id column in the sysdatabases table is defined as an identity column with seed 1 and increment 1. That means that, by default, SQL Server will always assign a positive DBID for any database you create.

However, it is possible to change,you can specify negative seeds and increments for identity columns,so you can reset the identity column of the sysdatabases table to -1, -1 and the next database you create will have ID -1, then -2, etc. -1 is a perfectly valid 16-bit integer value and so the identity field should have no trouble. You may have an issue with SQL Server naively checking the number of DBs it manages by checking the max DBID. I would not expect MSS to make that check in such a stupid way, but stranger things have happened, and I've never seen an instance with more than a couple dozen DBs on it.

like image 193
KeithS Avatar answered Sep 21 '22 12:09

KeithS