MySQL error cannot add foreign key constraint -


what wrong?

mysql> create table price(     -> p_code char(1) not null,     -> p_description varchar(20),     -> p_rentfee decimal(2,2) not null,     -> p_dylatefee decimal(2,2)); query ok, 0 rows affected (0.18 sec)  mysql> create table movie(     -> mv_no char(4) not null,     -> mv_name varchar(50) not null,     -> mv_year char(4) not null,     -> mv_cost decimal(2,2) not null,     -> mv_genre varchar(15) not null,     -> p_code char(1) not null,     -> foreign key (p_code) references price(p_code)); error 1215 (hy000): cannot add foreign key constraint  mysql> 

price.p_code not primary key price. try:

create table price( p_code char(1) not null primary key, p_description varchar(20), p_rentfee decimal(2,2) not null, p_dylatefee decimal(2,2)); 

in general, foreign keys must reference primary/unique key, whole primary/unique key, , nothing primary/unique key.

in rdbms, example sql server, can reference column unique index (not key) (see can have foreign key not primary key in other table?), non-standard behavior.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -