1. 程式人生 > >第二十章 資料庫操作

第二十章 資料庫操作

一、資料庫介紹
資料庫:
sql server : 微軟
oracle :甲骨文 收費 閉源
mysql :甲骨文 免費
SUN公司 小型機unix java mysql marrydb
其中MariaDB是MySQL的分支,也是它的進階產品,未來很有可能替代MySQL。
Fackbook 阿里 : 去IOE產品(IBM ORACLE EMC2(儲存櫃))拿自己的錢給別人交學費,不必過度依賴傳統的IOE架構
阿里:定製系統 伺服器 資料庫 定製

二、MYRIADB介紹
mysql:
簡單易用、免費、查詢速度快、安全可靠
獨立的伺服器,跟linux本身沒有關聯(包括命令、管理、遠端管理等)
資料庫結構: 資料庫、表、欄位、值
(表格、列/欄位、1條記錄/1行)

MariaDB有許多的新選項、擴充套件,儲存引擎和bug修復,而MySQL是沒有的
安裝myriadb伺服器:(在光碟上有rpm包)
yum install mariadb-server mariadb
============================
資料庫配置檔案:
設定客戶端:
vim /etc/my.cnf.d/mysql-clients.cnf

[mysql]
default-character-set=utf8

設定服務端:

vim /etc/my.cnf.d/server.cnf

[mysqld]
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_general_ci
skip-character-set-client-handshake

開啟慢查詢
slow_query_log = ON
slow_query_log_file = /usr/local/mysql/data/slow.log
long_query_time = 1


開啟mariadb:

Systemctl start mariadb

檢視服務狀態:
Systemctl status mariadb
netstat -antpl | grep 3306

=============================
mysql安裝後,預設沒有密碼
mysql命令直接進入管理

為mysql賬戶設定密碼:
mysqladmin -u 使用者名稱 -p password 密碼
再回車即可,若有原密碼,輸入原密碼回車實現改密碼
mysqladmin -uroot -p password 123123
以後進入mysql,通過
mysql -uroot -p123123

進入mysql資料庫配置模式:
mysql -u 使用者名稱
mysql -u root //root是mysql自己的管理員賬戶,預設沒有密碼

進入mysql資料庫配置模式(有密碼情況):
mysql -u 使用者名稱 -p密碼

mysql -u 使用者名稱 -p
輸入密碼再回車

如:mysql -u root -p123456
mysql -u root -p

退出mysql配置模式:
quit;

忘記root密碼:
vim /etc/my.cnf.d/server.cnf
在[mysqld]下方新增一行資訊:skip-grant-tables
儲存退出
Systemctl restart mariadb
mysql -u root
update mysql.user set password=PASSWORD('新密碼') where user='root';
quit;
vim /etc/my.cnf.d/server.cnf
註釋掉skip-grant-tables
儲存退出
Systemctl restart mariadb
生效!

=================================

檢視/列出所有資料庫:
show databases;

進入資料庫:
use 資料庫名;

檢視/列出所有表
show tables;

查看錶的內容:
select 欄位名 from student;
select * from 表名;
select user,password from 表名

檢視/列出表的所有欄位:
desc 表名;

建立新資料庫:
create database 資料庫名;

建立表: 主鍵
create table 庫名.表名 (欄位名 型別,欄位名 型別,欄位名 型別, primary key(欄位名));

註釋:
int 整數型別 xxxxxxxxxx 10位 4位元組
bit 位元型別 0/1
float 單精度浮點型別
char(n) 字串 固定長度
varchar(n) 字串 可變長度
text 字串 可變長度
date xxxx-xx-xx
time hh:mm:ss

新增一條記錄/值:
insert into 庫名.表名 [(欄位名,欄位名.....)] values (值,值.....);
------------------------------------------
更改表的欄位及型別:(更改表的結構)
help alter; //檢視alter命令的幫助資訊

修改表的欄位:
alter table 庫名.表名 add 欄位名 型別 [first/after 欄位名];
alter table 庫名.表名 drop 欄位名;
alter table 庫名.表名 modify 欄位名 型別;

alter table chengji add banji varchar (2);

修改一條記錄/值:
update 庫名.表名 set 欄位=值 [where 條件];
update cj set sex=NULL where 條件;
update cj set shuxue=60,yuwen=60 where name='lsy';
條件語法格式:欄位=值
注意:當值為數字時不需要加單引號,如是字串必須加單引號。

==============================
刪除記錄:
delete from 庫名.表名 where 條件;

刪除表:
drop table 庫名.表名;

刪除資料庫:
drop database 資料庫名;

如何備份資料庫,以及繞過資料庫使用者驗證登入
mysqldump -u root -p 庫名 表名 > 路徑檔案

進入mysql後
進入資料庫
source chengji.dmp;
可以恢復資料

如何授權遠端使用者登入資料庫管理表格
grant all privileges on . to [email protected]'172.18.11.%' identified by '123123';

status;
檢視資料庫狀態


查詢記錄:

select 欄位名列表 from 表名;
select 欄位名列表 from 表名 [where 條件表示式];

select * from t1 where age > 10;

註釋:
欄位名列表的表示方式:

  • 所有欄位
    欄位名1
    欄位名1,欄位名2, 欄位名N

條件的表示方式:
結構: 欄位 運算子 值

===============================================

Mysql作業
MYSQL更改root密碼和新增一個使用者的使用者密碼(忘記密碼的情況下)
MYSQL建立學籍表與成績表,進行關聯查詢,從兩個表取出:姓名,分數,班級,成績。
MYSQL資料備份,模擬庫刪除後的資料恢復。
*