MySQL數據庫從入門到實戰(四)
阿新 • • 發佈:2018-04-04
字符集 數據類型 主鍵 元數據 ——————————————————————
第一部分:字符集
——————————————————————
show charset; ---查看mysql支持字符集
1、服務器端
(1)實例級別
vim my.cnf
character-set-server=utf8 show create table t1;
2、客戶端
alter database oldboy CHARACTER SET utf8 collate utf8_general_ci;
alter table t1 CHARACTER SET latin1;
官方定義,修改後的字符集必須是之前字符集的嚴格超集。
————————————————————————————————
第二部分:列屬性
————————————————————————————————
1)約束類:
主鍵 :
一個表只能有一個,非空並唯一的列可以設置為主鍵
create table test (id int not null primary key,name varchar(20));
或者
CREATE TABLE
PRIMARY KEY (
); id int not null primary key ,
name varchar(20) not null ,
state enum(‘y‘,‘n‘) not null default ‘y‘);
唯一:
create table people (
p_id char(20) not null primary key,
name varchar(20) not null unique ,
age tinyint not null);
非負數
(2)其他
自增長 --- AUTO_INCREMENT
默認值 ---default ‘y‘
utf8 字符 ---->3字節
utf8mb4 字符 ---->4個字節
——————————————————————————————
第三部分:元數據獲取---Informatica_schema
——————————————————————————————
什麽是元數據?
1、數據庫、表對象的一些定義信息都可以把它稱之為元數據
2、數據庫的一些狀態統計
元數據是存放到數據庫的系統表(基表)
利用視圖批量操作實例:
1.批量備份腳本拼接
vim /etc/my.cnf
secure-file-priv=/tmp
mkdir /bak
select concat("mysqldump ", "-uroot", " -p123 ",TABLE_SCHEMA," ",TABLE_NAME," >/bak/",TABLESCHEMA,"",TABLE_NAME,".sql")from information_schema.tables into outfile ‘/tmp/tabbak.sh‘;
2.批量創建world 表結構相同的表
創建語句:
CREATE TABLE world.city_0 LIKE world.city;
USE world
SHOW TABLES;
批量操作:
SELECT CONCAT("create ","table ",TABLE_SCHEMA,"."
,TABLE_NAME," "
,"like ",TABLE_SCHEMA,".",TABLE_NAME,"_0")
FROM information_schema.tables
WHERE TABLE_SCHEMA=‘world‘;
3.information_schema 企業需求案例
統計數據量
SELECT
CONCAT(table_schema, ‘.‘, table_name) AS "Name"
,CONCAT(ROUND(table_rows / 1000, 2), ‘K‘) AS "Rows"
,CONCAT(ROUND(data_length / ( 1024 1024 ), 2), ‘M‘) AS "Row Size"
,CONCAT(ROUND(index_length / ( 1024 1024 ), 2), ‘M‘) AS "Index Size"
,CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 ), 2), ‘M‘) AS "Total"
,ROUND(index_length / data_length, 2) "Row / Index Ratio"
FROM information_schema.TABLES
ORDER BY data_length + index_length DESC
LIMIT 10;
等待事件查看
SELECT
r.trx_id waiting_trx_id
,r.trx_mysql_thread_id waiting_thread
,r.trx_query waiting_query
,b.trx_id blocking_trx_id
,b.trx_mysql_thread_id blocking_thread
,b.trx_query blocking_query
FROM information_schema.innodb_lock_waits w
JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;
第一部分:字符集
——————————————————————
show charset; ---查看mysql支持字符集
1、服務器端
(1)實例級別
vim my.cnf
character-set-server=utf8
(2)庫級別
create database oldboy charset UTF8;
create database oldboy1 charset gbk;
alter database oldboy charset utf8mb4;
show create database oldboy;
(3)表級別
create table t1 (id int ,name varchar(20)) default charset utf8;
2、客戶端
alter database oldboy CHARACTER SET utf8 collate utf8_general_ci;
alter table t1 CHARACTER SET latin1;
官方定義,修改後的字符集必須是之前字符集的嚴格超集。
————————————————————————————————
第二部分:列屬性
————————————————————————————————
1)約束類:
主鍵 :
一個表只能有一個,非空並唯一的列可以設置為主鍵
create table test (id int not null primary key,name varchar(20));
CREATE TABLE
test
(id
int(11) NOT NULL,name
varchar(20) DEFAULT NULL,PRIMARY KEY (
id
));
如果業務中沒有,非空和唯一條件的列。我們可以人為定義一個無關列,最 好是數字列。
create table ptab (
pid int not null primary key AUTO_INCREMENT,
pname varchar(20),
page tinyint,
pgen enum(‘M‘,‘F‘));
非空:
create table teacher (
name varchar(20) not null ,
state enum(‘y‘,‘n‘) not null default ‘y‘);
唯一:
create table people (
p_id char(20) not null primary key,
name varchar(20) not null unique ,
age tinyint not null);
非負數
(2)其他
自增長 --- AUTO_INCREMENT
默認值 ---default ‘y‘
utf8 字符 ---->3字節
utf8mb4 字符 ---->4個字節
——————————————————————————————
第三部分:元數據獲取---Informatica_schema
——————————————————————————————
什麽是元數據?
1、數據庫、表對象的一些定義信息都可以把它稱之為元數據
2、數據庫的一些狀態統計
元數據是存放到數據庫的系統表(基表)
利用視圖批量操作實例:
1.批量備份腳本拼接
vim /etc/my.cnf
secure-file-priv=/tmp
mkdir /bak
select concat("mysqldump ", "-uroot", " -p123 ",TABLE_SCHEMA," ",TABLE_NAME," >/bak/",TABLESCHEMA,"",TABLE_NAME,".sql")from information_schema.tables into outfile ‘/tmp/tabbak.sh‘;
2.批量創建world 表結構相同的表
創建語句:
CREATE TABLE world.city_0 LIKE world.city;
USE world
SHOW TABLES;
批量操作:
SELECT CONCAT("create ","table ",TABLE_SCHEMA,"."
,TABLE_NAME," "
,"like ",TABLE_SCHEMA,".",TABLE_NAME,"_0")
FROM information_schema.tables
WHERE TABLE_SCHEMA=‘world‘;
3.information_schema 企業需求案例
統計數據量
SELECT
CONCAT(table_schema, ‘.‘, table_name) AS "Name"
,CONCAT(ROUND(table_rows / 1000, 2), ‘K‘) AS "Rows"
,CONCAT(ROUND(data_length / ( 1024 1024 ), 2), ‘M‘) AS "Row Size"
,CONCAT(ROUND(index_length / ( 1024 1024 ), 2), ‘M‘) AS "Index Size"
,CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 ), 2), ‘M‘) AS "Total"
,ROUND(index_length / data_length, 2) "Row / Index Ratio"
FROM information_schema.TABLES
ORDER BY data_length + index_length DESC
LIMIT 10;
等待事件查看
SELECT
r.trx_id waiting_trx_id
,r.trx_mysql_thread_id waiting_thread
,r.trx_query waiting_query
,b.trx_id blocking_trx_id
,b.trx_mysql_thread_id blocking_thread
,b.trx_query blocking_query
FROM information_schema.innodb_lock_waits w
JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id
JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;
MySQL數據庫從入門到實戰(四)