SQLite - add a column if it does not exist - sql

I am new to SQLite. I want to add a column if it does not exist.
How to check if the column name exists and then add if it does not?
I tried
ALTER TABLE table ADD COLUMN colname INTEGER ON CONFLICT IGNORE
But it shows an error
Result: near "ON": syntax error
Any advice how it can be achieved?
Thanks in advance.

First get a list of table column names - as list - with something like:
select group_concat(c.name) from pragma_table_info('table_name') c;
Then do a CASE expression on whether the new column name you want to add exists in the list above. More info at: https://www.sqlite.org/lang_expr.html

Related

"Update" statement has "unrecognized name" problem on "Set"

When I try to update an existing table by inserting a new column, BigQuery states the error "Unrecognized name: start_geo_concat". This is odd because I already created a column in another table using this same query formula. I tried changing the name or casting as a STRING but that did also not work. Any ideas?
UPDATE
`red-freedom-357915.CyclisticBikeShare.BikeGeoData`
SET
start_geo_concat = CONCAT(slat_round, " , ", slng_round)
WHERE
ride_id IS NOT NULL
You say "inserting a new column". Does that mean that you're trying to create a new column using an UPDATE statement? If so, you can't do that. The column first has to exist in order to UPDATE it.
Try doing this first:
ALTER TABLE `red-freedom-357915.CyclisticBikeShare.BikeGeoData`
ADD COLUMN start_geo_concat STRING;

Syntax for explicitly referencing a column name

I'm trying to make a join to a date table on a column called 'date'.
The issue I have is that the column name is the same as the table and it seems to be making a referencing to the table rather than the column (hence the yellow highlighting). I've found a workaround which is to rename the table - though does anyone know how to explicitly reference the column in the join rather than the table if they have the same name?
You can give aliases to the table/column and use that instead. Otherwise, you can use the table name before the column with a . as a separator, i.e., TableName.date = date.

Select columns from table in schema different than public

In my PostgreSQL database I have table that is inside import schema. When I want to get all data from the column I do:
select * from import.master_plan
This query works fine. But when I try to for example get only title column values:
select import.master_plan.title from import.master_plan;
it returns:
ERROR: column master_plan.title does not exist
LINE 1: select import.master_plan.title from import.master_plan;
^
HINT: Perhaps you meant to reference the column "master_plan.  title".
I've also tried:
select title from import.master_plan;
but this also not works. I'm using PostgreSQL 10. How can I fix that?
I would suggest that you use a table alias instead:
select mp.title
from import.master_plan mp;
This is much easier to read and to type.
Judging from the error message, though, the name seems to have leading spaces. Something like:
select mp." title"
from import.master_plan mp;
might work. If this is the case, alter the table and rename the column.

Invalid Identifier/Ambiguously Defined

I have a SQL statement which ends in:
where <table_name>.<column_name> = '<column_value>'
I get the following error:
ORA-00904: "table_name"."column_name": invalid identifier
However, I know that the column is valid for sure. I also tried:
where <schema><table_name>.<column_name> = '<column_value>'
but got the same error. Lastly I tried without the identifiers:
where <column_name> = '<column_value>'
but that results in an column is ambiguously defined error.
What am I missing here?
Whole Query:
SELECT r.<COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc, t_append.*
FROM (
SELECT <COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc..
FROM <TABLE_NAME> ) r
inner join <TABLE_NAME> t_append on
t_append.<COLUMN_NAME_1> = r.<COLUMN_NAME_1>
AND t_append.<COLUMN_NAME_2> = r.<COLUMN_NAME_2>
AND etc...
WHERE <TABLE_NAME>.<COLUMN_NAME_1> = '<COLUMN_VALUE1>'
AND <TABLE_NAME>.<COLUMN_NAME_2> = '<COLUMN_VALUE2>'
This query takes composite key columns and value and then returns the composite key values followed by the row data which the key represents.
Apart from the above mentioned suggesstions,there may two possibilities according to me. You may get
ORA-00904: "table_name"."column_name": invalid identifier
1) if you don't have necessary permissions on the accessing objects. (confirm your permission on the object)
2) if your column was defined with double quotes like below
create table test("CheckMyColumn" number));
then it will be case sensitive. (Refer the table definition and try with same case)
The reason of a column ambiguously is because oracle doesn't know which column you are referring , it seems in your query you have specify 2 tables( from tab1 , tab2 ).
As for "table_name"."column_name": invalid identifier it means for sure column_name column for table table name doesn't exists, can you provide the ddl of the table.

In sqlite How to add column in table if same column is not exists in table

How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'