I have a few SQL scripts which contains table creation SQL statements. I want to remove all the statements having foreign key mappings.
For example below is one script.
CREATE TABLE x (
ORG_ID NUMBER primary key,
BILLING_doc xmltype
);
alter table x
add (constraint fk_x foreign key
(org_id) references organization\r
(org_id))
/
I want to remove the alter table statement referencing the foreign key statement and put it in another file. As you see the statement ends with a /. Some files end with a semicolon.
I want to do this using sed. Note that the alter statement shown here is spread across lines and is not on a single line.
Any help is appreciated.
While working I found the following but it is not working for all scripts:
sed -e '/./{H;$!d;}' -e 'x;/foreign/!d;' myfile.sql
Script where the above command is not working:
create table io_template
(
io_template_id number,
io_template_name varchar2(50),
acl_id number,
org_id number,
is_active varchar2(1),
xsl_template varchar2(50),
email_body varchar2(2000),
purpose varchar2(50) default 'CUSTOMER' not null,
content_type varchar2(50) default 'PDF' not null
);
alter table _io_template add constraint io_template_pk
primary key (io_template_id);
alter table _o_template add constraint io_template_acl_fk
foreign key (acl_id) references scum_acl(acl_id);
alter table io_template add constraint io_template_org_fk
foreign key (org_id) references scum_organization(org_id);
alter table io_template add constraint io_template_name_uk
unique (org_id, _io_template_name);