CPU,內(nèi)存,硬盤(存儲)安裝MySQLyum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes //安裝依賴文件rpm -Uvh mysql..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL安裝與基礎(chǔ)應(yīng)用

發(fā)布時(shí)間:2020-04-28 11:01:40 來源:網(wǎng)絡(luò) 閱讀:468 作者:肖純 欄目:MySQL數(shù)據(jù)庫

MySQL
構(gòu)建MySQL服務(wù)器---->CPU,內(nèi)存,硬盤(存儲)
安裝MySQL
yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes //安裝依賴文件
rpm -Uvh mysql-community-*.rpm //源碼包安裝
rpm -qa |grep -i mysql //查看安裝玩后狀態(tài)
配置文件
ls /etc/my.cnf
啟動服務(wù)
systemctl status mysqld
ps -C mysqld
netstat -utnalp |grep :3306

數(shù)據(jù)目錄syst
ls /var/lib/mysql/

grep mysql /etc/passwd 查看所有者所屬組
修改表

修改MySQL密碼
#ls /var/log/mysqld.log
#grep password /varlog/mysqld.log查看本地?cái)?shù)據(jù)庫的初始化密碼
#rpm -qf /usr/bin/mysql
#mysql -hlocalhost -uroot -p‘密碼’
mysql> set global validate_password_policy=0; //修改密碼只驗(yàn)證長度
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

把數(shù)據(jù)存儲到數(shù)據(jù)庫服務(wù)器上的過程
1、連接數(shù)據(jù)庫服務(wù)器
客戶端自己提供連接工具(圖形 命令行)
--命令行使用mysql
2、創(chuàng)建庫(文件夾)
創(chuàng)建庫
--庫名可用數(shù)字,字母,下劃線
--不能是純數(shù)字,關(guān)鍵詞,特殊符號
create database 庫名;
查看已有庫
show databases;
刪除庫
drop database 庫名;
切換庫
use 庫名;
查看庫里已有的表
show tables; //表,相當(dāng)于系統(tǒng)文件
查看當(dāng)前所在的庫
select database();

3、建表(文件)
create table 庫名.表名(
字段名 字符類型,
字段名 數(shù)值類型,
...... name char(10)
...... age int
);
插入表記錄
insert into 庫名.表名 values(值列表);

查看表結(jié)構(gòu)
describe 表名
查看表記錄
select * from 庫名.表名
刪除表記錄
delete from 庫名.表名
刪除表
drop table

mysql數(shù)據(jù)類型
支持的數(shù)據(jù)類型有那些?
--數(shù)值型:體重、身高、成績、工資
--字符型:姓名、工作單位、通信地址
--枚舉型:興趣愛好、性別
--日期時(shí)間型:出生日期、注冊時(shí)間

數(shù)值類型:整型、浮點(diǎn)型
根據(jù)存儲數(shù)值的范圍整型類型為:
tinyint smallint mediumint int bigint
unsigned 無符號

浮點(diǎn)型:根據(jù)存儲數(shù)值的范圍分為
單精度(n,m) 雙精度(n,m)
n表示總位數(shù)
m表示小數(shù)位的位數(shù)
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(字符數(shù))
最大長度255字符
不夠指定字符數(shù)時(shí)再右邊用空格補(bǔ)齊
字符數(shù)超出時(shí),無法寫入數(shù)據(jù)
--varchar(字符數(shù))
按數(shù)據(jù)實(shí)際大小分配存儲空間
字符數(shù)超出時(shí),無法寫入數(shù)據(jù)
--大文本類型:text/blob
字符數(shù)大與65535存儲時(shí)使用
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)
####################################################################
日期時(shí)間類型:
年 year YYYY 2017
日期 date YYYYMMDD 20171220
時(shí)間 time HHMMSS 155302
日期時(shí)間:
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)
####################################################
時(shí)間函數(shù)
now() 獲取調(diào)用次函數(shù)時(shí)的系統(tǒng)日期時(shí)間
sysdate() 執(zhí)行時(shí)動態(tài)獲得系統(tǒng)日期時(shí)間
sleep(N) 休眠N秒
curdate() 獲得當(dāng)前的系統(tǒng)日期
curtime() 獲得當(dāng)前的系統(tǒng)時(shí)刻
month() 獲得指定時(shí)間中的月份
date() 獲得指定時(shí)間中的日期
time() 獲取指定時(shí)間中的時(shí)刻

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)
###########################################################
枚舉類型:字段的值只能在列表的范圍內(nèi)選擇
字段名 enum(值列表) 只能選擇一個(gè)值,在賦值時(shí)可用數(shù)字選擇。
字段名 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 允許為空,默認(rèn)設(shè)置
--NO NULL 不允許為空
Key 索引類型
Default 設(shè)置默認(rèn)值,缺省為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)
#######################################################
修改表結(jié)構(gòu)
mysql> alter table 表名 執(zhí)行動作;

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

刪除字段
-drop 字段名;

修改字段類型
-modify 字段 類型(寬度) 約束條件; //不可修改為與已有值沖突的類型

修改字段名
-change 源字段名 新字段名 類型(寬度) 約束條件;

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI