简体   繁体   English

MySQL外键约束

[英]MySQL Foreign Key constraints

I'm a beginner with SQL and MySQL. 我是SQL和MySQL的初学者。 So when trynig to run this code generated with powerdesign 因此,当trynig运行使用powerdesign生成的代码时

drop table if exists Administrateur;

drop table if exists Cours;

drop table if exists Etudiant;

drop table if exists Fichier;

drop table if exists Message;

drop table if exists Offre;

drop table if exists OrganismeD_accueil;

drop table if exists Rapport;

drop table if exists Tag;

drop table if exists avoir;

drop table if exists demander;

drop table if exists s_adresser;

drop table if exists soumettre;

drop table if exists telecharger;

/*==============================================================*/
/* Table : Administrateur                                       */
/*==============================================================*/
create table Administrateur
(
   idAdmin              int not null,
   nomA                 varchar(254),
   prenomA              varchar(254),
   poste                varchar(254),
   primary key (idAdmin)
 );

 /*==============================================================*/
 /* Table : Cours                                                */
/*==============================================================*/
create table Cours
 (
   idF                  int not null,
   intituleC            varchar(254) not null,
   matiere              varchar(254),
   primary key (idF, intituleC)
 );

/*==============================================================*/
/* Table : Etudiant                                             */
/*==============================================================*/
create table Etudiant
(
   idE                  int not null,
   nomE                 varchar(254),
   prenomE              varchar(254),
   adresseMail          varchar(254),
   numCarteEtudiant     numeric(8,0),
   motDePasse           varchar(254),
   dateNaiss            datetime,
   tel                  numeric(8,0),
   groupe               char(1),
   primary key (idE)
);

/*==============================================================*/
/* Table : Fichier                                              */
/*==============================================================*/
create table Fichier
(
   idF                  int not null,
   titre                varchar(254),
   url                  varchar(254),
   typeF                varchar(254),
   primary key (idF)
 );

 /*==============================================================*/
 /* Table : Message                                              */
 /*==============================================================*/
 create table Message
 (
    idS                  int not null,
    dateMsg              datetime,
    dateLimite           datetime,
    typeMsg              varchar(254),
    primary key (idS)
 );

 /*==============================================================*/
 /* Table : Offre                                                */
/*==============================================================*/
create table Offre
(
   idF                  int not null,
   idO                  int not null,
   intitule             varchar(254),
   dateDebut            datetime,
   dateFin              datetime,
   typeOffre            varchar(254),
   primary key (idF, idO)
);

/*==============================================================*/
/* Table : OrganismeD_accueil                                   */
/*==============================================================*/
create table OrganismeD_accueil
 (
   idOrg                int not null,
   categorie            varchar(254),
   adress               varchar(254),
   emailOrg             varchar(254),
   numTel               numeric(8,0),
   nomOrg               varchar(254),
   nomResponsable       varchar(254),
   typeOrg              varchar(254),
   spaecialiteOrg       varchar(254),
   gouvernorat          varchar(254),
   categorieOrg         varchar(254),
   primary key (idOrg)
);

/*==============================================================*/
/* Table : Rapport                                              */
/*==============================================================*/
 create table Rapport
 (
   idF                  int not null,
   intituleR            varchar(254) not null,
   idOrg                int not null,
   dateDepot            datetime,
   dateSoutenance       datetime,
   dateValidation       datetime,
   specialite           varchar(254),
   tags                 varchar(254),
   primary key (idF, intituleR)
 );

 /*==============================================================*/
 /* Table : Tag                                                  */
 /*==============================================================*/
create table Tag
(
    idT                  int not null,
    tag                  varchar(254),
    dateT                datetime,
    primary key (idT)
 );

 /*==============================================================*/
 /* Table : avoir                                                */
 /*==============================================================*/
 create table avoir
 (
    idF                  int not null,
    idT                  int not null,
    primary key (idF, idT)
 );

/*==============================================================*/
/* Table : demander                                             */
/*==============================================================*/
 create table demander
 (
    idE                  int not null,
    idS                  int not null,
    primary key (idE, idS)
 );

  /*==============================================================*/
  /* Table : s_adresser                                           */
  /*==============================================================*/
 create table s_adresser
 (
   idAdmin              int not null,
   idE                  int not null,
   primary key (idAdmin, idE)
);

/*==============================================================*/
/* Table : soumettre                                            */
/*==============================================================*/
 create table soumettre
 (
   idF                  int not null,
   intituleR            varchar(254) not null,
   idE                  int not null,
   primary key (idF, intituleR, idE)
 );

/*==============================================================*/
/* Table : telecharger                                          */
/*==============================================================*/
 create table telecharger
 (
    idE                  int not null,
    idF                  int not null,
    primary key (idE, idF)
);

alter table Cours add constraint FK_Generalisation_3 foreign key (idF)
    references Fichier (idF) on delete restrict on update restrict;

alter table Offre add constraint FK_Generalisation_2 foreign key (idF)
   references Fichier (idF) on delete restrict on update restrict;

alter table Rapport add constraint FK_Generalisation_4 foreign key (idF)
   references Fichier (idF) on delete restrict on update restrict;

  alter table Rapport add constraint FK_appartenir foreign key ()
       references OrganismeD_accueil (idOrg) on delete restrict 
       on update 
       restrict;

  alter table avoir add constraint FK_avoir foreign key (idF)
  references Fichier (idF) on delete restrict on update restrict;

alter table avoir add constraint FK_avoir foreign key (idT)
  references Tag (idT) on delete restrict on update restrict;

alter table demander add constraint FK_demander foreign key (idE)
  references Etudiant (idE) on delete restrict on update restrict;

alter table demander add constraint FK_demander foreign key (idS)
  references Message (idS) on delete restrict on update restrict;

alter table s_adresser add constraint FK_s_adresser foreign key (idAdmin)
  references Administrateur (idAdmin) on delete restrict on update restrict;

alter table s_adresser add constraint FK_s_adresser foreign key (idE)
  references Etudiant (idE) on delete restrict on update restrict;

alter table soumettre add constraint FK_soumettre foreign key (idE)
  references Etudiant (idE) on delete restrict on update restrict;

alter table soumettre add constraint FK_soumettre foreign key 
 (idF,  intituleR)
 references Rapport (idF, intituleR) on delete restrict on update restrict;

alter table telecharger add constraint FK_telecharger foreign key (idE)
  references Etudiant (idE) on delete restrict on update restrict;

alter table telecharger add constraint FK_telecharger foreign key (idF)
  references Fichier (idF) on delete restrict on update restrict;

but i get this error and i don't know how to fix it 但我收到此错误,我不知道如何解决

SQL query: SQL查询:

alter table Rapport add constraint FK_appartenir foreign key ()
  references OrganismeD_accueil (idOrg) on delete restrict 
  on update   restrict

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near ')
references OrganismeD_accueil (idOrg) on delete restrict on update
rest' at line 1

i can not get the problem with "on delete restrict on update restrict" Any help please ? 我无法获得“在删除时限制更新时限制”的问题,请帮忙吗?

You did not mention the column of your foreign key: 您没有提及外键列:

alter table Rapport add constraint FK_appartenir foreign key (/*add here your key column*/) 
references OrganismeD_accueil (idOrg) on delete restrict on update restrict;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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