[] Using MySQL MySQL (connection, select the database, the database and display information table)

Chapter 3 Using MySQL

Simple record - MySQL must know will be - [English] Ben Forta

Learn how to access and log on to MySQL, how to execute MySQL statements, and how to obtain information databases and tables.

connection

Connect to MySQL, you need the following information:

  • Hostname (computer name) - If you connect to local MySQL server is localhost;

  • A valid user name; such as root;

  • (Port if other than the default port 3306);

  • User Password (password your user name corresponding to the set).

After connection, you can access the login name to access any of the databases and tables

Login: mysql -h hostname -P [port number] -u user name -p password

Exit: exit or ctrl + C

mysql [-h hostname] -P port number -u user name -p password
example: mysql -h localhost -P3306 -u root -p123456 there are no spaces in front of three will do, -p must be no spaces
-P port is port No mean, -h host is the host means, localhost means the local host.

If you are logged on locally and using the default port 3306, you can log in so

mysql -u root -p123456

C:\Users\x1c>mysql -h localhost -P 3306 -u root -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

C:\Users\x1c>mysql -h localhost -P 3306 -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

C:\Users\x1c>mysql  -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Here Insert Picture Description

Select Database

After connecting to MySQL, select a database operation, the database may be used to select USE keyword.

Terms: Keyword (key word) composed of a language as MySQL reserved word part, we can not name a table or column by keyword.

For example, to use the mysqlcrashcoursedatabase, you should enter the following USE mysqlcrashcourse:

mysql> USE mysqlcrashcourse;
Database changed
mysql>

Analysis: USE mysqlcrashcourseSelect the mysqlcrashcoursedatabase, Database changed the message on behalf of a selected database mysql command-line program mysqlcrashcoursesuccess

Note: We have to use USE open the corresponding database in order to read the data in the database.

Learn databases and tables

If you do not know how to do database name can be used?

How can display a list of available databases do?

Information database, table, column, users, rights, and the like are stored in the database tables (MySQL using MySQL to store this information) is stored. However, the internal tables generally do not directly access. MySQL SHOW commands available to display this information (MySQL extract this information from the internal table). Consider the following example:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mmall_learning     |
| mysql              |
| mysqlcrashcourse   |
| performance_schema |
| spring             |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

SHOW DATABASES; return a list of available databases. Included in this list may be a database (e.g., in the example mysql information_schema and the like) used inside the MySQL. Of course, there are many databases that you create (such as mmall_learning, mysqlcrashcourse ,, spring is that I created before.).

In order to obtain a list of tables in the database, using the SHOW TABLEScommand; as follows:

mysql> SHOW TABLES;
+----------------------------+
| Tables_in_mysqlcrashcourse |
+----------------------------+
| customers                  |
| orderitems                 |
| orders                     |
| productnotes               |
| products                   |
| vendors                    |
+----------------------------+
6 rows in set (0.01 sec)

mysql>

SHOW TABLES; returns a list of the currently selected database of available tables. SHOW also be used to display a table column, display customersa list of all of:

mysql> SHOW COLUMNS FROM customers;
+--------------+-----------+------+-----+---------+----------------+
| Field        | Type      | Null | Key | Default | Extra          |
+--------------+-----------+------+-----+---------+----------------+
| cust_id      | int(11)   | NO   | PRI | NULL    | auto_increment |
| cust_name    | char(50)  | NO   |     | NULL    |                |
| cust_address | char(50)  | YES  |     | NULL    |                |
| cust_city    | char(50)  | YES  |     | NULL    |                |
| cust_state   | char(5)   | YES  |     | NULL    |                |
| cust_zip     | char(10)  | YES  |     | NULL    |                |
| cust_country | char(50)  | YES  |     | NULL    |                |
| cust_contact | char(50)  | YES  |     | NULL    |                |
| cust_email   | char(255) | YES  |     | NULL    |                |
+--------------+-----------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

mysql>

Here Insert Picture Description

analysis:

SHOW COLUMNS required to give a table name (FROM customers in this example), it returns a row for each field, the row contains the field name, data type, whether to allow NULL, the key information, default values, and other information (e.g., field cust_id the auto_increment).

