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;
Related
How to create primary key with disable status using liquibase changeset?
I want to create partitioned index of primary key with storage parameters like init trans and max trans using liquibase changeset.
so first, I used to create primary key on column, then disable primarykey which drop unique index, than create partitioned index on primary key and than enable primarykey.
How to achieve in liquibase?
The only way to do this in Liquibase currently would be using the <sql> or <sqlFile> change types.
I have added a table to a database called settings. This table has a column called id (integer) which is the pirmary key. I have also added a column called settingsID to a table called sessions. What I want to do is add a foreign key to settingsID which references the primary key.
I don't want to create a new table as it is already created. All I want to do is to references the id from the settings table in settingsID which is in sessions table.
ALTER TABLE Sessions ADD FOREIGN KEY (_SettingsID) REFERENCES settings (id)
Here is my error:
near "FOREIGN": syntax error
Can someone tell me the right way to approach this?
Short answer: you can't.
This is documented as part of the SQL Features That SQLite Does Not Implement:
Only the RENAME TABLE, ADD COLUMN, and RENAME COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
Bold emphasis is mine.
So basically you would need to create the constraint at the time when the table is created. For your use case, one solution would be to:
create a new table (with the foreign key constraint)
copy the data from the old table
drop the old table (or better yet, rename it, so you have a backup)
rename the new table
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.
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
Using:
ALTER TABLE dbo.Table1Name
ADD FOREIGN KEY (colname)
REFERENCES dbo.Table2Name (colname)
I get a foreign key with a name like:
FK___colname__673F4B05
I want it to be named:
FK_Tabl1Name_Table2Name,
...so that it will be easy to read when browsing the DB structure in SSMS. I know I can go back into the GUI and do this, but I want to be able to script it.
So What's the SQL sytnax for adding a name to the FK? Nothing I've found online seems to bother with this.
Here's how you can assign your chosen name to the foreign key constraint:
ALTER TABLE dbo.Table1Name
ADD CONSTRAINT FK_Tabl1Name_Table2Name
FOREIGN KEY (colname) REFERENCES dbo.Table2Name (colname)