MySQL安装与使用

MySQL是什么?

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS(Relational Database Management System,关系数据库管理系统) 应用软件之一。

MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。

centos 7

安装MySQL

[root@ chenc01 ~]# yum -y install mariadb mariadb-server
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                   | 3.6 kB     00:00     
epel                                                   | 5.3 kB     00:00     
extras                                                 | 2.9 kB     00:00     
updates                                                | 2.9 kB     00:00 
# 启动MySQL
[root@ chenc01 ~]# systemctl start mariadb

1、连接数据库

[root@ chenc01 ~]# mysql -uroot -p
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

2、显示数据库

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
# MySQL刚安装完有两个数据库:mysql和test。mysql库非常重要,它里面有MySQL的系统信息,我们改密码和新增用户,实际上就是用这个库中的相关表进行操作。

3、创建数据库

MariaDB [(none)]> create database chenc charset=utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chenc              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

4、删除数据库

MariaDB [(none)]> drop database chenc;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

5、显示某个数据库中的表

MariaDB [(none)]> use chenc;
Database changed
MariaDB [chenc]> show tables;
Empty set (0.00 sec)

6、建表

use 库名;

create table 表名 (字段设定列表);

例如:在刚创建的aaa库中建立表name,表中有id(序号,自动增长),xm(姓名),xb(性别),csny(出身年月)四个字段

MariaDB [(none)]> use chenc;
Database changed
MariaDB [chenc]> create table cc (
    -> id int(3) auto_increment not null primary key,
    -> xm char(8),
    -> xb char(2),
    -> csny date);
Query OK, 0 rows affected (0.10 sec)
# 可以用describe命令察看刚建立的表结构
MariaDB [chenc]> describe cc;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(3)  | NO   | PRI | NULL    | auto_increment |
| xm    | char(8) | YES  |     | NULL    |                |
| xb    | char(2) | YES  |     | NULL    |                |
| csny  | date    | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

7、显示数据表的结构

desc 表名;

MariaDB [chenc]> desc cc;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(3)  | NO   | PRI | NULL    | auto_increment |
| xm    | char(8) | YES  |     | NULL    |                |
| xb    | char(2) | YES  |     | NULL    |                |
| csny  | date    | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

8、显示表中的记录

select * from 表名;

​ 例如:显示mysql库中user表中的纪录。所有能对MySQL用户操作的用户都在此表中。

​ select * from user;

MariaDB [chenc]> select * from cc;
Empty set (0.00 sec)

9、增加记录

例如:增加几条相关纪录。

MariaDB [chenc]> insert into cc values(" ","张三","男","1971-10-01");
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [chenc]> insert into cc values(" ","李四","男","1972-05-20");
Query OK, 1 row affected, 1 warning (0.00 sec)

# 可用select命令来验证结果
MariaDB [chenc]> select * from cc;
+----+--------+------+------------+
| id | xm     | xb   | csny       |
+----+--------+------+------------+
|  1 | 张三   || 1971-10-01 |
|  2 | 李四   || 1972-05-20 |
+----+--------+------+------------+
2 rows in set (0.00 sec)

10、修改记录

例如:将张三的姓名修改成白云

MariaDB [chenc]> update cc set csny="1971-01-10" where xm="白云";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

MariaDB [chenc]> select * from cc;
+----+--------+------+------------+
| id | xm     | xb   | csny       |
+----+--------+------+------------+
|  1 | 张三   || 1971-10-01 |
|  2 | 李四   || 1972-05-20 |
+----+--------+------+------------+
2 rows in set (0.00 sec)

11、删除记录

例如:删除张三的纪录。

MariaDB [chenc]> delete from cc where xm="张三";
Query OK, 1 row affected (0.00 sec)

MariaDB [chenc]> select * from cc;
+----+--------+------+------------+
| id | xm     | xb   | csny       |
+----+--------+------+------------+
|  2 | 李四   || 1972-05-20 |
+----+--------+------+------------+
1 row in set (0.00 sec)

12、删库和删表

drop database 库名;

drop table 表名;

MariaDB [chenc]> drop table cc;
Query OK, 0 rows affected (0.00 sec)

MariaDB [chenc]> drop database chenc;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

13、备份

例如:将上例创建的chenc库备份到文件back_aaa中

[root@ chenc01 ~]# mysqldump -u root -p chenc > back_aaa
Enter password: 
[root@ chenc01 ~]# ls
anaconda-ks.cfg  back_aaa

14、恢复

[root@ chenc01 ~]# mysql -u root -p chenc < back_aaa
Enter password: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值