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

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.

Related

Aliasing column names in SQL server

Is there a way in SQL server to alias column names for a particular table and store the aliases somewhere such that you can access the aliases while querying? I have a table where I cannot change column names and I am trying to figure out if there is a way to alias them to make them more user friendly.
CREATE VIEW vw_RenameTable
AS
Begin
Select GoodName1 = DumbName1
,GoodName2 = DumbName2
From MyTable
End
A view would be the natural answer to the question. But, if you want to access the columns in the same table, you can use computed columns:
alter table mytable add bettercolumn as [Bad Ugly Name];
You can then use the computed column in a select on the same table.

rename a column starting with # in Postgresql

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";

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'

Postgresql - Using subqueries with alter sequence expressions

Is it possible to use subqueries within alter expressions in PostgreSQL?
I want to alter a sequence value based on a primary key column value.
I tried using the following expression, but it wouldn't execute.
alter sequence public.sequenceX restart with (select max(table_id)+1 from table)
I don't believe you can do it like that but you should be able to use the setval function direction which is what the alter does.
select setval('sequenceX', (select max(table_id)+1 from table), false)
The false will make it return the next sequence number as exactly what is given.
In addition if you have mixed case object names and you're getting an error like this:
ERROR: relation "public.mytable_id_seq" does not exist
... the following version using regclass should be useful:
select setval('"public"."MyTable_Id_seq"'::regclass, (select MAX("Id") FROM "public"."MyTable"))