Home Database Mysql Tutorial MySQL易学易用之MYSQL不为人知的特性_MySQL

MySQL易学易用之MYSQL不为人知的特性_MySQL

Jun 01, 2016 pm 01:20 PM
program

bitsCN.com 本文将为你介绍这些不为人知的特性。

以XML格式查看查询结果

通过使用传统―xml 选项调用MySQL命令行客户程序,你可以以XML格式(而不是传统的列表形式)来查看MySQL查询结果。如果你打算将查询输出与其它程序集成在一起,这一技巧非常有用,这里是一个例子:

表A

shell> mysql --xml

mysql> SELECT * FROM test.stories;


1
This is a test
2005-07-28 00:14:57

2
This is the second test
2005-07-28 00:15:11

2 rows in set (0.11 sec)

快速重建索引

通常情况下,如果你想改变服务器的全文搜索变量,你需要在表格中重新建立全文索引,以确保你的更新得到映射。这一操作将会花费大量的时间,特别是如果你需要处理很多数据的时候。一种快速的解决方法是使用REPAIR TABLE命令,以下为演示过程:

表B

mysql> REPAIR TABLE content QUICK;
+-----------+--------+----------+----------+
| Table| Op| Msg_type | Msg_text |
+-----------+--------+----------+----------+
| content| repair | status| OK|
+-----------+--------+----------+----------+
1 row in set (0.05 sec)

压缩一定的表格类型

如果你处理的是只读MyISAM表格,MySQL允许你将其压缩以节省磁盘空间。对此可以使用包括myisampack,如下所示:

表C

shell> myisampackmovies.MYI
Compressing movies.MYD: (146 records)
- Calculating statistics
- Compressing file
41.05%

使用传统SQL

MySQL支持SQL查询中的传统用法,支持IF与CASE结构。以下是一个简单的例子:

表D

mysql> SELECT IF (priv=1, 'admin', 'guest') As usertype FROM privs WHERE username = 'joe';
+----------+
| usertype |
+----------+
| admin|
+----------+
1 row in set (0.00 sec)

以CSV格式输出表格数据

MySQL输出文件包含一个全部SQL命令列表。如果你想将输出文件导入到MySQL,这一功能非常实用,但如果目标程序(比如Excel)不能与SQL相互通讯,这一方法将行不通。在这种情况下,可以通过告诉MySQL以CSV格式建立输出文件,这种CSV格式很方便地导入到绝大部分的程序。这里演示了mysqldump的操作过程:

shell> mysqldump -T . --fields-terminated-by=", " mydbmytable

这将在当前目录中生成一个文本文件,包含来自mydb.mytable列表中以逗号为间隔符的记录。

以激活strict模式减少“bad”数据的出现

MySQL服务器能够以多种不同的模式运行,而每一种都针对于特定的目的而优化。在默认情况下,没有设置模式。然而,通过在服务器命令行中添加以下选项可以很容易地改变模式的设置并将MySQL以“strict”模式运行:

shell> mysqld --sql_mode="STRICT_ALL_TABLES" &

在“strict”模式下,通过MySQL的中止查询执行并返回一个错误,服务器的很多自动修正功能都被无效化。同样,该模式下也将会执行更为严格的时间检查。

监视服务器

你可以通过运行SHOW STATUS命令获得一份服务器运行与统计的报告,包括打开连接的次数,激活查询次数,服务器正常运行时间等等。例如:

表 E

mysql> SHOW STATUS;
+------------------+-------+
| Variable_name| Value |
+------------------+-------+
| Aborted_clients| 0|
| Aborted_connects | 0|
...
| Uptime| 851|
+------------------+-------+
156 rows in set (0.16 sec)

自动返回CREATE TABLE代码

MySQL允许你自动获得SQL命令重新建立一个特定的表格。只简单地运行SHOW CREATE TABLE命令,并查看表格建立代码,如下所示:

表 F

