I created a table (using NetBeans I went to Databases, into jdbc, into app and created a table). I am 80% sure this is a SQL table but I could be wrong.
I named one of my columns as secretQuestion but now I need to change it to securityQuestion.
I looked online and found the following:
ALTER table app.mytable CHANGE secretQuestion to securityQuestion;
ALTER table app.mytable RENAME secretQuestion to securityQuestion varchar (100);
neither CHANGE nor RENAME are recognized.
I am a slow-witted newbie so be very specific in your answer so that I can follow along!
I am using Derby!
See here.
RENAME COLUMN statement
Syntax
RENAME COLUMN table-Name.simple-Column-Name TO simple-Column-Name
Examples
To rename the manager column in table employee to supervisor, use the
following syntax:
RENAME COLUMN EMPLOYEE.MANAGER TO SUPERVISOR
i have one syntax please remove 'to' keyword. it worked for me. Be sure about the table name and old column name
ALTER TABLE tablename CHANGE name newname DATATYPE;
Related
In my Java app, I have the following migration file:
-- code omitted for brevity
create table if not exists demo_table
(
id bigint not null,
"company" varchar(50) not null,
"name" varchar(50) not null
);
create unique index if not exists demo_table_uuid_company_key
on demo_table (uuid, "company");
create index if not exists demo_table_name_company_key
on demo_table ("name", "company");
Although I can run the sql part part part or at a time on PostgreSQL query window, when running my Java app, it throws the following error:
"Unable to create index (name, company) on table demo_table: database column 'name' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)"
I tried many thing e.g. removing the related migration row from flyway_schema_history table, delete indexes on demo_table, etc. But still the same error. If I try to remove double quotes ("") from name, it gives checksum error. So, as the name is reserve word, I use with double quotes. How can I fix it?
On the other hand, I am not sure if I should change these parameters on application.yml:
spring:
flyway:
enabled: true
jpa:
hibernate:
ddl-auto: update
Some minor issues with the script:
Missing comma on first column of create.
This column is also called id but the index references uuid.
Resolving this allowed the script to work perfectly for me (with the quotes as you have them)
If you make these changes and get a checksum error, please run flyway repair
I have Workspace/Schema EDUCATION in Oracle XE.
In my Java code I want execute queries like this: SELECT * FROM Table instead of SELECT * FROM EDUCATION.Table.
When I write query without EDUCATION I have error: table or view does not exist.
I tried to set the default schema to % (screenshot), but it did not help.
How to avoid writing Workspace/Schema name?
If I understand correctly, you want to access tables in other schemas without using the schema name.
One simple way to do this uses synonyms. In the schema you are connect to:
create synonym table for education.table;
Then you can use table where you would use education.table.
I have a Jackcess table and everything is working just fine. BUT I can't find anything on how to change a column name in the Jackcess table.
The reason I want to change the column name is that I have a ResultSet which is converted to Microsoft Access using Jackcess. a column is named "RELATION" but when Jackcess converts it, the name is changed to "xRelation". it must be some kind of Blacklisted word...
I want to change the column name back to "RELATION" and changing "RELATION" itself to something else is not an option.
How can I do this ?
Jackcess cannot alter the structure of the table after it has been created. So, if you want that column to be named Relation instead of xRelation you will probably need to
explicitly create the table first (e.g., using TableBuilder) with Relation as the column name, and then
use ImportUtil.importResultSet to import into the table you created:
ImportUtil.importResultSet(rs, db, "ExistingTable", new SimpleImportFilter(), true);
That form of importResultSet will import into an existing table instead of creating a new one.
I want to modify MySQL database Table column name according to the jTable header name by java code
To rename column name
alter table tablename change oldname newname newcolumndatatype ;
check it
You can write java code which connect to MySQL database and rename columns by using above suggestion
Check with java sample code
If I have a SQL table with columns:
NR_A, NR_B, NR_C, NR_D, R_A, R_B, R_C
and on runtime, I add columns following the column's sequence such that the next column above would be R_D followed by R_E.
My problem is I need to reset the values of columns that starts with R_ (labeled that way to indicate that it is resettable) back to 0 each time I re-run my script . NR_ columns btw are fixed, so it is simpler to just say something like:
UPDATE table set col = 0 where column name starts with 'NR_'
I know that is not a valid SQL but I think its the best way to state my problem.
Any thoughts?
EDIT: btw, I use postgres (if that would help) and java.
SQL doesn't support dynamically named columns or tables--your options are:
statically define column references
use dynamic SQL to generate & execute the query/queries
Java PreparedStatements do not insulate you from this--they have the same issue, just in Java.
Are you sure you have to add columns during normal operations? Dynamic datamodels are most of the time a realy bad idea. You will see locking and performance problems.
If you need a dynamic datamodel, take a look at key-value storage. PostgreSQL also has the extension hstore, check the contrib.
If you don't have many columns and you don't expect the schema to change, just list them explicitly.
UPDATE table SET NR_A=0;
UPDATE table SET NR_B=0;
UPDATE table SET NR_C=0;
UPDATE table SET NR_D=0;
Otherwise, a simple php script could dynamically build and execute your query:
<?php
$db = pg_connect("host=localhost port=5432 user=postgres password=mypass dbname=mydb");
if(!$db) die("Failed to connect");
$reset_cols = ["A","B","C","D"];
foreach ($col in $reset_cols) {
$sql = "UPDATE my_table SET NR_" . $col . "=0";
pg_query($db,$sql);
}
?>
You could also lookup table's columns in Postgresql by querying the information schema columns tables, but you'll likely need to write a plpgsql function to loop over the query results (one row per table column starting with "NR_").
if you rather using sql query script, you should try to get the all column based on given tablename.
maybe you could try this query to get all column based on given tablename to use in your query.
SELECT attname FROM
pg_attribute, pg_type
WHERE typname = 'tablename' --your table name
AND attrelid = typrelid
AND attname NOT IN ('cmin', 'cmax', 'ctid', 'oid', 'tableoid', 'xmin', 'xmax')
--note that this attname is sys column
the query would return all column with given tablename except system column