Can table in psql be named with 'references' - psql

I'm wondering can I rename a table to "references"? I want to name a table back to "references" but there is a syntax error. Is there anyway to fix it?
alter table referencesc rename to references;
ERROR: syntax error at or near "references"
LINE 1: alter table referencesc rename to references;

The issue is probably being caused by that references is a keyword in Postgres. To get around this, you can try escaping the table name in double quotes:
alter table referencesc rename to "references";
But in general you should avoid naming columns, tables, etc. using reserved keywords, because you will likely have to escape such names everywhere.

Related

DB2 stored procedure - changing the table structure

I have to add a new column to one of the existing DB2 tables. If I do so, do I need to recompile all the stored procedures accessing that table? What happens if I don't do that?
Documentation is pretty clear on that:
Adding a column to a table will result in invalidation of all packages with INSERT usage on the altered table. If the added column is the first user-defined structured type column in the table, packages with DELETE usage on the altered table will also be invalidated.
If the new column has a constraint on it, packages with other uses of the table may be invalidated as well.
Invalid packages will be automatically revalidated on their next use. In other words, you don't need to do anything.

How to delete specific table's column using FMDB?

I am trying to delete column last_name from Persons using FMDB,
let query = "ALTER TABLE Persons DROP COLUMN last_name;"
try FMDBHelper.database.executeUpdate(query, values: nil)
But comes with error
DB Error: 1 "near "DROP": syntax error".
sqlite does not support DROP COLUMN in ALTER TABLE.
You can only rename tables and add columns.
If you need to remove columns, create a new table, copy the data there, drop the old table and rename the table to its intented name.
Reference: http://www.sqlite.org/lang_altertable.html
Please note that I flagged that your question could be duplicated, I will provide an answer to make it more clear.
I think that you are missing a point, which is: The FMDB is (as mentioned in their repo description):
This is an Objective-C wrapper around SQLite
Keep in mind that since FMDB is built on top of SQLite, it is not a limitation from the library itself; it is related to how SQLite ALTER TABLE works.
The SQLite ALTER TABLE statement is limited to rename a table and add a new column to the desired table:
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE
command in SQLite allows the user to rename a table or to add a new
column to an existing table.
http://www.sqlite.org/lang_altertable.html
For achieving what are you looking for:
You could check the answers of Delete column from SQLite table.

Rollback migration that changes column type from string to text where db is postgresql in rails 3.2

I have understood the solution for changing the column type from string to text while using postgresql and rails 3.2 provided here. I have also implemented it. But when I rollback this migration, it fails with "PG::StringDataRightTruncation: ERROR: value too long" error. How should we tackle this problem?
You have new values that're too long for the old type. PostgreSQL would have to throw away data to change to varchar(255) if the values are longer than 255 chars. It refuses to do so because it won't cause data loss without being told very specifically to do so.
If you don't mind truncating these long values, permanently and unrecoverably discarding data, you can use the USING clause of ALTER COLUMN ... TYPE. This is the same approach used when converting string columns to integer.
ALTER TABLE mytable
ALTER COLUMN mycolumn
TYPE varchar(255)
USING (substring(mycolumn from 1 for 255));
I don't think there is any way to express this symmetrically in a Rails migration; you will need separate clauses for the up- and down- migrations, with the up-migration being a simple:
ALTER TABLE mytable
ALTER COLUMN mycolumn
TYPE text;
Frankly though, I think it's a terrible idea to do this in a migration. The migration should fail. This action should require administrator intervention to UPDATE the columns that have data that is too long, then run the migration.

Most efficient way to duplicate a row in Sqlite for iOS

What is the most efficient way to duplicate a row in an Sqlite3 database exactly except with an updated PrimaryKey?
Use an insert .. select where both the insert and select reference the same relation. Then the entire operation will occur as a single statement within SQLite code itself which cannot be beaten for "efficiency".
It will be easier with an auto-PK (just don't select the PK column), although an appropriate natural key can be assigned as well if such can be derived.
See SQLite INSERT SELECT Query Results into Existing Table?

ActiveRecord and illegal column names

I want to access the Limesurvey database via ActiveRecord. Some tables have column names like '79924X192X1240'. When I want to access the model, I get the following error:
ActionView::Template::Error (/usr/local/rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.1.10/lib/active_record/attribute_methods/time_zone_conversion.rb:44: syntax error, unexpected tINTEGER
def 79924X192X1240=(original_time)
^):
I guess, the error appears, because it's not allowed, that a method starts with a digit. But I can't alter the column name, because Limesurvey generates these columns.
This error appears only if the data type of a column (like 79924X192X1240) is datetime. Other data types like varchar don't make any problems.
How can I access these datetime columns, too?
You need to do raw sql queries for that. ActiveRecord is written for ruby which does not allow method names to start with numbers. Since each column on the table is equivalent to a method, then trying to access that information will throw a syntax error every time.
You need to use: ActiveRecord::Base.connection.execute

Resources