DB的全称是database,即数据库的意思。数据库实际上就是一个文件集合,是一个存储数据的仓库,数据库是按照特定的格式把数据存储起来,用户可以对存储的数据进行增删改查操作。
SQL包含三个部分:
DDL 数据定义语言包含定义数据库及其对象的语句,例如表,视图,触发器,存储过程等。
DML 数据操作语言包含允许更新和查询数据的语句。(在这里查询语言可分为DQL)
DCL 数据控制语言允许授予用户权限访问数据库中特定数据的权限。
MySQL是一个关系型数据库管理系统 ,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
非关系型数据库主要是基于“非关系模型”的数据库(由于关系型太大,所以一般用“非关系型”来表示其他类型的数据库)
非关系型模型比如有:
存储的数据是一列列的。关系型数据库以一行作为一个记录,列模型数据库以一列为一个记录。(这种模型,IO很快,主要是一些分布式数据库)
https://www.mysql.com //mysql官网
yum安装
[root@mysql ~]# yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
[root@mysql ~]# userdel -r mysql
[root@mysql ~]# rm -rf /etc/my*
[root@mysql ~]# rm -rf /var/lib/mysql
[root@mysql ~]# yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
我们要装MySQL5.7的源,不装8.0的源,因此得修改配置文件
一种方法是直接修改配置文件
[root@new ~]# vim /etc/yum.repos.d/mysql-community.repo
第二种方法:
#yum-config-manager --disable mysql80-community 关闭80
# yum-config-manager --enable mysql57-community 打开57
这俩种方法都是永久关闭,还有一种临时关闭
yum install -y mysql-community-server --enablerepo mysql57-community --disablerepo mysql80-community
推荐前俩种永久关闭!
# yum -y install mysql-community-server
# systemctl start mysqld 启动服务
yum安装的MySQL,密码会在日志文件里
# grep "password" /var/log/mysqld.log
2018-12-26T22:41:24.218413Z 1 [Note] A temporary password is generated for root@localhost: %i+g10uS.dre
第一次登录MySQL,输入日志文件里面的密码
# mysql -uroot -p'%i+g10uS.dre'
第一次登录MySQL,什么都干不了!
mysql> alter user 'root'@'localhost' identified by "new-password"
mysqladmin -uroot -p'旧密码' password "新密码"
特别注意:-p与旧密码之间没有空格,新密码与password之间有空格
但是修改的时候有密码强度策略,得有大小写 数字 特殊符号
#源码安装对密码强度没有要求!
想要设置简单的MySQL密码,不允许怎么办?
# 关闭mysql密码强度策略,生产环境切勿尝试,首次启动不可关闭
# vim /etc/my.cnf
写入
validate-password=OFF
修改完重启服务mysqld
# 跳过密码进入数据库,用于忘记密码时使用
跳过授权表
# vim /etc/my.cnf
写入
skip-grant-tables
以上俩个参数不可同时出现
validate-password=OFF,关闭密码强度策略
skip-grant-tables,跳过授权表
修改完配置文件重启服务,然后登录mysql
mysql> select * from mysql.user\G;
查看 表
mysql> update mysql.user set authentication_string=password("QianFeng012345") where User='root' and Host="localhost";
mysql> flush privileges; 刷新
[root@new ~]# mysql -uroot -p"zyq123"
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 8
Server version: 5.7.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
也可以让密码以密文的形式输入,增加安全性
[root@new ~]# mysql -p
Enter password:
登录数据库后,可以查看数据库里面有些什么库
一共有5个库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database db2 default charset 'utf8';
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
其实也可以直接用命令: mysql> create database + 库名;
这种创建出来的库 ,不兼容除英文外其他语言,所以推荐使用第一种方法创建库。
mysql> drop database db1;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db2 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
这种删除库的操作在企业,尽量不要使用!
每一个数据库里面有表,表是用来存储数据的。知道了数据库的创建删除查看,那么如何进入数据库里查看表,对表进行操作增删改查呢
mysql> use mysql; ----这个数据库是MySQL数据库自带的
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed -----出现此行意味着进入数据库成功!
mysql> show tables; ----查看库里面的表
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
mysql> use db2;
Database changed
mysql> show tables;
Empty set (0.00 sec)
新创建的库里面没有任何东西
那么我一会进入这个库,一会进入那个库 我怎么知道自己当前在哪呢
mysql> select database();
+------------+
| database() |
+------------+
| db2 |
+------------+
1 row in set (0.00 sec)
mysql> create table t2(id int,name varchar(10),age int);
Query OK, 0 rows affected (0.01 sec)
创建了一个表,里面定义了他的字段 有id,name名字,age年纪。那么怎么查看这个表的结构呢?
mysql> desc t2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table t1 add(address varchar(10)); -------添加字段
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
用枚举法
mysql> alter table t1 add (gender enum("男","女"));
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+----------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| IDnumber | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
+----------+-------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
添加的字段默认是紧接着上一个字段后
mysql> alter table t1 add phone int after id;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table t1 add IDnumber int first;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| IDnumber | int(11) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> alter table t1 drop IDnumber;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table t1 change phone IDnumber int;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| IDnumber | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| address | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
这时候我们虽然有表的结构了,但是表里面没有任何数据,我们给表内添加数据
mysql> insert into t2 values(1,"zhangsan",18);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2(id,name,age) values(2,"aaa",19),(3,"bbb",20),(4,"ccc",21);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t2;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
| 2 | aaa | 19 |
| 3 | bbb | 20 |
| 4 | ccc | 21 |
+------+----------+------+
4 rows in set (0.00 sec)
mysql> select id from t2;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
mysql> select id from 表名
意思是 从什么表中查看什么 可以查看的就是你当初给表设定的属性(id,name,age,address,phone)等等
mysql> select * from t2; -----查看表的数据
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
| 2 | aaa | 19 |
| 3 | bbb | 20 |
| 4 | ccc | 21 |
+------+----------+------+
4 rows in set (0.01 sec)
mysql> delete from t2 where id=1; -----删除id=1的数据
Query OK, 1 row affected (0.00 sec)
mysql> select id from t2; -----id=1的数据成功删除
+------+
| id |
+------+
| 2 |
| 3 |
| 4 |
+------+
3 rows in set (0.00 sec)
将表中的age全部修改为22
mysql> update t2 set age=22;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from t2;
+------+------+------+
| id | name | age |
+------+------+------+
| 2 | aaa | 22 |
| 3 | bbb | 22 |
| 4 | ccc | 22 |
+------+------+------+
3 rows in set (0.00 sec)
将id=2的用户的名字修改为jack,年龄修改为28
mysql> update t2 set name="jack",age=28 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t2;
+------+------+------+
| id | name | age |
+------+------+------+
| 2 | jack | 28 |
| 3 | bbb | 22 |
| 4 | ccc | 22 |
+------+------+------+
3 rows in set (0.00 sec)
我觉得的我t2这个表明听起来太low了,修改表名怎么修改
mysql> rename table t2 to t1;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| t1 |
+---------------+
1 row in set (0.00 sec)
mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified