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 可以修改字段类型
修改表结构创建索引
猜你喜欢
转载自blog.csdn.net/feiyucity/article/details/84641687
今日推荐
周排行