您的位置:首页 > 数据库 > MySQL

MySQL 学习日记 2011-8-29

2011-08-29 16:06 309 查看
前言

NoSQL都出来很久了。。。我最近刚开始重温重学MySQL的确被某同学鄙视了一下= =

=============================

今天又在折腾那user 数据表。。。

最终成果为以下....// 这是所谓的倒叙写法吗? 看来我小学语文没忘记,可喜可贺的~

mysql> desc user;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| name       | varchar(20) | YES  | UNI | NULL    |                |
| password   | varchar(20) | YES  |     | NULL    |                |
| sex        | varchar(2)  | YES  |     | NULL    |                |
| email      | varchar(20) | YES  | UNI | NULL    |                |
| reg_time   | date        | YES  |     | NULL    |                |
| permission | int(3)      | NO   |     | NULL    |                |
| ID         | int(3)      | NO   | PRI | NULL    | auto_increment |
+------------+-------------+------+-----+---------+----------------+


虽然这个表有点挫,还有sex明显用gender似乎比较好。。//也许自己懒得改?

那就一切从后往前写吧。。最后一步是把ID附上主键、自增属性的

mysql> alter table user add ID int(3) key  auto_increment;
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0


之前我试过。。。

mysql> alter table user add IDD int identity(1,1);

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 'ident
ity(1,1)' at line 1
mysql> alter table user add ID int identity(1,1);
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 'ident
ity(1,1)' at line 1
mysql> alter table user add ID int(3) identity(1,1);
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 'ident
ity(1,1)' at line 1
mysql> alter table user add ID int(3) unique not null auto-increment;
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 'auto-
increment' at line 1
mysql> alter table user add ID int(3)  not null auto-increment;
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 'auto-
increment' at line 1
mysql> alter table user add ID int(3)  not null auto_increment;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto colum
n and it must be defined as a key
mysql> alter table user add ID int(3)  auto_increment;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto colum
n and it must be defined as a key


实在是蛋疼。。。

直到我无意瞄到一个关键字。。。这个关键字就是 key。。。

ERROR 1075 (42000): Incorrect table definition; there can be only one auto colum
n and it must be defined as a key


于是就出现我上文使用的最后一条代码。。。

mysql> alter table user add ID int(3) key  auto_increment;


因为我翻w3school找如何(往已有字段)增加AUTO_INCREMENT时一直没有找到。。。于是心情烦躁,就不找Key想碰碰运气随便写写。。就成功了。。。

而这个增加,自增属性的语句。。在W3SCHOOL上解决不能厚,查mysql手册时没查到= =(不过事后查到了=- =)

于是投奔googole。。。然后搜到

http://www.cnblogs.com/me-sa/articles/532528.html

里的

自增列不能直接修改,必须将原有ID列删除,然后重新添加一列具有identity属性的ID字段,语句如下

alter table 表名
drop column ID

alter table 表名
add ID int identity(1,1)


才弄得差不多的。。。

---------------

总之就是,要添加,自增,属性,就要先删才能加。。所谓的从无到有咯~?

---------------

以下,附图一张。。mysql手册里的介绍...

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: