开发者

I need to create a SQLite table

开发者 https://www.devze.com 2023-02-11 08:21 出处:网络
I need to be able to enter duplicate entries in开发者_如何学编程 Table B, with different foreign keys (Table B references Table A\'s key).Like this:

I need to be able to enter duplicate entries in开发者_如何学编程 Table B, with different foreign keys (Table B references Table A's key). Like this:

Table A:
Record with key of 11
Record with key of 22

Table B:
Record referring to Table A record 11, with a field marked unique: value 101
Record referring to Table A record 22, with a field marked unique: value 101  <--- violates the unique key

This is what I have tried, but didn't work:

CREATE TABLE Readings (
    SITE_ID TEXT REFERENCES SiteData
    , LOOP_NBR TEXT
    , LOOP_CLOSED BINARY
    , SEQ INTEGER
    , STA TEXT UNIQUE
    , BS TEXT
    , FS TEXT
    , HI TEXT
    , DESC TEXT
 )  

INSERT INTO Readings (SITE_ID, SEQ) VALUES (' + databaseKey + ', 0)

Anybody know how to do this?


If table B has a unique constraint on a column, then if you want to enter a duplicate value into that column, you have to remove the unique constraint.

Edit: In other words, remove the word UNIQUE from your table creation code. I assume that "a field marked unique" is referring to the STA column, because that's the only unique column you have in there.

Edit the second: According to your comments below, you want to put a constraint on unique combinations of the SITE_ID and STA fields. You'd change your SQL query to be:

CREATE TABLE Readings (
    SITE_ID TEXT REFERENCES SiteData,
    LOOP_NBR TEXT,
    LOOP_CLOSED BINARY,
    SEQ INTEGER,
    STA TEXT, 
    BS TEXT,
    FS TEXT,
    HI TEXT,
    DESC TEXT,
    CONSTRAINT UNIQUE (SITE_ID, STA)
 ) 


CREATE TABLE Readings (
    SITE_ID TEXT UNIQUE REFERENCES SiteData 
    , LOOP_NBR TEXT
    , LOOP_CLOSED BINARY
    , SEQ INTEGER
    , STA TEXT UNIQUE
    , BS TEXT
    , FS TEXT
    , HI TEXT
    , DESC TEXT
 )

Adding UNIQUE to the foreign key will refuse only rows with same SITE_ID and STA

0

精彩评论

暂无评论...
验证码 换一张
取 消