I have a column (line number) that I need to update with sequential numbering starting with 001.
Am I able to run an update for this?
Line 1 001
Line 2 002
Line 3 003
Etc..
You can add a new identity column in your table as below-
ALTER TABLE your_table
ADD ID INT IDENTITY(1,1)
This will add a new column 'ID' to your table and also fill with value starts from 1 to end (row count of the table) with incremental value like 1,2,3.... Once this is done, either you can keep the column, or taking value from this new column you can update your column LineNumber as per your required format.
Related
I have a table of many columns in Postgresql database. Some of these columns are in text type and have several rows of values. The values also recur. I would like to change these text values with unique integer values.
This is my table column:
Country_Name
------------
USA
Japan
Mexico
USA
USA
Japan
England
and the new column I want is:
Country_Name
------------
1
2
3
1
1
2
4
Each country name is assigned (mapped) to a unique integer and all the recurrences of the text is replaced with this number. How can I do this?
Edit 1: I want to replace my column values on the fly if possible. I don't actually need another column to keep the names but it would be nice to see the actual values too. Is it possible to do:
Create a column country_id with the same values of country_name column in the same table
And for country_id replace each name with a unique integer with an update statement or procedure without requiring a new table or dictionary or map.
I don't know if this is possible but this will speed up things because I have a total of 220 columns and millions of rows. Thank you.
assuming the country_name column is in a table called country_data
create a new table & populate with unique country_names
-- valid in pg10 onwards
-- for earlier versions use SERIAL instead in the PK definition
CREATE TABLE countries (
country_id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
country_name TEXT);
INSERT INTO countries (country_name)
SELECT DISTINCT country_name
FROM country_data;
alter the table country_data & add a column country_id
ALTER TABLE country_data ADD COLUMN country_id INT
Join country_data to countries and populate the country_id column
UPDATE country_data
SET country_id = s.country_id
FROM countries
WHERE country_data.country_name = countries.country_name
At this point the country_id is available to query, but a few following actions may be recommended depending on the use case:
set up country_data.country_id as a foreign key referring to countries.country_id
drop the column country_data.country_name as that's redundant through the relationship with countries
maybe create an index on country_data.country_id if you determine that it will speed up the queries you normally run on this table.
I am using SQL Server 2012.
I have two tables, tblPerson and tblGender.
tblPerson has 4 columns
ID
Name
Email
GenderID (foreign Key)
tblGender has 2 columns
ID
Gender
tblGender has only two entry, male and female, having id 1 and 2.
Now, if I insert bad data to the GenderId column, like 3, 4 etc. it rejects the value but it increments the Identity column value, and when I insert another data even if it is valid, it gives the next id number.
How can I solve this problem?
At first, as it was already mentioned, that is normal behavior of an IDENTITY column. SQL Server inserts the row first, increments Identity column value, but then rejects this row because of failed constraint.
An advice will be following:
1. Leave your ID column with gaps.
2. For very sequential 'EmployeeID' number you can use Sequence, which you insert from a variable: https://msdn.microsoft.com/en-us/library/ff878058(v=sql.110).aspx
Then you are supposed to have no gaps in your 'EmployeeID'.
I created a table and inserted 4 rows into it. I ran the below query
SELECT seed_value as SeedValue, last_value as identityValue
FROM sys.identity_columns
WHERE object_id=OBJECT_ID('ALJtest1')
and got the result as
SeedValue| identityValue
-------------------------
1 | 4
Then I reseeded the table using
DBCC CHECKIDENT('DBO.ALJtest1', RESEED, 10)
When I ran the below query this time
SELECT seed_value as SeedValue, last_value as identityValue
FROM sys.identity_columns
WHERE object_id=OBJECT_ID('ALJtest1')
I got the result as
SeedValue| identityValue
-------------------------
1 | 10
Is there a way to find the last applied seed value on a table in SQL Server 2012?
RESEED, despite the name, doesn't change the identity's seed value, instead it simply sets the next identity value to generate. There is no way to change an identity column's actual seed value after it's created. From the documentation:
The seed value is the value inserted into an identity column for the
very first row loaded into the table. All subsequent rows contain the
current identity value plus the increment value where current identity
value is the last identity value generated for the table or view.
You cannot use DBCC CHECKIDENT to perform the following tasks:
Change the original seed value that was specified for an identity column when the table or view was created.
Reseed existing rows in a table or view.
To change the original seed value and reseed any existing rows, you
must drop the identity column and recreate it specifying the new seed
value. When the table contains data, the identity numbers are added to
the existing rows with the specified seed and increment values. The
order in which the rows are updated is not guaranteed.
So to answer your question: no, there is no way to know the last value specified in a DBCC CHECKIDENT(..., RESEED), because the current identity value may have already changed after inserts.
I am trying to add a unique ID to an existing table that has data inserted into it. I don't need a new value with each row, rather, with each instance of an insert. The time stamp indicates a new insert. Can anyone be kind enough point me in the right direction? My current table is basically column a and the time stamp.
ID COLUMN A TIME STAMP
1 abc 05-09-2013 11:00:23
1 bcd 05-09-2013 11:00:23
1 ab3 05-09-2013 11:00:23
2 abc 05-09-2013 11:15:00
2 123 05-09-2013 11:15:00
3 abc 05-09-2013 11:18:07
4 abc 05-09-2013 11:19:55
4 123 05-09-2013 11:19:55
4 165 05-09-2013 11:19:55
4 def 05-09-2013 11:19:55
Can't think of any easy way to put unique id based on each insert instance. One idea can be to use trigger on the table where you can inspect how the data getting inserted and add ID value before data gets inserted into table.
First create a ID column which allows null.
Then write a procedure which has a cursor on a query which does a order by and group by on time stamp column.
For every row increase a count by one
update table with id = counter whwre times tamp = time stamp in query
At end of this add constraint on id column to make it not null
You are going to likely want an auto-increment field. Use IDENTITY for this in SQL Server. This will enable you to insert without supplying this p_id value and the value will be auto incremented.
You don't want to use timestamps for uniqueness because it is difficult and costly to index. It is also difficult to ensure uniqueness among time values.
CREATE TABLE Persons
(
P_Id int PRIMARY KEY IDENTITY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
If your insert is not very frequent. You can use time stamp to get unique Id.
e.g. 130509110023
You can do some math to get appropriate function as required data length, which will give you unique id
I have two columns in my primary key (id, type), id is identity and type is foreign key.
I want to set seed for id column like following:
id type
10000 1
10001 1
10000 2
10001 2
10002 1
10002 2
10000 3
I could do this from code (or dml), but wonder is it possible in ddl or SqlServer table properties?
An id column increments by 1 for each row. There is no way to have it repeat. Any reason why you can't just have the identifier column be the pk for the table? You may have to resort to using a trigger to do this.
Microsoft SQL Server does not allow you to add or alter an Identity on an existing column via TSQL very easily. To change the original seed value and reseed any existing rows, you must drop the identity column and recreate it specifying the new seed value. When the table contains data, the identity numbers are added to the existing rows with the specified seed and increment values.