2, the basic operation of the database

My name He Zhang, greedy lust. A qualified LINUX operation and maintenance engineers, focusing on LINUX study and research, was responsible for the website operation and maintenance of a medium-sized enterprises, loving Buddhist and running.
Personal blog: Chuan Songzhen
author micro letter: zhanghe15069028807

1, database connections

When using a local connection mysql -u root -pyou can connect to the database, but this is only a local connection, in which we work on a connection to the database on the server, rather than a direct local connection.

-P // designated port, the default is 3306
-h // Specify the host IP
-u // specify the account name
-p // specify the user's password

[root@mysql01 ~]# mysql -u root -p
Enter password: 

2, the basic operation of the database

1, check the version of the database

MariaDB [(none)]> select version();
+----------------+
| version()      |
+----------------+
| 5.5.64-MariaDB |
+----------------+
1 row in set (0.00 sec)

2, create a database DDL

MariaDB [(none)]> create database bgx_edu;
Query OK, 1 row affected (0.00 sec)

note

Strict distinction between the database name zoomed ⼩ write
database name must be unique ⼀
database name is not allowed Using the digital
database name can not be named Use the keywordcreate select

3, to see all the databases

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bgx_edu            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
//执行命令不区分大小写
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bgx_edu            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

4, delete the database and delete data inside the table

//删除bgx_edu数据库
MariaDB [(none)]> drop database bgx_edu;
//删除bgx_edu数据库里面的t1表
MariaDB [(none)]> drop database bgx_edu.t1;

5, a library look-up table

//进入某个数据库
MariaDB [(none)]> use bgx_edu;
Database changed
//列出当前库里面的表;
MariaDB [bgx_edu]> show tables;
Empty set (0.00 sec)
//查询表结构
MariaDB [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
//查看创建某张表用的语句
MariaDB [mysql]> show create table mysql.slow_log\G
*************************** 1. row ***************************
       Table: slow_log
Create Table: CREATE TABLE `slow_log` (
  `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `user_host` mediumtext NOT NULL,
  `query_time` time(6) NOT NULL,
  `lock_time` time(6) NOT NULL,
  `rows_sent` int(11) NOT NULL,
  `rows_examined` int(11) NOT NULL,
  `db` varchar(512) NOT NULL,
  `last_insert_id` int(11) NOT NULL,
  `insert_id` int(11) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `sql_text` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
1 row in set (0.00 sec)

3, database CRUD

In the MYSQLmanagement software, can SQLstatement DMLto achieve operational data (Data Manipulation Language) is mainly used in the following three instructions:

INSERT data increases
UPDATE data changes
DELETE delete data
SELECT data query

1, an operating environment data table prepared

//创建后面要用到的数据库和表
MariaDB [(none)]> create database bgx;
MariaDB [(none)]> use bgx;
MariaDB [bgx]> create table t1
    -> (id int,
    -> name varchar(10),
    -> sex enum('man','gril'),
    -> age int);
//查看表结构
MariaDB [bgx]> desc t1;
+-------+--------------------+------+-----+---------+-------+
| Field | Type               | Null | Key | Default | Extra |
+-------+--------------------+------+-----+---------+-------+
| id    | int(11)            | YES  |     | NULL    |       |
| name  | varchar(10)        | YES  |     | NULL    |       |
| sex   | enum('man','gril') | YES  |     | NULL    |       |
| age   | int(11)            | YES  |     | NULL    |       |
+-------+--------------------+------+-----+---------+-------+

2、insert

insert the complete insertion syntax 1: insert into table-name (field1, field2, field3, field4) values ​​(values1, values2, values3, values4);

MariaDB [bgx]> insert into t1 (id,name,sex,age) values ("1","zhanghe","man","22");
MariaDB [bgx]> select * from t1;
+------+---------+------+------+
| id   | name    | sex  | age  |
+------+---------+------+------+
|    1 | zhanghe | man  |   22 |
+------+---------+------+------+

Full insertion syntax insert 2 (recommended): insert table-name values ​​(values1, values2, values3, values4);

MariaDB [bgx]> insert t1 values ("2","zhangjia","gril","13");
MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    1 | zhanghe  | man  |   22 |
|    2 | zhangjia | gril |   13 |
+------+----------+------+------+

insert insertion syntax specified field: insert table-name (field1, field2) values ​​(values1, values2);

MariaDB [bgx]> insert t1 (id,name) values ("3","zhangwei");
MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    1 | zhanghe  | man  |   22 |
|    2 | zhangjia | gril |   13 |
|    3 | zhangwei | NULL | NULL |
+------+----------+------+------+

//insert插入多条数据
MariaDB [bgx]> insert t1 (id,name) values
    -> ("4","zhangsan"),
    -> ("5","lisi");
MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    1 | zhanghe  | man  |   22 |
|    2 | zhangjia | gril |   13 |
|    3 | zhangwei | NULL | NULL |
|    4 | zhangsan | NULL | NULL |
|    5 | lisi     | NULL | NULL |
+------+----------+------+------+

3、update

Changing a field of routines:
first with a DESClook at the name of the field
and then SELECTview the contents of the table
again before use UPDATEchanges, pay attention to WHEREthe spirit modify
most used FLUSHto refresh


//更改表中的某一个字段的值
MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    1 | zhanghe  | man  |   22 |
|    2 | zhangjia | gril |   13 |
|    3 | zhangwei | NULL | NULL |
|    4 | zhangsan | NULL | NULL |
|    5 | lisi     | NULL | NULL |
+------+----------+------+------+
MariaDB [bgx]> update t1 set age=22 where name="zhanghe";
MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    1 | zhanghe  | man  |   22 |
|    2 | zhangjia | gril |   13 |
|    3 | zhangwei | NULL | NULL |
|    4 | zhangsan | NULL | NULL |
|    5 | lisi     | NULL | NULL |
+------+----------+------+------+
//用update修改root的密码
///先找出要修改的什么字段
MariaDB [mysql]> desc mysql.user;
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
| Password               | char(41)                          | NO   |     |         |       |
///然后将要修改的字段列出来
MariaDB [mysql]> select host,user,password from user;
+-----------+------+----------+
| host      | user | password |
+-----------+------+----------+
| localhost | root |          |
| mysql01   | root |          |
| 127.0.0.1 | root |          |
| ::1       | root |          |
| localhost |      |          |
| mysql01   |      |          |
+-----------+------+----------+
///用update进行修改密码
MariaDB [mysql]> update mysql.user set password=password("cba-123") where host='localhost' and user='root';
MariaDB [mysql]> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *D1BE934B99C7CFFE2843BDE71DB34BBB0894B2DD |
| mysql01   | root |                                           |
| 127.0.0.1 | root |                                           |
| ::1       | root |                                           |
| localhost |      |                                           |
| mysql01   |      |                                       
///最后再刷新一下
MariaDB [(none)]> flush privileges;

4、delete

Syntax: DELETE FROM table WHERE CONITION

MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    1 | zhanghe  | man  |   22 |
|    2 | zhangjia | gril |   13 |
|    3 | zhangwei | NULL | NULL |
|    4 | zhangsan | NULL | NULL |
|    5 | lisi     | NULL | NULL |
+------+----------+------+------+
MariaDB [bgx]> delete from t1 where name="zhanghe";
MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    2 | zhangjia | gril |   13 |
|    3 | zhangwei | NULL | NULL |
|    4 | zhangsan | NULL | NULL |
|    5 | lisi     | NULL | NULL |
+------+----------+------+------+
//清空表数据
MariaDB [bgx]> select * from t1;
+------+----------+------+------+
| id   | name     | sex  | age  |
+------+----------+------+------+
|    2 | zhangjia | gril |   13 |
|    3 | zhangwei | NULL | NULL |
|    4 | zhangsan | NULL | NULL |
|    5 | lisi     | NULL | NULL |
+------+----------+------+------+
MariaDB [bgx]> truncate t1;
MariaDB [bgx]> select * from t1;
Empty set (0.00 sec)

Guess you like

Origin www.cnblogs.com/yizhangheka/p/11903579.html