0

From a table, how can I create another table without the data and with only the primary key columns?

I am able to get the primary key columns using the following query:

SELECT column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
  ON tc.constraint_name = kcu.constraint_name
WHERE tc.table_schema = 'MY_SCHEMA' 
  AND tc.table_name = 'MY_TABLE' 
  AND tc.constraint_type = 'PRIMARY KEY';

Then, I thought of using this method to create the new table:

CREATE TABLE MY_NEW_SCHEMA.MY_NEW_TABLE AS 
SELECT TOP 0 WROTE_HERE_A_WAY_TO_GET_THE_PRIMARY_COLUMN_DYNAMICALLY 
FROM OLD_SCHEMA.OLD_TABLE;

However, I need to replace WROTE_HERE_A_WAY_TO_GET_THE_PRIMARY_COLUMN_DYNAMICALLY with the result of the first SQL query dynamically, and I’m not sure how to achieve that.

It’s important to note that when I try to use LISTAGG for this purpose, I encounter the following error:

ERROR: Specified types or functions (one per INFO message) not supported on Redshift tables. [ErrorId: 1-6759af45-0bca22fc1857f4ec7ecb34c6]
Info: Column "c.conkey" has unsupported type "smallint[]".

Example of the LISTAGG query I attempted:

SELECT LISTAGG(column_name, ', ')
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
  ON tc.constraint_name = kcu.constraint_name
WHERE tc.table_schema = 'MY_SCHEMA' 
  AND tc.table_name = 'MY_TABLE' 
  AND tc.constraint_type = 'PRIMARY KEY';
5
  • I suspect that you can't use the result of a query to define the table column that you want to select. You would need to construct the query outside of Redshift and then submit it as a finished statement. Or, you might be able to do it via a Stored Procedure. See: Redshift: Executing a dynamic query from a string - Stack Overflow Commented Dec 10, 2024 at 22:04
  • To create a procedure, i'm pretty sure i would need to use LISTAGG. Currently, I have a error with it. Commented Dec 10, 2024 at 22:31
  • The error you are getting is due to the use of "leader node only" table. You can select from these tables but you cannot perform "compute node" operations, like JOIN, on them. There are a few ways you can go - extract the data and build the information outside of Redshift, extract the data but route it back to Redshift via a cursor, or look for equivalent data that exists in compute node accessible tables. Commented Dec 11, 2024 at 15:22
  • I just realised that there was a "info" that may be usefull to debug. I edited the post and added them. Commented Dec 11, 2024 at 15:29
  • This question is similar to: Redshift: Executing a dynamic query from a string. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Mar 8 at 3:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.