2

If we have query for creating table like this..

create table if not exists food
(
     id int not null auto_increment,
     user_id int,
     name varchar(30),

     constraint pk_food primary key(id,name),
     foreign key(user_id) references userss(id)
);

What does pk_food mean in this example? I know this is a constraint name, but for what we should be give a name for constraint, if its working without?

create table if not exists food
(
     id int not null auto_increment,
     user_id int,
     name varchar(30),

     primary key (id, name),
     foreign key (user_id) references userss(id)
);

I mean.. how to use these names and for what we need it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Wembley
  • 27
  • 6

1 Answers1

5

You gives constraints names for basically two reasons:

  • You can better understand the error message when the constraint is violated.
  • You can more easily find the constraint if you want to delete it.
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786