简体   繁体   中英

Can we use constraint name of a primary key as foreign key reference?

For example, I'm creating two tables as shown below:

create table A (
  department_id int,
  college_id int,
  constraint Pk_name primary key(department_id,college_id)
);

create table B (
  student_name varchar(75),
  department_id int,
  college_id int,
  foreign key(department_id,college_id) references A(Pk_name)
);

Can I write like this?

I don't think so because there's no way that the RDBMS could know whether PK_name is a column or a constraint name so I suggest if you stick with the usual:

create table A ( department_id int, college_id int, constraint Pk_name primary key(department_id,college_id) );

create table B ( student_name varchar(75), department_id int, college_id int, foreign key(department_id,college_id) references A(department_id,college_id) );

I will update the answer once I find an other answer.

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