Primary key (two columns) identity increment - sql-server

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.

Related

Adding a primary key column to an existing table in SQL Server?

I need to add a new column and make it the primary key in SQL Server; the table in question does not yet have a unique key column.
Here is the sample table http://www.sqlfiddle.com/#!18/8a161/1/0
My goal is simply to have a column ID and insert values from 1 to 1160 (total number of records in this table) and make that the primary key. Also is there a way to automatically add the numbers from 1-1160 without adding each record one by one since there are 1000+ rows in this table?
Thank you!
Simply alter the table and add the column with the appropriate characteristics.
alter table x add id smallint identity(1,1) not null primary key;
Thrown together quickly and you probably should get in the habit of naming constraints. fiddle. I will note that you may or may not want to use an identity column - think carefully about your actual goal and how you want to continue using this table after the alteration.

How to solve Identity column increment issue when foreign key relationship violates?

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'.

Find the last applied seed value on a table in sql server 2012

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.

Sequence column population in a table

I have a log table in SQL Server. This table is populated by 2 different sources - UI and goanywhere. This table has a log id column which will have sequence numbers for each entered record. What is the way to populate this column with correct sequence of numbers when the population is from 2 different channels. Suggestions please
In SQL Server, use an INT IDENTITY column in your table:
CREATE TABLE dbo.Log
(
LogID INT NOT NULL IDENTITY(1,1),
.... other columns...
)
and when you insert rows into this table, just don't specify the LogID column in your list of columns to insert into - SQL Server will automatically add values to it.
See the MSDN documentation on IDENTITY for more detail.
Use auto increment feature of mysql . It does not matter how many channels are there.

Some tables id column do not auto increment despite being identity

I am troubleshooting a db (not my forte) that has some tables that are auto incrementing on the id column and some tables are not?
Now all the tables are set as identity and marked to disallow null. I am using SSMS what else can I check or do to get these tables back to auto incrementing?
TIA
Interestingly to me...probably old news to you guys. The issue had to do with existing data. So for example a table that had 100 rows in did NOT have the identity column setup. So I would go in and make it an identity with a seed of 1 incrementing 1. Well the seed was somehow having trouble because there was already 100 rows in there. So for all my tables I had to do a row count and seed the identity from the next row. Now it is working as expected.
Having an IDENTITY column is pretty straightforward for a table, and if you are not seeing the auto incrementing behavior of a column on inserts, then I'd first verify that your column is indeed an IDENTITY column:
use <Your Database Name>;
go
select
name,
is_identity
from sys.columns
where object_id = object_id('<Your Table Name>')
order by column_id;
Substitute <Your Database Nam> and <Your Table Name> for their appropriate values.
The other possibility is that data that "appears" to be non-incrementing could have been pushed out to that with a session that set identity insert and pushed out explicit values.
ALTER TABLE YourTable MODIFY COLUMN YourTable_id int(4) auto_increment

Resources