简体   繁体   中英

Regex to find (and replace) a foreign key in SQL script

I have an sql script with table creation scripts for multiple tables. I need a script (sed) to update a foreign key reference in one of them.

Is it possible to detect the FOREIGN KEY (MY_KEY_ID) REFERENCES fff(ID) ON DELETE CASCADE , from the following script.

Note that it might not be the only occurrence of that particular text, therefore I need to find the one that occurs after CREATE TABLE MYTABLE .

CREATE TABLE MYTABLE (
  blah
  .....
  FOREIGN KEY (MY_KEY_ID) REFERENCES fff(ID) ON DELETE CASCADE ,
  FOREIGN KEY ....
)

UPDATE:

The end goal is to use a bash script to remove ON DELETE CASCADE from one key and add it to the next key.

This will find the FOREIGN KEY string after the CREATE TABLE string but now what?

$ awk '
    index($0,"CREATE TABLE MYTABLE") { f=1 }
    f && index($0,"FOREIGN KEY (MY_KEY_ID) REFERENCES fff(ID) ON DELETE CASCADE ,")
' file
FOREIGN KEY (MY_KEY_ID) REFERENCES fff(ID) ON DELETE CASCADE ,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM