Solution to multiple primary key defined error message in MySQL

Source: Internet
Author: User

There are two ways to create a primary key:

CREATE TABLE Table name (
Field Name Type,
Field Name Type,
......
Primary key (name)
);

or a

CREATE TABLE Table name (
Field Name Type primary key,
Field Name Type,
......
);

However, it is important to note that you cannot use the second method if you want to create a composite primary key.
Here is a small example:


Here is an error multiple primary key defined, which indicates that multiple primary keys are defined, and the correct wording is as follows:

If the key to set the composite primary key, the characteristics of the composite primary key is created at the same time, delete, so you need to delete the primary key, but here set the self-increment, you need to first remove the primary key, if you do not delete the auto-increment, and directly delete the primary key will be an error (as follows).

You need to delete the self-increment first:

Here we create the composite primary key, remove the normal index on the name field first, and then create the composite primary key for the stu_id and name fields:

Summary: Two ways to create a primary key at the beginning of the article, the second method can not be used when creating a composite primary key, the composite primary key to be created at the same time, delete.

Solution to multiple primary key defined error message in MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.