Description: What is the automatic increment? Some table columns require unique values. For example, customer ID (in the example shown above). When you add each row into the table, MySQL automatically for the next available number assigned to each row, do not assign a unique value when a row is added manually (to do this you must remember the values ​​last used). This feature is called auto-increment. If you need it, you must create a table it as part of the table definition.

Tip: DESCRIBE DESCRIBE statement with MySQL support as a shortcut to the SHOW COLUMNS FROM. In other words, DESCRIBE customers; is SHOW COLUMNSFROM customers; a shortcut.

Here Insert Picture Description

SHOW supported by other statements are:

  • SHOW STATUS, for displaying a wide range of server status information;

  • SHOW CREATE DATABASE and SHOW CREATE TABLE, it is used to display a specific statement to create a MySQL database or table;

  • SHOW GRANTS, used to display security permissions granted to users (all users or specific users);

    mysql> show grants;
    +---------------------------------------------------------------------+
    | Grants for root@localhost                                           |
    +---------------------------------------------------------------------+
    | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
    | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
    +---------------------------------------------------------------------+
    2 rows in set (0.00 sec)
    
  • SHOW ERRORS and SHOW WARNINGS, to display the server error or warning message.

prompt:

Learn more about SHOW please mysql command-line utility, execute the command HELP SHOW; display allows the SHOW statement.

mysql> HELP SHOW;
Name: 'SHOW'
Description:
SHOW has many forms that provide information about databases, tables,
columns, or status information about the server. This section describes
those following:

SHOW {BINARY | MASTER} LOGS
SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
SHOW CHARACTER SET [like_or_where]
SHOW COLLATION [like_or_where]
SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [like_or_where]
SHOW CREATE DATABASE db_name
SHOW CREATE EVENT event_name
SHOW CREATE FUNCTION func_name
SHOW CREATE PROCEDURE proc_name
SHOW CREATE TABLE tbl_name
SHOW CREATE TRIGGER trigger_name
SHOW CREATE VIEW view_name
SHOW DATABASES [like_or_where]
SHOW ENGINE engine_name {STATUS | MUTEX}
SHOW [STORAGE] ENGINES
SHOW ERRORS [LIMIT [offset,] row_count]
SHOW EVENTS
SHOW FUNCTION CODE func_name
SHOW FUNCTION STATUS [like_or_where]
SHOW GRANTS FOR user
SHOW INDEX FROM tbl_name [FROM db_name]
SHOW MASTER STATUS
SHOW OPEN TABLES [FROM db_name] [like_or_where]
SHOW PLUGINS
SHOW PROCEDURE CODE proc_name
SHOW PROCEDURE STATUS [like_or_where]
SHOW PRIVILEGES
SHOW [FULL] PROCESSLIST
SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n]
SHOW PROFILES
SHOW RELAYLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
SHOW SLAVE HOSTS
SHOW SLAVE STATUS [FOR CHANNEL channel]
SHOW [GLOBAL | SESSION] STATUS [like_or_where]
SHOW TABLE STATUS [FROM db_name] [like_or_where]
SHOW [FULL] TABLES [FROM db_name] [like_or_where]
SHOW TRIGGERS [FROM db_name] [like_or_where]
SHOW [GLOBAL | SESSION] VARIABLES [like_or_where]
SHOW WARNINGS [LIMIT [offset,] row_count]

like_or_where:
    LIKE 'pattern'
  | WHERE expr

If the syntax for a given SHOW statement includes a LIKE 'pattern'
part, 'pattern' is a string that can contain the SQL % and _ wildcard
characters. The pattern is useful for restricting statement output to
matching values.

Several SHOW statements also accept a WHERE clause that provides more
flexibility in specifying which rows to display. See
https://dev.mysql.com/doc/refman/5.7/en/extended-show.html.

URL: https://dev.mysql.com/doc/refman/5.7/en/show.html


mysql>

summary

A brief introduction of how to connect and log in MySQL, how to select the database with the USE, how to view the MySQL database with SHOW, tables and internal information.

mysql -h localhost -P 3306 -u root -p123456;
use mysqlcrashcourse;
show databases;
show tables;
show columns from customers;
help show;
show status;
show grants;
show grants;
show errors;
show warnings;
Published 85 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41569732/article/details/104455092