Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
create table employee(


emp_id       int,
insurance   varchar(20),
constraint employee_pk
primary key (emp_id),




);

create table selesperson(

shift               varchar(20),
emp_id int
constraint employee_pk
primary key (emp_id)
foreign key references employee(emp_id),


);


What I have tried:

I got an error in employee_pk as primary in table sales person the error said
"
Msg 2714, Level 16, State 5, Line 117
There is already an object named 'employee_pk' in the database.
Msg 1750, Level 16, State 0, Line 117
Could not create constraint. See previous errors.

Completion time: 2022-12-13T18:55:54.4809094+02:00
"

as I got confused because salesperson is supposed to be subclass from employee table so I make employee_id pk in table employee and i make it also pk and fk in table sales person so help please if you could?
Posted
Updated 13-Dec-22 6:07am

1 solution

Quote:
There is already an object named 'employee_pk' in the database.

That's fairly clear, isn't it? You already defined an object name employee_pk as the primary key constraint on the employee table. Rename the primary key constraint on the salesperson table. Perhaps to salesperson_pk.
 
Share this answer
 
Comments
Noran Azab 13-Dec-22 13:06pm    
but in the disjoint table does the pk is supposed to be the same in parent class as well as subclass
k5054 13-Dec-22 14:05pm    
It's not a requirement that a column that is used as a foreign key reference use the same name as the column that it references, they just need to be of the same data type. In this case, your DBS is trying to tell you've already created an object called "employee_pk", which is the name of a constraint on your employee table. When you define your salesperson table, the name of the constrain needs to be unique. The actual name isn't important. You could just as easily do
create table employee(
  emp_id int,
  name varchar(20),
  constraint "jim_bob" primary key (empl_id) );

create table salesperson(
   empl_id int,
   shift varchar(20),
   constraint "billy_bob"  primary key (empl_id),
   constraint "daisy_duke" foreign key (empl_id)  );
As long as the constraint names are unique, and don't conflict with other database objects (tables, indexes, etc), then you're good to go. It's rare to need to refer to a constraint by its name, so the actual name is usually unimportant. Having said which, its a good idea to use a naming convention that makes it clear to a human looking at the database schema what an object might be.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900