I have a set of tables that I am conflating together. I want to be able to go back and access the 'raw' data later if I need to add more. The way I'm doing this by adding a reference column to the conflation table that will contain the table name of the table from which the data came from.
How can I access the names of my tables as I query from them?
EDIT: Details:
The table I'm creating looks like:
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name
thing1.shape
FROM
public.thing1_source_table
UNION
SELECT
thing2.name
thing2.shape
FROM
public.thing2_source_table);
And I want to add the "source" field:
ALTER TABLE combined_things ADD COLUMN source_id character varying(100);
ALTER TABLE comnined_things SET COLUMN source_id = {table_name}
And I don't know how to pull the {table_name}
You could add them as a string constant when you create the table
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name,
thing1.shape,
CAST('public.thing1_source_table' AS CHAR(100)) source_id
FROM
public.thing1_source_table
UNION
SELECT
thing2.name,
thing2.shape,
'public.thing2_source_table'
FROM
public.thing2_source_table);
Note that there is no way of casting the column to varchar
Related
table name = TRANSACTION, i want to create another table trans_temp from transaction table but rename column acc_no to account_no. this all operation made in one query, Thank you...
You could use the CREATE ... AS SELECT syntax:
CREATE TABLE trans_temp AS
SELECT
acc_no AS account_no,
tr_date,
amt,
type_of_tr,
mode_of_pay
FROM transaction
The name of the columns returned by the query will become column names in the new table.
Note that the CTAS syntax does not take care of transferring constraints or indexes on the target table. You would need to recreate them manually aferwards.
There is another method if you don't want to list out all the columns:
create table trans_temp as
select *
from transaction;
alter table trans_temp modify acc_no account_no <type goes here>;
Perhaps you don't need a temporary table at all, and a view would suffice.
How to add or insert specific column from one table to other ? I tried writing like this
ALTER TABLE info_apie_zaideja
ADD SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
or this
UPDATE info_apie_zaideja
ADD COLUMN SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
but that did not work. Oh, and the table where I want to insert column is view table if that helps somehow. All answers will be appreciated.
You need to do this in two steps. First alter the table to add the new column:
ALTER TABLE info_apie_zaideja
ADD COLUMN Rank INT AFTER Nick;
Then fill it in by copying from corresponding rows in the other table:
UPDATE info_apie_zaideja AS z
JOIN info_apie_match AS m ON z.id = m.zaideja_id
SET z.Rank = m.Rank
I had to guess at the column that relates the two tables. Correct the ON clause to match your actual table relations.
Also, consider whether you really need the column in both tables. With this redundancy, you'll need to make sure that whenever you update one table, the other one is updated as well. Instead, you could just use a JOIN whenever you need the value from the other table.
Apologies if I am mistaken, but is there any way to create a table inside a column in MySql?
Brief: I have a table named test_table which contains a column named name test_column. Now I want to create a table inside test_column. Is this possible?
You would create a "child" table with an id that is referenced in the column of the main table. You wouldn't create a "table" in a column.
For example
Table 1
columm_pk int
column_fk int
table 2
column_pk (this is what goes in table 1)
other columns as needed.
then you just join the tables based on that fk id. You can have multiple fk column in the first table that link to different child tables. You can also look in to SET data types in MySql although I wouldn't recommend them.
btw, If your question is MySql specific then you shouldn't use the oracle tags.
No nested tables in MySql, but there is a SET datatype that you can use in a table
http://dev.mysql.com/doc/refman/5.0/en/set.html
That approach is not possible. What you are looking for is a second table that is linked using a field in the first table.
Example:
test_table:
ID | column1 | some more columns
test_table2:
table1_ID | column1| column2...
You can then access them using JOIN commands. For example:
SELECT *
FROM test_table t1
INNER JOIN test_table2 t2
ON t1.ID = t2.table1_ID
This way you can have multiple rows for each ID in table 1, which has the effect you are looking for.
This is not possible in MySQL.
I want use a stored procedure to update multiple tables in a db. Each table has a GUID as the PK and there are FK's between the tables.
For example, one table is "Tool" with a column ID (the guid) and another table is "Type" with ID as guid again. There is a column in Tool called "TYPE_ID" that is a FK to the table Type with Type's Guid stored in it. I want to first update the Tool table and then after, update the Type table based on that FK.
UPDATE Tool
SET Name=#Name, [Enabled]=#Enabled, TestMode=#TestMode, SerialNumber=#SerialNumber,
Andon=#Andon, ChimeZone=#ChimeZone, Number=#ToolNumber
WHERE ID=#ID
Update Type
SET Type=#Type
WHERE Tool.ID=#ID AND
Tool.TYPE_ID=Type.ID
I know that this code is incorrect for the second update, but this is the gist of how I would like to be able to do it. Is there a way to not have to SELECT the FK Guid, store it, and use it the next update? If that is the only way, how do I do that?
Write your second update like this, joining the Tool table to the Type table:
UPDATE ty
SET Type = #Type
FROM Tool to
INNER JOIN Type ty
ON to.TYPE_ID = ty.ID
WHERE to.ID = #ID
Actually you can UPDATE FROM clause:
UPDATE Type
SET Type=#Type
FROM Tool INNER JOIN TYPE
ON Tool.TYPE_ID=Type.ID
WHERE Tool.ID=#ID;
See http://msdn.microsoft.com/en-us/library/ms177523.aspx for full UPDATE syntax.
I have a mysql table. How do I remove partial duplicates based on three columns? Is it posibble to apply the changes to the current table? If not what is the solution?
Example:
The current table
Surname.........First Name.......Company Name........Responsibilities.......Column5
McKain...............Christine.........XYZ...............................A................................A
McKain.......... ....Christine .........XYZ .............................B............................... B
Morrell............... Sally ..............ABC ..............................A ...............................A
Motu ..................Timothy........... EFG .............................I ................................I
The resulting Table:
Surname.........First Name.......Company Name........Responsibilities.......Column5
McKain...............Christine.........XYZ...............................A................................A
Morrell............... Sally ..............ABC ..............................A ...............................A
Motu ..................Timothy........... EFG .............................I ................................I
and if possible someone please tell me an easy way to add a table here in stack overflow..
Replace tableName with your table's name and the GROUP BY/UNIQUE INDEX fields with the fields you want to be unique.
Warning: The GROUP BY used will only select the first entry when removing duplicates.
If something goes wrong, you still have a copy of your original data in tableName_old.
-- Create a new table that's filtered
CREATE TABLE newTable
(SELECT *
FROM tableName
GROUP BY LastName, FirstName, Company);
-- Add a unique index of the fields you want
ALTER TABLE newTable ADD UNIQUE INDEX ix_unique (LastName, FirstName, Company);
-- Rename the tables around
RENAME TABLE tableName TO tableName_old;
RENAME TABLE newTable TO tableName;