(root@localhost) [d01]> alter table student change id id int primary key auto_increment;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [d01]> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
(root@localhost) [d01]> alter table student change id id int primary key;
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [d01]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
(root@localhost) [d01]> alter table student drop index index_name;
(root@localhost) [d01]> alter table student add index index_name(name);
(root@localhost) [d01]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | NO | | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
(root@localhost) [d01]> create index index_name on student(name(11));
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [d01]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
(root@localhost) [d01]> show create table student\G
*************************** 1. row ***************************
Table: student
Create Table: CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` char(20) NOT NULL,
`age` tinyint(2) NOT NULL DEFAULT '0',
`dept` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_name` (`name`(11))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
(root@localhost) [d01]> show index from student\G
*************************** 1. row ***************************
Table: student
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: student
Non_unique: 1
Key_name: index_name
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 0
Sub_part: 11
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
(root@localhost) [d01]> create index ind_name_dept on student(name,dept);
(root@localhost) [d01]> drop index ind_name_dept on student;
(root@localhost) [d01]> create index ind_name_dept on student(name(11),dept(10));
(root@localhost) [d01]> create unique index uni_ind_dept on student(dept);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [d01]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | NO | MUL | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
(root@localhost) [d01]> CREATE TABLE employee (id int(10),name char(20),phone int(12));
(root@localhost) [d01]> INSERT INTO employee(id,name,phone) VALUES(01,'Tom',110110110);
ERROR 1105 (HY000): Percona-XtraDB-Cluster prohibits use of DML command on a table (d01.employee) without an explicit primary key with pxc_strict_mode = ENFORCING or MASTER
(root@localhost) [d01]> alter table employee change id id int primary key;
(root@localhost) [d01]> select * from stu;
+----+------+-----+------+
| id | name | age | dept |
+----+------+-----+------+
| 1 | abc | 12 | eee |
| 2 | tom | 13 | fff |
+----+------+-----+------+
2 rows in set (0.00 sec)
(root@localhost) [d01]> delete from stu;
Query OK, 2 rows affected (0.03 sec)
(root@localhost) [d01]> select * from stu;
Empty set (0.00 sec)
(root@localhost) [d01]> show create table stu\G
*************************** 1. row ***************************
Table: stu
Create Table: CREATE TABLE `stu` (
`id` int(11) NOT NULL,
`name` char(20) NOT NULL,
`age` tinyint(2) NOT NULL DEFAULT '0',
`dept` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)