大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

MySQL安装与基础应用

MySQL
构建MySQL服务器---->CPU,内存,硬盘(存储)
安装MySQL
yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes //安装依赖文件
rpm -Uvh mysql-community-*.rpm //源码包安装
rpm -qa |grep -i mysql //查看安装玩后状态
配置文件
ls /etc/my.cnf
启动服务
systemctl status mysqld
ps -C mysqld
netstat -utnalp |grep :3306

成都创新互联是一家专业提供茅箭企业网站建设,专注与网站制作、网站设计HTML5、小程序制作等业务。10年已为茅箭众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

数据目录syst
ls /var/lib/mysql/

grep mysql /etc/passwd 查看所有者所属组
修改表

修改MySQL密码
#ls /var/log/mysqld.log
#grep password /varlog/mysqld.log查看本地数据库的初始化密码
#rpm -qf /usr/bin/mysql
#mysql -hlocalhost -uroot -p‘密码’
mysql> set global validate_password_policy=0; //修改密码只验证长度
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=6; //修改密码长度为6
Query OK, 0 rows affected (0.00 sec)
mysql> alter user user() identified by "123456" //修改登陆密码

使用永久配置文件
vim /etc/my.cnf
validate_password_policy=0
validate_password_length=6

把数据存储到数据库服务器上的过程
1、连接数据库服务器
客户端自己提供连接工具(图形 命令行)
--命令行使用mysql
2、创建库(文件夹)
创建库
--库名可用数字,字母,下划线
--不能是纯数字,关键词,特殊符号
create database 库名;
查看已有库
show databases;
删除库
drop database 库名;
切换库
use 库名;
查看库里已有的表
show tables; //表,相当于系统文件
查看当前所在的库
select database();

3、建表(文件)
create table 库名.表名(
字段名 字符类型,
字段名 数值类型,
...... name char(10)
...... age int
);
插入表记录
insert into 库名.表名 values(值列表);

查看表结构
describe 表名
查看表记录
select * from 库名.表名
删除表记录
delete from 库名.表名
删除表
drop table

mysql数据类型
支持的数据类型有那些?
--数值型:体重、身高、成绩、工资
--字符型:姓名、工作单位、通信地址
--枚举型:兴趣爱好、性别
--日期时间型:出生日期、注册时间

数值类型:整型、浮点型
根据存储数值的范围整型类型为:
tinyint smallint mediumint int bigint
unsigned 无符号

浮点型:根据存储数值的范围分为
单精度(n,m) 双精度(n,m)
n表示总位数
m表示小数位的位数
pay float(5,2)
最大999.99
最小-999.99

mysql> create table t1(id tinyint unsigned zerofill);
Query OK, 0 rows affected (0.40 sec)
mysql> desc t1;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned zerofill | YES | | NULL | |
+-------+------------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> create table t2(pay float(7,2));
Query OK, 0 rows affected (0.46 sec)

mysql> desc t2;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| pay | float(7,2) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> create table t4(
-> age float(7,2),
-> high float(3,2)
-> );
Query OK, 0 rows affected (0.36 sec)

mysql> desc t4;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| age | float(7,2) | YES | | NULL | |
| high | float(3,2) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
insert into t4 values(11211,1.82);
###########################################################
字符类型
--定长:char(字符数)
最大长度255字符
不够指定字符数时再右边用空格补齐
字符数超出时,无法写入数据
--varchar(字符数)
按数据实际大小分配存储空间
字符数超出时,无法写入数据
--大文本类型:text/blob
字符数大与65535存储时使用
mysql> create table t8(
-> name char(10),
-> class char(7),
-> address char(15),
-> mail varchar(30)
-> );
mysql> desc t8;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| class | char(7) | YES | | NULL | |
| address | char(15) | YES | | NULL | |
| mail | varchar(30) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into t8 values("jim","nsd1709","beijing","123456@qq.com")
Query OK, 1 row affected (0.04 sec)

mysql> select * from t8;
+------+---------+---------+---------------+
| name | class | address | mail |
+------+---------+---------+---------------+
| jim | nsd1709 | beijing | 123456@qq.com |
+------+---------+---------+---------------+
1 row in set (0.00 sec)
####################################################################
日期时间类型:
年 year YYYY 2017
日期 date YYYYMMDD 20171220
时间 time HHMMSS 155302
日期时间:
datetime YYYYMMDDHHMMSS
timestamp YYYYMMDDHHMMSS

mysql> create table t9(
-> name char(10),
-> age tinyint,
-> s_year year,
-> uptime time,
-> birthday date,
-> party datetime
-> );
Query OK, 0 rows affected (0.37 sec)

