Solution to the Multiple primary key defined error in MySQL

Solution to the Multiple primary key defined error in MySQL

There are two ways to create a primary key:

create table table name(
Field name type,
Field name type,
…
primary key(name)
);

or

create table table name(
Field name type primary key,
Field name type,
…
);

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

Here the error "Multiple primary key defined" is reported, indicating that multiple primary keys are defined. The correct way to write it is as follows:

If a composite primary key is set for an item, the composite primary key is created and deleted at the same time, so the primary key needs to be deleted. However, auto-increment is set here, so the auto-increment needs to be deleted first before the primary key can be deleted. If the auto-increment is not deleted first and the primary key is deleted directly, an error will be reported (as shown below).

You need to delete the auto-increment first:

If we create a composite primary key here, we first delete the normal index on the name field, and then create a composite primary key for both the stu_id and name fields:

Summary: Of the two ways to create primary keys written at the beginning of the article, the second method cannot be used when creating a composite primary key; composite primary keys must be created and deleted at the same time.

The above is the solution to the Multiple primary key defined error message in MySQL. Thank you for reading and supporting 123WORDPRESS.COM.

You may also be interested in:
  • Problems and solutions for IDEA connecting to MySQL
  • IDEA reports an error when connecting to MySQL! Server returns invalid timezone. Go to tab and set serverTimezone property
  • Solution to the error "using password: NO" when springboot connects to mysql
  • Detailed explanation of the error problem when setting the default value of 0000-00-00 in the date and datetime types of Mysql database
  • Solve the error "Microsoft Visual C++ 14.0 is required" when installing mysqlclient
  • How to deal with errors in MySQL's emoji table storage [change encoding to utf8mb4]
  • Problems and solutions of error 08001 when linking to MySQL in IDEA and no table display after successful connection

<<:  An in-depth introduction to React refs

>>:  Solution to Linux not supporting all commands

Recommend

Detailed tutorial on using cmake to compile and install mysql under linux

1. Install cmake 1. Unzip the cmake compressed pa...

Use PHP's mail() function to send emails

Sending emails using PHP's mail function The ...

How to modify the scroll bar style in Vue

Table of contents First of all, you need to know ...

Brief analysis of MySQL union and union all

In the database, both UNION and UNION ALL keyword...

A simple way to build a Docker environment

First, let’s understand what Docker is? Docker is...

Split and merge tables in HTML (colspan, rowspan)

The code demonstrates horizontal merging: <!DO...

How to use tcpdump to capture packets in Linux system

Let me look at the example code first: 1. Common ...

MySQL and sqlyog installation tutorial with pictures and text

1. MySQL 1.1 MySQL installation mysql-5.5.27-winx...

Centos 7 64-bit desktop version installation graphic tutorial

If you think the system is slow and want to chang...

Use of kubernetes YAML files

Table of contents 01 Introduction to YAML files Y...

vue+tp5 realizes simple login function

This article example shares the specific code of ...

How to remove inline styles defined by the style attribute (element.style)

When modifying Magento frequently, you may encount...

Detailed explanation of how to access MySQL database remotely through Workbench

Preface Workbench is installed on one computer, a...

Why is the MySQL auto-increment primary key not continuous?

Table of contents 1. Introduction 2. Self-increme...

Detailed description of the use of advanced configuration of Firewalld in Linux

IP masquerading and port forwarding Firewalld sup...