MySQL 学习日记 2011-8-29
2011-08-29 16:06
309 查看
前言
NoSQL都出来很久了。。。我最近刚开始重温重学MySQL的确被某同学鄙视了一下= =
=============================
今天又在折腾那user 数据表。。。
最终成果为以下....// 这是所谓的倒叙写法吗? 看来我小学语文没忘记,可喜可贺的~
虽然这个表有点挫,还有sex明显用gender似乎比较好。。//也许自己懒得改?
那就一切从后往前写吧。。最后一步是把ID附上主键、自增属性的
之前我试过。。。
mysql> alter table user add IDD int identity(1,1);
实在是蛋疼。。。
直到我无意瞄到一个关键字。。。这个关键字就是 key。。。
于是就出现我上文使用的最后一条代码。。。
因为我翻w3school找如何(往已有字段)增加AUTO_INCREMENT时一直没有找到。。。于是心情烦躁,就不找Key想碰碰运气随便写写。。就成功了。。。
而这个增加,自增属性的语句。。在W3SCHOOL上解决不能厚,查mysql手册时没查到= =(不过事后查到了=- =)
于是投奔googole。。。然后搜到
http://www.cnblogs.com/me-sa/articles/532528.html
里的
自增列不能直接修改,必须将原有ID列删除,然后重新添加一列具有identity属性的ID字段,语句如下
才弄得差不多的。。。
---------------
总之就是,要添加,自增,属性,就要先删才能加。。所谓的从无到有咯~?
---------------
以下,附图一张。。mysql手册里的介绍...
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手册里的介绍...
相关文章推荐
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记10)
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记16)
- 【菜鸟数据库学习日记】从头开始学MySQL(1)
- 跟我一起学习MySQL技术内幕(第五版):(第二章学习日记9)
- Django学习日记 第二天 mysql配置 和 模板的使用
- MySql学习 日记
- 跟我一起学习MySQL技术内幕(第五版):(第一章学习日记2)
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记3)
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记8)
- 【菜鸟数据库学习日记】从头开始学MySQL(2)
- 跟我一起学习MySQL技术内幕(第五版):(第二章学习日记11)
- mysql+myeclipse 小练习 学习日记
- php学习日记第四棒(MySQL)
- 跟我一起学习MySQL技术内幕(第五版):(第二章学习日记6)
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记14)
- 跟我一起学习MySQL技术内幕(第五版):(第二章学习日记3)
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记17)
- Java学习日记3-MySql中的模糊查询
- 跟我一起学习MySQL技术内幕(第五版):(第一章学习日记7下)
- 【菜鸟数据库学习日记】从头开始学MySQL(4)