Mysql study concluded (6) - MySql detailed interpretation of the ALTER command usage

Usage MySql Alter command syntax, which is more of a grammar usage, and function is very powerful.

[sql]  view plain copy
  1. USE learning; (we have to built in advance)  
  2. CREATE TABLE student(id INT NOT NULL,  
  3. name CHAR(10) NOT NULL,  
  4. class INT NOT NULL,  
  5. age INT  
  6. );  
Good look at the new table

First, delete, add or modify fields table

  • Delete table field


Use the following command ALTER command over the age field and delete DROP clause to create a table:

[sql]  view plain copy
  1. ALTER TABLE student  DROP age;  
Look at the results:

If the data in the table only remaining one field you can not use DROP to delete the field.

  • Adding table fields

ADD clause using MySQL data table would like to add a column, adding the following examples student age field in the table, the data type and define:

[sql]  view plain copy
  1. ALTER TABLE student  ADD age INT NOT NULL;  
执行以上命令后,i 字段会自动添加到数据表字段的末尾。

SHOW COLUMNS  FROM student来看表结构

                                                             
如果你需要指定新增字段的位置,可以使用MySQL提供的关键字 FIRST (设定位第一列), AFTER 字段名(设定位于某个字段之后)。
尝试以下 ALTER TABLE 语句, 在执行成功后,使用 SHOW COLUMNS 查看表结构的变化:

[sql]  view plain copy
  1. ALTER TABLE student  ADD sex CHAR(2) FIRST;  


FIRST 和 AFTER 关键字只占用于 ADD 子句,所以如果你想重置数据表字段的位置就需要先使用 DROP 删除字段然后使用 ADD 来添加字段并设置位置。
[sql]  view plain copy
  1. ALTER TABLE student  DROP sex;  
  2. ALTER TABLE student  ADD sex CHAR(2) AFTER age;  


  • 修改表字段

修改字段类型及名称
如果需要修改字段类型及名称, 你可以在ALTER命令中使用 MODIFY 或 CHANGE 子句 。
例如,把字段 name 的类型从 CHAR(10) 改为 CHAR(100),可以执行以下命令:


[sql]  view plain copy
  1. ALTER TABLE student  MODIFY age CHAR(100);  


使用 CHANGE 子句, 语法有很大的不同。 在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段的类型及名称。尝试如下实例:

[sql]  view plain copy
  1. ALTER TABLE student CHANGE id  stu_id BIGINT PRIMARY KEY;  

                                               

ALTER TABLE 对 Null 值和默认值的影响
当你修改字段时,你可以指定是否包含只或者是否设置默认值。
以下实例,指定字段sex为 NOT NULL 且默认值为男 。

[sql]  view plain copy
  1. ALTER TABLE sutdent  MODIFY sex  CHAR(2)  NOT NULL DEFAULT '男';  

如果你不设置默认值,MySQL会自动设置该字段默认为 NULL。
你也可以使用 ALTER 命令及 DROP子句来删除字段的默认值,如下实例:

[sql]  view plain copy
  1. ALTER TABLE student ALTER sex DROP DEFAULT;  
  2.  SHOW COLUMNS FROM student;  

                                                              

修改数据表类型,可以使用 ALTER 命令及 TYPE 子句来完成。尝试以下实例,我们将表 student的类型修改为 MYISAM :
注意:查看数据表类型可以使用 SHOW CREATE TABLE  语句。

[sql]  view plain copy
  1. ALTER TABLE student ENGINE = MYISAM  
  2. SHOW CREATE TABLE student;  


二、修改表名

如果需要修改数据表的名称,可以在 ALTER TABLE 语句中使用 RENAME 子句来实现。
尝试以下实例将数据表 student 重命名为 student_1:


mysql> ALTER TABLE student RENAME TO student_1;


三、主键和索引修改

  • 删除表中主键

[sql]  view plain copy
  1. ALTER TABLE student  DROP PRIMARY KEY;  


  • Add a primary key

[sql]  view plain copy
  1. ALTER TABLE student   ADD CONSTRAINT PK_STUDENT  PRIMARY KEY (id,class);  


Add index

[sql]  view plain copy
  1. ALTER TABLE student ADD INDEX index_name (name);  

View Index

[sql]  view plain copy
  1. SHOW INDEX FROM student;  


Adding a unique constraint index

[sql]  view plain copy
  1. ALTER TABLE  student   ADD UNIQUE  emp_name (age);    


Delete Index

ALTER TABLE student  DROP INDEX index_name;

Reproduced in: https: //my.oschina.net/zhanghaiyang/blog/606213

Guess you like

Origin blog.csdn.net/weixin_33806300/article/details/92658258