修改表結構建立索引
阿新 • • 發佈:2018-12-03
mysql> create database feiyu_bbs; Query OK, 1 row affected (0.00 sec) mysql> use feiyu_bbs; Database changed mysql> create table bbs_user( -> id int unsigned not null auto_increment primary key, -> username varchar(32) not null unique, -> password char(32) not null, -> rtime int not null default 0, -> rip int not null default 0 -> )engine=myisam default charset=utf8; Query OK, 0 rows affected (0.01 sec) mysql> desc bbs_user; +----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(32) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | rtime | int(11) | NO | | 0 | | | rip | int(11) | NO | | 0 | | +----------+------------------+------+-----+---------+----------------+ 5 rows in set (0.03 sec) mysql> create table bbs_user1( -> id int unsigned not null auto_increment, -> username varchar(32) not null, -> password char(32) not null, -> primary key(id), -> unique bbs_user_username(username) -> )engine=myisam default charset=utf8; Query OK, 0 rows affected (0.01 sec) mysql> desc bbs_user1; +----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(32) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | +----------+------------------+------+-----+---------+----------------+ 3 rows in set (0.03 sec) mysql> create table bbs_user2( -> id int unsigned not null, -> username varchar (32)not null -> )engine=myisam default charset=utf8; Query OK, 0 rows affected (0.01 sec) mysql> desc bbs_user2; +----------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | | NULL | | | username | varchar(32) | NO | | NULL | | +----------+------------------+------+-----+---------+-------+ 2 rows in set (0.03 sec) mysql> alter table bbs_user2 add primary key(id); // alter修改表屬性 add新增 Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbs_user2; +----------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | NULL | | | username | varchar(32) | NO | | NULL | | +----------+------------------+------+-----+---------+-------+ 2 rows in set (0.03 sec) mysql> alter table bbs_user2 modify id int unsigned not null auto_increment; Query OK, 0 rows affected (0.02 sec) //modify 修改欄位屬性 Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbs_user2; +----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(32) | NO | | NULL | | +----------+------------------+------+-----+---------+----------------+ 2 rows in set (0.03 sec) mysql> alter table bbs_user2 change id uid unsigned not null auto_increment prim ary key ; 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 'unsig ned not null auto_increment primary key' at line 1 mysql> alter table bbs_user2 change id uid int unsigned not null auto_increment primary key ; //報錯 以有建立主鍵 再次建立 ERROR 1068 (42000): Multiple primary key defined //去掉primary key ; mysql> alter table bbs_user2 change id uid int unsigned not null auto_increment; Query OK, 0 rows affected (0.01 sec) //change 修改 Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bbs_user2; +----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+ | uid | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(32) | NO | | NULL | | +----------+------------------+------+-----+---------+----------------+ 2 rows in set (0.02 sec) 注意:change 可以改變欄位名稱 modify 可以修改欄位型別