Results 1 to 2 of 2

Thread: [RESOLVED] Sql Compact: The constraint specified is not valid.

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Resolved [RESOLVED] Sql Compact: The constraint specified is not valid.

    Hello.

    I'm trying to create a couple of tables in my SqlServerCE database (sdf).
    I'm using the following commands:

    Code:
    CREATE TABLE SUBJECTS(
    	CODE bigint NOT NULL CONSTRAINT SUBJECTS_PK PRIMARY KEY,
    	NAME nVarChar(40) NOT NULL,
    	SOKR nVarChar(10) NULL
    )
    
    CREATE TABLE REGIONS(
    	CODE bigint NOT NULL CONSTRAINT REGIONS_PK PRIMARY KEY,
    	SUBJ_CODE bigint NULL CONSTRAINT SUBJECTS_CODE FOREIGN KEY REFERENCES SUBJECTS(CODE),
    	NAME nVarChar(40) NOT NULL,
    	SOKR nVarChar(10) NULL
    )
    The first one goes OK, but the second one throws an exception saying:
    The constraint specified is not valid.

    The question is, how should I add a foreign key to a table?

  2. #2

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Sql Compact: The constraint specified is not valid.

    Never mind. Figured out.
    The correct syntax is here:

    Code:
    CREATE TABLE REGIONS(
    	CODE bigint NOT NULL CONSTRAINT [PK_REGIONS] PRIMARY KEY,
    	SUBJ_CODE bigint NULL,
    	NAME nVarChar(40) NOT NULL,
    	SOKR nVarChar(10) NULL,
    	FOREIGN KEY (SUBJ_CODE) REFERENCES SUBJECTS(CODE)
    )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width