Updating a column in a table from another table using postgreSQL - sql

I have table named ´cities´ with 3 columns named:
state, name, pop
And a table cities1 with:
state, name
The columns state and name are common for both tables. I want the column pop alone from cities to be inserted in cities1 table.
How this can be done using postgreSQL?

First you need to add a column pop of specific type to your cities1 table, and then fill it.
ALTER TABLE cities1 ADD COLUMN pop [datatype here]
Update pop column in table cities1 with values from cities where state,name are the common columns.
UPDATE cities1
SET pop = cities.pop
FROM cities
WHERE cities1.state = cities.state
AND cities1.name = cities.name

Related

SQL only delete values in specific colums?

What I am trying to do is delete all rows from a table, but leave values in 1 specific column from the table.
For example the table 'member' with the columns 'membernumber', 'name', lastname' and 'zipcode', I only want to keep all the zipcodes as they are, but delete all other data.
If you want to change your table structure, you use Alter Table along with Drop Column instead of Delete which is for your records. Here is a sample:
ALTER TABLE member
DROP COLUMN membernumber, name, lastname

I want to add two columns names to an existing table in Impala Query

I am writing the following query to add a column at a specified position but getting the below error:
alter table quantum_raw_dev.rpt_backup_allocation
change upt_type upt_type STRING after tray_size;
You can add one or more columns to the end of the column list using:
ALTER TABLE <table_name> ADD COLUMNS (col_name col_type, ...);
[note: there is NO comma between column name and type]
Adding or Removing Columns
You can add one or more columns to the end of the column list using ADD COLUMNS,
or (with Impala only) you can delete columns using DROP COLUMN.
The general syntax is
ALTER TABLE tablename ADD COLUMNS (col1 TYPE1,col2 TYPE2,… );
ALTER TABLE tablename DROP COLUMN colname;
For example, you can add a bonus integer column to the employees table:
ALTER TABLE employees ADD COLUMNS (bonus INT);
Or you can drop the office_id column from the employees table:
ALTER TABLE employees DROP COLUMN office_id;
Notes
DROP COLUMN is not available in Hive, only in Impala. However, see “Replacing All Columns” below.
You can only drop one column at a time.
To drop multiple columns, use multiple statements or use the method to replace columns (see below).
You cannot add a column in the middle of the list rather than at the end.
You can, however, add the column then change the order (see above) or use the method to replace columns (see below).
As with changing the column order, these do not change the data files.
If the table definition agrees with the data files before you drop any column other than the last one,
you will need to recreate the data files without the dropped column's values.
If you drop the last column, the data will still exist but it will be ignored when a query is issued.
If you add columns for which no data exists, those columns will be NULL in each row.
Replacing All Columns
You can also completely replace all the columns with a new column list.
This is helpful for dropping multiple columns,
<h1>or if you need to add columns in the middle of the list<h1>
<h2>(like your use case)<h2>
The general syntax is
ALTER TABLE tablename REPLACE COLUMNS (col1 TYPE1,col2 TYPE2,… );
This completely removes the existing list of columns and replaces it with the new list.
Only the columns you specify in the ALTER TABLE statement will exist, and they will be in the order you provide.
Note
Again, this does not change the data files, only the metadata for the table,
so you'll either want the new list to match the data files or need to recreate the data files to match the new list.
I do not think you can add columns in between columns in Impala like above.
You can backup the data, drop the and recreate with new structure, and load the table from backup. Also if you have HIVE in your system you can try to do below steps -
Add column first and then use below commands to move columns around.
ALTER TABLE tab ADD COLUMNS (id BIGINT);
This moves id column to the beginning.
ALTER TABLE tab CHANGE COLUMN id id BIGINT first;
This moves existing_col after id.
ALTER TABLE tab CHANGE COLUMN existing_col existing_col string AFTER id;
Please refresh/invalidate metadata after applying all DDLs.
You cannot add column in between. Best way is to archive the data in another table. Drop the impala old table and create a fresh table with new columns as per the desired location and then reinsert the data.

How to add a record into table, that is related to another table?

Good day!
I have two tables.
TABLE 1:
GENERIC [GE_ID / number] [GE_DATEIN / date] [GE_PERSON / number] + ...
TABLE 2:
WORKFORCE [WF_ID / number] [WF_NAME / text] [WF_SHIFT / number] + ...
Column [GE_PERSON] from table #1 is related to the column [WF_ID] from table #2 as many-to-one relationship. I have a simple form to add a data to the table #1 with several drop down boxes. One of these drop down boxes contains a list of names taken from table #2 (column [WF_NAME]) by SQL statement.
So when I am ready to add a record to table #1, I know the person name chosen from drop down box, but column [GE_PERSON] is numeric and I have to add a number equal to the column [WF_ID] which is pointing on name in table #2 (column [WF_NAME]).
QUESTION: how should I build the SQL statement (INSERT INTO) to make this work?
Thank you!
MS Access' combobox control allows the bound column to be hidden from the user while showing identifiable data columns that correspond to hidden field. See this section of MS tutorial. Once you do so, you will have a relevant number for your querying needs.
Brief steps to hide and set primary key as bound column:
For the [WF Names] combobox, include [WF_ID] in the Row Source query under Data tab of Property Sheet as first column:
SELECT [WF_ID], [WF_NAME]
FROM [WORKFORCE]
Under Data tab make [WF_ID] the bound column by placing 1.
Under Format tab, set Column Count to 2 (or more for each field in query) but give Column Width to first column nothing, something like: 0; 2
Now the combobox's value is the corresponding [WF_ID] of the selected [WF_NAME] in drop down list, so any query pointing to the control will be a valid number:
INSERT INTO [GENERIC] ([GE_PERSON])
VALUES (Forms!formname!WFNamesComboBox)
SELECT * FROM [WORKFORCE]
WHERE [WF_ID] = Forms!formname!WFNamesComboBox

Replace data in column with foreign key to same data in other table

Hopefully you guys can help me figure out how to do something I haven't come across before.
I have a table, call it TableA, with many columns. One of them is 'state', with entries such as:
state
'MA'
'NJ'
'HI'
and so forth. I would like to create some sort of way to pull these values out into a new foreign-keyed table, call it StateTable, which would have columns state_key and state.
For example, here's how I envision it proceeding row-by-row:
Row 1: Value in 'state' column in TableA is 'MA'. Check to see if StateTable has an entry where the 'state' column is 'MA'. If so, get the state_key for that row, and replace the entry in TableA with the foreign key, so that row now has a FK to 'MA' instead of storing that value directly. If StateTable has no 'MA' entry, insert it, and do the same with the new FK. And so on, for each row.
So the end result would be two tables:
TableA
state
1
2
3
StateTable
state_key state
1 'MA'
2 'NJ'
3 'HI'
There shouldn't be any hard-coded stuffs going on because I'll need to do this for other columns as well, like state, that have a somewhat small number of finite values.
tl;dr a way to preserve data in a column while turning the column into a FK'd table.
Any ideas? Thanks!!
Create the state table with an auto-enumeration field "state_key", use SELECT DISTINCT ... INTO to fill it, and use something like
UPDATE TableA SET TableA.state=
(SELECT state_key FROM StateTable where TableA.state=StateTable.state)
to get the values into it.
My suggestion would be to do the following
Create your new state table (either manually or using an insert into statement)
if manually creating your state table, insert unique records into it from TableA
Create a new column in TableA (StateID) foreign key column, linked to your new state table
Insert value into your new StateID column by matching TableA state to the state in your state table and pulling the ID from that table.
If you need a hand with the queries, let me know.

Add a column to a DB2/400 table with a specific ordinal position

Is there an SQL command on the AS400/iSeries/System-i/whatever to add a column to a table in a specific ordinal position, or moving an existing column to a different position?
IBM i 7.1 now allows you to add a column in front of another.
ALTER TABLE table ADD COLUMN colname ... BEFORE othercolumn
No. The ALTER TABLE statement will allow you add a column to a table, but, according to the documentation:
The new column is the last column of the table; that is, if initially there are n columns, the added column is column n+1.
If you'd like to change the order of columns in your table, your best bet is to:
Use the RENAME statement to rename the table.
Recreate the table, with its original name, with the columns in the order that you want.
Use an INSERT SELECT to populate the new table with the data from the renamed table.
When you're sure the data is intact, you can drop the renamed version of the table.