mysql常用命令

来源:互联网 发布:网络手游 编辑:程序博客网 时间:2024/06/07 05:33
创建/删除数据库:
create database tmp_qw DEFAULT CHARSET=gbk;
drop database tmp_qw;


授权语句:
grant select,insert,update,delete on database_name.* to 'user'@'%' identified by 'password';




创建/查看/删除表:
create table test1(a int unsigned not null auto_increment primary key, b varchar(32))ENGINE=InnoDB DEFAULT CHARSET=gbk;
show create table test1\G
drop table test1;
alter table table_name rename to test.tmp_qw_table_name;




新增/删除字段:
alter table test1 add c bigint;
alter table test1 drop c;




修改字段属性/修改字段名字:
alter table test1 change b d varchar(32);
alter table test1 modify d varchar(256);




添加/删除索引:
alter table add index idx_t_d(d(32));
alter table drop index idx_t_d;
alter table add unique key uk_t_d(d(32));




CREATE VIEW tmp_qw.v1 AS SELECT * FROM test1;
表和视图共享数据库中相同的名称空间,因此,数据库不能包含具有相同名称的表和视图。


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




写入数据:
insert into test1(a,b) values(1,'asd');
insert into test1(a,b) values(2,'zxc'),(3,'qwe'),(4,'rty');
insert into test1(b) values('asd'),('qwe'),('zxc');




更新数据:
update test1 set b='asd' where a=1;




查询数据:
select a,b from test1 where b='asd';




删除数据:
delete  from test1 where b='asd';




===================================================================================
日期函数:DATE_FORMAT(date,format) 
mysql中使用now().不要使用sysdate()
eg:
root@(none) 11:09:03>select date_format(now(),'%Y-%m-%d');
+-------------------------------+
| date_format(now(),'%Y-%m-%d') |
+-------------------------------+
| 2011-10-14                    |
+-------------------------------+
1 row in set (0.00 sec)