MySQL | MySQL database system (2)-basic operations of SQL statements

MySQL | MySQL database system (2)-basic operations of SQL statements

Foreword
In the previous article, we are familiar with how to build and log in the MySQL database system, the article "MySQL | MySQL Database System (1)" Then, next, through this article, we will learn how to use the basic operation of the MySQL database system. It will also be used in our future work.

1. View which libraries are in the current server
show databases statement: Used to view which libraries are included in the current MySQL database system.

After initializing the MySQL database, there are four libraries by default:


1information_schema
2mysql
3performance_schema
4test
5(mysql库中包含了用户认证的相关表)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

1.1 Check which tables are in the library currently in use.
Show tables statement: used to check the tables contained in the library currently in use.

Before operation, you need to use the use statement to switch to the library you want to use.

If you want to know which tables are contained in the mysql library you are currently using, you can switch to the mysql target library and execute the statement: show tables; to view the tables in the mysql library.


mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

1.2 The structure of the query table
describe statement: used to display the structure of the table and the information of the fields that make up the table.

Need to specify "library name, table name" as parameters. When specifying the table name, first use the use statement to switch to the library to be used.

If you query the table structure, you can use the following statement to view the user table structure in the mysql library, and you can also execute the statement: describe mysql.user; view, the output structure is consistent.


mysql> describe user;
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
| Password               | char(41)                          | NO   |     |         |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N       |       |
| max_user_connections   | int(11) unsigned                  | NO   |     | 0       |       |
| plugin                 | char(64)                          | YES  |     |         |       |
| authentication_string  | text                              | YES  |     | NULL    |       |
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.00 sec)

1.3 View the current database version
select version(); statement: used to view the current mysql database version;

mysql> select version();
+------------+
| version()  |
+------------+
| 5.5.22-log |
+------------+
1 row in set (0.02 sec)

1.4 Check which library you are currently in
select database(); Statement: used to check which library you are currently in;


mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

1.5 Display
in the MySQL database in column format , and add \G after the executed SQL statement to indicate that the query results will be printed in columns.

By default, the query results of the MySQL database are output horizontally.

The first row represents the column header, and the second column represents the record set;


mysql> select * from db\G
*************************** 1. row ***************************
                 Host: %
                   Db: test
                 User: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
*************************** 2. row ***************************
                 Host: %
                   Db: test\_%
                 User: 
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: N
         Execute_priv: N
           Event_priv: Y
         Trigger_priv: Y
2 rows in set (0.00 sec)

2. Create database
create database statement: used to create a new database, you need to specify the new database name as a parameter;

auth will be used as the library name for this new library, and the database does not contain any tables.


mysql> create database auth;
Query OK, 1 row affected (0.45 sec)

A folder with the same name as the newly created library will be automatically generated in the /usr/local/mysql/data directory of the newly created library.


[root@localhost ~]# cd /usr/local/mysql/data
[root@localhost data]# ls
auth     ib_logfile0  localhost.err  mysql             mysql-bin.000002  mysql-bin.000004  mysql.error.log     test
ibdata1  ib_logfile1  localhost.pid  mysql-bin.000001  mysql-bin.000003  mysql-bin.index   performance_schema

3. Create table
create table statement: used to create a new table in the current auth library, specify the data table name as a parameter to create a new table, and define the fields used to create the new table;

Before creating a table, you need to clarify the table structure, field names, types and other information you want to create.

The basic syntax format for creating a table

create table table name (field 1 name type, field 2 name type,..., PRIMARY KEY (primary key name))

Create the users table, use the default statement in the field definition part to set the default password characters, and the primary statement to set the primary key field name;


mysql> use auth;
Database changed
mysql> create table users (user_name char(16) not null, user_passwd char(48) default '', primary key (user_name));
Query OK, 0 rows affected (0.66 sec)

4. Delete the data table and database
drop table statement: used to delete the database table, you need to specify "database name, table name" as parameters;

To specify the table name parameter, first execute the "use" statement to switch to the target library;

Perform the following operations to delete the users table in the auth library.


mysql> drop table auth.users;
Query OK, 0 rows affected (0.09 sec)

drop database statement: used to delete the specified library, you need to specify the library name as a parameter;

Perform the following operations to delete the auth library.

mysql> drop database auth;
Query OK, 0 rows affected (0.02 sec)

5. Manage the data records
in the table. In the previous steps, we introduced the creation of the library and the table. Below we will manage the data in the table based on the auth library. (Insert, query, modify and delete records in the database);

Insert data

insert into statement: used to insert new data into the target table.

Basic sentence format


insert into 表名(字段 1 ,字段 2 ,···) values(字段 1 的值,字段 2 的值,···)