mysql> desc t9;
+----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| s_year | year(4) | YES | | NULL | |
| uptime | time | YES | | NULL | |
| birthday | date | YES | | NULL | |
| party | datetime | YES | | NULL | |
+----------+------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> insert into t9 values("Tom",24,1992,073000,19920221122020,20180131122100);
Query OK, 1 row affected, 1 warning (0.04 sec)
mysql> select * from t9;
+------+------+--------+----------+------------+---------------------+
| name | age | s_year | uptime | birthday | party |
+------+------+--------+----------+------------+---------------------+
| Tom | 24 | 1992 | 07:30:00 | 1992-02-21 | 2018-01-31 12:21:00 |
+------+------+--------+----------+------------+---------------------+
1 row in set (0.00 sec)
####################################################
时间函数
now() 获取调用次函数时的系统日期时间
sysdate() 执行时动态获得系统日期时间
sleep(N) 休眠N秒
curdate() 获得当前的系统日期
curtime() 获得当前的系统时刻
month() 获得指定时间中的月份
date() 获得指定时间中的日期
time() 获取指定时间中的时刻

mysql> select from t9;
+-------+------+--------+----------+------------+---------------------+
| name | age | s_year | uptime | birthday | party |
+-------+------+--------+----------+------------+---------------------+
| Tom | 24 | 1992 | 07:30:00 | 1992-02-21 | 2018-01-31 12:21:00 |
| Jerry | 25 | 1991 | 06:50:55 | 1991-08-19 | 2018-01-31 12:21:00 |
+-------+------+--------+----------+------------+---------------------+
2 rows in set (0.00 sec)
mysql> insert into t9 values("kenji",19,year(now()),time(now()),date(now()),now());
Query OK, 1 row affected (0.04 sec)
mysql> select
from t9;
+-------+------+--------+----------+------------+---------------------+
| name | age | s_year | uptime | birthday | party |
+-------+------+--------+----------+------------+---------------------+
| Tom | 24 | 1992 | 07:30:00 | 1992-02-21 | 2018-01-31 12:21:00 |
| Jerry | 25 | 1991 | 06:50:55 | 1991-08-19 | 2018-01-31 12:21:00 |
| kenji | 19 | 2017 | 03:55:12 | 2017-12-20 | 2017-12-20 03:55:12 |
+-------+------+--------+----------+------------+---------------------+
3 rows in set (0.00 sec)
###########################################################
枚举类型:字段的值只能在列表的范围内选择
字段名 enum(值列表) 只能选择一个值,在赋值时可用数字选择。
字段名 set(值列表) 多选

mysql> create table t12( name char(10), sex enum("boy","girl"), yourlikes set("book","film","game","study") );
Query OK, 0 rows affected (0.43 sec)

mysql> desc t12;
+-----------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------------------------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl') | YES | | NULL | |
| yourlikes | set('book','film','game','study') | YES | | NULL | |
+-----------+-----------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t12 values("ZhouMing","boy","book,film");
Query OK, 1 row affected (0.04 sec)

mysql> select * from t12;
+----------+------+-----------+
| name | sex | yourlikes |
+----------+------+-----------+
| ZhouMing | boy | book,film |
+----------+------+-----------+
1 row in set (0.00 sec)
##############################################################
约束条件:作用限制赋值
--Null 允许为空,默认设置
--NO NULL 不允许为空
Key 索引类型
Default 设置默认值,缺省为NULL

mysql> create table t13( name char(10) not null, sex enum('man','woman') not null default "man", age tinyint not null default 23 );
Query OK, 0 rows affected (0.37 sec)

mysql> desc t13;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name | char(10) | NO | | NULL | |
| sex | enum('man','woman') | NO | | man | |
| age | tinyint(4) | NO | | 23 | |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into t13(name) values("chihiro");
Query OK, 1 row affected (0.04 sec)

mysql> select * from t13;
+---------+-----+-----+
| name | sex | age |
+---------+-----+-----+
| chihiro | man | 23 |
+---------+-----+-----+
1 row in set (0.00 sec)
#######################################################
修改表结构
mysql> alter table 表名 执行动作;

添加新字段
-add 字段(宽度)约束条件;
-add 字段(宽度)约束条件 first; //添加至表的最前面
-add 字段(宽度)约束条件 after 字段名; // 添加至指定字段名的后面

删除字段
-drop 字段名;

修改字段类型
-modify 字段 类型(宽度) 约束条件; //不可修改为与已有值冲突的类型

修改字段名
-change 源字段名 新字段名 类型(宽度) 约束条件;

修改表名
alter table 源表名 rename 新表名


本文题目:MySQL安装与基础应用
网页URL:http://dzwzjz.com/article/pedpig.html
在线咨询
服务热线
服务热线:028-86922220
TOP