2018-04-22-3章-约束以及修改数据表

三 约束以及修改数据表

3-1 MySQL外键约束的要求解析

约束

  1. 约束保证数据的完整性和一致性

  2. 约束分为表级约束和列级约束

  3. 约束类型包括:

    ​ NOT NULL (非空约束)

    ​ PRIMARY KEY (主键约束)

    ​ UNIQUE KEY (唯一约束)

    ​ DEFAULT (默认约束)

    ​ FOREIGN KEY (外键约束)

FOREIGN KEY

保持数据的一致性,完整性

实现一对一或一对多关系

外键约束的要求

  1. 父表和子表必须使用相同的存储引擎,而且禁止使用临时表

  2. 数据表的存储引擎只能为InnoDB

  3. 外键列和参照列必须具有相似的数据类型。

    1. 其中数字的长度或是否有符号位必须相同;
    2. 而字符的长度可以不同
  4. 外键列和参照列必须创建索引。如果外键列不存在的话,

    MySQL将自动创建索引

编辑数据表的默认引擎

MySQL配置文件

​ default-storage-engine=INNODB

MySQL支持外键,它允许您在表中交叉引用相关数据,并支持 外键约束,这有助于保持这种扩展数据的一致性。在CREATE TABLEor ALTER TABLE语句中外键约束定义的基本语法如下所示:

[CONSTRAINT [symbol]] FOREIGN KEY
    [index_name] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name,...)
    [ON DELETE reference_option]
    [ON UPDATE reference_option]

reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT

index_name代表外键ID。index_name如果在可支持外键的子表上已经有显式定义的索引,则该值将被忽略。否则,MySQL会隐式创建一个按照以下规则命名的外键索引:

  • 如果已定义,CONSTRAINT symbol则使用该值。否则,使用该FOREIGN KEY index_name值。
  • 如果既没有CONSTRAINT symbolFOREIGN KEY index_name定义,使用引用外键列的名称所产生的外键索引名。
创建两张表 `province`和`city`,给城市表添加外键p_id字段添加外键约束

mysql> create table `province`(
    -> id smallint(5) unsigned primary key auto_increment,
    -> name varchar(30) not null
    -> );
Query OK, 0 rows affected (0.17 sec)

数据类型不一致添加失败
mysql> create table `city`(
    -> id smallint primary key auto_increment,
    -> name varchar(30) not null,
    -> p_id bigint,
    -> foreign key (p_id) references province(id)
    -> );
ERROR 1215 (HY000): Cannot add foreign key constraint

有无符号不一致,添加失败
mysql> create table `city`(
    -> id smallint primary key auto_increment,
    -> name varchar(30),
    -> p_id smallint,
    -> foreign key (p_id) references province(id)
    -> );
ERROR 1215 (HY000): Cannot add foreign key constraint

添加成功

mysql> create table `city`(
    -> id smallint primary key auto_increment,
    -> name varchar(30),
    -> p_id smallint unsigned,
    -> foreign key (p_id) references province (id)                                                                
    -> );
Query OK, 0 rows affected (0.07 sec)


如果外键列不存在得话,MySQL会自动创建索引
查看索引

mysql> show indexes from province;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| province |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> show indexes from city;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| city  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| city  |          1 | p_id     |            1 | p_id        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)




mysql> show indexes from city\G;
*************************** 1. row ***************************
        Table: city
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: city
   Non_unique: 1
     Key_name: p_id
 Seq_in_index: 1
  Column_name: p_id
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

ERROR: 
No query specified

3-2 外键约束得参数操作

在外键约束当中,在实际的开发过程中,我们很少使用物理的外键约束,很多都去使用逻辑的外键约束,因为物理的外键约束只有INNODB这种引擎才会支持,像我们另外的一种引擎MYISAM的引擎则不支持,反过来说,如果我想创建的数据表,假设存储引擎为MYISAM,而且又想使用外键约束的话,其实是不可能实现的,所以说,我们在实际的项目开发中,我们不去定义物理的外键,所谓的逻辑外键指的是就是我们在定义两张表的结构的时候,我们是按照存在的某种结构的方式去定义,但是不去使用FOREIGN KEY这个关键词来定义。

3-3 表级约束与列级约束

  • 对一个数据列建立的约束,称为列级约束
  • 对多个数据列建立的约束,称为表级约束
  • 列级约束既可以在列定义时声明,也可以在列定义后声明
  • 表级约束只能在列定义后声明

NOT NULL 和DEFAULT 这两种约束只有列级约束

其他的主键、唯一、外键都可以存在列级约束和表级约束

3-4 mysql修改数据表 添加/删除列

添加单列

ALTER TABLE tbl_name ADD [COLUMN] col_name

column_definition [FIRST | AFTER col_name]


默认添加到最后

mysql> alter table `user` add xueli varchar(30) not null;
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from `user`;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
| xueli    | varchar(30)         | NO   |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


添加sex列在age后面

mysql> alter table `user` add sex varchar(20) null after age;
Query OK, 0 rows affected (0.36 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from `user`;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| sex      | varchar(20)         | YES  |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
| xueli    | varchar(30)         | NO   |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

添加多列

ALTER TABLE tbl_name ADD [COLUMN]

(col_name column_definition,…)

添加多列用括号括起来,并且多列不能指定位置

删除列

ALTER TABLE tbl_name DROP [COLUMN] col_name


删除一列

mysql> alter table user drop xueli;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from user;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
| sex      | varchar(20)         | YES  |     | NULL    |       |
| salary   | float(8,2) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


删除两列

mysql> alter table user drop sex,drop salary;
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from user;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20)         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

3-5 修改数据表–添加约束

添加主键约束

ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]]

PRIMARY KEY [index_type] (index_col_name,…)


创建一张 t1 表

mysql> create table `t1`(
    -> name varchar(30) not null,
    -> p_id smallint unsigned
    -> );
Query OK, 0 rows affected (0.07 sec)

mysql> show create table t1;
+-------+------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                       |
+-------+------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `name` varchar(30) NOT NULL,
  `p_id` smallint(5) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)


添加一个 id 字段

mysql> alter table t1 add id smallint unsigned;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns form t1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form t1' at line 1
mysql> show columns from t1;
+-------+----------------------+------+-----+---------+-------+
| Field | Type                 | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| name  | varchar(30)          | NO   |     | NULL    |       |
| p_id  | smallint(5) unsigned | YES  |     | NULL    |       |
| id    | smallint(5) unsigned | YES  |     | NULL    |       |
+-------+----------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)



把 id 字段 添加主键约束

mysql> alter table t1 add constraint PK_t1_id primary key (id);
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from t1;
+-------+----------------------+------+-----+---------+-------+
| Field | Type                 | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| name  | varchar(30)          | NO   |     | NULL    |       |
| p_id  | smallint(5) unsigned | YES  |     | NULL    |       |
| id    | smallint(5) unsigned | NO   | PRI | NULL    |       |
+-------+----------------------+------+-----+---------+-------+


mysql> show create table t1;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                   |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `name` varchar(30) NOT NULL,
  `p_id` smallint(5) unsigned DEFAULT NULL,
  `id` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

添加唯一约束

ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]]

UNIUQE  [INDEX|KEY] [index_name] [index_type]
(index_col_name,...)


给t1 表 的name字段添加唯一约束

mysql> alter table t1 add unique (name);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t1;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `name` varchar(30) NOT NULL,
  `p_id` smallint(5) unsigned DEFAULT NULL,
  `id` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

添加外键约束

ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]]

FOREIGN KEY [index_name] (index_col_name,…)

reference_definition


给 t1 表 p_id 字段添加外键约束 关联到 province 表的id 字段


mysql> show create table t1;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `name` varchar(30) NOT NULL,
  `p_id` smallint(5) unsigned DEFAULT NULL,
  `id` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

添加外键约束

mysql> alter table t1 add foreign key (p_id)
    -> references province (id)
    -> ;
Query OK, 0 rows affected (0.30 sec)
Records: 0  Duplicates: 0  Warnings: 0


再次查看

mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                    |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `name` varchar(30) NOT NULL,
  `p_id` smallint(5) unsigned DEFAULT NULL,
  `id` smallint(5) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `p_id` (`p_id`),
  CONSTRAINT `t1_ibfk_1` FOREIGN KEY (`p_id`) REFERENCES `province` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


添加/删除 默认约束

ALTER TABLE tbl_name ALTER [COLUMN] col_name

{SET DEFAULT literal | DROP DEFAULT}


给 t1 添加 age 字段

mysql> alter table t1 add age varchar(30);
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

查看age 字段 默认值 

mysql> show columns from t1;
+-------+----------------------+------+-----+---------+-------+
| Field | Type                 | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| name  | varchar(30)          | NO   | UNI | NULL    |       |
| p_id  | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id    | smallint(5) unsigned | NO   | PRI | NULL    |       |
| age   | varchar(30)          | YES  |     | NULL    |       |
+-------+----------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

修改 age 字段默认值

mysql> alter table t1 alter age set default 15;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

查看  age 字段默认值

mysql> show columns from t1;
+-------+----------------------+------+-----+---------+-------+
| Field | Type                 | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| name  | varchar(30)          | NO   | UNI | NULL    |       |
| p_id  | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id    | smallint(5) unsigned | NO   | PRI | NULL    |       |
| age   | varchar(30)          | YES  |     | 15      |       |
+-------+----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


删除  age 字段的默认值

mysql> alter table t1 alter age drop default;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

查看 age 字段的默认值

mysql> show columns from t1;
+-------+----------------------+------+-----+---------+-------+
| Field | Type                 | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| name  | varchar(30)          | NO   | UNI | NULL    |       |
| p_id  | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id    | smallint(5) unsigned | NO   | PRI | NULL    |       |
| age   | varchar(30)          | YES  |     | NULL    |       |
+-------+----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3-6 修改数据表–删除主键约束

删除主键约束

