집 >데이터 베이스 >MySQL 튜토리얼 >MySQL常用操作的几种方法
你是否对MySQL常用操作的获得感到十分头疼?如果是这样子的话,以下的文章将会给你相应的解决方案,以下的文章主要是对MySQL常用操作的介绍,以下就是相关内容的具体描述。 注意:MySQL中每个命令后都要以分号;结尾。 1、显示数据库 mysql > showdatabases;
你是否对MySQL常用操作的获得感到十分头疼?如果是这样子的话,以下的文章将会给你相应的解决方案,以下的文章主要是对MySQL常用操作的介绍,以下就是相关内容的具体描述。
注意:MySQL中每个命令后都要以分号;结尾。
1、显示数据库
<ol class="dp-xml"> <li class="alt"><span><span>mysql</span><span class="tag">></span><span> show databases; </span></span></li> <li><span>+----------+ </span></li> <li class="alt"><span>| Database | </span></li> <li><span>+----------+ </span></li> <li class="alt"><span>| mysql| </span></li> <li><span>| test| </span></li> <li class="alt"><span>+----------+ </span></li> <li><span>2 rows in set (0.04 sec) </span></li> </ol>
Mysql刚安装完有两个数据库:mysql和test。mysql库非常重要,它里面有MySQL的系统信息,我们改密码和新增用户,实际上就是用这个库中的相关表进行操作。
2、MySQL常用操作;显示数据库中的表
<ol class="dp-xml"> <li class="alt"><span><span>mysql</span><span class="tag">></span><span> use mysql; (打开库,对每个库进行操作就要打开此库,类似于foxpro ) </span></span></li> <li><span>Database changed </span></li> <li class="alt"> <span>mysql</span><span class="tag">></span><span> show tables; </span> </li> <li><span>+-----------------+ </span></li> <li class="alt"><span>| Tables_in_mysql | </span></li> <li><span>+-----------------+ </span></li> <li class="alt"><span>| columns_priv| </span></li> <li><span>| db | </span></li> <li class="alt"><span>| func| </span></li> <li><span>| host| </span></li> <li class="alt"><span>| tables_priv| </span></li> <li><span>| user| </span></li> <li class="alt"><span>+-----------------+ </span></li> <li><span>6 rows in set (0.01 sec) </span></li> </ol>
3、显示数据表的结构:
<ol class="dp-xml"><li class="alt"><span><span>describe 表名; </span></span></li></ol>
4、显示表中的记录:
<ol class="dp-sql"><li class="alt"><span><span class="keyword">select</span><span> * </span><span class="keyword">from</span><span> 表名; </span></span></li></ol>
例如:显示mysql库中user表中的纪录。所有能对MySQL用户操作的用户都在此表中。
<ol class="dp-sql"><li class="alt"><span><span class="keyword">Select</span><span> * </span><span class="keyword">from</span><span> </span><span class="func">user</span><span>; </span></span></li></ol>
5、建库:
<ol class="dp-sql"><li class="alt"><span><span class="keyword">create</span><span> </span><span class="keyword">database</span><span> 库名; </span></span></li></ol>
例如:创建一个名字位aaa的库
<ol class="dp-sql"><li class="alt"><span><span>mysql> </span><span class="keyword">create</span><span> databases aaa; </span></span></li></ol>
6、建表:
<ol class="dp-sql"> <li class="alt"><span><span>use 库名; </span></span></li> <li> <span class="keyword">create</span><span> </span><span class="keyword">table</span><span> 表名 (字段设定列表); </span> </li> </ol>
例如:在刚创建的aaa库中建立表name,表中有id(序号,自动增长),xm(姓名),xb(性别),csny(出身年月)四个字段
<ol class="dp-sql"> <li class="alt"><span><span>use aaa; </span></span></li> <li> <span>mysql> </span><span class="keyword">create</span><span> </span><span class="keyword">table</span><span> </span><span class="keyword">name</span><span> (id </span><span class="keyword">int</span><span>(3) auto_increment </span><span class="op">not</span><span> </span><span class="op">null</span><span> </span><span class="keyword">primary</span><span> </span><span class="keyword">key</span><span>, xm </span><span class="keyword">char</span><span>(8),xb </span><span class="keyword">char</span><span>(2),csny </span><span class="keyword">date</span><span>); </span> </li> </ol>
可以用describe命令察看刚建立的表结构。
<ol class="dp-xml"> <li class="alt"><span><span>mysql</span><span class="tag">></span><span> describe name; </span></span></li> <li><span> </span></li> <li class="alt"><span>+-------+---------+------+-----+---------+----------------+ </span></li> <li><span>| Field | Type| Null | Key | Default | Extra | </span></li> <li class="alt"><span>+-------+---------+------+-----+---------+----------------+ </span></li> <li><span>| id| int(3) | | PRI | NULL| auto_increment | </span></li> <li class="alt"><span>| xm| char(8) | YES || NULL|| </span></li> <li><span>| xb| char(2) | YES || NULL|| </span></li> <li class="alt"><span>| csny | date| YES || NULL|| </span></li> <li><span>+-------+---------+------+-----+---------+----------------+ </span></li> </ol>
7、MySQL常用操作:增加记录
例如:增加几条相关纪录。
mysql> insert into name values('','张三','男','1971-10-01');
mysql> insert into name values('','白云','女','1972-05-20');
可用select命令来验证结果。
<ol class="dp-xml"> <li class="alt"><span><span>mysql</span><span class="tag">></span><span> select * from name; </span></span></li> <li><span>+----+------+------+------------+ </span></li> <li class="alt"><span>| id | xm | xb | csny | </span></li> <li><span>+----+------+------+------------+ </span></li> <li class="alt"><span>| 1 | 张三 | 男 | 1971-10-01 | </span></li> <li><span>| 2 | 白云 | 女 | 1972-05-20 | </span></li> <li class="alt"><span>+----+------+------+------------+ </span></li> </ol>
8、修改纪录
例如:将张三的出生年月改为1971-01-10
<ol class="dp-xml"><li class="alt"><span><span>mysql</span><span class="tag">></span><span> update name set </span><span class="attribute">csny</span><span>=</span><span class="attribute-value">'1971-01-10'</span><span> where </span><span class="attribute">xm</span><span>=</span><span class="attribute-value">'张三'</span><span>; </span></span></li></ol>
9、删除纪录
例如:删除张三的纪录。
<ol class="dp-xml"><li class="alt"><span><span>mysql</span><span class="tag">></span><span> delete from name where </span><span class="attribute">xm</span><span>=</span><span class="attribute-value">'张三'</span><span>; </span></span></li></ol>
10、删库和删表
<ol class="dp-xml"> <li class="alt"><span><span>drop database 库名; </span></span></li> <li><span>drop table 表名; </span></li> </ol>
九、增加MySQL用户
格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"
例1、增加一个用户user_1密码为123,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令:
<ol class="dp-sql"><li class="alt"><span><span>mysql> </span><span class="keyword">grant</span><span> </span><span class="keyword">select</span><span>,</span><span class="keyword">insert</span><span>,</span><span class="keyword">update</span><span>,</span><span class="keyword">delete</span><span> </span><span class="keyword">on</span><span> *.* </span><span class="keyword">to</span><span> user_1@</span><span class="string">"%"</span><span> Identified </span><span class="keyword">by</span><span> </span><span class="string">"123"</span><span>; </span></span></li></ol>
例1增加的用户是十分危险的,如果知道了user_1的密码,那么他就可以在网上的任何一台电脑上登录你的MySQL数据库并对你的数据为所欲为了,解决办法见例2。
例2、增加一个用户user_2密码为123,让此用户只可以在localhost上登录,并可以对数据库aaa进行查询、插入、修改、删除的操作(localhost指本地主机,即MySQL数据库所在的那台主机),这样用户即使用知道user_2的密码,他也无法从网上直接访问数据库,只能通过MYSQL主机来操作aaa库。
<ol class="dp-xml"><li class="alt"><span><span>mysql</span><span class="tag">></span><span>grant select,insert,update,delete on aaa.* to user_2@localhost identified by "123"; </span></span></li></ol>
用新增的用户如果登录不了MySQL,在登录时用如下命令:
<ol class="dp-xml"><li class="alt"><span><span>mysql -u user_1 -p -h 192.168.113.50 (-h后跟的是要登录主机的ip地址) </span></span></li></ol>
十、MySQL常用操作:备份与恢复
1、备份
例如:将上例创建的aaa库备份到文件back_aaa中
[root@test1 root]# cd /home/data/mysql (进入到库目录,本例库已由val/lib/mysql转到/home/data/mysql,见上述第七部分内容)
<ol class="dp-xml"><li class="alt"><span><span>[root@test1 mysql]# mysqldump -u root -p --opt aaa </span><span class="tag">></span><span> back_aaa </span></span></li></ol>
2、恢复
<ol class="dp-xml"><li class="alt"><span><span>[root@test mysql]# mysql -u root -p ccc </span><span class="tag"><span> </span><span class="tag-name">back_aaa</span><span> </span></span></span></li></ol>