mysql> SHOW CREATE TABLE products;
-----------------------------------------------------
| Table| Create Table
+----------+-----------------------------------------
| products | CREATE TABLE `products` (
`id` int(8) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`price` int(10) default NULL,
PRIMARY KEY(`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+----------+-----------------------------------------
1 row in set (0.27 sec)

建立一个更为有用的命令提示:

在缺省情况下,MySQL命令行客户程序显示一个简单的mysql>提示符。然而,你可以使用特定的修改内容来改变这一提示符使之变得更为有效,这些内容包括:当前用户名称,主机名称,以及当前选择的数据库。如下所示:

表 G

mysql> prompt /U://d>
PROMPT set to '/U://d>'
root@localhost:/db1>

从这里的文档文件你可以获得支持MySQL客户程序更改的一个完整列表。bitsCN.com

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)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

Fix: Operator denied request error in Windows Task Scheduler Fix: Operator denied request error in Windows Task Scheduler Aug 01, 2023 pm 08:43 PM

To automate tasks and manage multiple systems, mission planning software is a valuable tool in your arsenal, especially as a system administrator. Windows Task Scheduler does the job perfectly, but lately many people have reported operator rejected request errors. This problem exists in all iterations of the operating system, and even though it has been widely reported and covered, there is no effective solution. Keep reading to find out what might actually work for other people! What is the request in Task Scheduler 0x800710e0 that was denied by the operator or administrator? Task Scheduler allows automating various tasks and applications without user input. You can use it to schedule and organize specific applications, configure automatic notifications, help deliver messages, and more. it

How to sort photos by face on Windows 10 and 11 How to sort photos by face on Windows 10 and 11 Aug 08, 2023 pm 10:41 PM

The operation of Windows is getting better and better with every version, with attractive features to improve the user experience. One feature users will want to explore on Windows 10 and 11 is the ability to sort photos by faces. This feature allows you to group photos of friends and family using facial recognition. Sounds fun, right? Read on to learn how to take advantage of this feature. Can I group photos by faces on Windows? Yes, you can use the Photos app to group pictures by faces on Windows 10 and 11. However, this feature is not available on the Photos app version. Additionally, you can link these photos to contacts using the People tab. Therefore, using this function you can

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

How to write a simple countdown program in C++? How to write a simple countdown program in C++? Nov 03, 2023 pm 01:39 PM

C++ is a widely used programming language that is very convenient and practical in writing countdown programs. Countdown program is a common application that can provide us with very precise time calculation and countdown functions. This article will introduce how to use C++ to write a simple countdown program. The key to implementing a countdown program is to use a timer to calculate the passage of time. In C++, we can use the functions in the time.h header file to implement the timer function. The following is the code for a simple countdown program

How to open a website using Task Scheduler How to open a website using Task Scheduler Oct 02, 2023 pm 11:13 PM

Do you frequently visit the same website at about the same time every day? This can lead to spending a lot of time with multiple browser tabs open and cluttering the browser while performing daily tasks. Well, how about opening it without having to launch the browser manually? It's very simple and doesn't require you to download any third-party apps, as shown below. How do I set up Task Scheduler to open a website? Press the key, type Task Scheduler in the search box, and then click Open. Windows On the right sidebar, click on the Create Basic Task option. In the Name field, enter the name of the website you want to open and click Next. Next, under Triggers, click Time Frequency and click Next. Select how long you want the event to repeat and click Next. Select enable

How to Automatically Toggle iPhone Orientation Lock for Specific Apps How to Automatically Toggle iPhone Orientation Lock for Specific Apps Jun 06, 2023 am 08:22 AM

In iOS, many apps display different views when you rotate your iPhone from portrait to landscape. Depending on the app and how it's used, this behavior isn't always desirable, which is why Apple includes an orientation lock option in Control Center. However, some apps work more usefully with orientation lock disabled—think YouTube or the Photos app, where rotating the device to landscape provides a better full-screen viewing experience. If you prefer to stay locked down, you'll have to disable it in Control Center to get a full-screen experience every time you open these types of apps. Then when you close the app, you have to remember to turn orientation lock back on, which isn't ideal. Fortunately, you can create

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

See all articles