Add table fieldsalter 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-emptyalter 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-emptyalter table table name change field original name field new name field type [whether non-empty is allowed Delete a fieldALTER TABLE mytable DROP column name; Adding a unique keyALTER TABLE `test2` ADD UNIQUE ( `userid`) Modify the primary keyALTER TABLE `test2` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `id` ) Adding IndexALTER 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 followsNew 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:
|
<<: 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
Table of contents 1. Register an account on Baidu...
When customizing the installation of software, yo...
Preface I have been working on some front-end pro...
Nowadays, application development is basically se...
MQTT Protocol MQTT (Message Queuing Telemetry Tra...
Table of contents 1. What is a closure? 2. The ro...
1. Use curl command to access by default: # curl ...
Recently, our small team needs to share a shared ...
Download Tomcat8 image [root@localhost ~]# docker...
1. What is Docker Swarm? Docker Swarm is a cluste...
1. Introduction to Navicat 1. What is Navicat? Na...
Specific method: 1. Press [ win+r ] to open the r...
Table of contents 1. Implementation 2. Problems 3...
Shtml and asp are similar. In files named shtml, s...
When I configured mysql, I set the default storag...