Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assign a number to each row in a table representing the record number?

How can I show the number of rows in a table in a way that when a new record is added the number representing the row goes higher and when a record is deleted the number gets updated accordingly?
To be more clear,suppose I have a simple table like this :

ID int (primary key) Name varchar(5)

The ID is set to get incremented by itself (using identity specification) so it can't represent the number of row(record) since if I have for example 3 records as:

ID NAME
1 Alex
2 Scott
3 Sara

and I delete Alex and Scott and add a new record it will be:

3 Sara
4 Mina 

So basically I'm looking for a sql-side solution for doing this so that I don't change anything else in the source code in multiple places.

I tried to write something to get the job done but it failes. Here it is :

SELECT        COUNT(*) AS [row number],Name
FROM          dbo.Test
GROUP BY ID, Name
HAVING        (ID = ID)

This shows as:

row number            Name
1                     Alex
1                     Scott
1                     Sara

while I want it to get shown as:

row number            Name
1                     Alex
2                     Scott
3                     Sara
like image 272
Hossein Avatar asked Feb 17 '13 18:02

Hossein


People also ask

How do I assign a row number to each row in SQL?

To add a row number column in front of each row, add a column with the ROW_NUMBER function, in this case named Row# . You must move the ORDER BY clause up to the OVER clause. SELECT ROW_NUMBER() OVER(ORDER BY name ASC) AS Row#, name, recovery_model_desc FROM sys.

How do I assign a number to a row in SQL?

If you'd like to number each row in a result set, SQL provides the ROW_NUMBER() function. This function is used in a SELECT clause with other columns. After the ROW_NUMBER() clause, we call the OVER() function. If you pass in any arguments to OVER , the numbering of rows will not be sorted according to any column.

How do you display the number of records in a table?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

How can we get the number of records of rows in a table?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows. The above syntax is the general SQL 2003 ANSI standard syntax.


3 Answers

If you just want the number against the rows while selecting the data and not in the database then you can use this

select row_number() over(order by id) from dbo.Test

This will give the row number n for nth row.

like image 176
Saksham Avatar answered Oct 18 '22 03:10

Saksham


Try

SELECT id, name, ROW_NUMBER() OVER (ORDER BY id) AS RowNumber
FROM   MyTable
like image 45
DevelopmentIsMyPassion Avatar answered Oct 18 '22 02:10

DevelopmentIsMyPassion


What you want is called an auto increment.

For SQL-Server this is achieved by adding the IDENTITY(1,1) attribute to the table definition.

Other RDBMS use a different syntax. Firebird for example has generators, which do the counting. In a BEFORE-INSERT trigger you would assign the ID-field to the current value of the generator (which will be increased automatically).

like image 2
alzaimar Avatar answered Oct 18 '22 02:10

alzaimar