This question already has answers here:
SQL Server, How to set auto increment after creating a table without data loss?
(7 answers)
Closed 9 years ago.
I have imported a table from one database to another, the issue is the auto increment ID has now been removed, I want to add the primary key back to the original column on my copied table and get it working again by taking the last max ID when it was working and continuing to add 1 as normal, is this possible.
Thanks P
Just Check the Table.Whether Identity is must be there..Remove the identity..So it wil start from 1
The Query
DBCC CHECKIDENT('Your Table Name', RESEED, 0)
Related
This question already has answers here:
How do I alter the position of a column in a PostgreSQL database table?
(10 answers)
Change column order in table of postgres
(1 answer)
How to change the position of column in postgresql without dumping
(1 answer)
Closed 2 years ago.
When a new column is added to a current table, this appears in the last column. eg:
added AGE columns to Students table
SELECT * FROM Students; "would show the AGE column right at the end.
Question: is it possible to change the order of these columns when such commands are executed?
If you have an existing table, you could create a VIEW with the columns reorganized, then of course, you could also type out the columns. You could also create a new table.
This question already has answers here:
oracle constraints datatype
(2 answers)
Closed 2 years ago.
I am making a naive ratings platform db and I want to restrict a rating to be whole numbers from 1 to 5. To clarify I do not want to round or truncate, I want it to show a constraint violation if anything except 1,2,3,4,5 is entered.
The data type I'm using is smallint for rating. If I input 2.7, say, it truncates to 2 and proceeds to add the relation instance to the table. Which I don't want. How can I add a constraint to prevent this?
This is what the CHECK keyword is for (https://www.w3schools.com/sql/sql_check.asp)
If you had a table
CREATE TABLE Reviews (
Rating smallint CHECK (Rating IN (1,2,3,4,5)
);
This question already has answers here:
How to get the identity of an inserted row?
(15 answers)
Closed 5 years ago.
Here's what I'm trying to do: I add a record to the table. This record receives its unique ID number (identity). Then I want to use this ID in my code. How can I find out which ID number has just received a newly added record in my SQL Server database table?
Add an output parameter to your insert statement and return scope_identity(). If this isn't sufficient just search my answer, it will be in here a bunch.
This question already has answers here:
Default row ordering for select query in oracle
(8 answers)
Closed 7 years ago.
Hi allWhile working on Oracle I came across a situation where I need to select the rows from a table in which the recently inserted row should be at the top of my selection.
But here in my table I am not using any Identity column. I know how to do the same when there is an Identity column present, but is there any way to do this without Identity column?
Unless you have a sequential ID or Timestamp or some other field you can ORDER BY, Oracle provides no guarantee of order and you should not rely on it.
I have a requirement for a program I'm working on to store job numbers as YY-###### with incrementing numbers starting at 000001 in each year preceded by the last two digits of the year.
The only method I've been able to come up with is to make a CurrentJob table and use an identity column along with the last two digits of the year and an ArchiveJob table and then combining the two via a union in a view. Then I'd have to copy the CurrentJob to ArchiveJob at the begining of the year and truncate CurrentJob.
Is there an easier way to restart the numbering (obviously not having it be an Identity column) in one table?
The client is closed on New Years so there should be no data entry at the change of the year (for a school).
An identity column is by far the fastest and most concurrent solution to generating sequential numbers inside SQL Server. There's no need to make it too complicated though. Just have one table for generating the identity values, and reset it at the end of the year. Here's a quick example:
-- Sequence generating table
create table SequenceGenerator (
ID integer identity(1, 1) primary key clustered
)
-- Generate a new number
insert into SequenceGenerator default values
select ##identity
-- Reset the sequence
truncate table SequenceGenerator
if ident_current('SequenceGenerator') <> 1 begin
dbcc checkident('SequenceGenerator', reseed, 0)
dbcc checkident('SequenceGenerator', reseed)
end else begin
dbcc checkident('SequenceGenerator', reseed, 1)
end
There is a similar question #761378.. (Note: it uses MySql but the principle is the same)
The accepted answer suggested using a second table to manage the current ID.
However the most popular question was to not do this! Please note HLGEM's answer on the post for reasons why not to.
You can use the "reseed" command from here to reset the starting value.