MySQL
– 当今社会的数据概念越来越重,对于数据的处理也越来越大,所以就有了数据库的理念。当下有Oracle , MySQL , moogdb ,… 等等,都是用于处理数据的软件。
SQL
1 .数据库管理系统的优点:
- 相互关联的数据集合一起使用
- 较少的数据冗余
- 程序与数据相互独立
- 保证数据的安全、可靠
- 最大限度的保证数据正确性
- 数据可以并发使用并且同时保持一致性
数据库的架构
- 单机架构
- 大型主机/终端架构
- 主从式架构(C/S)
- 分布式架构
范式 —范式只是给个规范,没有明确的指定非得这样规定。
- 1NF:每一列都是不可分割的基本数据项,同一列中不能有多个值,即实体中的某个属性不能有多个值或者不能有重复的属性。除去同类型的字段,就是无重复的列
- 2NF:第二范式必须先满足第一范式,要求表中的每个行必须可以被唯一地区分。通常为表加上一个列,以存储各个实例的唯一标识PK。非PK的字段需要与整个PK有直接相关性
- 3NF: 满足第三范式必须先满足第二范式。第三范式要求一个数据库表中不包含已在其它表中已包含的非主关键字信息,非PK的字段间不能有从属关系
` - - - 理论的东西不多做介绍了,下面来实际操作
MySQL
1.安装
下载地址:https://downloads.mariadb.org/
安装参考官方文档:https://mariadb.com/kb/en/library/installing-mariadb-binary-tarballs/
1.yum安装
yum 安装的话 CentOS6 装的是MySQL的5.1版本而 CentOS7 装的是mariadb 5.5 (这里提到的mariadb是MySQL的一个分支,因为MySQL被Oracle收购后,开发出MySQL的创始人又重新开发了个mariadb,里面的代码和开发结构跟MySQL没多大区别)
- CentOS 6
yum install -y mysql 装了有 mysql .x86_64 mysql-libs.x86_64 mysql-server.x86_64qt-mysql.x86_64 这几个包 - CentOS 7
yum -y groupinstall mariadb mariadb-client 对应的包名 mariadb.x86_64,mariadb-libs.x86_64,mariadb-server.x86_64
2.二进制编辑安装
事先下好了mariadb的最新10.2.8版本
Ⅰ.准备mysql的用户和组
[root@localhost ~]# groupadd -r -g 306 mysql 创建系统组ID为306名称为mysql
[root@localhost ~]# useradd -r -g 306 -u 306 -m -d /app/data mysql 创建mysql用户 UID,GID 306 家目录/app/data
Ⅱ.准备二进制程序
tar xf mariadb-VERSION-linux-x86_64.tar.gz -C /usr/local --- 此项建意放置/usr/local 目录里
cd /usr/local && ln -sv mariadb-10.2.8-linux-x86_64 mysql ---也可以直接把mariadb-10.2.8改名为 mysql
chown -R root:mysql /usr/local/mysql/ ---递归附于mysql组的权限
Ⅲ.准备配置文件
mkdir /etc/mysql/ --- 创建/etc/mysql/ 的配置目录
cp support-files/my-large.cnf /etc/mysql/my.cnf --- 复制模板配置文件
chown -R root:mysql /etc/mysql/ --- 给目录附于权限
在【mysqld】中添加三个选项:datadir=/app/data , innodb_file_per_table = on , skip_name_resolve = on 这三项分别为,指定数据库的默认路径 , 开启innodb模式(即独立的表空间,一张表一个文件) 和 禁止主机名解析
Ⅳ.创建数据库文件
cd /usr/local/mysql && ./scripts/mysql_install_db --datadir=/app/data --user=mysql
Ⅴ.准备日志文件
touch /var/log/mysqld.log && chown mysql:mysql /var/log/mysqld.log
Ⅵ.准备服务脚本,并启动服务
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
创建一个脚本/etc/profile.d/mysql.sh
PATH=/usr/local/mysql/bin:$PATH
service mysqld start
Ⅶ.安装完成后初始化数据库设置
[root@localhost mysql]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Sorry, passwords do not match.
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
2.mysql使用简介
进入mysql
mysql -uUSER@IP|HOSTNAME -p:PASSWORD 进入mysql数据库命令,因为进入的用户为user和IP或user HOSTNAME 两个条件组成,所以进入加上。 -p是密码
退出mysql
exit或quit
SQL语句:
DDL:数据定义语言
CREATE , DROP , ALTER
DML:数据处理语言
INSERT , DELETE , UPDATE
DCL:数据控制语言
GRANT , REVOKE
DQL:查询语言
SELECT
**命令帮助**
HELP KEYWORD 或 ?KEYWORD
创建数据库及删除、查看
CREATE --
CREATE DATABASE "DB_NAME"
如:CREATE DATABASE benbendb; (;分号是命令的结语标识)
我们之前定义的数据库目录在/app/data所以当我们创建一个数据库后,/app/data会增加创建的数据库目录
[root@localhost data]# ls /app/data
aria_log.00000001 ib_buffer_pool ib_logfile1 multi-master.info mysql-bin.000002
aria_log_control ibdata1 ibtmp1 mysql mysql-bin.index
benbendb ib_logfile0 localhost.localdomain.pid mysql-bin.000001 performance_schema
DROP --
DROP DATABASE "DB_NAME"
如:DROP DATABASE benbendb; 删除库了自然目录文档也一并被删除
SHOW --
SHOW DATABASES; 查看当前所有的数据库
创建数据表前先做下数据表的数据类型:
- 数据表的数据类型。
- 整数
- 浮点
- 定点数
- 字符串
- 二进制数据
整数
- tinyint(m) ———— 1个字节,范围(-128~127)
- smalint(m) ———– 2个字节,范围(-32768~32767)
- mediumint(m)——-3个字节,范围(-8388608~8388607)
- int(m)——————4个字节,范围(-2147483648~2147483647)
- bigint(m)————-8个字节,范围(-2^63到2^63-1(即从-9,223,372,036,854,775,808到 9,223,372,036,854,775,807)之间的整数)
- unsigned,显示的类型加上unsigned后只会显示正整数
浮点型
- float(m,d) 单精度浮点型 8位精度(4字节)m总个数,d小数位
- double(m,d) 双精度浮点型 16位精度(8字节)
定点数—-在数据库中存放的是精确值,存为十进制
- decimal(m,d) m<65 总个数,d<30,d< m 小数位
字符串
- char(n) —————- 固定长度,最多255个字符
- varchar(n)————–可变长度,最多65535个字符
- tinytext—————–可变长度,最多255个字符
- text———————可变长度,最多65536个字符
- mediumtext———-可变长度,最多2^24次方-1个字符
- longtext —————可变长度,最多2^32次方-1个字符
- BINARY(M)————固定长度,可存二进制或字符,允许长度为0-m字节
- VARBINARY(M)——可变长度,可存二进制或字符,允许长度为0-m字节
二进制数据:BLOB
- BLOB和text存储方式不同,TEXT以文本方式存储,英文存储区分大小写,而blob是二进制方式,不区分大小写,而且只能是整体读出,TEXT可以需要指定字符集,blob不用指定
日期时间类型
- data ———— 日期
- time ———— 时间
- datatime —— 日期时间
- timestamp —-自动存储记录修改的时间即时间戳
- YEAR(2),YEAR(4): 年份
修饰符:
- NULL ————数据列可包含NULL值 (NULL 空值)
- NOT NULL —– 数据列不允许包含NULL值
- DEFAULT ——-默认值
- PRIMARY KEY -主键
- UNIQUE KEY – 唯一键
- CHARACTER SET name —- 指定一个字符集
数值型:
- AUTO_INCREMENT ——– 自动递增,适用于整数类型
- UNSIGNED ——————-无符号
3.mysql操作
数据表操作:
创建数据表
MariaDB [benbendb]> CREATE TABLE student (id tinyint unsigned primary key,name varchar(20) not null,age tinyint unsigned,sex char(1) default "m" );
Query OK, 0 rows affected (0.02 sec)
查看数据表
MariaDB [benbendb]> show tables;
+--------------------+
| Tables_in_benbendb |
+--------------------+
| student |
| student2 |
+--------------------+
2 rows in set (0.00 sec)
查看表内容
MariaDB [benbendb]> desc student;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| sex | char(1) | YES | | m | |
+-------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
删除表
MariaDB [benbendb]> DROP TABLE student2;
Query OK, 0 rows affected (0.01 sec)
MariaDB [benbendb]> show tables;
+--------------------+
| Tables_in_benbendb |
+--------------------+
| student |
+--------------------+
1 row in set (0.00 sec)
修改表
ALTER TABLE student RENAME students; -- 修改表名
ALTER TABLE students ADD phone varchar(11) AFTER name; -- 在name行下添加phone varchar(11)行
MariaDB [benbendb]> desc students;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| phone | varchar(11) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| sex | char(1) | YES | | m | |
+-------+---------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
ALTER TABLE students MODIFY phone int; -- 把phone 改为 int类型
MariaDB [benbendb]> desc students;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| phone | int(11) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| sex | char(1) | YES | | m | |
+-------+---------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
ALTER TABLE students CHANGE COLUMN phone mobile char(11); -- 把phone 名改为mobile char(11)类型
MariaDB [benbendb]> desc students;
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| mobile | char(11) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| sex | char(1) | YES | | m | |
+--------+---------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
ALTER TABLE students drop phone; -- 删除phone表内字行
MariaDB [benbendb]> desc students;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| sex | char(1) | YES | | m | |
+-------+---------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
创建索引 -- 索引是定义查找时作为条件的字段,优点查询速度有提升,缺点,点用额外空间
MariaDB [benbendb]> desc students; -- 查看当前表结构
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| sex | char(1) | YES | | m | |
| id | char(11) | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
MariaDB [benbendb]> SHOW INDEXES FROM students; -- 查看students内的索引
Empty set (0.00 sec)
MariaDB [benbendb]> ALTER TABLE students ADD INDEX(id); -- 创建id索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [benbendb]> SHOW INDEXES FROM students\G; -- 竖状显示当前索引
*************************** 1. row ***************************
Table: students
Non_unique: 1
Key_name: id
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
ERROR: No query specified
往表内添加数据
MariaDB [benbendb]> INSERT INTO students (name,age,sex,id) VALUES ("li",18,"man",1)
MariaDB [benbendb]> SELECT * FROM students;
+------+------+------+------+
| name | age | sex | id |
+------+------+------+------+
| li | 18 | 1 | 1 |
+------+------+------+------+
1 row in set (0.00 sec)
紧后命令补充下:
SELECT user(); -- 查看当前用户
DROP USER@LOCALHOST; -- 删除空用户