Alter Table in SQL [duplicate] - sql

I have a PostgreSQL (9.0) database with a column card_id which is currently of type integer
I need to change this to type text
What is the most best way to achieve this?
The only solution I can find involves creating a temporary column, dropping the original then renaming, I thought they might be a better method??

Have you tried what the fine manual suggests:
ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
Depending on the current and the new type you may need to add USING ... to this statement.
But in your specific case that should not be necessary I believe.

ALTER TABLE table ALTER COLUMN card_id SET DATA TYPE text;

Related

Rename table via sp_rename or ALTER SCHEMA

I am going to perform a table-wide update on a huge table (+200Millon records) so I am going to populate the data via SELECT into a separate table, than drop the original and rename the new one.
In one of the articles someone mentioned that it is better to create the new table with the same name in a temporary schema (e.g. 'clone') and switch it to the used one (e.g. 'dbo'), than to use the original schema with a temporary name and call sp_rename after the data is in place and the old table is dropped.
I was looking into this, but I cannot think of anything why the schema switch is better than the sp_rename. Can anyone find any good reason why is better to use the first or the second approach?
Thanks!
EDIT: I want to update the values in a specific column
EDIT2: Ultimately my question is, if I decide to go down the way of creating a new table to transfer data to which alternative to use:
CREATE TABLE dbo.newTable
...
DROP TABLE dbo.originalTable
EXEC sp_rename N'dbo.newTable', N'dbo.originalTable'
OR
CREATE TABLE clone.originalTable
...
DROP TABLE dbo.originalTable
ALTER SCHEMA dbo TRANSFER clone.originalTable
By the way, I would suggest that you WON'T populate the table by using SELECT * INTO. This will lock your source table for everyone else during the insertion, which could take quite a time.
Just a suggestion, try this instead:
SELECT TOP 0 INTO [newTable]
FROM [oldTable]
INSERT INTO [newTable]
SELECT * FROM [oldTable]
By the way, you can use sp_rename to rename your table to another name. But it won't change the schema. If you try to change the schema too it will produce a buggy table name.
You can instead try to move the table to another name. Example below:
EXEC sp_rename N'oldTable', N'oldTable_Backup'
EXEC sp_rename N'newTable', N'oldTable'
Hopefully this will help you.
Based on your edited answer the quickest way to do that is:
If you have to include default value to the column
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
[WITH VALUES]
and then drop the old column from the table.
ALTER TABLE {TABLENAME} DROP COLUMN {OLD COLUMN}
If you have to update table column based calculated values
Disable index on the column which you are updating
Create index on the column which are in WHERE clause
Update statistics
Use WITH(NOLOCK) table hint [if you are fine with dirty read]
Update
As per edit 2, your first statement is about changing table name and second statement is about changing schema. They both are different and does not related to moving data or updating value. In this case, changing schema would be the best bet
If locking was an issue before it still is regardless of what version SQL Server that you are using. When you drop and rename you are also losing all the rights on the table.

Alter the data type of a column in MonetDB

How can I alter the type of a column in an existing table in MonetDB? According to the documentation the code should be something like
ALTER TABLE <tablename> ALTER COLUMN <columnname> SET ...
but then I am basically lost because I do not know which standard the SQL used by MonetDB follows here and I get a syntax error. If this statement is not possible I would be grateful for a workaround that is not too slow for large (order of 10^9 records) tables.
Note: I ran into this problem while doing some bulk data imports from csv files into a table in my database. One of the columns is of type INT but the values in the file at some point exceed the INT limit of 2^31-1 (yes, the table is big) and so the transaction aborts. After I found out the reason for this failure, I wanted to change it to BIGINT but all versions of SQL code I tried failed.
This is currently not supported. However, there is a workaround:
Example table for this example, say we want to change the type of column b from integer to double.
create table a(b integer);
insert into a values(42);
Create a temporary column alter table a add column b2 double;
Set data in temporary column to original data update a set b2=b;
Remove the original column alter table a drop column b;
Re-create the original column with the new type alter table a add column b double;
Move data from temporary column to new column update a set b=b2;
Drop the temporary column alter table a drop column b2;
Profit
Note that this will change the ordering of columns if there are more than one. However, this is only a cosmetic issue.

Stop allowing null in a table column

I have a table in SQL server 2008.
I need a column that until now wasn't necessary, not to allow NULL anymore, without droping the table.
I tried to do something like this:
ALTER TABLE [Sessions] ALTER COLUMN region_id NOT NULL;
EDIT: It was a small syntax error. solved.
You have to specify the data type as well when you alter a column:
ALTER TABLE [Sessions] ALTER COLUMN region_id int /* ? */ NOT NULL;
you are missing datatype.
ALTER TABLE [Sessions] ALTER COLUMN region_id int NOT NULL;
You have to first set all values that are NULL to a non NULL value:
UPDATE [Sessions]
SET region_id=-1
WHERE region_id IS NULL
Instead of -1 you should choose something that would represent the formerly NULL values so you may distinguish them.
To get away with saving prevention you may go on:
Tools>Options>Designers> and unclick Prevent Saving Changes that require table re-creation
First off to save table chances like this you need to either script the changes, or when saving from the UI, you need to set SSMS to drop and recreate table for you. To do this;
Go to Tools
Options
Designers
UNTICK prevent saving changes that require table re-creations.
If you script the changes, you will need to first alter the table, and add a new column allowing nulls. Then update the column and remove nulls and then after you will be able to set the column to NOT NULL.
If you follow these steps you will solve your issue.

Oracle SQL. How to change table field datatype CLOB-->VARCHAR2

I have database and some text fields are CLOB type, I need to change most of these into VARCHAR2.
Have tried to do that using SQL Developer tool, by clicking edit on table, but get error like this one:
The following SQL statement failed:
ALTER TABLE TBL_PEOPLE MODIFY (PERSON VARCHAR2(150) )
Want to ask, how can this change be done
You can't, directly. The code you tried will have got an `ORA-22859, presumably. (It's helpful to show the actual errors you get, of course).
You'll need to add a new varchar2 column; copy the data across - or a substring of it if it might be larger than the new column you're creating; drop the clob column. You can rename the columns so it looks fairly transparent.
As in this SQL Fiddle:
alter table tbl_people rename column person to clob_person;
alter table tbl_people add (person varchar2(150));
update tbl_people set person = clob_person;
alter table tbl_people drop column clob_person;
Obviously don't drop the old column until you're sure the data has copied without errors. Also take into account any constraints, indexes, etc. that might exist in the old column; they will need to be recreated. And anything that references the old column will have been invalidated - generally procedures will recompile themselves on next use.
So be careful, test it first, and plan some down time.
ALTER TABLE tablename ADD (FIELD_LIST_TEMP VARCHAR2);
UPDATE tablename SET FIELD_LIST_TEMP = FIELD_LIST;
ALTER TABLE tablename DROP COLUMN FIELD_LIST;
ALTER TABLE tablename RENAME COLUMN FIELD_LIST_TEMP TO FIELD_LIST;
Here FIELD_LIST existing column which is defined it as CLOB. With above query it will change from CLOB to VARCHAR2.

PostgreSQL Convert column from integer to text

I have a PostgreSQL (9.0) database with a column card_id which is currently of type integer
I need to change this to type text
What is the most best way to achieve this?
The only solution I can find involves creating a temporary column, dropping the original then renaming, I thought they might be a better method??
Have you tried what the fine manual suggests:
ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
Depending on the current and the new type you may need to add USING ... to this statement.
But in your specific case that should not be necessary I believe.
ALTER TABLE table ALTER COLUMN card_id SET DATA TYPE text;