您好,登錄后才能下訂單哦!
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
數據目錄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 新表名
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。