简体   繁体   中英

Create relationship for association table

Normally when creating constraint between two tables i used to use following script:

ALTER TABLE userVendor
ADD CONSTRAINT myrelation
FOREIGN KEY (DataSource, userId) 
REFERENCES user(DataSource, userId)

so userVendor table references user table

Now let's say userVendor becomes association table between two tables. Therefore we have additionally third table called Vendor which has datasource and vendor id as composite key. All of them by the way having composite keys.

How to modify my script to say like (wrong script so far):

ALTER TABLE userVendor
ADD CONSTRAINT myrelation
FOREIGN KEY (DataSource, userId, vendorid) 
REFERENCES user(DataSource, userId)
REFERENCES user(DataSource, vendorid)

You'll have to add two foreign key constraints, each referencing one table. It is no problem if a column appears in both constraints.

You will need two ADD CONSTRAINT clauses in your ALTER TABLE statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM