Mysql SQL statement operation to add or modify primary key

Mysql SQL statement operation to add or modify primary key

Add table fields

alter table table1 add transactor varchar(10) not Null;
alter table table1 add id int unsigned not Null auto_increment primary key

Modify the field type of a table and specify it as empty or non-empty

alter table table namechange field namefield namefield type[whether non-empty is allowed];

alter table table name modify field name field type [whether non-empty is allowed];

alter table table name modify field name field type [whether non-empty is allowed];

Modify the field name of a table and specify it as empty or non-empty

alter table table name change field original name field new name field type [whether non-empty is allowed

Delete a field

ALTER TABLE mytable DROP column name;

Adding a unique key

ALTER TABLE `test2` ADD UNIQUE ( `userid`)

Modify the primary key

ALTER TABLE `test2` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `id` )

Adding Index

ALTER TABLE `test2` ADD INDEX ( `id` )
ALTER TABLE `category` MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST ,ADD PRIMARY KEY (`id`);

The SQL statement block for modifying the primary key is as follows

New fields in the mailbox table

DROP PROCEDURE IF EXISTS mailbox_column_update;
CREATE PROCEDURE mailbox_column_update() BEGIN
 -- Add a delete flag column IF NOT EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='cbs' AND table_name='mailbox' AND COLUMN_NAME='delete_flag') THEN
   ALTER TABLE mailbox ADD delete_flag int DEFAULT 2 NOT NULL;
 END IF;
 -- Add a new deletion date column IF NOT EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='cbs' AND table_name='mailbox' AND COLUMN_NAME='delete_date') THEN
   ALTER TABLE mailbox ADD delete_date int DEFAULT 0 NOT NULL;
 END IF;
 -- If the field account_mail exists, modify the field length IF EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='cbs' AND table_name='mailbox' AND COLUMN_NAME='email_account')
 THEN
  alter table mailbox modify column email_account varchar(320);
 END IF;
 -- If there is no primary key column, set a double primary keyIF ((SELECT count(*) FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA ='cbs' AND table_name='mailbox' AND CONSTRAINT_NAME ='PRIMARY' AND (COLUMN_NAME ='email_account' OR COLUMN_NAME = 'company_id'))=0)THEN
    ALTER TABLE mailbox ADD primary key (company_id,email_account);
 -- If there is only one primary key column ELSEIF ((SELECT count(*) FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA ='cbs' AND table_name='mailbox' AND CONSTRAINT_NAME ='PRIMARY' AND (COLUMN_NAME ='email_account' OR COLUMN_NAME = 'company_id'))<2) THEN
    ALTER TABLE mailbox DROP PRIMARY KEY,ADD primary key (company_id,email_account);
 END IF;
  
END;
CALL mailbox_column_update();
DROP PROCEDURE IF EXISTS mailbox_column_update;

Supplement: mysql modifies the primary key to increase automatically, and adds a joint primary key

ALTER TABLE `onduty_history`
MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST ,
MODIFY COLUMN `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `id`,
MODIFY COLUMN `onduty_date` datetime NOT NULL AFTER `name`,
ADD UNIQUE KEY (`id`),
ADD PRIMARY KEY (`name`, `onduty_date`);

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • In-depth discussion on auto-increment primary keys in MySQL
  • The difference and advantages and disadvantages of Mysql primary key UUID and auto-increment primary key
  • MySQL primary key naming strategy related
  • What to do if the auto-increment primary key in MySQL is used up
  • Solution to running out of MySQL's auto-increment ID (primary key)
  • In-depth analysis of why MySQL does not recommend using uuid or snowflake id as primary key
  • Detailed explanation of non-primary key column overflow monitoring in MySQL tables
  • Use prometheus to count the remaining available percentage of MySQL auto-increment primary keys

<<:  Use nginx to dynamically convert image sizes to generate thumbnails

>>:  Detailed explanation of Vue's front-end system and front-end and back-end separation

Recommend

Introduction to common MySQL storage engines and parameter setting and tuning

MyISAM, a commonly used storage engine in MySQL c...

Vue two-choice tab bar switching new approach

Problem Description When we are working on a proj...

MySQL 4G memory server configuration optimization

As the number of visits to the company's webs...

Linux disk sequential writing and random writing methods

1. Introduction ● Random writing will cause the h...

Solution to index failure in MySQL due to different field character sets

What is an index? Why create an index? Indexes ar...

Personal opinion: Talk about design

<br />Choose the most practical one to talk ...

Vue development tree structure components (component recursion)

This article example shares the specific code of ...

Tutorial on binary compilation and installation of MySql centos7 under Linux

// It took me a whole afternoon to install this, ...

Solution to span width not being determined in Firefox or IE

Copy code The code is as follows: <html xmlns=...

Analysis of the differences between Iframe and FRAME

1. Use of Iframe tag <br />When it comes to ...

JavaScript implements the nine-grid mobile puzzle game

This article shares the specific code for JavaScr...

Interpretation of Vue component registration method

Table of contents Overview 1. Global Registration...

Vue+el-table realizes merging cells

This article example shares the specific code of ...

VMware configuration VMnet8 network method steps

Table of contents 1. Introduction 2. Configuratio...