DROP a table with a Foreign key : how

  • I have a table, buily with this SQ

    CREATE TABLE dbo.Authors

    (

    Au_ID                               INT IDENTITY(1,1) NOT NULL,

    Author                              NVARCHAR(50),

    [Year Born]                         SMALLINT DEFAULT (0),

    CONSTRAINT aaaaaAuthors_PK PRIMARY KEY NONCLUSTERED ( Au_ID )

    )

    ----------------------

    I tried to DROP it (DROP TABLE dbo.Authors)and it failed, because of this error from SQLExecMS, all other tables are deleted, except for this one.

    Descr=Could not drop object 'dbo.Authors' because it is referenced by a FOREIGN KEY constraint.

    SQLState=42S03

    Number=-2147217900

    Native Error=3726

    Any ideas

  • Well, the error message is pretty much self-describing. There you have another table referencing it. You need to drop that foreign constraint first in order to drop thast table.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • I tried that but, it did not work

    ALTER TABLE dbo.Authors NOCHECK CONSTRAINT ALL

    Batch ran ok, then I attempted to DROP table, that got the above error message, any ideas

  • NOCHECK is not the same as DROP.

    create table t

    (

     c1 int primary key

    )

    create table t1

    (

     c2 int constraint fk_me foreign key references t(c1)

    )

    drop table t

    Server: Nachr.-Nr. 3726, Schweregrad 16, Status 1, Zeile 9

    Das t-Objekt konnte nicht gelöscht werden, da eine FOREIGN KEY-Einschränkung auf das Objekt verweist.

    Something like this should work:

    create table t

    (

     c1 int primary key

    )

    create table t1

    (

     c2 int constraint fk_me foreign key references t(c1)

    )

    alter table t1 drop constraint fk_me

    drop table t

    drop table t1

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply