MySql在两列上创建唯一键

MySql在两列上创建唯一键,mysql,many-to-many,mariadb,Mysql,Many To Many,Mariadb,我试图创建一个多对多关系,但很难使中间表在两列上唯一。这是我对这三个表的模式 create table users ( id int primary key auto_increment, username varchar(255) not null, password varchar(255) not null, email varchar(255) not null ) create table products ( id int primary key auto_incremen

我试图创建一个多对多关系,但很难使中间表在两列上唯一。这是我对这三个表的模式

create table users 
( 
id int primary key auto_increment,
username varchar(255) not null,
password varchar(255) not null,
email varchar(255) not null

)

create table products
(
 id int primary key auto_increment,
 product_name varchar(255) not null,
 product_description text(999) not null,
 product_price int not null
)

create table users_products
(
  id int primary key auto_increment,
  user_id int null,
  product_id int null
  unique key 'users_product_index' ('user_id', 'product_id')
)
在我使用这个SQL代码的地方,它给出了这个错误

#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 ''user_id', 'product_id')
)' at line 6
这显然只是一种做法,但整个项目允许多个用户列出产品进行销售

谢谢你的帮助。

试试这个:

create table users_products
(
  id int primary key auto_increment,
  user_id int null,
  product_id int null,
  unique key `users_product_index` (`user_id`, `product_id`)
)
请注意
product_id int null
后面的逗号和'instead of'


希望有帮助。

你漏掉了一个逗号。但该模式存在一些不太理想的问题。请参阅提示:

唯一键“users\u product\u index”(“user\u id”,“product\u id”)
中指定列时,是否尝试使用反勾号
`
插入单引号
可能在
product\u id int null
后缺少逗号。您显示的示例实际起作用。感谢您的帮助和网站。这看起来是我真正需要的资源。