I want one table to inherit from another - sql

I have 2 SQL tables say all_cities, regular_cities, (and capitol_cities).
all_cities has 2 columns : name, population
regular_cities has 2 columns : name, population
capitol_cities has 3 columns : name, population, state
These tables are already created. I want to connect them using table partitioning but first I need to make sure that tables regular_cities and capitol_cities inherit from all_cities.
Is there anyway to have these tables inherit from another table even after the tables are created or only when creating a new table?

After searching around the correct way to do this is with a simple ALTER statement
ALTER TABLE <child_table> INHERIT <parent_table>;
That's it!
Documentation here
https://www.postgresql.org/docs/9.6/static/sql-altertable.html

Related

Generate dynamic GraphQL Schema

I'm creating a system where the no. of columns in a table is not fixed. I'll explain with an example.
Consider a people table with 3 columns.
people
----------
id | name | email
This table will be exposed to a GraphQL API and I can query the table. In my system, the user will be able to add custom columns to the people table. Let's say they add a nationality column. When they do this, the nationality won't be available in the API because it is not defined in the Schema.
So how can I make my schema dynamic that allows the user to query the people table with every extra column they add?
I can query the information_schema table or a fields table and get the extra fields for the people table and then use the GraphQLObjectType to build out my schema rather than using SDL.
this is not the answer to your question but the suggestion because creating dynamic columns in SQL doesn't look like a good idea to me. Instead of thinking how can you create dynamic schema I think you should rethink on your DB structure.
Instead of adding new columns in Person table you should have different table for your custom columns like create person_columns table like
person_columns
----------
id | people_id | column_name | column_value
so for every people you can have multiple columns and its corresponding values and you dont have to worry about dynamically creating your graphQL schema. (depending on your requirement you can add more columns to person_columns table for more control)

How to copy data efficiently from one schema to another schema table using postgresql - Without column names

I have two schemas and a table in each schema. Table is called "concept". One Schema is "cdm" schema and other is "vocab" schema.
I would like to copy records from "concept" table of "vocab" schema to "concept" table of "cdm" schema based on certain conditions
concept table has a primary key called concept_id and several other non primary columns.
Ex: vocab.concept has 200 records. I would like to copy only records that satisfy a condition like concept_id > 150
Currently the concept table in CDM schema (cdm.concept) is filled with 150 records. So, I would like to add/insert the remaining 50 records (which is concept_id > 150)
Can you let me know how can I do this copy or kind of ignore duplicates and copy only new records (ie newly added 50 records)
INSERT INTO cdm.concept(concept_id,Name,Age,Info)
SELECT concept_id,Name,Age,Info
FROM vocab.concept
where concept_id > 150;
Questions
Is this above sql correct?
Is it the only way because my table might have more than 25 columns and I don't wish to type in column names every time I wish to copy.
Please note that table already exists in cdm schema. So, I don't wish to use "Create Table" approach

Update JOIN table contents

I have a table joined from two other tables. I would like this table to stay updated with entries in the other two tables.
First Table is "employees"
I am using the ID, Last_Name, and First_Name.
And the second Table is "EmployeeTimeCardActions"
using columns ID, ActionTime, ActionDate, ShiftStart, and ActionType.
ID is my common column that the join was created by..Joined Table...
Because I usually have a comment saying I did not include enough information, I do not need a exact specific code sample and I think I have included everything needed. If there is a good reason to include more I will, I just try to keep as little company information public as possible
Sounds like you're having your data duplicated across tables. Not a smart idea at all. You can update data in one table when a row is updated in a different one via triggers but this is a TERRIBLE approach. If you want to display data joined from 2 tables, the right approach here is using an SQL VIEW which will display the current data.

Migrate data between two SQL databases with identity column

Here is the scenario... I've two databases (A & B) with same schema but different records. I'd like to transfer B's data into corresponding tables in DB A.
Lets say we have tables named Question and Answer in both databases. DB A contains 10 records in Question table and 30 in Answer table. Both tables have identity column Id starting with 1(& auto increment), and there is 1 to many relation between Question and Answer.
In DB B, we have 5 entries in Question table and 20 in Answer.
My requirement is to copy data of both tables from source DB B into destination DB A without having any conflict in identity column while maintaining the relation between two tables during data transfer.
Any solution or potential workaround would be highly appreciated.
I will not write SQL here but here is what I think can be done. Make sure to use Identity insert ON and OFF.
Take maxids of both tables from DB A like A_maxidquestion and A_maxidanswer.
Select from B_question . In select column add derived col QuestionID+A_maxidquestion.This will be your new ID.
Select from B_Answer . In select column add derived col AnswerID+A_maxidanswer and fk id as QuestionID+A_maxidquestion.
Note- Make sure Destination table is not beeing used by any other process for inserting values while you are inserting
One of the best approaches to something like this is to use the OUTPUT clause. https://learn.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-2017 You can insert the new parent and capture the newly inserted identity value which you can use to insert the children.
You can do this set based if you also include a temp table which would hold the original identity value and the new identity value.
With no details of the tables that is the best I can do.

What are "descendant tables" in Postgresql?

Database dumps from Postgresql use ALTER TABLE ONLY tablename instead of ALTER TABLE tablename which I am familiar with. I was curious what the ONLY keyword does, so I looked it up in the Postgresql documentation, and it says the following:
name
The name (optionally schema-qualified) of an existing table to alter. If ONLY is specified before the table name, only that table is altered. If ONLY is not specified, the table and all its descendant tables (if any) are altered. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included.
What are descendant tables?
PostgreSQL implements table inheritance, which can be a useful tool
for database designers. (SQL:1999 and later define a type inheritance
feature, which differs in many respects from the features described
here.)
Let's start with an example: suppose we are trying to build a data
model for cities. Each state has many cities, but only one capital. We
want to be able to quickly retrieve the capital city for any
particular state. This can be done by creating two tables, one for
state capitals and one for cities that are not capitals. However, what
happens when we want to ask for data about a city, regardless of
whether it is a capital or not? The inheritance feature can help to
resolve this problem. We define the capitals table so that it inherits
from cities:
CREATE TABLE cities (
name text,
population float,
altitude int -- in feet
);
CREATE TABLE capitals (
state char(2)
) INHERITS (cities);
In this case, the capitals table inherits all the columns of its
parent table, cities. State capitals also have an extra column, state,
that shows their state.
In PostgreSQL, a table can inherit from zero or more other tables, and
a query can reference either all rows of a table or all rows of a
table plus all of its descendant tables. The latter behavior is the
default.
Source: https://www.postgresql.org/docs/8.4/static/ddl-inherit.html
Descendant tables of a table are all tables that inherit from it, either directly or indirectly. So if table B inherits table A, and table C inherits table B, then:
Tables B and C are descendant tables of A.
Table C is a descendant table of B.
A query against a table (without ONLY) is a query against the table and all descendant tables. So, for example, a SELECT on a table with descendant tables is effectively a UNION of SELECT ... FROM ONLY across that table and all of its descendant tables. (In fact, if you inspect the query plan for a SELECT query on a table with descendants, you'll see that the plan is nearly identical to such a UNION query.)
If you are not using table inheritance, then the ONLY keyword has no effect on queries, as the set of descendant tables is empty.