數據建立,查詢,更新和刪除操做都是經過數據引擎來進行的。不一樣的存儲引擎存儲限制不一樣,支持不一樣的索引機制等。html
MySQL 5.7.2支持的存儲引擎有:InnoDB,MRG_MYISAM,MEMORY,BLACKHOLE,MyISAM,CSV,ARCHIVE,PERFORMANCE_SCHEMA,FEDERATED。默認的存儲引擎是InnoDB。mysql
SHOW ENGINES; mysql> SHOW ENGINES; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec)
InnoDB的特色:存儲限制64TB,支持事務,支持樹索引和數據緩存,外鍵。web
MySQL常見的數據類型有整型,字符串類型,時間類型,二進制類型。sql
MySQL主要提供的整型有:TINYINT、SMALLINT、MEDIUMINT、INT、BIGINT。不一樣的整數類型支持的取值範圍不一樣,須要的存儲空間也不一樣。使用時根據需求選擇合適的類型,節省存儲空間提升查詢的效率。 數據庫
MySQL的浮點數類型有單精度(FLOAT)和雙精度(DOUBLE)以及定點類型(DECIMAL)。浮點類型和定點類型均可以用(M, D)來表示,其中M稱爲精度,表示總共的位數;D稱爲標度,表示小數的位數。 DECIMAL 的默認 D 值爲 0、M 值爲 10。緩存
MySQL 中的字符串類型有 CHAR、VARCHAR、TINYTEXT、TEXT、MEDIUMTEXT、LONGTEXT、ENUM、SET 等。 app
###二進制類型 MySQL中的二進制字符串有: BIT、BINARY、VARBINARY、TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。 ide
MySQL中表示日期的數據類型有:YEAR、TIME、DATE、DTAETIME、TIMESTAMP。 spa
主鍵,英文名稱:PRIMARY KEY,可以惟一表示每一行,由一列或者多列組成。.net
MySQL 外鍵約束(FOREIGN KEY)用來在兩個表的數據之間創建連接,它能夠是一列或者多列。一個表能夠有一個或多個外鍵。
外鍵對應的是參照完整性,一個表的外鍵能夠爲空值,若不爲空值,則每個外鍵的值必須等於另外一個表中主鍵的某個值。
外鍵是表的一個字段,不是本表的主鍵,但對應另外一個表的主鍵。定義外鍵後,不容許刪除另外一個表中具備關聯關係的行。
#建立表cluster 主鍵爲id create table cluster( id Int(20) primary key, name char(20) not null, status char(10) not null, userId Int(10) not null, createAt datetime not null, delAt datetime not null , updateAt datetime not null )ENGINE=InnoDB DEFAULT CHARSET=utf8; #建立表instance主鍵爲id,外鍵爲fk_clusterId,和cluster表的id字段對應 create table instance( id Int(20) primary key, name char(20) not null, clusterId Int(20) not null, status char(10) not null, isBilling tinyint not null, delAt datetime not null, CONSTRAINT fk_clusterId foreign key (clusterId) REFERENCES cluster(id) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
鏈接是多表查詢的基礎。
#cluster表 mysql> select * from cluster; +-------+-----------+--------+--------+---------------------+---------------------+---------------------+ | id | name | status | userId | createAt | delAt | updateAt | +-------+-----------+--------+--------+---------------------+---------------------+---------------------+ | 18888 | qws-test | 200 | 18 | 2019-04-11 20:24:24 | NULL | 2019-04-11 20:24:24 | | 18889 | qws-test1 | 300 | 18 | 2019-04-11 20:24:24 | 2019-04-11 20:30:30 | 2019-04-11 20:24:24 | +-------+-----------+--------+--------+---------------------+---------------------+---------------------+ 2 rows in set (0.00 sec) #instance表 mysql> select * from instance; +----+---------------+-----------+--------+-----------+---------------------+ | id | name | clusterId | status | isBilling | delAt | +----+---------------+-----------+--------+-----------+---------------------+ | 1 | qws-test-1-1 | 18888 | 200 | 1 | NULL | | 2 | qws-test-2-1 | 18888 | 200 | 1 | NULL | | 3 | qws-test-3-1 | 18888 | 200 | 1 | NULL | | 4 | qws-test1-1-1 | 18889 | 400 | 0 | 2019-04-11 20:30:30 | | 5 | qws-test1-2-1 | 18889 | 400 | 0 | 2019-04-11 20:30:30 | | 6 | qws-test1-3-1 | 18889 | 400 | 0 | 2019-04-11 20:30:30 | +----+---------------+-----------+--------+-----------+---------------------+ 6 rows in set (0.00 sec)
返回知足的鏈接條件的全部行,相似於兩個集合的交集。
#查詢已刪除的集羣名稱和實例名稱 mysql> select c.name,d.name from cluster c inner join instance d on c.delAt = d.delAt; +-----------+---------------+ | name | name | +-----------+---------------+ | qws-test1 | qws-test1-1-1 | | qws-test1 | qws-test1-2-1 | | qws-test1 | qws-test1-3-1 | +-----------+---------------+ 3 rows in set (0.00 sec) 等價於: mysql> select c.name,d.name from cluster c ,instance d where c.delAt = d.delAt; +-----------+---------------+ | name | name | +-----------+---------------+ | qws-test1 | qws-test1-1-1 | | qws-test1 | qws-test1-2-1 | | qws-test1 | qws-test1-3-1 | +-----------+---------------+ 3 rows in set (0.00 sec)
返回左表中全部的記錄以及符合鏈接條件的記錄。把左表當作A集合,右表看出B集合。左鏈接的返回結果爲:A並上AB的交集。
mysql> select c.name,c.status,d.name from cluster c left outer join instance d on c.delAt = d.delAt ; +-----------+--------+---------------+ | name | status | name | +-----------+--------+---------------+ | qws-test1 | 300 | qws-test1-1-1 | | qws-test1 | 300 | qws-test1-2-1 | | qws-test1 | 300 | qws-test1-3-1 | | qws-test | 200 | NULL | +-----------+--------+---------------+ 4 rows in set (0.00 sec) 等價於 <pre>mysql> select c.name,d.name from cluster c ,instance d where c.delAt = d.delAt; +-----------+---------------+ | name | name | +-----------+---------------+ | qws-test1 | qws-test1-1-1 | | qws-test1 | qws-test1-2-1 | | qws-test1 | qws-test1-3-1 | +-----------+---------------+ 3 rows in set (0.00 sec</pre>
返回右表中全部的記錄和符合鏈接條件的記錄。把左表當作A集合,右表看出B集合。左鏈接的返回結果爲:B並上AB的交集。
mysql> select c.name,c.status,d.name from cluster c right outer join instance d on c.delAt = d.delAt ; +-----------+--------+---------------+ | name | status | name | +-----------+--------+---------------+ | qws-test1 | 300 | qws-test1-1-1 | | qws-test1 | 300 | qws-test1-2-1 | | qws-test1 | 300 | qws-test1-3-1 | | NULL | NULL | qws-test-1-1 | | NULL | NULL | qws-test-2-1 | | NULL | NULL | qws-test-3-1 | +-----------+--------+---------------+ 6 rows in set (0.00 sec)
返回兩個表的笛卡爾乘積,好比集合a={x,y} b={1,2}其迪卡爾乘積爲(x,1)(x,2) (y,1) (y,2)。
mysql> select c.name,d.name from cluster c cross join instance d ; +-----------+---------------+ | name | name | +-----------+---------------+ | qws-test | qws-test-1-1 | | qws-test1 | qws-test-1-1 | | qws-test | qws-test-2-1 | | qws-test1 | qws-test-2-1 | | qws-test | qws-test-3-1 | | qws-test1 | qws-test-3-1 | | qws-test | qws-test1-1-1 | | qws-test1 | qws-test1-1-1 | | qws-test | qws-test1-2-1 | | qws-test1 | qws-test1-2-1 | | qws-test | qws-test1-3-1 | | qws-test1 | qws-test1-3-1 | +-----------+---------------+ 12 rows in set (0.00 sec)
文中的部分概念來自:http://c.biancheng.net/mysql/
原文出處:https://www.cnblogs.com/webDepOfQWS/p/10691550.html