rename a column starting with # in Postgresql - sql

I add some shape files in Postgres and there is a column which is named #id. I wanted to select this column but there is a syntax error because of #.
I have many tables which containing this field, how can I change it in not manually way?

You actually can select #id by either using the unicode character or by surrounding the column name with double-quotes:
SELECT U&"\0040id" FROM tablename;
SELECT "#id" FROM tablename;
You can use either method to rename the column as well:
ALTER TABLE tablename RENAME COLUMN U&"\0040id" to "id";
ALTER TABLE tablename RENAME COLUMN "#id" to "id";

The principle is the same, you just have to make sure you have the name enclosed in double quotes.
ALTER TABLE "some_table"
RENAME COLUMN "old_name" TO "new_name";

Related

How to select value from Oracle table having field name as default keyword

We have Oracle table having default keyword(i.e in as field name) field name.Now i am querying table but unable to extract specific field data.
select a.filename,a.in from table a
Following error appears "invalid field name.
Try using double quotes.
select a."IN" from table a
You can use default (oracle reserved) keywords as the name of the columns but yes it is not advisable to use it.
Anyway, If you want to use oracle reserved keywords then you must have to enclose them in the double-quotes.
Note that oracle is case insensitive in terms of its object names until and unless it is wrapped in the double-quotes. it means if you enclose any object name in double-quotes then you must have to use them anywhere in the entire DB as case sensitive manner.
So if your table definition is:
CREATE TABLE YOUR_TABLE ("IN" NUMBER);
Then you need to use "IN" wherever you want to refer the column but if your table definition is:
CREATE TABLE YOUR_TABLE ("in" NUMBER);
Then you need to use "in" wherever you want to refer the column. -- case sensitive names.
I hope it will clear all your doubts.
Cheers!!

How To Reference a Column that has been concatenated in SQL

How do you reference a column in a table that has been concatenated? I am trying to reference the 'UniqueID' column in a join, but all the ways that I have tried it throw the ORA-00904 error saying "T2.UNIQUE ID:Invalid identifier".
create table cdm_user.uniquesubjectIDDEW as (
select distinct concat (site,screening_no) "UniqueID" , visit, site, Screening_no
from databrowser.v_data_entry_workflow
where study = '3508'
);
commit;
Select *
from cdm_user.uniquesubjectIDDEW t1
left join cdm_user.uniquesubjectIDDEW t2
on t1.UniqueID = t2.UniqueID
and t2.visit = 'Screening'
Where t1.visit = 'Week_52'
and t2.visit is null
Any help is much appreciated as I am new to SQL.
Unless quoted, identifiers such as table and columns names will be mapped to upper case. So your select will be interpreted as needing a column name UNIQUEID but you created the column name as "UniqueId" with quotes so it doesn't match.
You'll need to either unquote the name when you create the table or quote it in all queries.
Generally it is better not to use quoted, case-sensitive column names, which is why a lot of databases use underscores in table/column names as word separators rather than some variant of camel case.
Use quoted column name in createing command:
alter table cdm_user.uniquesubjectIDDEW add primary key ("UniqueID");

"syntax error at or near 'order'"" in PostgreSQL

I'm trying to add a column named order to my table. I realize that order is a reserved word in SQL. So, how do I do it?
My command:
alter table mytable add column order integer;
I've also tried:
alter table mytable add column 'order' integer;
PostgreSQL 9.1.
Use this:
alter table mytable add column "order" integer;
But, you might want to consider using a non-reserved name instead, like sort_order or something similar that reflects what the column is used for (and isn't a reserved word).
I think you don't need "column". Plus "order" is a keyword in SQL, so you should use a different name for your column. Follow this syntax:
ALTER TABLE table_name ADD column_name datatype
Source: W3Schools
ALTER TABLE table_name
ADD COLUMN "order" integer
You are using order which is a reserved keyword you should consider renaming that to something like orders. And the problem should go away.

What is the following sql statement giving an error?

I get an error when i try to do this using SQL. ftp.server is the key whose value i would like to change:
UPDATE OL_PREF SET ftp.server='dev.isinet.com'
If the column name is really ftp.server, then the correct Oracle syntax for updating it should be
UPDATE OL_PREF SET "ftp.server" ='dev.isinet.com'
In Oracle, double quotes are the correct way to handle column names that have non-standard characters, including lower-case letters and the period. Note that you must have the exact correct column name for this to work, including case.
You can verify the column name with:
SELECT column_name FROM user_tab_columns WHERE table_name='OL_PREF' ORDER BY column_name;
If what you really mean is that you have a table that stores key-value pairs, and that 'ftp.server' is a key, then you probably want an update like the one in Mark Wilkins' answer.
UPDATE OL_PREF SET [ftp.server]='dev.isinet.com'
or
UPDATE OL_PREF SET [ftp].[server]='dev.isinet.com'
if ftp is a schema and server is the fieldname.
You can't put the column name in quotes.
You could use brackets, which have the same functionality as quotes.
UPDATE OL_PREF SET [ftp.server] ='dev.isinet.com'
Also, it's a good idea to use "two part naming", specifying the schema as well:
UPDATE dbo.OL_PREF SET [ftp.server] ='dev.isinet.com'
UPDATE OL_PREF
SET ftp.server ='dev.isinet.com'
no '' for column names
SQL Update
'ftp.server' is in quotes. (and that should not be)
Are you getting the error ORA-00904 string : invalid identifier?
If so read this
I did a quick test on this
SQL> create table t_tab_1 ( tname varchar2(3), "ftp.service" varchar2(20) );
Table created.
SQL> insert into t_tab_1(tname,"ftp.service") values('21','ftp://fila');
1 row created.
SQL> update t_tab_1 set "ftp.service"='fila.real.com';
1 row updated.
If there is more than I row in your table remember to use the where clause in your update statement.

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'