ALTER TABLE tbl_name DROP PRIMARY KEY

为什么不需要指定字段名?因为(每张表只有一个外键)

mysql> alter table t1 drop primary key;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

删除唯一约束

ALTER TABLE tbl_name DROP { INDEX | KEY} index_name

需要指定 index_name 其实就是key_name,因为一张表可以有

多个唯一约束


查看创建表 city的详细信息

mysql> show create table city;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                       |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| city  | CREATE TABLE `city` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `p_id` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `p_id` (`p_id`),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`p_id`) REFERENCES `province` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


查看city表的索引


mysql> show indexes from city;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| city  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| city  |          0 | name     |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| city  |          1 | p_id     |            1 | p_id        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)




mysql> show columns from city;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name  | varchar(32) | NO   | UNI | NULL    |                |
| p_id  | smallint(6) | YES  | MUL | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


删除city 表的唯一约束 name

其实drop 后面跟 index 和 key 都行

mysql> alter table city drop key name;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from city;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name  | varchar(32) | NO   |     | NULL    |                |
| p_id  | smallint(6) | YES  | MUL | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


删除外键约束

ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol

fk_symbol 就是 constraint 后面跟的city_ibfk_1

CONSTRAINT city_ibfk_1 FOREIGN KEY (p_id) REFERENCES province

删除 city 表的外键约束 p_id


首先查看一下


mysql> show create table city;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                         |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| city  | CREATE TABLE `city` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `p_id` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `p_id` (`p_id`),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`p_id`) REFERENCES `province` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


删除外键约束 CONSTRAINT `city_ibfk_1`


mysql> alter table city drop foreign key `city_ibfk_1`;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0


再来产看一下,发现索引还存在  KEY `p_id` (`p_id`)
mysql> show create table city;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                           |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| city  | CREATE TABLE `city` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `p_id` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `p_id` (`p_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


删除索引KEY `p_id` (`p_id`)

mysql> alter table city drop index `p_id`;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0


最后再来查看
外键约束 CONSTRAINT `city_ibfk_1
索引KEY `p_id` (`p_id`)
都被删除了


mysql> show create table city;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                    |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| city  | CREATE TABLE `city` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `p_id` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)



3-7 修改数据表–修改列定义

ALTER TABLE tbl_name MODIFY [COLUMN] col_name

column_definition [FIRST|AFTER col_name]


修改city表的p_id字段移到最前面

查看

mysql> show columns from city;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name  | varchar(32) | NO   |     | NULL    |                |
| p_id  | smallint(6) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


修改

mysql> alter table city modify p_id smallint
    -> first;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0


再查看

mysql> show columns from city;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| p_id  | smallint(6) | YES  |     | NULL    |                |
| id    | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name  | varchar(32) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)



修改city表的p_id字段的数据类型
从smallint 改为 tinyint unsigned

注意再修改列定义的时候,从大存储范围到小存储范围
可能会出现存储范围的问题


mysql> alter table city modify p_id
    -> tinyint unsigned;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0



mysql> show columns from city;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| p_id  | tinyint(3) unsigned | YES  |     | NULL    |                |
| id    | smallint(6)         | NO   | PRI | NULL    | auto_increment |
| name  | varchar(32)         | NO   |     | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


3-8 修改数据表–修改列的名称

ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name

new_col_name column_definition [FIRST|AFTER col_name]

mysql> alter table city change p_id
    -> mdp_id smallint unsigned;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from city;
+--------+----------------------+------+-----+---------+----------------+
| Field  | Type                 | Null | Key | Default | Extra          |
+--------+----------------------+------+-----+---------+----------------+
| mdp_id | smallint(5) unsigned | YES  |     | NULL    |                |
| id     | smallint(6)          | NO   | PRI | NULL    | auto_increment |
| name   | varchar(32)          | NO   |     | NULL    |                |
+--------+----------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

3-9 修改数据表–修改表的名称

ALTER TABLE tbl_name RENAME [TO|AS] new_tbl_name


将 表名 city 改为 city_md

mysql> alter table city rename city_md;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+-----------------+
| Tables_in_my_db |
+-----------------+
| city_md         |
| province        |
+-----------------+
2 rows in set (0.00 sec)

修改mysql某个用户的密码

mysql> select Host,User,password from user;
+-----------+-------+-------------------------------------------+
| Host      | User  | password                                  |
+-----------+-------+-------------------------------------------+
| localhost | root  | *8DCDD69CE7D121DE8013062AEAEB2A148910D50E |
| ubuntu64  | root  |                                           |
| 127.0.0.1 | root  |                                           |
| ::1       | root  |                                           |
| localhost |       |                                           |
| ubuntu64  |       |                                           |
| %         | admin | *0E6A5B1727D044B45CEE7A396EE99EC967DB7B98 |
+-----------+-------+-------------------------------------------+
7 rows in set (0.00 sec)

mysql> update mysql.user set password=password('Root110qwe') where User="admin";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

猜你喜欢

转载自blog.csdn.net/github_38970218/article/details/88616506