【问题标题】:SQL create table primary key and foreign key syntaxSQL创建表主键和外键语法
【发布时间】:2013-10-26 11:19:31
【问题描述】:

我正在为家庭作业创建一个 MySQL 数据库,并在 phpmyadmin 中遇到语法错误 #1005。我认为这与外键有关,但如果 w3schools 正确,我的语法应该很好。

这是 SQL 语句;

create table if not exists customers
(
    id int not null auto_increment, 
    cust_gname varchar(20) not null, 
    cust_fname varchar(30) not null, 
    cust_street varchar(30) not null, 
    cust_suburb varchar(30) not null, 
    cust_state varchar(6) not null, 
    cust_postcode varchar(4) not null, 
    cust_email varchar(50) not null, 
    cust_phone varchar(12), 
    cust_mobile varchar(12), 
    cust_user_id int, 
    foreign key (cust_user_id) references users(id),
    primary key (id)
);

create table if not exists ingredients
(
    id int, 
    name varchar(30) not null,
    primary key (id)
);

create table if not exists recipes
(
    id int, 
    name varchar(30) not null, 
    recipes_menu_id int,
    foreign key (recipes_menu_id) references menus(id)
    image varchar(30),
    primary key (id)
);

create table if not exists ingredients_recipes
(
    id int, 
    ingredients_recipes_ingredient_id int,
    foreign key (ingredients_recipes_ingredient_id) references ingredients(id), 
    ingredients_recipes_recipe_id int,
    foreign key (ingredients_recipes_recipe_id) references recipes(id),
    primary key (id)
);

create table if not exists menus
(
    id int, 
    description varchar(30) not null, 
    menus_restaurant_id int, 
    foreign key (menus_restaurant_id) references restaurants(id),
    primary key (id)
);

create table if not exists restaurants
(
    id int, 
    name varchar(30) not null, 
    address1 varchar(30) not null, 
    address 2 varchar(30), 
    suburb varchar(30) not null, 
    state varchar(10) not null, 
    postcode varchar(4) not null,
    primary key (id)
);

create table if not exists customers_ingredients
(
    id int, 
    customers_ingredients_customer_id int,
    foreign key (customers_ingredients_customer_id) references customers(id), 
    customers_ingredients_ingredient_id int, 
    foreign key (customers_ingredients_ingredient_id) references ingredients(id),
    primary key (id)
);

create table if not exists users
(
    id int, 
    username varchar(40) not null, 
    password varchar(50) not null, 
    group_id int,
    created DATETIME, 
    modified DATETIME,
    primary key (id)
);

create table if not exists groups
(
    id int, 
    name varchar(10) not null, 
    created DATETIME, 
    modified DATETIME,
    primary key (id)
);

【问题讨论】:

  • recipes 表不正确,缺少 ,...
  • w3schools 不是学习 SQL 的好地方。他们的网站充满了错误。 DBMS 手册是查找正确语法的好地方

标签: mysql sql syntax foreign-keys primary-key


【解决方案1】:

如果您要创建具有外键引用的表,则它所引用的表必须已经存在。您在脚本开头创建了一个 customers 表,该表引用了 users 表,该表直到接近尾声时才创建。脚本中还有其他示例。

您需要以正确的顺序创建表格,或者使用顶部的set foreign_key_checks = 0; 来禁用此要求。创建完所有表后,请确保在最后set foreign_key_checks = 1

注意:您的脚本中可能存在其他语法错误 - 我还没有全部检查。

【讨论】:

  • 太棒了,我不知道。你会认为他们会在外键页面上的 w3schools 上提到这一点......
猜你喜欢
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2020-07-07
  • 2013-09-09
相关资源
最近更新 更多