<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: cascade constraints in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841992#M4872835</link>
    <description>&lt;P&gt;&amp;gt; you must alter the child table to drop the constraint before the parent table can be dropped.&lt;/P&gt;
&lt;P&gt;That would violate the Watcom Rule &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Even as far back as V5 the Help says "Also, all indexes and keys for the table are dropped by the DROP TABLE statement."&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jul 2017 17:10:41 GMT</pubDate>
    <dc:creator>Breck_Carter</dc:creator>
    <dc:date>2017-07-24T17:10:41Z</dc:date>
    <item>
      <title>cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaq-p/13841988</link>
      <description>&lt;P&gt;In PostgreSQL and Oracle one can specify "cascade"  "cascade constraints respectively with the drop table to ensure foreign key constraints are no longer applied.
This also allows for "dropping" a table in any order, which is useful during testing.&lt;/P&gt;
&lt;P&gt;I can't find a similar "cascade" in the drop table documentation. How can one achieve something similar?&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2017 12:27:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaq-p/13841988</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-07-24T12:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841990#M4872833</link>
      <description>&lt;P&gt;Do you mean cascading DELETE (which is supported by SQL Anywhere) or really dropping a table that has child records. In the latter case, you must alter the child table to drop the constraint before the parent table can be dropped.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2017 13:45:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841990#M4872833</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2017-07-24T13:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841991#M4872834</link>
      <description>&lt;P&gt;In PostgreSQL one need not remove constraint prior to dropping, even if data exists, for example: 
&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;ft_node=# create table t1 (x numeric,y varchar(10));&lt;P&gt;&lt;/P&gt;
&lt;P&gt;CREATE TABLE&lt;/P&gt;
&lt;P&gt;ft_node=# alter table t1 add constraint pk_t1 primary key (x);&lt;/P&gt;
&lt;P&gt;ALTER TABLE&lt;/P&gt;
&lt;P&gt;ft_node=# create table t2 (x numeric, y varchar(10));&lt;/P&gt;
&lt;P&gt;CREATE TABLE&lt;/P&gt;
&lt;P&gt;ft_node=# alter table t2 add constraint fk_t2 foreign key (x) references t1 (x);&lt;/P&gt;
&lt;P&gt;ALTER TABLE&lt;/P&gt;
&lt;P&gt;ft_node=# insert into t1 values (1,'Yellow');&lt;/P&gt;
&lt;P&gt;INSERT 0 1&lt;/P&gt;
&lt;P&gt;ft_node=# insert into t2 values (1,'Mellow');&lt;/P&gt;
&lt;P&gt;INSERT 0 1&lt;/P&gt;
&lt;P&gt;ft_node=# drop table t1 cascade;&lt;/P&gt;
&lt;P&gt;NOTICE:  drop cascades to constraint fk_t2 on table t2&lt;/P&gt;
&lt;P&gt;DROP TABLE&lt;/P&gt;
&lt;P&gt;ft_node=# \\d+ t1&lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;Did not find any relation named "t1".
&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Same goes for Oracle:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;
SQL&amp;gt; create table t1 (x numeric,y varchar(10));

Table created.

SQL&amp;gt; alter table t1 add constraint pk_t1 primary key (x);

Table altered.

SQL&amp;gt; create table t2 (x numeric, y varchar(10));

Table created.

SQL&amp;gt; alter table t2 add constraint fk_t2 foreign key (x) references t1 (x);

Table altered.

SQL&amp;gt; insert into t1 values (1,'Yellow');

1 row created.

SQL&amp;gt; insert into t2 values (1,'Mellow');

1 row created.

SQL&amp;gt; drop table t1 cascade constraints;

Table dropped.

SQL&amp;gt; desc t1
ERROR:
ORA-04043: object t1 does not exist
&lt;/LI-CODE&gt;

&lt;P&gt;Its really useful when you have to generate continuous builds when unit testing. Looks like you can't do something similar in SQL Anywhere?&lt;/P&gt;&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 24 Jul 2017 15:46:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841991#M4872834</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-07-24T15:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841989#M4872832</link>
      <description>&lt;P&gt;The &lt;A href="http://dcx.sap.com/index.html#sqla170/en/html/816f4ddf6ce210148586a738180418cb.html*loio816f4ddf6ce210148586a738180418cb"&gt;&lt;B&gt;SQL Anywhere Help for DROP TABLE&lt;/B&gt;&lt;/A&gt; says "All indexes and keys for the table are dropped as well."&lt;/P&gt;
&lt;P&gt;That includes foreign keys; here's a demo:&lt;/P&gt;
&lt;PRE&gt;CREATE TABLE parent ( 
   pkey INTEGER NOT NULL PRIMARY KEY );
INSERT parent VALUES ( 1 );
COMMIT;
CREATE TABLE child (
   child_pkey  INTEGER NOT NULL PRIMARY KEY,
   parent_pkey INTEGER NOT NULL,
   CONSTRAINT fkey_parent FOREIGN KEY ( parent_pkey ) REFERENCES parent ( pkey ) );
INSERT child VALUES ( 111, 1 );
COMMIT;
SELECT * FROM parent ORDER BY pkey;
SELECT * FROM child ORDER BY child_pkey;

       pkey 
----------- 
          1 

 child_pkey parent_pkey 
----------- ----------- 
        111           1 
&lt;/PRE&gt;

&lt;P&gt;Here's what child looks like before parent is dropped...&lt;/P&gt;
&lt;PRE&gt;-- dba.child (table_id 3005) in ddd17 - Jul 24 2017 4:56:38PM - Print - Foxhound © 2016 RisingRoad
CREATE TABLE dba.child ( -- empty
   child_pkey    /* PK        */ INTEGER NOT NULL,
   parent_pkey   /*    FK     */ INTEGER NOT NULL,
   CONSTRAINT ASA82 PRIMARY KEY (
      child_pkey )
 );
-- Parents of dba.child
-- dba.parent
-- Children
-- none --
ALTER TABLE dba.child ADD CONSTRAINT fkey_parent NOT NULL FOREIGN KEY (
      parent_pkey )
   REFERENCES dba.parent (
      pkey )
   ON UPDATE RESTRICT ON DELETE RESTRICT;
&lt;/PRE&gt;

&lt;P&gt;This code shows you can still insert a row in child after dropping parent...&lt;/P&gt;
&lt;PRE&gt;DROP TABLE parent;
INSERT child VALUES ( 222, 2 );
COMMIT;
SELECT * FROM child ORDER BY child_pkey;

 child_pkey parent_pkey 
----------- ----------- 
        111           1 
        222           2 
&lt;/PRE&gt;

&lt;P&gt;Here's what child looks like after parent was dropped...&lt;/P&gt;
&lt;PRE&gt;-- dba.child (table_id 3005) in ddd17 - Jul 24 2017 4:57:41PM - Print - Foxhound © 2016 RisingRoad
CREATE TABLE dba.child ( -- 2 rows, 12k total = 4k table + 0 ext + 8k index
   child_pkey    /* PK        */ INTEGER NOT NULL,
   parent_pkey                   INTEGER NOT NULL,
   CONSTRAINT ASA82 PRIMARY KEY ( -- 8k
      child_pkey )
 );
-- Parents of dba.child
-- none --
-- Children
-- none --
&lt;/PRE&gt;

&lt;P&gt;In other words, SQL Anywhere does things the way they should be done &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2017 17:07:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841989#M4872832</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2017-07-24T17:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841992#M4872835</link>
      <description>&lt;P&gt;&amp;gt; you must alter the child table to drop the constraint before the parent table can be dropped.&lt;/P&gt;
&lt;P&gt;That would violate the Watcom Rule &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Even as far back as V5 the Help says "Also, all indexes and keys for the table are dropped by the DROP TABLE statement."&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2017 17:10:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841992#M4872835</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2017-07-24T17:10:41Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841993#M4872836</link>
      <description>&lt;P&gt;Oops, did not know that, and in my book, it's not the expected behaviour - I would at least expect a warning about lost FKs...&lt;/P&gt;
&lt;P&gt;But your sample proves me wrong.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2017 17:52:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841993#M4872836</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2017-07-24T17:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841994#M4872837</link>
      <description>&lt;P&gt;&amp;gt; it's not the expected behaviour&lt;/P&gt;
&lt;P&gt;Have you been spending a lot of time using other products? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://images.memes.com/meme/1006741" width="150" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jul 2017 10:14:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841994#M4872837</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2017-07-25T10:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841995#M4872838</link>
      <description>&lt;P&gt;Not really compared to SA.&lt;/P&gt;
&lt;P&gt;I drop tables rarely, but when I do, I drop them in the opposite order I created them, so I rarely ran into that problem:)&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jul 2017 17:37:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841995#M4872838</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2017-07-25T17:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: cascade constraints</title>
      <link>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841996#M4872839</link>
      <description>&lt;P&gt;&amp;gt; I drop them in the opposite order I created them, so I rarely ran into that problem&lt;/P&gt;
&lt;P&gt;Just so other people are clear: in SQL Anywhere there is no "problem" if you drop tables in the same order you created them. As the original question stated, that makes it easy the write schema scripts that can be run multiple times to drop and recreate everything.&lt;/P&gt;
&lt;P&gt;In fact, it is something I often do when creating reproducibles for this forum... I rarely get everything right the first time (or the second... or the fifth... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; so I have to keep re-running the scripts.&lt;/P&gt;
&lt;P&gt;In other words, "drop them in the opposite order" is unnecessary effort.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2017 07:57:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cascade-constraints/qaa-p/13841996#M4872839</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2017-07-26T07:57:19Z</dc:date>
    </item>
  </channel>
</rss>

