I created a table in SQL Server like this:
CREATE TABLE [UserName]
(
[ID] [int] NOT NULL ,
[Name] [nvarchar] (50) NOT NULL ,
[Address] [nvarchar] (200) NULL
CONSTRAINT [PK_UserName] PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]
GO
If I want to make ID an identity column, what do I need? Do I need to drop and create this table and set ID to [ID] [int] IDENTITY(1,1) NOT NULL .
By using drop and create, all of data from UserName table are lost .
Is there another way to set IDENTITY COLUMN to created table's column without losing data?
I use SQL Server 2008 R2 :)
ALTER TABLE [UserName] DROP COLUMN [ID];
ALTER TABLE [UserName]
ADD [ID] integer identity not null;
Try this one -
DECLARE #temp TABLE
(
ID INT NOT NULL
, Name NVARCHAR(50) NOT NULL
, [Address] NVARCHAR(200) NULL
)
INSERT INTO #temp (ID, Name, [Address])
SELECT ID, Name, [Address]
FROM dbo.UserName
DROP TABLE dbo.UserName
CREATE TABLE dbo.UserName
(
[ID] [int] IDENTITY(1,1) NOT NULL
, [Name] [nvarchar] (50) NOT NULL
, [Address] [nvarchar] (200) NULL
CONSTRAINT [PK_UserName] PRIMARY KEY CLUSTERED
([ID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
SET IDENTITY_INSERT dbo.UserName ON
INSERT INTO dbo.UserName (ID, Name, [Address])
SELECT ID, Name, [Address]
FROM #temp
SET IDENTITY_INSERT dbo.UserName OFF
The easiest way:-
Right click on the table in object explorer and select 'Design'
Select the column for which you want to set identity and go to Column Properties
Under 'Identity Specification' change '(Is Identity)' to 'Yes'
Click Save.... Done :)
Hope This Helps
Here is a solution that is minimally logged.
SELECT
IDENTITY(INT, 1,1) AS ID,
Name, [Address]
INTO dbo.UserName_temp
FROM dbo.UserName;
ALTER TABLE dbo.UserName_temp
ADD CONSTRAINT [PK_UserName] PRIMARY KEY CLUSTERED;
DROP TABLE dbo.UserName;
EXEC sp_rename 'dbo.UserName_temp', 'UserName';
SRC:
http://sqlmag.com/sql-server/appending-identity-column-temporary-table
But the IDENTITY() function can "only in a SELECT statement with an INTO table clause."
http://msdn.microsoft.com/en-us/library/ms189838.aspx
Related
I have a table created like this:
CREATE TABLE address_user
(
[username] VARCHAR(13) NOT NULL,
[address] CHAR(58) NOT NULL,
[id] BIGINT NOT NULL,
CONSTRAINT [PK_ address_user]
PRIMARY KEY CLUSTERED ([id] ASC)
);
Now I want to be able to keep the history modification of this table, so I want to make it as temporal table. I know the script to create a temporal table, the final result should be:
CREATE TABLE address_user
(
[username] VARCHAR(13) NOT NULL,
[address] CHAR(58) NOT NULL,
[id] BIGINT NOT NULL,
[sys_start_time] DATETIME2(7)
GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
[sys_end_time] DATETIME2 (7)
GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME ([sys_start_time], [sys_end_time]),
CONSTRAINT [PK_ address_user]
PRIMARY KEY CLUSTERED ([id] ASC)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE=[dbo].[address_user_history], DATA_CONSISTENCY_CHECK=ON));
The easy way to do that is just delete the previous table, and recreate the table with the good schema.
However, I have a lot of information in my table, save the data and delete the table, recreate it and re-insert the data make me uncomfortable.
So if you have a solution to transform the first table in temporal table without the need to delete everything and recreate it, it should be a great help!
Create the new table address_user_new, insert the data, then use sp_rename to rename address_user to address_user_old and address_user_new to address_user. This can all be done in a transaction to ensure ensure that the transition is atomic and apparently-instantaneous. eg
if object_id('address_user') is not null
ALTER TABLE address_user SET ( SYSTEM_VERSIONING = OFF)
go
if object_id('address_user_new') is not null
ALTER TABLE address_user_new SET ( SYSTEM_VERSIONING = OFF)
go
drop table if exists address_user
drop table if exists address_user_history
drop table if exists address_user_new
drop table if exists address_user_old
go
CREATE TABLE address_user
(
[username] VARCHAR(13) NOT NULL,
[address] CHAR(58) NOT NULL,
[id] BIGINT NOT NULL,
CONSTRAINT [PK_address_user]
PRIMARY KEY CLUSTERED ([id] ASC)
);
go
CREATE TABLE address_user_new
(
[username] VARCHAR(13) NOT NULL,
[address] CHAR(58) NOT NULL,
[id] BIGINT NOT NULL,
[sys_start_time] DATETIME2(7)
GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
[sys_end_time] DATETIME2 (7)
GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME ([sys_start_time], [sys_end_time]),
CONSTRAINT [PK_address_user_new]
PRIMARY KEY CLUSTERED ([id] ASC)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE=[dbo].[address_user_history], DATA_CONSISTENCY_CHECK=ON));
go
set xact_abort on
begin transaction
insert into address_user_new(username,address,id)
select username,address,id
from address_user with (tablockx)
exec sp_rename 'address_user', 'address_user_old', 'OBJECT'
exec sp_rename 'PK_address_user', 'PK_address_user_old', 'OBJECT'
exec sp_rename 'address_user_new', 'address_user', 'OBJECT'
exec sp_rename 'PK_address_user_new', 'PK_address_user', 'OBJECT'
commit transaction
I have a table with column "ID" as datatype int.
CREATE TABLE [dbo].[STUDENT]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[DOB] [datetime] NOT NULL ,
[NAME] [nvarchar](250) NULL,
[CLASS] [nvarchar](50) NULL,
CONSTRAINT [PK_STUDENT_DATA]
PRIMARY KEY CLUSTERED ([ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
) ON [PRIMARY]
Now I want to change datatype from int to uniqueidentifier. I have deleted constraint PK_STUDENT_DATA. My ALTER statement is
ALTER TABLE dbo.STUDENT COLUMN ID uniqueidentifier
I am getting error:
Identity column 'HISTORY_ID' must be of data type int, bigint,
smallint, tinyint, or decimal or numeric with a scale of 0, and
constrained to be nonnullable.
The identity column is noncompatible with UniqueIdentifier data type Column.So you need to drop the column and create the new column with UniqueIdentifier data type.Since IDENTITY cannot be used with GUID.Use NEWID instead.
Perform Following Steps:
1. Initially, you need to remove the constraint from table
DROP Index [PK_STUDENT_DATA] ON STUDENT
2. After that
ALTER TABLE STUDENT drop COLUMN ID
3. And finally
ALTER TABLE STUDENT
ADD [Id] UNIQUEIDENTIFIER NOT NULL
PRIMARY KEY NONCLUSTERED DEFAULT NEWID()
Please follow below steps:
Firstly, you should remove PrimaryKey in ID column.
Secondly, you should ALTER ID column with [ID] [int] IDENTITY(1,1) NULL and call update command with NULL value.
Thirdly, call command ALTER TABLE dbo.STUDENT COLUMN ID uniqueidentifier NOT NULL and set primary key to this column.
Case 1: If you want to alter colums without constraint
First you need to drop existing column if you want to change from INT To UNIQUEIDENTIFIER. Do this by following way:
ALTER TABLE TableName
DROP COLUMN COLUMN1, COLUMN2 --IF you want multiple/single column
After that you can add columns by following way:
ALTER TABLE TableName
ADD COLUMN1 UNIQUEIDENTIFIER,
COLUMN2 UNIQUEIDENTIFIER
Case 2: If you want to alter colums which contains constraint
Then firstly remove that constraint and do the above steps.
I have this SQL Server table:
CREATE TABLE [dbo].[Synonym]
(
[SynonymId] [int] NOT NULL,
[WordFormId] [int] NOT NULL,
[Ascii] AS (ascii([Text])) PERSISTED,
[Text] [varchar](max) NOT NULL,
[Version] [timestamp] NULL,
[CreatedBy] [int] NULL,
[CreatedDate] [datetime] NULL,
[ModifiedBy] [int] NULL,
[ModifiedDate] [datetime] NULL,
PRIMARY KEY CLUSTERED ([SynonymId] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
How can I set the CreatedDate to the current date for all of the rows?
If want to save the date when the record was created, add a default constraint to the table:
ALTER TABLE [Synonym] ADD CONSTRAINT createdDate DEFAULT GETDATE() FOR [CreatedDate]
Then if you insert a record you don't need to specify the createdDate and get the current date value.
If you want to set the date for all existing records simply run an update
UPDATE [Synonym] SET [CreatedDate] = GETDATE()
or for all records whose CreatedDate is null:
UPDATE [Synonym] SET [CreatedDate] = GETDATE() WHERE [CreatedDate] IS NULL
Select Table Column Name where you want to get default value of current date:
ALTER TABLE [dbo].[Table_Name]
ADD CONSTRAINT [Constraint_Name]
DEFAULT (getdate()) FOR [Column_Name]
Alter table query:
Alter TABLE [dbo].[Table_Name]
(
[PDate] [datetime] Default GetDate()
)
you can also do this from SSMS GUI.
Put your table in design view (Right click on table in object
explorer->Design)
Add a column to the table (Click on the column you
want to update if it already exists)
In Column Properties, enter
(getdate()) in Default Value or Binding field as pictured below
create table stud(
Student_Id int primary key,
Student_Name varchar(30),
Student_surname varchar(12),
Student_Initial varchar(10))
I had created a table stud. Now i want to add Identity to Student_Id column using alter query
alter table stud alter column student_Id int identity
I get error as
Incorrect syntax near the keyword 'identity'.
ALTER TABLE MyTable
ADD ID INT IDENTITY(1,1) NOT NULL
You cannot make an already existing column as an IDENTITY column. Either you drop and recreate the table with the column marked as IDENTITY', or drop the column and add a newIDENTITY` column.
If Stud contains data, you could always make a shadow table, e.g. Stud2, which contains the Identity column, then run
ALTER TABLE dbo.stud SWITCH TO dbo.stud2
Then you can reseed Stud2, drop Stud, and rename Stud2 to Stud.
That way you can keep the data while dropping/recreating the table with Identity.
Syntax:
IDENTITY [ (seed , increment) ]
alter your table like as this:
create table stud(
Student_Id int IDENTITY(1,1) primary key,
Student_Name varchar(30),
Student_surname varchar(12),
Student_Initial varchar(10));
you can use below query to set identity
CREATE TABLE [dbo].[stud](
[Student_Id] [int] IDENTITY(1,1) NOT NULL,
[Student_Name] [varchar](30) NULL,
[Student_surname] [varchar](12) NULL,
[Student_Initial] [varchar](10) NULL,
PRIMARY KEY CLUSTERED
(
[Student_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
I have an SQL script for creating a table, i would like the default of all but a few columns to be "" the others require a integer default of 0
The following creates the table. Some columns are removed because there are lots
CREATE TABLE [dbo].[PMIPatients]
(
[PID] [varchar](30) NOT NULL,
[PatientFirstName] [varchar](30) NULL,
[PatientLastName] [varchar](30) NULL,
[PatientDOB] [varchar](30) NULL,
[PatientDoctor] [varchar](30) NULL,
[PatientDiet] [varchar](50) NULL,
[PatientFallRiskLevel] [int] NULL,
[BedId] [int] NULL,
[BedDisplayInfo] TEXT NOT NULL DEFAULT ''
CONSTRAINT [PK_HL7Patient] PRIMARY KEY CLUSTERED
([PID] ASC) WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
)
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
I wish to set a different default on selected columns, the following code does not work as it says that there is already a default constraint set. So i assumne i have to drop the constraint first.
ALTER TABLE [dbo].[PMIPatients] ADD
DEFAULT ((0))
FOR [PatientFallRiskLevel]
http://www.w3schools.com/sql/sql_default.asp says the follow code should be able to drop the DEFAULT like this
ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT
but i get a syntax error on DEFAULT
How do i alter/drop the DEFAULT constraint of specific columns
When you add a default, you should use names for your constraints. This way you can later refer to those constraints by name.
ALTER TABLE [dbo].[PMIPatients] ADD CONSTRAINT [PatientFallRiskLevel_Default] DEFAULT ((0)) FOR PatientFallRiskLevel
Then you can drop it using:
ALTER TABLE [dbo].[PMIPatients] DROP CONSTRAINT [PatientFallRiskLevel_Default]
The syntax is:
ALTER TABLE [dbo].[PMIPatients] ADD CONSTRAINT NameYourConstraint DEFAULT ((0)) FOR [PatientFallRiskLevel]
ALTER TABLE [dbo].[PMIPatients] DROP CONSTRAINT NameYourConstraint
When you create a constraint without a name, the SQL Server automatically create internal name. It can be see in the [sys].[default_constraints].
For example:
DROP TABLE IF EXISTS [dbo].[StackOverflow];
CREATE TABLE [dbo].[StackOverflow]
(
[colA] INT
,[colB] INT DEFAULT(0)
,[colC] CHAR(1) DEFAULT('x')
);
GO
SELECT *
FROM [sys].[default_constraints]
WHERE [parent_object_id] = OBJECT_ID('[dbo].[StackOverflow]');
GO
You can use the name to drop the constraint.
If you have more of them, you can use one of the scripts below:
-- all suported editions
DECLARE #DynamicTSQLStataement NVARCHAR(MAX)
,#TableNameWithSchema SYSNAME;
SET #TableNameWithSchema = '[dbo].[StackOverflow]';
SELECT #DynamicTSQLStataement = STUFF
(
(
SELECT ';ALTER TABLE ' + #TableNameWithSchema + ' DROP CONSTRAINT ' + QUOTENAME([name])
FROM [sys].[default_constraints]
WHERE [parent_object_id] = OBJECT_ID('[dbo].[StackOverflow]')
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1
,1
,''
);
EXEC sp_executesql #DynamicTSQLStataement;
GO
or
-- SQL Server 2016 or later
DECLARE #DynamicTSQLStataement NVARCHAR(MAX)
,#TableNameWithSchema SYSNAME;
SET #TableNameWithSchema = '[dbo].[StackOverflow]';
SELECT #DynamicTSQLStataement = STRING_AGG('ALTER TABLE ' + #TableNameWithSchema + ' DROP CONSTRAINT ' + QUOTENAME([name]), ';')
FROM [sys].[default_constraints]
WHERE [parent_object_id] = OBJECT_ID('[dbo].[StackOverflow]');
EXEC sp_executesql #DynamicTSQLStataement;
GO