Home  >  Q&A  >  body text

关于mysql primary key的解释

我看网上说primary key 就等于唯一+不为空,而且一个表里只能有一个primary key,但是我在数据库创建表的时候PRIMARY KEY (username,bm_URL),这样不就2个主键了?而且我insert数据的时候 username 有重复'admin',并没有报错啊,怎么解释第一句话呢

伊谢尔伦伊谢尔伦2742 days ago1003

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:37:33

    What you write like that is a joint primary key. Username can be repeated, but under the same username, bm_URL can be different

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:37:33

    The meaning of primary key is the unique identification of a record, similar to the ID of an ID card.
    The primary key can be a field or a combination of multiple fields. If it is a field, the primary keys of different records or different rows are not allowed to be repeated; if it is a combination of multiple fields, the combination of these fields is not allowed to be the same. .
    So in your case, as long as the username and bm_URL are not the same at the same time, there will be no conflict.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:37:33

    There can only be one primary key in a table. The primary key is self-increasing and not empty. There can be multiple uniques in the table. It is recommended that you use visual phpadmin or Navicat

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:37:33

    Multiple primary keys are called joint primary keys, which can also be called joint primary keys. You can imagine it as a product order table. The user ID in the record can be repeated, and the user ID and order number must be unique together.

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:37:33

    The index (index or key) in the database is not specific to one column, so a key can be composed of multiple columns (such as the username and bm_URL you set here). Therefore, the uniqueness of the primary key means that all the columns it contains cannot be repeated together. It does not matter if you repeat the username alone~

    reply
    0
  • Cancelreply