简体   繁体   中英

Problem with alter and adding foreign keys in MySQL with phpMyadmin

A constant error is happening to me when creating a foreign key for a project and that is that phpMyAdmin detects that there is a syntax error when I launch this instruction.

alter table articles add constraint idUsers
foreign key idUsers
references users ('idUsers');

And it gives as an error the following.

#1064 - Algo está equivocado en su sintax cerca 'references users ('idUsers')' en la linea 3

It still gives me a syntax problem. I'm going to pass the entire code.

drop database if exists blog;
create database if not exists blog;
use blog;
create table if not exists users(
    idUsers int not null,
    name varchar(45) not null,
    password varchar(32) not null,
    email varchar(45) not null,
    primary key (idUsers)
)ENGINE=InnoDB;

create table if not exists category(
    idCategory int not null,
    category varchar(45) not null,
    primary key (idCategory)
)ENGINE=InnoDB;

create table if not exists articles(
    idArticles int not null,
    title varchar(45),
    content text(255),
    time date not null,
    primary key (idArticles)
)ENGINE=InnoDB;

alter table articles add constraint fk_idUsers
foreign key idUsers
references blog.users (idUsers);

alter table articles add constraint fk_idCategory
foreign key idCategory
references blog.category (idCategory);

And the error message is...

#1064 - Algo está equivocado en su sintax cerca 'references blog.users (idUsers)' en la linea 3

I have already succeeded. I pass the complete code of how it should be. Thank you all for answering my questions.

drop database if exists blog;
create database if not exists blog;
use blog;
create table if not exists users(
    idUsers int primary key,
    name varchar(45) not null,
    password varchar(32) not null,
    email varchar(45) not null
)ENGINE=InnoDB;

create table if not exists category(
    idCategory int primary key,
    category varchar(45) not null
)ENGINE=InnoDB;

create table if not exists articles(
    idArticles int primary key,
    title varchar(45),
    content text(255),
    id_users int,
    id_category int,
    time date not null
)ENGINE=InnoDB;

alter table articles add foreign key(id_users)
references blog.users(idUsers);

alter table articles add foreign key(id_category)
references blog.category(idCategory);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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