增删改

删除表
命令:drop table <表名>
例如:删除表名为 MyClass 的表
mysql> drop table MyClass;

插入数据
命令:insert into <表名> [( <字段名 1>[,..<字段名 n > ])] values ( 值 1 )[, ( 值 n )]
例子:
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);

查询表中的数据
查询所有行
mysql> select * from MyClass;

查询前几行数据
例如:查看表 MyClass 中前 2 行数据
mysql> select * from MyClass order by id limit 0,2;
或者
mysql> select * from MyClass limit 0,2;

删除表中数据
命令:delete from 表名 where 表达式
例如:删除表 MyClass 中编号为 1 的记录
mysql> delete from MyClass where id=1;

修改表中数据
命令:update 表名 set 字段=新值,... where 条件
mysql> update MyClass set name='Mary' where id=1;

在表中增加字段
命令:alter table 表名 add 字段 类型 其他;
例如:在表 MyClass 中添加了一个字段 passtest,类型为 int(4),默认值为 0
mysql> alter table MyClass add passtest int(4) default '0'

更改表名
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改为 YouClass
mysql> rename table MyClass to YouClass;

更新字段内容
命令:update 表名 set 字段名 = 新内容
update 表名 set 字段名 = replace(字段名, '旧内容', '新内容');
例如:文章前面加入 4 个空格
update article set content=concat('    ', content);

 

【修改表的语法】

一张表,创建完毕,有了N列.

之后还有可能要增加或删除或修改列 

Alter table 表名 add 列名称 列类型 列参数;  [加的列在表的最后]

例: alter table m1 add birth date not null default '0000-00-00';

Alter table 表名 add 列名称 列类型 列参数 after 某列 [把新列加在某列后]

例: alter table m1 add gender char(1) not null default '' after username;

 

Alter table 表名 add 列名称 列类型 列参数 first [把新列加在最前面]

例: alter table m1 add pid int not null default 0 first; 

 

删除列:

Alter table 表名  drop 列名

 

修改列类型:

Alter table 表名 modify 列名 新类型  新参数

例:alter table m1 modify gender char(4) not null default '';

 

修改列名及列类型

Alter table 表名 change 旧列名 新列名 新类型 新参数

例:alter table m1 change id uid int unsigned;

 

posted @ 2016-09-01 18:41  熬夜与烟  阅读(314)  评论(0编辑  收藏  举报