mysql常用命令(2)对表操作

建表:

create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);

例子:

CREATE TABLE  my_test(

    `id` int unsigned  not null AUTO_INCREMENT PRIMARY KEY COMMENT '用户id',

    `name` varchar(20) not null COMMENT '用户姓名',

  `sex` char(3) not null default 'm' COMMENT '用户性别'

)ENGINE=INnoDB DEFAULT CHARSET=utf8;

获取表结构:

desc 表名,或者show columns from 表名;

删除表:

drop table <表名>

插入数据:

insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )];

例如:

insert into my_test (name,sex) value ("杨元甫",20);

查询表中数据:

select <字段1,字段2,...> from < 表名 > where < 表达式 >;

查询指定的行数据:

select * from my_test limit n,m; //从第 n+1 行到第 m 行

删除表中数据:

delete from 表名 where 表达式

例如:

delete from my_test where id=1;//删除id为1的数据

修改表中数据:

update 表名 set 字段=新值,… where 条件

例如:

update my_test set name="羊羊羊" where id=2; //修改id为2的 name

在表中增加字段:

alter table 表名 add 字段 类型 其他;

例如:

alter table my_test add address varchar(30) default "中国";

修改表的名字:

rename table 原表名 to 新表名;

更新字段内容:

      update 表名 set 字段名 = 新内容 where 条件;

你可能感兴趣的:(mysql常用命令(2)对表操作)