SQL Syntax Error in creating FK

0

I want to add a foreign key in the CONSULT However, MySql reports the following error:

  

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'REFERENCES doctor (crm)) ENGINE = InnoDB DEFAULT CHARSET = latin1' at line 9

The SQL code is written as follows:

CREATE TABLE 'medico' (
  'crm' float(12) NOT NULL,
  'cpf' varchar(15) DEFAULT NULL,
  'dataInscricao' varchar(20) DEFAULT NULL,
  'bairro' varchar(50) DEFAULT NULL,
  'cidade' varchar(50) DEFAULT NULL,
  'logradouro' varchar(90) DEFAULT NULL,
  'uf' varchar(2) DEFAULT NULL,
  'nome' varchar(50) DEFAULT NULL,
  'nomeUser' varchar(12) DEFAULT NULL,
  'senhaUser' varchar(12) DEFAULT NULL,
  'cnpj' varchar(15) NOT NULL,
  PRIMARY KEY ('crm')
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE 'consulta' (
  'codConsulta' int(11) NOT NULL,
  'data' varchar(10) DEFAULT NULL,
  'horario' varchar(10) DEFAULT NULL,
  'crm' float(12) NOT NULL,
  'cpf' varchar(15) NOT NULL,
  'codTipoCons' int(11) NOT NULL,
  PRIMARY KEY ('codConsulta'),
  FOREIGN KEY 'crm' REFERENCES 'medico'('crm')
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

What is the problem?

    
asked by anonymous 06.11.2016 / 20:52

2 answers

1

Friend, try to create the table and then change the table by setting the desired FK.

Ex:

    CREATE TABLE 'cidades' ( 
    'codcidade' INT NOT NULL ,
    'descricao' VARCHAR( 50 ) NOT NULL) 
    ENGINE = innodb;

    ALTER TABLE 'clientes' ADD CONSTRAINT 'fk_cidade' FOREIGN KEY ( 'codcidade' ) REFERENCES 'cidade' ( 'codcidade' ) ;

Ref: link

    
07.11.2016 / 13:33
0

() is missing on foreign key creation. It would look like this:

FOREIGN KEY('crm') REFERENCES 'medico'('crm')
    
07.11.2016 / 14:10