on 2017 Jul 24 1:27 PM
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.
I can't find a similar "cascade" in the drop table documentation. How can one achieve something similar?
The SQL Anywhere Help for DROP TABLE says "All indexes and keys for the table are dropped as well."
That includes foreign keys; here's a demo:
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
Here's what child looks like before parent is dropped...
-- 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;
This code shows you can still insert a row in child after dropping parent...
DROP TABLE parent; INSERT child VALUES ( 222, 2 ); COMMIT; SELECT * FROM child ORDER BY child_pkey; child_pkey parent_pkey ----------- ----------- 111 1 222 2
Here's what child looks like after parent was dropped...
-- 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 --
In other words, SQL Anywhere does things the way they should be done 🙂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
70 | |
10 | |
10 | |
10 | |
10 | |
8 | |
8 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.