Is it possible to alter table add MULTIPLE columns in a single statement in sqlite?
The following would not work.
alter table test add column mycolumn1 text, add column mycolumn2 text;
No, you have to add them one at a time. See the syntax diagram at the top of SQLite's ALTER TABLE documentation:
There's no loop in the ADD branch so no repetition is allowed.
The only thing so far possible that I use is
BEGIN TRANSACTION;
ALTER TABLE tblName ADD ColumnNameA TEXT DEFAULT '';
ALTER TABLE tblName ADD ColumnNameB TEXT DEFAULT '';
ALTER TABLE tblName ADD ColumnNameC TEXT DEFAULT '';
COMMIT
Note that there are ; on purpose to make the query be read as multiple lines.
Then I run this query and get multiple columns added in on run... So no not in one line, but yes in one query its possible.
The answer from '#mu is too short' is right. Providing an optimized solution for adding multiple columns using the transactions feature in SQL.
String alterTableQuery = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN ";
List<String> newColumns = ..// Your new columns
db.beginTransaction();
for (String column : newColumns){
db.execSQL(alterTableQuery + column + " VARCHAR");
}
db.setTransactionSuccessful();
db.endTransaction();
I hope this will help someone.
alter table test add column mycolumn1 text; alter table test add column mycolumn2 text;
use the above redifined query
Related
I want to update and concatenate 3 columns in a new a attribute I've already created in my table. However when I execute my code, it puts attributes that before had values in null.
My code example:
update saber2012_1
set estu_fecha_nacimiento = Concat(estu_nacimiento_dia,'/',estu_nacimiento_mes,'/',estu_nacimiento_anno);
In this picture all values that is in null before these had values.
when I make this update
ALTER TABLE saber2012_uno ADD COLUMN punt_c_naturales int;
update saber2012_uno set punt_c_naturales = round((PUNT_BIOLOGIA::numeric + PUNT_FISICA::numeric + PUNT_QUIMICA::numeric) / 3,0);
alter table saber2012_uno DROP COLUMN PUNT_BIOLOGIA;
alter table saber2012_uno DROP COLUMN PUNT_FISICA;
alter table saber2012_uno DROP COLUMN PUNT_QUIMICA;
all is well. but I do not know because previous update is incorrect.
Perhaps you want concat_ws():
update saber2012_1
set estu_fecha_nacimiento = concat_ws('/', estu_nacimiento_dia, estu_nacimiento_mes, estu_nacimiento_anno);
This will ignore any of the arguments that are NULL.
However, your update will not change any other columns in the table. Perhaps the results are just coming back in a different order, when you query after the update.
Imagine that I have a table named "TableA" and this table has the columns "IDtable", "Tabname" and "Suffix".
IDtable and Tabname are correctly filled, but the suffix has all the records null.
I have to develop a stored procedure which allows to fill the Suffix column. For that, I know that I have to filter the name of the records in tabname.
Example:
Tabname: a_type_price
Suffix: price
Tabname: a_d_ser_sales
Suffix: sales
I think that I have to develop a for cycle, which looks for the " _ " in the names of Tabname and filters everything after the last " _ ".
Anyone have any idea of the best way to perform this stored procedure?
You'll want to update the table - you don't even really need a stored procedure unless this is something you ahve to run as a job or frequently.
You could do so like this:
CREATE PROCEDURE usp_Upd_TableASuffix()
AS
BEGIN
SET NOCOUNT ON
UPDATE TableA SET Suffix = RIGHT(Tabname, CHARINDEX('_', REVERSE(Tabname)) - 1)
END
Quick explanation of that: You want the RIGHT part of the string after the last _ character - to get the last one, use CHARINDEX on the REVERSE of the string.
You can't do this with a default constraint since you'd have to reference another column, but you could use a trigger, assuming IDtable is your primary key:
CREATE TRIGGER trg_TableA_Suffix
ON TableA
AFTER INSERT, UPDATE
AS
UPDATE TableA SET Suffix = RIGHT(i.Tabname, CHARINDEX('_',REVERSE(i.Tabname))-1)
FROM inserted i
WHERE TableA.IDTable = i.IDTable
GO
Note that this won't work if IDTable is non-unique, or if your table lacks a primary key entirely.
One last option - and this is probably the best if you really want Suffix to only ever contain the last part of Tabname and never want to change it - you could make Suffix a computed column.
ALTER TABLE TableA DROP COLUMN Suffix
ALTER TABLE TableA ADD Suffix AS RIGHT(Tabname, CHARINDEX('_',REVERSE(Tabname))-1) PERSISTED
For informix.
As #dan-field said, and explain, you can do the update using:
UPDATE tablea
SET suffix = RIGHT(tabname, CHARINDEX('_', REVERSE(tabname)) - 1);
Another way is:
UPDATE tablea
SET suffix = SUBSTR(tabname, INSTR(tabname, '_', -1)+1);
INSTR will give you the index of the first occurrence from the right (-1) of ''. SUBSTR will give you the substring starting from the index you pass, in this case we add 1 because we don't want the '' to be outputted.
Bear in mind that RIGHT, CHARINDEX, REVERSE and INSTR functions are only available from 11.70 onward.
On 11.50 you can try this ugly solution:
CREATE PROCEDURE test()
DEFINE i_IDtable LIKE tablea.IDtable;
DEFINE c_suffix LIKE tablea.tabname;
DEFINE i INT;
FOREACH cur1 WITH HOLD FOR
SELECT IDtable ,Tabname
INTO i_IDtable, c_suffix
FROM tablea
LET i = LENGTH(c_suffix);
WHILE i > 0
IF SUBSTR(c_suffix,i) LIKE '\_%' THEN
LET c_suffix = SUBSTR(c_suffix,i+1);
EXIT WHILE;
ELSE
LET i = i -1;
END IF
END WHILE;
UPDATE tablea
SET suffix = c_suffix,
WHERE idtable = i_IDtable;
END FOREACH;
END PROCEDURE;
But if suffix is always this part of the tabname it's not a good practice to store on the table.
You can easily get it from the already stored tabname. And even programmatically is easy to deal with it.
I created a table and inserted 3 rows. Then I added a new column using alter. How can I add values to the column without using any null values?
Two solutions.
Provide a default value for the column. This value will be used initially for all existing rows. The exact syntax depends on your database, but will will usually look like ..
this:
ALTER TABLE YourTable
ADD YourNewColumn INT NOT NULL
DEFAULT 10
WITH VALUES;
Add the column with null values first. Then update all rows to enter the values you want.
Like so:
ALTER TABLE YourTable
ADD YourNewColumn INT NULL;
UPDATE YourTable SET YourNewColumn = 10; -- Or some more complex expression
Then, if you need to, alter the column to make it not null:
ALTER TABLE YourTable ALTER COLUMN YourNewColumn NOT NULL;
Why don't you use UPDATE statement:
UPDATE tablename SET column=value <WHERE ...>
WHERE is optional. For instance in T-SQL for table:
I can update column NewTestColumn by this statement:
UPDATE [dbo].[Table] SET [NewTestColumn] = 'Some value'
Suppose you have a Employee table with these columns Employee_ID, Emp_Name,Emp_Email initially. Later you decide to add Emp_Department column to this table. To enter values to this column, you can use the following query :
Update *Table_Name* set *NewlyAddedColumnName*=Value where *Columname(primary key column)*=value
Example update TblEmployee set Emp_Department='Marketing' where Emp_ID='101'
I think below SQL useful to you
update table_name set newly_added_column_name = value;
update table_name
set new_column=value
Update table_name set column_name = value where 'condition';
suppose emp is the table and Comm is the new column then fire the below query .
update emp set Comm=5000
For Microsoft SQL (T-SQL):
UPDATE TABLE_NAME SET COLUMN_NAME=10;
here 10 means it will set all values by default to 10
I would like to set the default value for every column in a number of tables equal to Null. I can view the default constraint under information_schema.columns.column_default. When I try to run
update information_schema.columns set column_default = Null where table_name = '[table]'
it throws "ERROR: cannot update a view HINT: You need an unconditional ON UPDATE DO INSTEAD rule."
What is the best way to go about this?
You need to run an ALTER TABLE statement for each column. Never ever try to do something like that by manipulating system tables (even if you find the correct one - INFORMATION_SCHEMA only contains view to the real system tables)
But you can generate all needed ALTER TABLE statements based on the data in the information_schema views:
SELECT 'ALTER TABLE '||table_name||' ALTER COLUMN '||column_name||' SET DEFAULT NULL;'
FROM information_schema.columns
WHERE table_name = 'foo';
Save the output as a SQL script and then run that script (don't forget to commit the changes)
I am trying to alter a datatype for a derby db column. The current price column is set as DECIMAL(5,0). I would like to alter it to DECIMAL(7,2). I did this :
alter table item alter column price set data type DECIMAL(7,2);
But it did not work, and showing the error:
Error: Only columns of type VARCHAR may have their length altered.
May I know how is it possible to alter it? Thank you.
Here is the Derby SQL script to change column MY_TABLE.MY_COLUMN from BLOB(255) to BLOB(2147483647):
ALTER TABLE MY_TABLE ADD COLUMN NEW_COLUMN BLOB(2147483647);
UPDATE MY_TABLE SET NEW_COLUMN=MY_COLUMN;
ALTER TABLE MY_TABLE DROP COLUMN MY_COLUMN;
RENAME COLUMN MY_TABLE.NEW_COLUMN TO MY_COLUMN;
I think you can do like this:
ALTER TABLE SCHEMA.TABLE ALTER "COLUMN-NAME" SET DATA TYPE VARCHAR(255);
(column-Name SET DATA TYPE VARCHAR(integer)) for Datatype String as an example...
Here's a slightly more complicated way to alter the column's data type in this fashion:
Add a new column, of the desired data type
Issue "update ... set new-column = old-column to copy the data from the old column to the new column
drop the old column
Rename the new column to have the name of the old column.
Slightly more steps, but in the end the effect will be the same.
If you have trouble working out the exact details of the SQL to do this, let us know and we'll help.
You can alter table like this:
ALTER TABLE [table] ALTER COLUMN [column] SET DATA TYPE [type];
Or in Rails, just use:
change_column :table_name, :column_name, :integer
Posgtes Solution :
ALTER TABLE prices_table ALTER price_column TYPE decimal (7,2 )