in Sql Server I have a table StudentMaster in which I have One ID(Primary) column and other "StudentRollNo"(Not Primary) Column . I need to convert StudentRollNo as Another Identity Column .. I Tried this :
ALTER TABLE studentMaster alter column StudentRollNo AS ID + 0
But this is not working 'AS' and '+' are not acceptable in this Query . Is there any other Option in Sql Server ??
CREATE SEQUENCE seq START WITH watever_number INCREMENT BY 1;
ALTER TABLE studentMaster ADD CONSTRAINT constraint_name
DEFAULT NEXT VALUE FOR seq FOR StudentRollNo
Replace watever_number with the highest value in your column
You can use SQL Computed column on your database table
alter table TableName add ComputedColumnName as Id -- Identity column name
Related
the table i used is ,
create or replace table abc(id int autoincrement, name varchar,date_of_birth date, age number, ts timestamp_ntz);
I used the below query to remove auto increment attribute in snowflake
alter table abc modify id int
here i have redefined the column name without the auto increment attribute . it works fine in mysql workbench but it is not working in snowflake .
is there is some other way to disable/remove auto increment attribute in snowflake?
"Drop default" is doing the trick:
alter table abc alter id drop default;
I already created table in database. Now, I need to add "Identity" Column. Please suggest.
Create Cus(id int Pk,Name varchar2(50),Age int);
insert into Cus(id,Name,Age) values (1,'abc',12);
// here i need to add "Identity"
alter table Cus alter column id Identity(1,1)
You cannot use Alter command to add an identity to the table.
Here, you need to create dummy column and drop existing one.
Create table Cus(id int ,[Name] varchar(50),Age int);
insert into Cus(id,[Name],Age) values (1,'abc',12);
Alter Table Cus Add dummyid int identity(1,1)
Alter Table Cus Drop Column id
Exec sp_rename 'Cus.dummyid ', 'id', 'Column'
No you cannot make any column identity after creating from the query.
You have 2 options, either make it from SQL Management Studio or Create another column and copy with identity .
From Management Studio.
Step 1: Select Table design.
Step 2: Change Column properties.
Step 3: Save
Or
You need to create new column with identity.
Create column with identity `Alter table Tablename add newcol int identity(1,1)
Then copy your data from previous column to this column by setting Identity_Insert ON.
Then drop your previous column.
After that change column name by using sp_rename.
Note: But this will change the ordinal position of your column.
ANOTHER OPTION
Create new table with similar structure just make your column
identity whichever you want to be.
Copy data from your old table to new table.
Drop old table.
Change name of new table with old table.
Edit:
For case of Foreign Key relationship
If they are not so many and feasible, then you may drop the constraint.
ALTER TABLE Yourtable
DROP FOREIGN KEY FK_PersonOrder;
Then follow the above steps and recreate them at the last.
ALTER TABLE Yourtable
ADD FOREIGN KEY (yourid) REFERENCES Persons(PersonID);
Finally i got Solution,
I added new column in 'Cus' table.
alter table Cus add tempCusId int identity;
i removed FK relation in User's Table
and i updated identity values in User Table
update user set id=1 where id= 1;
I Compared Id and TempCusId. After update I removed "Pk" relation in Cus table droped Column "Id",i made "TempCusId" as a "Pk" and identity. Finally User table it self "Id" Column I made FK relation.
And if u have multiple values there than go for a "While" loop
DECLARE #NumberofRowint int=30;
DECLARE #inirow INT=23;
Declare #Grade int ;
WHILE #inirow<= #NumberofRow
BEGIN
DECLARE #ProductID INT=(select Id from [Cus] where id=#inirow)
Set #Grade=(select id from Cus where id=#ProductID)
IF(#Grade= #inirow)
BEGIN
updatetbl_Users set Id=#inirow where id=#ProductID
END
SET #inirow = #inirow + 1;
END;
In my database I have a table with columns col1,col2,col3.
I want to give the fourth column i.e..,col4 from the front end(when I click add column button in my front end).
Now the updated table should be like col1,col2,col3,col4 after altering the fourth column.
Is it possible to alter a column from Frontend? If possible what is the stored procedure/query for that?
From the front-end, you can send the following query.
ALTER TABLE table_name
ADD column_name datatype
For more information on simple Alter statement, , click here:
SQL ALTER TABLE STATEMENT
Hope. This is very helpful.
ALTER TABLE dbo.doc_exy ALTER COLUMN column_a DECIMAL (5, 2) ;
I have a table in MSSQL server 2008. I would like to change one of the column in that table to computed column. Could somebody tell me how do I do that ?
Preserve the old data:
EXEC sp_rename 'MyTable.OldCol', 'RenamedOldCol', 'COLUMN';
Add computed column
ALTER TABLE MyTable ADD ComputedCol AS (some expression);
Then, when you're happy
ALTER TABLE MyTable DROP COLUMN RenamedOldCol;
I have a persisted computed column in a large table in in SQL Server 2005.
I want to convert it to a regular column, keeping current values.
Do I have to recreate the column and update the entire table in transaction,
or is it possible to just alter a computed column specification, and how to do it?
-- Create a new Column (unpersisted):
ALTER TABLE MyTable
ADD newColumn DatatypeOfPersistedColumn
GO
UPDATE myTable
SET newColumn = PersistedColumn
GO
-- Delete the persisted column
ALTER TABLE MyTable
DROP COLUMN PersistedColumn
GO
-- Rename new column to old name
EXEC sp_rename 'MyTable.newColumn', 'PersistedColumn', 'COLUMN'
GO
The poster wants to keep the name of the column. I have added a line at the end of Mitch's code to do a rename after dropping the PersistedColumn
-- Create a new Column (unpersisted):
ALTER TABLE MyTable
ADD newColumn DatatypeOfPersistedColumn
UPDATE myTable
SET newColumn = PersistedColumn
-- Delete the persisted column
ALTER TABLE MyTable
DROP COLUMN PersistedColumn
-- Rename the new column to the old name
EXEC sp_rename 'MyTable.newColumn', 'PersistedColumn', 'COLUMN'
#Mitch Wheat's solution works great. However, occasionally this will throw an error with 'Invalid Column name: newColumn' because the table has not been updated before it tries to run the update.
To fix this, add a GO statement to separate the two into batches:
-- Create a new Column (unpersisted):
ALTER TABLE MyTable
ADD newColumn DatatypeOfPersistedColumn
GO
UPDATE myTable
SET newColumn = PersistedColumn
-- Delete the persisted column
ALTER TABLE MyTable
DROP COLUMN PersistedColumn
-- Rename new column to old name
EXEC sp_rename 'MyTable.newColumn', 'PersistedColumn', 'COLUMN'
Assuming that the reason for converting the computed column into a "real" column is that you want to keep the existing values/functionality, but add the ability to override it where desired, you could add a new column (to be populated only where the existing derived value is to be overridden), and change the definition of the computed column to be COALESCE(NewColumn, Old Calculation Definition ) .
Just remove the formula from "Computed Column Specifications" in desing mode of the table in SSMS. The values will stay in column as it is.