MySQL資料庫從入門到實戰(四)

來源:互聯網
上載者:User

標籤:字元集   資料類型   主鍵   中繼資料   

——————————————————————
第一部分:字元集
——————————————————————
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;
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 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 (
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;

MySQL資料庫從入門到實戰(四)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.