常用mysql命令

来源:互联网 发布:氰化物中毒表现知乎 编辑:程序博客网 时间:2024/05/29 09:05

用户连接数据库 mysql -h主机地址 -u用户名 -p用户密码

修改用户密码 mysqladmin -u 用户名 (-p 旧密码)password 新密码

增加新用户并控制权限 grant select,insert,update,delete,create,drop,alter on 数据库.*to 用户名@登录主机 identified by "密码";

创建数据库 create database <数据库名>;

显示数据库 show databases;

删除数据库 drop database <数据库名>;

                       删除一个不确定存在的数据库 drop database if exsits <数据库名>;

使用数据库 use <数据库名>;

select命令表示当前选择(连接)的数据库 select database();

显示mysql的版本 select version();

显示当前时间 selectnow();

显示年日月 selectyear(current_date); select month(current_date); selectdayofmonth(current_date);

串接字符串 selectCONCAT(f_name, " ", l_name) as Name from employee_data where title ='Person';

创建数据表 createtable <表名> (<字段名1><类型1> [,.. <字段名n> <类型n>]); auto_increment primary key notnull default '0'

获取数据表结构 desc 表名; 或 show columns from 表名;

删除数据表 drop table<表名>;

向表中插入数据 insert into <表名> [(<字段名1> [,… <字段名n>])] values (值1)[, (值n)];

查询表中命令 select *from <表名> 查询前5行:select * from <表名> limit 0, 2;

删除表中数据 delete from 表名 where 表达式;

修改表中数据 update 表名 set 字段=新值 where 条件;

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

加索引 alter table 表名 add index 索引名(字段名1[, 字段名2 …]);

加主关键字的索引 alter table 表名 add primary key (字段名);

删除某个索引 alter table 表名 drop index 索引名;

修改原字段名称及类型 alter table table_nmae change old_field_name new_filed_nmae field_type;

删除字段 alter table table_nmae drop field_name;

修改表名 rename table 原表名 to 新表名;

导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名

导出一个表 mysqldump-u 用户名 -p 数据库名 表名 > 导出的文件名

0 0