The following operation will insert a record into the user table in the auth library. The user_name (user name) is:'jacktian', and the corresponding user_passwd (password) is: '666666'.

It should be noted that the value inserted in values ​​must correspond to the previously specified fields.

mysql> use auth;
Database changed
mysql> show tables;
+----------------+
| Tables_in_auth |
+----------------+
| servers        |
| users          |
+----------------+
2 rows in set (0.00 sec)
mysql> INSERT INTO users(user_name,user_passwd) VALUES('jacktian', PASSWORD('666666'));                                                                                                        
Query OK, 1 row affected (0.00 sec)

Check whether the table structure is established correctly;

mysql> describe users;
+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name   | char(16) | NO   | PRI | NULL    |       |
| user_passwd | char(48) | YES  |     |         |       |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

Insert a new data record. If this record completely includes the values ​​of all fields in the table, some fields of the specified fields in the inserted statement can be omitted.


mysql> insert into users values ('jake' , password('888888'));
Query OK, 1 row affected (0.01 sec)

View data

select statement: used to view the data records that meet the conditions in the currently specified table;

Basic sentence format


select 字段名 1,字段名 2,··· from 表名 where 条件表达式

When you want to query all fields, you can use the wildcard "*" to display all data records, and the where condition will be omitted. When you perform the following operations, you will be able to view all the data in the users table in the auth library. The user_passwd string is encrypted, so the actual password content will not be displayed.


mysql> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| jacktian  | *B2B366CA5C4697F31D4C55D61F0B17E70E5664EC |
| jake      | *DA28842831B3C40F4BC1D3C76CF9AD8CBFDAE1CB |
+-----------+-------------------------------------------+
2 rows in set (0.07 sec)

When you need to query data based on specified conditions, you can use where conditions to query. For example, you only want to query the record whose user_name (user name) in the users table in the auth database is:'jacktian', and the displayed data includes: username and password field information.

mysql> select user_name,user_passwd from auth.users where user_name='jacktian';
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| jacktian  | *B2B366CA5C4697F31D4C55D61F0B17E70E5664EC |
+-----------+-------------------------------------------+
1 row in set (0.01 sec)

change the data

update statement: used to modify and update the data in the target table.

Basic sentence format


update 表名 set 字段名 1=字段值 1[,字段名 2=字段值 2] where 条件表达式

Use the following operations to modify the user name'jake' record in the users table, set the password to a null value, and verify whether the record finds that the password of the user name jake has been changed to a null value.

mysql> update auth.users set user_passwd=password ('') where user_name = 'jake';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| jacktian  | *B2B366CA5C4697F31D4C55D61F0B17E70E5664EC |
| jake      |                                           |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

Normally, in the MySQL database, the various user information we use to access the database will be stored in the user table in the mysql library, and the data can be directly modified.

mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| user                      |
+---------------------------+
23 rows in set (0.06 sec)

At this time, we can set the password of the database user root to '666666'. When accessing the MySQL database again, we need to use "mysql -u root -p" to access the MySQL database system, and this password must be used for authentication, otherwise You will not be able to log in to the MySQL database system.

It should be noted that the database user password can be set more complicated to ensure data security;

mysql> update mysql.user set password=password ('666666') where user= 'root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
mysql> FLUSH PRIVILEGES; // 刷新用户授权信息

If you are in the Linux system command line terminal, you can also use the mysqladmin tool to set the password.


[root@localhost data]# mysqladmin -u root -p password '666666'
Enter password: 

delete data

delete statement: used to delete the specified data in the table;

Basic sentence format

delete from 表名 where 条件表达式

If you want to delete the data with the user_name user name:'jake' in the users table of the auth library, you can execute the following statement to perform the operation and verify whether the data content has found that the data of user jake has been deleted.


mysql> delete from auth.users where user_name = 'jake';
Query OK, 1 row affected (0.03 sec)

mysql> select * from auth.users;
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| jacktian  | *B2B366CA5C4697F31D4C55D61F0B17E70E5664EC |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)

In the MySQL database system, an empty user who accesses the database from this machine is added by default. The following user and password fields are all empty values. To consider the security of the database, these empty users can be deleted directly. The conditional expression after where filters the users whose user field is empty, and verifies whether the data of the empty users has been deleted.

mysql> select user,host,password from mysql.user where user = '';
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
|      | localhost |          |
+------+-----------+----------+
1 row in set (0.00 sec)

mysql> delete from mysql.user where user = '';
Query OK, 1 row affected (0.04 sec)

mysql> select user,host,password from mysql.user where user = '';
Empty set (0.00 sec)

Guess you like

Origin blog.51cto.com/15067236/2606194