MySQL database technology - daily operation of production environment [alter]

In the daily production process, the online environment needs to synchronize the table structure of the development environment database and the production environment database, which involves a large number of online operations, which are recorded for backup;

1. Create and modify indexes

Create an index:

CREATE INDEX indexName ON table_name (column_name)

For CHAR and VARCHAR types, length can be less than the actual length of the field; for BLOB and TEXT types, length must be specified.

Modify table structure (add index)

ALTER table tableName ADD INDEX indexName(columnName)


Specify directly when creating the table

CREATE TABLE mytable(  
ID INT NOT NULL,   
username VARCHAR(16) NOT NULL,  
INDEX [indexName] (username(length))   
);  

delete index

DROP INDEX [indexName] ON mytable; 


2. ALTER command to add and delete indexes

There are four ways to add indexes to data tables:

ALTER TABLE tbl_name ADD PRIMARY KEY (column_list);

This statement adds a primary key, which means that index values ​​must be unique and cannot be NULL.

ALTER TABLE tbl_name ADD UNIQUE index_name (column_list);

The value of the index created by this statement must be unique (except NULL, NULL may appear multiple times).

ALTER TABLE tbl_name ADD INDEX index_name (column_list);

Add a normal index, the index value can appear multiple times.

ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list);

This statement specifies that the index is FULLTEXT for full-text indexing.

The following example adds an index to the table:

ALTER TABLE testalter_tbl ADD INDEX (c);

Indexes can also be dropped using the DROP clause in the ALTER command:

ALTER TABLE testalter_tbl DROP INDEX c;


3. ALTER command to add and delete primary key

The primary key acts on the column (one column or multiple columns can be combined with the primary key). When adding the primary key index, you need to ensure that the primary key is not empty by default (NOT NULL). Examples are as follows:

ALTER TABLE testalter_tbl MODIFY i INT NOT NULL;
ALTER TABLE testalter_tbl ADD PRIMARY KEY (i);

Primary keys can also be dropped using the ALTER command:

ALTER TABLE testalter_tbl DROP PRIMARY KEY;

You only need to specify the PRIMARY KEY when deleting the primary key, but you must know the index name when deleting the index.

4. Display index information

You can use the SHOW INDEX command to list related index information in a table.
The output information can be formatted by adding \G.

\G: Output the queried horizontal table vertically for easy reading.
 

おすすめ

転載: blog.csdn.net/philip502/article/details/130975376