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

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


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

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools