mysql 关系型数据库
mariadb yum安装
mysql = mariadb 一个软件 操作方式
源码/rpm yum
数据库
关系型数据库 存储方式是以表结构的形式存储的
非关系型数据库 存储是以键值对的形式存储
mysql5.7
安装:
两种安装方式
1.rpm安装
rpm
rpm 安装 顺序
common--libs--client--devel--sever
[root@localhost ~]# yum -y remove mariadb-libs
15 rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm
16 rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm
17 rpm -ivh mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm
18 rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm
19 rpm -ivh mysql-community-devel-5.7.33-1.el7.x86_64.rpm
20 rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm
mysql 的特点:
1.关系型数据库 sql语句进行管理
2.开源的 跨平台 linux window
3.支持主从集群部署
4.支持多种连接方式 lamp php java python
5.支持备份 索引 事务
6.支持多种数据类型 int char
mysql.com 小海豚
#启动服务
[root@localhost ~]# systemctl start mysqld #开启
[root@localhost ~]# systemctl stop mysqld #关闭
[root@localhost ~]# systemctl enable mysqld #加入开机自启
[root@localhost ~]# systemctl disable mysqld #移除开机自启
#5.7mysql如果没有密码是没有办法登录 mysql有默认密码 如果命令行中出现密码 不可以使用上键
[root@localhost ~]# cat /var/log/mysqld.log | grep password
2021-11-01T05:55:11.226445Z 1 [Note] A temporary password is generated for root@localhost: b%KCKgEwV6nE
[root@localhost ~]# mysql -uroot -p'b%KCKgEwV6nE'
mysql> set password=password('1234.Com'); #更改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@localhost ~]# mysql -uroot -p'1234.Com'
查看数据库中的变量
查看密码策略的变量
mysql> show variables like "%password%";
| validate_password_length | 8
| validate_password_number_count | 1
| validate_password_mixed_case_count | 1
| validate_password_policy | MEDIUM #严格模式
| validate_password_special_char_count | 1
mysql> set global validate_password_length=4; #密码长度
Query OK, 0 rows affected (0.02 sec)
mysql> set global validate_password_policy=0; 密码策略为宽容模式
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_mixed_case_count=0; #取消大小混合
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_special_char_count=0; #特殊字符0个
mysql> set password=password("12345");
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -uroot -p12345
库 -- 表
mysql user 存放用户信息 密码 授权
mysql> show databases; #查看库
mysql> use mysql #进入mysql库
mysql> show tables; #查看表
mysql> create database test; #创建库
mysql> drop database test; #删除库
创建一个abc库 使用abc这个库
mysql> create database abc;
mysql> use abc;
创建表
create table 表明 (
字段一 数据类型 [完整约束条件],
字段二 数据类型 [完整约束条件],
字段三 数据类型 [完整约束条件],
)
数据类型: 指的是数据的种类 例如 数据 小数 浮点数 字符串 时间等
整数的数据类型 INT, BIT, BOOL , TINY INT, SAMLL INT, BIG INT
浮点数: float bouble
字符串:char varchar text
日期: date datetime year
https://www.cnblogs.com/-xlp/p/8617760.html 有关数据类型的文档
字段---表头
数据类型----表头下的数据是什么类型 数字 字符
什么叫做完整约束条件
就是对于字端的限制,要求用户对该数据符合特点的要求 如果不能满足完整性约束条件数据库系统将不会执行用户写入的操作 目的为了保证数据的完整性
1. 主键 要求主键的字段非空且唯一
一个当中只能有一个主键
create table 表名
字段 数据类型 primary key
);
mysql> create table test_pri (
-> id int,
-> name char(10) primary key
-> );
mysql> desc test_pri;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(10) | NO | PRI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.03 sec)
表当中插入数据
insert into 表名 values(数据);
验证唯一性
mysql> insert into test_pri values (1,"lala");
mysql> insert into test_pri values (2,"lala");
ERROR 1062 (23000): Duplicate entry 'lala' for key 'PRIMARY'
不能为空
mysql> insert into test_pri values (2,NULL);
ERROR 1048 (23000): Column 'name' cannot be null
删除主键
alter table 表名 add/drop 主键
mysql> alter table test_pri drop primary key;
mysql> desc test_pri;
添加主键
mysql> alter table test_pri add primary key(name);
mysql> desc test_pri;
多字段联合主键
一个表当中只能有一个主键
create table 表名 (
字段,
primary key(字段1,字段2)
)
mysql> create table test_pri1 (
-> id int,
-> name char(10),
-> primary key(id,name)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> desc test_pri1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | NO | PRI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
唯一约束 unique 要求为止约束的字段数据唯一
非空约束 not null 要求费控约束的字段不能有空值
在表当中如果没有主键 对于一个字段设置了非空且唯一的特性,那么这个字段会被作为主键
create table 表名 (
字段 数据类型 unique,
字段 数据类型 not null,
字段 数据类型 unique not null
)
mysql> create table test_un (
-> id int unique,
-> name char(10) not null,
-> phone_num int unique not null
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc test_un;
删除非空
alter table 表名 modify 字段 null;
mysql> alter table test_un modify phone_num int null;
mysql> desc test_un;
删除唯一
alter table 表名 drop index 字段名;
mysql> alter table test_un drop index phone_num;
mysql> desc test_un;
添加非空唯一
alter table 表名 modify 字段 not null unique;
mysql> alter table test_un modify phone_num int not null unique;
默认值约束 default 当字段中没有数据写入 则使用默认设置的值
create table 表名 (
字段 数据类型 default "值";
)
mysql> create table test_def (
-> id int default "10",
-> name char(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> desc test_def;
mysql> insert into test_def(name) values("lala");
mysql> select * from test_def;
+------+------+
| id | name |
+------+------+
| 10 | lala |
删除默认值
alter table 表名 alter column 字段名 drop default;
mysql> alter table test_def alter column id drop default;
mysql> desc test_def;
添加或修改默认值
alter table 表名 modify 字段 default ‘值’;
mysql> alter table test_def modify id int default '5';
mysql> desc test_def;
mysql> alter table test_def modify id int default '6';
外键
实现多张表之间数据的完整性和一致性
设定外键链接表的字段 必须是主键
用法
create table 表一 (
字段1 数据类型 primary key
);
create table 表二 (
字段1 数据类型
constraint 外键名 foreign key (字段名) references 表一 (字段名)
)
mysql> create table class (
-> id int primary key,
-> class_name char(10),
-> t_name char(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> create table student (
-> id int,
-> name char(10),
-> c_id int,
-> constraint c_s foreign key(c_id) references class(id)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> insert into class values (1,"yiban","zhangsan");
Query OK, 1 row affected (0.01 sec)
mysql> insert into student values (1,"aa",1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values (1,"bb",2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`abc`.`student`, CONSTRAINT `c_s` FOREIGN KEY (`c_id`) REFERENCES `class` (`id`))
删除外键
alter table 表名 drop foreign key 外键名;
mysql> alter table student drop foreign key c_s;
添加外键
alter table 表名 add constraint 外键名 key (外键字段) refenrences 外表名 主键字段
mysql> alter table student add constraint c_s foreign key(c_id) references class(id);
自增约束 auto_increment 自增字段会按照顺序写入数值
create table 表名 (
字段名 数据类型 auto_increment
)auto_increment=1;从几开始自增
每次自增几个需要更改变量 或者更改配置文件 这里默认每次自增一个
mysql> create table test_auto (
-> id int primary key auto_increment,
-> name char(10)
-> )auto_increment=1;
Query OK, 0 rows affected (0.00 sec)
mysql> desc test_auto;
mysql> insert into test_auto(name) values('aa');
mysql> insert into test_auto(name) values('bb');
mysql> select * from test_auto;
删除自增
alter table 表名 modify column 字段名 first;
mysql> alter table test_auto modify column id int first;
mysql> desc test_auto;
添加:
alter table 表名 modify column 字段名 auto_increment;
mysql> alter table test_auto modify column id int auto_increment;
mysql> desc test_auto;