mysql中show指令详细使用方法

来源:互联网 发布:中国帮非洲知乎 编辑:程序博客网 时间:2024/05/16 05:46

我经常在命令行下来使用MySQL,以下是一些MySQL命令行下的常用命令,方便有需要的同学. 
链接到192.168.0.11数据库(回车后输入密码,如果没有密码,则不需要-p参数,不过建议有密码,并且密码复杂点) 

mysql -h192.168.0.11 -uroot -p

如果是连接自己本机的话(不需要-h,默认连的就是本机)

mysql -uroot -p

显示所有数据

show databases; 

显示以te开头的数据库

show databases like 'te%';

显示包含te的数据库

show databases like '%te%';

显示以te结尾的数据库

show databases like '%te';

创建数据库testabc

create database testabc;

删除数据库testabc

drop database testabc;

创建字符集为utf-8的数据库test(建议创建utf8)

create database test collate 'utf8_general_ci';

显示test数据库的创建语句

show create database test;

使用数据库

use test;

显示所有数据库(和显示数据库一样也可以使用like)

show tables;

创建一个名为userinfo的表,包括user_id,user_name,password三个字段(简单示例)

create table userinfo (user_id int( 10 ) unsigned not null auto_increment primary key ,user_name char( 20 ) not null ,password char( 32 ) not null) engine = myisam;

删除表

drop table userinfo;

用以下三种方式来显示userinfo的表结构(推荐用第一个,简洁明了)

desc userinfo; 
describe userinfo; 
show columns from userinfo;

显示userinfo表的创建语句

show create table userinfo;

添加索引(把上面的user_name字段添加为索引)

alter table userinfo add index user_name_index (user_name);

删除索引

alter table userinfo drop index user_name_index;

添加联合索引(把user_name和password做成联合索引)

alter table userinfo add index user_union_index (user_name,password);

插入一条记录到数据库中(方法1:有字段名)

insert into userinfo (user_id, user_name, password) values (2, 'test2', 'e10adc3949ba59abbe56e057f20f883e');

插入一条记录到数据库中(方法2:没有字段名)

insert into userinfo values (1, 'test1', 'e10adc3949ba59abbe56e057f20f883e');

插入一条记录到数据库中(方法3:插入部分字段)

insert into userinfo (user_name, password) values ('test3', 'e10adc3949ba59abbe56e057f20f883e');

查询user_info里的记录(简单列几种形式)

select * from userinfo; 
select user_name,password from userinfo; 
select * from userinfo limit 1; 
select user_name from userinfo where user_id = 2 and user_name = 'test2' limit 1;

给现有表userinfo增加新字段email

alter table userinfo add column email char (20);

一次添加多个字段

alter table userinfo add column mobile char(11),add column age tinyint(3) unsigned;

把字段改名(把age改成user_age),把类型由tinyint(3)改成tinyint(2)

alter table userinfo change age user_age tinyint(2) unsigned;

把某个字段删除掉

alter table userinfo drop column user_age;

更新单个字段

update userinfo set email='single@email.com' where user_name = 'test3';

更新多个字段(比如把user_name为test2的email和mobile跟新)

update userinfo set email='email@email.com', mobile='13800000000' where user_name = 'test2';

限制只更新一条(采用limit 1,一般情况下能确定条数的话,最好limit一下,以免发生意外,更新不该更新的数据)

update userinfo set email='limit1@email.com' limit 1;

删除数据

delete from userinfo where mobile = '13800000000';

限制只删除一条(最好limit一下,以免发生意外)

delete from userinfo where mobile is null limit 1;

修改数据表名称比如把userinfo改成user

rename table userinfo to user;

退出MySQL命令行

exit

以上命令都在MySQL5.1上通过测试,请放心使用

原创粉丝点击