Home Database Mysql Tutorial MySQL常用操作的几种方法

MySQL常用操作的几种方法

Jun 07, 2016 pm 04:12 PM
mysql several kinds Commonly used operate method

你是否对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>
Copy after login

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>
Copy after login

3、显示数据表的结构:

<ol class="dp-xml"><li class="alt"><span><span>describe 表名;  </span></span></li></ol>
Copy after login

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>
Copy after login

例如:显示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>
Copy after login

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>
Copy after login

例如:创建一个名字位aaa的库

<ol class="dp-sql"><li class="alt"><span><span>mysql> </span><span class="keyword">create</span><span> databases aaa; </span></span></li></ol>
Copy after login

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>
Copy after login

例如:在刚创建的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>
Copy after login

可以用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> 
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

10、删库和删表

<ol class="dp-xml">
<li class="alt"><span><span>drop database 库名;   </span></span></li>
<li><span>drop table 表名;  </span></li>
</ol>
Copy after login

九、增加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>
Copy after login

例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>
Copy after login

用新增的用户如果登录不了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>
Copy after login

十、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>
Copy after login

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>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

See all articles