10分钟学会MySQL基础教程

10分钟学会MySQL基础操作 1分钟安装

Part1:写在最前

MySQL安装的方式有三种:

rpm包安装

二进制包安装

源码安装

这里我们推荐二进制包安装,无论从安装速度还是用于生产库安装环境来说,都是没问题的。现在生产库一般采用MySQL5.6,测试库采用MySQL5.7。

MySQL5.6安装看这里

MySQL5.7安装看这里 

8分钟数据库操作

Part1:登录

MySQL的登录方式为:

-u为用户名,-p为密码,如果您用了上述本文的安装脚本,默认密码为MANAGER

[root@HE3 ~]# mysql -uroot -pMANAGER

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 11

Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

Part2:表基础操作

查看数据库中有哪些库

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| he1                |

| he3                |

| maxscale           |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

7 rows in set (0.00 sec)


增删改查

同Excel一样,数据库中也是要增删改查的主要涉及的语法如下:

查:

首先选择相应的库

mysql> use maxscale

Database changed

select * from 表名;是查询这张表所有内容的意思;

select 列名,列名 from 表名;是查询这张表想看的列内容;

mysql> select a,b from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 写入   | b    |

| 测试   | c    |

| 于浩   | d    |

| 贺磊   | e    |

+--------+------+

6 rows in set (0.00 sec)

增:

insert into 表名 values('想插入的内容'); 往表中插入一条记录的意思;

mysql> insert into helei values('插入','f');

Query OK, 1 row affected (0.01 sec)

mysql> select a,b from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 写入   | b    |

| 测试   | c    |

| 于浩   | d    |

| 贺磊   | e    |

| 插入   | f    |

+--------+------+

6 rows in set (0.00 sec)

我这里表名叫helei;

删:

delete from helei where b='f';删除helei表中b列是f的所有记录;

mysql> delete from helei where b='f';

Query OK, 1 row affected (0.01 sec)

mysql> select * from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 写入   | b    |

| 测试   | c    |

| 于浩   | d    |

| 贺磊   | e    |

+--------+------+

5 rows in set (0.00 sec)

可以看到这里b列为f的整个一行就被删除掉了。

改:

update 表名 set 列名='改成所需内容' where 限定条件。

mysql> update helei set b='改' where a='贺磊';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 写入   | b    |

| 测试   | c    |

| 于浩   | d    |

| 贺磊   | 改   |

+--------+------+

5 rows in set (0.00 sec)

表级操作

创建表

创建表t,这里用生产库的来做例子,id列自增主键,log为varchar类型,可以存30个字符;

mysql> CREATE TABLE `t` (

-> `id`  int UNSIGNED NOT NULL AUTO_INCREMENT ,

-> `log`  varchar(30) NOT NULL DEFAULT '' ,

-> PRIMARY KEY (`id`)

-> )

-> ;

Query OK, 0 rows affected (0.01 sec)

删除表

删除表t,整表删除;

mysql> drop table t;

Query OK, 0 rows affected (0.02 sec)

Part3:库基础操作

创建库

mysql> CREATE DATABASE helei DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| he1                |

| he3                |

| helei              |

| maxscale           |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

8 rows in set (0.00 sec)

删除库

删除名为helei的库,注意,这一操作会删除掉helei库中所有的表;

mysql> drop database helei;

Query OK, 0 rows affected (0.00 sec)

1分钟系统级操作

Part1:启停数据库

[root@HE3 ~]# /etc/init.d/mysqld status

SUCCESS! MySQL running (3173)

[root@HE3 ~]# /etc/init.d/mysqld stop

Shutting down MySQL.... SUCCESS! 

[root@HE3 ~]# /etc/init.d/mysqld start

Starting MySQL.. SUCCESS!

附录

Part1:常用SQL

创建和授权用户

CREATE USER 'helei'@'%' IDENTIFIED BY 'MANAGER';

GRANT SELECT,insert,update,delete ON *.* TO 'helei'@'%';

创建数据库:

CREATE DATABASE www CHARACTER SET utf8 COLLATE utf8_bin;

密码变更:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a434eff84b0d390c7b489a5dfb4bdeb7.html