1. 程式人生 > >MySQL表創建及外鍵

MySQL表創建及外鍵

MySQL表及外鍵

模板數據下載
https://github.com/datacharmer/test_db

1. Employees數據庫安裝

(1)Employees數據庫介紹
Employees用於學習和測試的數據庫,大約160M

(1)Employees安裝
官方文檔安裝
下載地址 https://github.com/datacharmer/test_db

簡單安裝
mysql < employees.sql

2. 表(Table)

(1)表介紹
表是關系數據庫的核心
表=關系
表是記錄的集合
Mysql默認存儲引擎都是基於行存儲

(2)表是數據的集合
select * from table_name limit 1;
集合是無序的。從表中隨機取出一條數據,結果無確定性。

只有通過order by 排序之後取出的數據才是確定的。

(3)創建表
官方文檔表創建的語法
(1)臨時表創建
create database temp;
use temp;
create temporary table temp_a(a int);

"root@localhost:mysql.sock [temp]>show create table temp_a\G;
1. row
Table: temp_a
Create Table: CREATE TEMPORARY TABLE temp_a (
a int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

1 row in set (0.00 sec)

默認存儲引擎是InnoDB
insert into temp_a values(123);
select * from temp_a;

註意:臨時表是session級別的。當前用戶退出或者其他用戶登錄上來是看不到這張表的
如果臨時表和普通表同名時,當前用戶只能看到同名的臨時表

(2)臨時表的存儲引擎
show variables like "default%tmp%";
"root@localhost:mysql.sock [temp]>show variables like "default%tmp%";

+----------------------------+--------+
| Variable_name | Value |
+----------------------------+--------+
| default_tmp_storage_engine | InnoDB |
+----------------------------+--------+
1 row in set (0.00 sec)

臨時表的存儲位置
system ls -l /tmp/

"root@localhost:mysql.sock [temp]>system ls -l /data/mysql_data/ | grep ib
-rw-r-----. 1 mysql mysql 744 Feb 24 15:48 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Mar 12 14:02 ibdata1
-rw-r-----. 1 mysql mysql 4294967296 Mar 12 13:51 ib_logfile0
-rw-r-----. 1 mysql mysql 4294967296 Feb 24 15:43 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Mar 12 14:04 ibtmp1

其中ibtmp1就是表結構對應的位置

查看臨時表大小
show variables like "innodb_temp%";

(3)查看表結構
有3中方法
show create table temp_a \G;
desc temp_a\G;
show table status like "temp_a"\G;

(4)alter table
alter table temp_a add column b char(10);
alter table temp_a drop column b ;
註意:當表記錄非常大時,alter table 會耗時,影響性能。

具體查看官網文檔 ONLINE DDL

3. 外鍵索引

(1)外鍵的介紹
官方文檔

CREATE TABLE product (
category INT NOT NULL,
id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id) )
ENGINE=INNODB;

CREATE TABLE customer (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;

CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
INDEX (customer_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (customer_id) REFERENCES customer(id) ) ENGINE=INNODB;

(2)外鍵操作
表結構來自MySQL官方文檔
create table parent(
id int not null,
primary key(id)
)engine=innodb;

create table child (
id int,
parent_id INT,
index par_ind (parent_id),
foreign key (parent_id)
references parent(id)
on delete cascade on update cascade
) engine=innodb;

(1)我們插入一條數據,id=1,parent_id=1
insert into child values(1,1);

報錯信息如下:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (temp.child, CONSTRAINT child_ibf<br/>k_1 FOREIGN KEY (parent_id) REFERENCES parent (id) ON DELETE CASCADE ON UPDATE CASCADE)

insert into parent values(1);
insert into child values(1,1);

外鍵約束,可以讓數據進行一致性更新,但是會有一定的性能損耗 ,線上業務使用不多。通常上述級聯更新和刪除都是由應用層業務邏輯進行判斷並實現。

MySQL表創建及外鍵