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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

Mobile phone film has become one of the indispensable accessories with the popularity of smartphones. To extend its service life, choose a suitable mobile phone film to protect the mobile phone screen. To help readers choose the most suitable mobile phone film for themselves, this article will introduce several key points and techniques for purchasing mobile phone film. Understand the materials and types of mobile phone films: PET film, TPU, etc. Mobile phone films are made of a variety of materials, including tempered glass. PET film is relatively soft, tempered glass film has good scratch resistance, and TPU has good shock-proof performance. It can be decided based on personal preference and needs when choosing. Consider the degree of screen protection. Different types of mobile phone films have different degrees of screen protection. PET film mainly plays an anti-scratch role, while tempered glass film has better drop resistance. You can choose to have better

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.
