What is the best way to generate Id or Code column(primary key) in a table in SQL Server? My requirement is a big table which stores more than 1000000 rows.
So generally we create auto identity column of a table but is it the best way? For example, I have an employee_master table and there are three columns like
emp_id auto increment
Emp_code varchar(20)
and also default like Rowno of a table.
So in this scenario, it has 3 unique columns of a table. Is it the best way?
Or what is the best way to generate id/code column both master and transaction table.
Also is there any way which we generate financial year wise auto-generate column.(any function)
The following SQL statement defines the "ID" column to be an auto-increment primary key field in the "Persons" table
CREATE TABLE Persons (
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
I think this will work..
Related
Consider a table like this one (postgresql):
create table t1 (
id serial not null primary key,
code1 int not null,
company_id int not null
);
create unique index idx1 on t1(code1, company_id);
I have to a new column code2 that will be not null and with a unique index like idx1, so the migration could be like:
alter table t1 add column code2 int not null;
create unique index idx2 on t1(code2, company_id);
The problem is: how do you manage the migration with the data that are already in the table?
You cannot apply this migration neither I can define a default value for the column.
What I was thinking to do is:
create and execute migration that just add the column without any constraint
insert the data manually in the column not with a migration
create and execute another migration that adds the constraints to the column
Is this a good practice or there are other ways?
As migration tool I am using flyway but I think it's the same for every database migration tool.
How do I auto increment the primary key in a SQL Server database table? I've had a look through the forum but can't see how to do this.
I've looked at the properties but can't see an option. I saw an answer where you go to the Identity specification property and set it to yes and set the Identity increment to 1, but that section is grayed out and I can't change the no to yes.
There must be a simple way to do this but I can't find it.
Make sure that the Key column's datatype is int and then setting identity manually, as image shows
Or just run this code
-- ID is the name of the [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)
the code will run, if ID is not the only column in the table
image reference fifo's
When you're creating the table, you can create an IDENTITY column as follows:
CREATE TABLE (
ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
...
);
The IDENTITY property will auto-increment the column up from number 1. (Note that the data type of the column has to be an integer.) If you want to add this to an existing column, use an ALTER TABLE command.
Edit:
Tested a bit, and I can't find a way to change the Identity properties via the Column Properties window for various tables. I guess if you want to make a column an identity column, you HAVE to use an ALTER TABLE command.
You have to expand the Identity section to expose increment and seed.
Edit: I assumed that you'd have an integer datatype, not char(10). Which is reasonable I'd say and valid when I posted this answer
Expand your database, expand your table right click on your table and select design from dropdown.
Now go Column properties below of it scroll down and find Identity Specification, expand it and you will find Is Identity make it Yes. Now choose Identity Increment right below of it give the value you want to increment in it.
CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.
In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.
Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5).
To insert a new record into the "Persons" table, we will NOT have to specify a value for the "Personid" column (a unique value will be added automatically):
Perhaps I'm missing something but why doesn't this work with the SEQUENCE object? Is this not what you're looking for?
Example:
CREATE SCHEMA blah.
GO
CREATE SEQUENCE blah.blahsequence
START WITH 1
INCREMENT BY 1
NO CYCLE;
CREATE TABLE blah.de_blah_blah
(numbers bigint PRIMARY KEY NOT NULL
......etc
When referencing the squence in say an INSERT command just use:
NEXT VALUE FOR blah.blahsequence
More information and options for SEQUENCE
When you're using Data Type: int you can select the row which you want to get autoincremented and go to the column properties tag. There you can set the identity to 'yes'. The starting value for autoincrement can also be edited there. Hope I could help ;)
I had this issue where I had already created the table and could not change it without dropping the table so what I did was:
(Not sure when they implemented this but had it in SQL 2016)
Right click on the table in the Object Explorer:
Script Table as > DROP And CREATE To > New Query Editor Window
Then do the edit to the script said by Josien; scroll to the bottom where the CREATE TABLE is, find your Primary Key and append IDENTITY(1,1) to the end before the comma. Run script.
The DROP and CREATE script was also helpful for me because of this issue. (Which the generated script handles.)
You can use the keyword IDENTITY as the data type to the column along with PRIMARY KEY constraint when creating the table.
ex:
StudentNumber IDENTITY(1,1) PRIMARY KEY
In here the first '1' means the starting value and the second '1' is the incrementing value.
If the table is already populated it is not possible to change a column to IDENTITY column or convert it to non IDENTITY column. You would need to export all the data out then you can change column type to IDENTITY or vice versa and then import data back.
I know it is painful process but I believe there is no alternative except for using sequence as mentioned in this post.
Be carefull like if you want the ID elements to be contigius or not. As SQLSERVER ID can jump by 1000 .
Examle: before restart ID=11
after restart , you insert new row in the table, then the id will be 1012.
You could do the following: New Table Creation:
-- create new table with Column ID which is Primary Key and Auto Increment --
CREATE TABLE titles(
id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, --Primary Key with Auto-Increment --
keyword VARCHAR(260),
status VARCHAR(10),
);
If you Table Already exists and need to make the changes to ID column to be auto-increment and Primary key, then see below:
ALTER TABLE table DROP COLUMN id; // drop the existing ID in the table
ALTER TABLE table ADD id int IDENTITY(1, 1) NOT NULL; // add new column ID with auto-increment
ALTER TABLE table ADD CONSTRAINT PK_ident_test PRIMARY KEY CLUSTERED (id); // make it primary key
I now have a requirement that a column has to be added to a table that holds a unique ID (guid). This ID is used to match records in different tables and databases but there will be NO FK constraints. Would it be better to store the guid as a varchar(32) or as a uniqueidentifier type?
There will be joins done using this column but not on a regular basis. This ID is NOT a PK. I'm asking in terms of storage and performance.
If it's a GUID, store it as a uniqueidentifier
I have the below table.
create table mytable(
physical_id int auto_increment,
logical_id int,
data varchar(20),
version_start_date datetime,
version_end_date datetime,
primary key(physical_id),
unique key(logical_id,version_start_date, version_end_date)
);
The idea behind the schema is, I want to keep track of modification to
every row and find the valid row on any particular date by checking the
version_start_date and version_end_date. I want my logical id to be
auto_increment, but mysql allows only one id to be auto_increment.
So, I want to set logical_id to physical_id, when creating a new row. I
am able to do it using the trigger.
delimiter $$
create trigger myTrigger before insert on mytable for each row begin set
new.logical_id = (select auto_increment from information_schema.tables
where table_schema = database() and table_name = 'mytable') ; end$$
delimiter ;
Few other options I checked out are, http://feedblog.org/2007/06/20/portable-sequence-generation-with-mysql/ and http://www.redhat.com/docs/en-US/JBoss_Hibernate/3.2.4.sp01.cp03/html/Reference_Guide/Native_SQL-Custom_SQL_for_create_update_and_delete.html
The problem with these approaches is, I have to create a new sequence table and keep inserting a record into that table.
Is there a better alternative?
Thank you
Bala
-- Update
I am not sure why #tpdi, why I need parent, when I can just emulate this with below table.
create table logical_id_seq (
logical_id int auto_increment,
primary key(logical_id)
);
create table mytable (
physical_id int auto_increment,
logical_id int not null references parent(logical_id),
data varchar(20),
version_start_date datetime not null,
version_end_date datetime not null,
primary key(physical_id),
foreign key (logical_id) references logical_id_seq(logical_id),
unique key (logical_id,version_start_date,version_end_date)
);
This is why I prefer Oracle/PostgreSQL sequences to MySQL's auto_increment and SQL Server's IDENTITY - you'd be able to define multiple sequences for this.
Creating a separate table solely for a column in order to use auto_increment for an additional auto_increment is the best solution I can think of, accessed via trigger or stored procedure.
Why wouldn't you want to have a table with the current version, and then an additional table for a history? Then, you use an insert into the current version table to generate your ID, and only query the history table when you actually need the history information.
Now I understand your issue :). Changing my answer.
You will need to use some trigger so that whenever some row is edited logical id is incremented by 1 finding max of logical id available in table.
create table parent (
logical_id int auto_increment,
physical_id int null references mytable(id)
);
create table mytable(
physical_id int auto_increment,
logical_id not null references parent(logical_id),
data varchar(20),
version_start_date datetime,
version_end_date datetime,
primary key(physical_id)
);
To insert a new instance of an existing record, insert into mytable ...., then capture new mytable.id, and update parent's physical_id with it (possibly in a trigger).
Now, to look up the current record, look up the logical id in parent, and use parent.physical_id to join to the correct record in "mytable"
Parent points to the current valid record (of all records) in "mytable".
To find all instances of a logical record, use the logical_id in "mytable".
To insert a wholly new record, first insert into parent to get the new logical_id, then insert the data in "mytable"; this is why we allow parent.physical_id to be nullable.
As to the OP's update: yes, you can "emulate" by just "stealing" a sequence, but that doesn't model what's really going on. "Stealing" the sequence is just an implementation detail; having two tables as I've shown above makes explicit what's really going on, namely: I have a current state (which parent points to) in "mytable", and 0, 1 or many prior states in "mytable". It's a cleaner separation of concerns, a table that tells you "what's current for any id", and a table that tells you "the data of that current state, and the data of prior states".
Is the following possible? I am unable to do so. Do I have to have a permanent table to create index?
declare #Beatles table
(
LastName varchar(20) ,
FirstName varchar(20)
)
CREATE CLUSTERED INDEX Index_Name_Clstd ON #Beatles(LastName)
Not on a table variable, but on a temp table see this http://www.sqlteam.com/article/optimizing-performance-indexes-on-temp-tables
No, you cannot create indices on a table variable - see this article here and this posting here comparing local, global temporary tables to table variables.
Restrictions
You cannot create a non-clustered
index on a table variable, unless the
index is a side effect of a PRIMARY
KEY or UNIQUE constraint on the table
(SQL Server enforces any UNIQUE or
PRIMARY KEY constraints using an
index).
According to this post - YES you can.
The following declaration will generate 2 indexes:
DECLARE #Users TABLE
(
UserID INT PRIMARY KEY,
UserName VARCHAR(50),
FirstName VARCHAR(50),
UNIQUE (UserName,UserID)
)
The first index will be clustered, and will include the primary key.
The second index will be non clustered and will include the the columns listed in the unique constraint.
Here is another post, showing how to force the query optimizer to use the indexes generated dynamically, because it will tend to ignore them (the indexes will be generated after the execution plan is evaluated)