虽然mysql命令平常用的不少,但是有了IDE的图形化建表、改表操作,这些mysql命令忘的很快,所以在此记录一下备忘
建表:
命令:create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0',
> degree double(16,2));
插入数据:
命令:insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]
如:mysql> insert into MyClass(id, name, source) values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
查询前几行数据
例如:查看表 MyClass 中前2行数据
mysql> select * from MyClass order by id limit 0,2;
或:
mysql> select * from MyClass limit 0,2;
修改表中数据: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(字段名,'旧内容','新内容');
join链接查询
SELECT fields
FROM table1 JOIN table2
ON table1.field1=table2.field1
以上内容摘自mysql常用命令
在旧版本mysql中,对于group by允许有以下用法:
select a, b, c from table1 group by a;
但是在新版mysql中,使用以上语句会导致报错,应改为:
select a, b, c from table1 group by a, b, c;
具体的新旧版本范围不详