我试图在mariadb中创建一个表,但每次我尝试添加一个外键时,它都会返回一个错误消息。当我尝试创建没有外键的表时,它可以工作。下面是没有执行外键的代码。
create table countries(
country_id int(249) auto_increment primary key,
name varchar(50) not null,
area decimal(10,2) not null,
national_day date,
region_id int not null);
下面是我添加外键的SQL
create table countries(
country_id int(249) auto_increment primary key,
name varchar(50) not null,
area decimal(10,2) not null,
national_day date,
region_id int not null,
foreign key(region_id)
);
当我尝试它时得到的错误是下面的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 ')' at line 1
我也尝试过
create table countries(
country_id int(249) auto_increment primary key,
name varchar(50) not null,
area decimal(10,2) not null,
national_day date,
region_id int not null foreign key
);
它仍然给我的错误,我觉得它应该很容易添加一个外键,但我迷路了。
2条答案
按热度按时间kx1ctssn1#
手册会告诉您外键的正确[格式][1]
fiddle
wribegjk2#
请尝试此代码您还没有给出对表的引用