Create table as in Redshift defining primary key - primary-key

I am trying to replicate a table using CTAS clause in redshift by additionally specifying a primary key to the table.
Tried below syntax but no luck. However, I was able to specify DISTKEY/SORTKEY using the same syntax
create table date_dim
PRIMARY KEY(date_key)
--DISTKEY ( date_key )
as
select date_key,
calendar_date,.....;
I want to use primary key as part of merge logic I am designing in my flow.
TIA!

Many people consider primary and foreign keys in Redshift to be an anti-pattern (because they're unenforced), but my team built a small tool (a Python script) that supports this scenario.
You write your select statement in a normal SQL file, define primary key, foreign keys, distkey, etc in a YAML configuration file, and then use the script to generate (and optionally execute) SQL to create and populate the table.
We also include an Airflow operator to make it simple to schedule and automate this.
The repo is here, and we wrote a bit more about it on our team blog

You can only specify distkey and sortkey in CTAS . Here is the below link which describes what all options you can specify
Redshift CTAS

If the column you are wishing to dub as primary key is already non-nullable you can use this:
ALTER TABLE <table_name> ADD CONSTRAINT <a_name_for_this_constraint> PRIMARY KEY (<attribute_name>)
e.g.: ALTER TABLE member ADD CONSTRAINT pk_1 PRIMARY KEY (member_id);

Redshift doesn't support primary and foreign key constraints: http://docs.aws.amazon.com/redshift/latest/dg/t_Defining_constraints.html

Related

Liquibase drop composite key command

How to drop composite key in liquibase using SQL commands ? My composite key doesn't have a name for it.
I tried searching liquibase documentation but it says about primary keys only.
A composite key is a primary key. So if you found a way to drop a primary key, then use it.
As you want to use SQL command, not Liquibase XML, JSON etc., then this is a question how to drop a composite key in SQL. An answer depends on a database engine you use. For example in MySQL, you can do that like this:
ALTER TABLE CITIES DROP PRIMARY KEY;
A primary key from the CITIES table will be deleted even if it is a composite key.
To make it runnable from Liquibase, you just add a comment line with Liquibase meta data. For example:
--changeset Harsh:1
ALTER TABLE CITIES DROP PRIMARY KEY;

CREATE TABLE...LIKE not preserving primary keys in Redshift

I'm creating a copy of a table using the CREATE TABLE... LIKE statement in Redshift, and the copy is working, but primary key is not being preserved.
CREATE TABLE new_table (LIKE orig_table);
Am I missing a parameter that should be specified to ensure this works?
Thanks
From CREATE TABLE
Tables created with the LIKE option don't inherit primary and foreign key constraints. Distribution style, sort keys, BACKUP, and NULL properties are inherited by LIKE tables, but you can't explicitly set them in the CREATE TABLE ... LIKE statement.

Alter table in impala : make a column a primary key

Using Hue, how can I alter a table to make a prexisting column a primary key?
I check and things like :
ALTER TABLE table_name ADD CONSTRAINT colname PRIMARY KEY (cs_id);
is not syntactically correct.
NB: data is stored using Kudu file system.
First, Impala does not support alter contraint as an option in alter table.
Second, primary keys are very limited:
The primary key columns must be the first ones specified in the CREATE TABLE statement.
I don't think you can change the primary key after it has been defined. In Impala, the data is clustered (i.e. sorted) by the primary key, so any change would be quite expensive.
You probably need to recreate the table and reload it with data.
When you are storing as Kudu, you need to consider that the PK columns need to be all created at the creation of the table.
Impala does not support altering primary keys.
I'm afraid you need to delete and create the table again.

PostgreSQL FOREIGN KEY with second database

I'm running the following queries on PostgreSQL 9.3:
CREATE TABLE "app_item"
(
"id" SERIAL NOT NULL PRIMARY KEY,
"location_id" UUID NOT NULL
);
CREATE INDEX app_item_e274a5da
ON "app_item" ("location_id");
ALTER TABLE "app_item"
ADD CONSTRAINT app_item_location_id_5cecc1c0b46e12e2_fk_fias_addrobj_aoguid
FOREIGN KEY ("location_id") REFERENCES "fias_addrobj" ("aoguid") deferrable
initially deferred;
Third query returns:
ERROR: relation "fias_addrobj" does not exist
app_item - table in first database
fias_addrobj - table in second database
How to do correct query with this databases?
A local table must be referenced
However, as stated within the below link, you could maybe use a trigger which uses a cross server join (facilitated by dblink) to simulate the built-in methods for constraining?
For instance, you could have a trigger set up that on INSERT, checks to see if a given FK exists to aid with enforcing referential integrity, or on DELETE to cascade
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=101322
P.S. Would avoid this at all costs.
I've not had occasion to use this myself, but you might want to look into Foreign Data Wrappers, which are essentially the successor to dblink. In particular, postgres-fdw.
Once the general setup of the fdw is in place (steps 1-3 in the link above), you could create a foreign table via CREATE FOREIGN TABLE, defined like the table in your remote DB, and then use that table as part of the foreign key CONSTRAINT, and see if it works.
If that doesn't work, another option would be to have a process which ETL's the data (say, via a Python script) from the remote server over to the local server (say, on an hourly or daily basis, depending on the size), and then you would have a true local table to use in the foreign key CONSTRAINT. It wouldn't be real-time, but depending on your needs, may suffice.

How do i delete a foreign key constraint programmatically in Microsoft Access

How do i delete a foreign key constraint programmatically in Microsoft Access, preferable using SQL. For starters i don't know how to find the name of the foreign key.
I connect to Access from a Java application using the JDBC-ODBC bridge. I want to execute the SQL from my Java application.
I can see the relationship in Access, in the RelationShip view, but there seems to be no way of finding out the name. If i could find out the name i expect i could drop it with an ALTER TABLE statement.
Determine the relationship using
SELECT szRelationship FROM
Msysrelationships WHERE szObject =
'childtablename' and
szReferencedObject = 'parenttablename'
THEN
Use the ALTER TABLE command. Something along the line of this
ALTER TABLE Table2 DROP CONSTRAINT Relation1
I've tried accessing the foreign key name via JDBC's DataBaseMetadata object, but the JDBC-ODBC bridge does not implement the required functions. So i've resorted to droping and recreating the entire table with the foreign key.