MySQL 5中的多列主键 [英] Multi-Column Primary Key in MySQL 5

查看:117
本文介绍了MySQL 5中的多列主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用键并打破习惯,即我的所有表中的所有行都必须具有SERIAL类型ID.同时,我还在进行多对多关系,因此在需要协调关系的表的任一列上要求唯一值会妨碍这一点.

I'm trying to learn how to use keys and to break the habit of necessarily having SERIAL type IDs for all rows in all my tables. At the same time, I'm also doing many-to-many relationships, and so requiring unique values on either column of the tables that coordinate the relationships would hamper that.

如何定义表上的主键,以便任何给定的值都可以在任何列中重复,只要永不完全重复所有列中的值组合即可?

How can I define a primary key on a table such that any given value can be repeated in any column, so long as the combination of values across all columns is never repeated exactly?

推荐答案

创建表语法页面:

主键可以是多列 指数.但是,您无法创建 多列索引使用 列中的PRIMARY KEY键属性 规格.这样做仅是标记 该单列作为主要列.你 必须使用单独的PRIMARY KEY(index_col_name,...)子句.

A PRIMARY KEY can be a multiple-column index. However, you cannot create a multiple-column index using the PRIMARY KEY key attribute in a column specification. Doing so only marks that single column as primary. You must use a separate PRIMARY KEY(index_col_name, ...) clause.

类似的事情可以用于多列主键:

Something like this can be used for multi-column primary keys:

CREATE TABLE
    product (
        category INT NOT NULL,
        id INT NOT NULL,
        price DECIMAL,
        PRIMARY KEY(category, id)
    );

来自 13.1.20.6外键约束

这篇关于MySQL 5中的多列主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