1. 程式人生 > 實用技巧 >mysql 外來鍵

mysql 外來鍵

自增補充

這是檢視怎麼建立的表, \G示旋轉90度顯示錶的內容
表的自增的關鍵是** AUTO_INCREMENT=3**,在表中新增資料後,這個會自動改變,通過alert可以改變這個預設值

mysql> show create table t1 \G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

下一次新增的內容的id會從20處新增

alter table t10 AUTO_INCREMENT=20;

自增步長

mysql是的預設步長是基於會話session的,sqlserver是基於表的。
檢視全域性變數,其中預設是1

mysql> show session variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

設定步長基於會話步長,只能自該自己的會話

set session auto_increment_increment=2; 	設定會話步長
set session auto_increment_offset=10; 設定開始的位置

基於全域性的級別的,可以修改全部的會話

show global variables like 'auto_inc%';	    檢視全域性變數
set global auto_increment_increment=2; 	    設定會話步長
set global auto_increment_offset=10;				

唯一索引

unique

create table t1(
			id int ....,
			num int,
			xx int,
			unique 唯一索引名稱 (列名,列名),
			constraint ....
		)

這了唯一的意思是:

  • 約束不能重複(可以為空)
  • 主鍵不能重複(不能為空)

作用是加速查詢

外來鍵的變種

  • 單列

  • 聯合

關聯一對多

create table userinfo(
	id  int auto_increment primary key,
	username varchar,
	usertype int,
)engine=innodb default charset=utf8;


create table admin(
	id  int auto_increment primary key,
	user_id int,
	passwprd varchar,
	unique index(user_id),
	constraint fk_key1 foreign key (user_id) references userinfo(id)
)engine=innodb default charset=utf8;

-- 外來鍵關聯多個列
create table t2(
	nid int not null auto_increment,
	pid int not null,
	num int,
	primary key(nid,pid)-- 這裡的關聯兩個列的主鍵
)engine=innodb default charset=utf8;


create table t3(
	id int auto_increment primary key,
	name char,
	id1 int,
	id2 int,
	constraint fk_t3_t2 foreign key (id1,id2) references t2(nid,pid)
)engine=innodb default charset=utf8;

多對多

-- 多對多

-- 使用者表 
create table userinfo(
	id  int auto_increment primary key,
	username varchar,
	gender int,
)engine=innodb default charset=utf8;

-- 主機表

create table computer(
	id  int auto_increment primary key,
	name varchar,
)engine=innodb default charset=utf8;

-- 使用者主機關係表

create table userandcom(
	id  int auto_increment primary key,
	user_id int,
	host_id int,
	unique index(user_id,host_id),
	constraint fk_key2 foreign key (user_id) references userinfo(id),
	constraint fk_key3 foreign key (host_id) references computer(id)
)engine=innodb default charset=utf8;

SQL語句資料行操作補充

-- 增加單條資料
insert into t1 (name) values('ddd');
增加多條資料
-- insert into t1 (name) values('ddd'),('eee');

-- 從一個表中新增另一個內容
insert into t4(name) select name from t1;
+------+------+
| id   | name |
+------+------+
| NULL | aaa  |
| NULL | aaa  |
| NULL | ccc  |
| NULL | ddd  |
| NULL | eee  |
+------+------+
這裡出現null的原因是在建立表的時候沒有新增自增和主鍵

在除錯中發現char後面不加長度,預設的長度是1,所以要新增一個長度。這個是根據需求


			delete from tb12;
			delete from tb12 where id !=2 
			delete from tb12 where id =2 
			delete from tb12 where id > 2 
			delete from tb12 where id >=2 
			delete from tb12 where id >=2 or name='a'
update tb12 set name='a' where id>12 and name='xx'
update tb12 set name='a',age=19 where id>12 and name='xx'

select * from tb12;
select id,name from tb12;
select id,name from tb12 where id > 10 or name ='xxx';
select id,name as cname from tb12 where id > 10 or name ='xxx';
select name,age,11 from tb12;

select * from tb12 where id != 1
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
select * from tb12 where id in (select id from tb11)
select * from tb12 where id between 5 and 12;

  • 萬用字元
    萬用字元的意識替換的意思
    %能夠替換多個字元
    _只能替換一個字元
select * from tb12 where name like "a%"
select * from tb12 where name like "aa_"
  • 分頁
select * from tb12 limit 10;					
select * from tb12 limit 0,10;
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;

後期的Python應用

# page = input('請輸入要檢視的頁碼')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10; 第一頁1 
# select * from tb12 limit 10,10;第二頁2
  • 排序
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc;  小到大
select * from tb12 order by age desc,id desc; # 這是優先順序 先按照age倒序,後按照id排序(ID中有相同的)
 
取後10條資料:先倒序後去取
select * from tb12 order by id desc limit 10;
mysql> select * from t5 order by name desc,id desc;
+----+------+
| id | name |
+----+------+
|  5 | eee  |
|  4 | ddd  |
|  3 | ccc  |
|  2 | aaa  |
|  1 | aaa  |
+----+------+
5 rows in set (0.00 sec)

mysql> select * from t5 order by name desc,id asc;
+----+------+
| id | name |
+----+------+
|  5 | eee  |
|  4 | ddd  |
|  3 | ccc  |
|  1 | aaa  |
|  2 | aaa  |
+----+------+

  • 分組

select count(id),max(id),part_id from userinfo5 group by part_id;

- count
- max
- min
- sum
- avg

如果對於聚合函式結果進行二次篩選時?必須使用having ,不能使用where
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;

  • 連表操作

連表操作主要是把兩張表顯示在一張表上,主要用過join

select * from userinfo5,department5 -- 這種是笛卡爾積的形式 即所有的乘積

select * from userinfo5,department5 where userinfo5.part_id = department5.id;

左邊全部顯示
select * from userinfo5 left join department5 on userinfo5.part_id = department5.id

右邊全部顯示
select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
select * from department5 left join userinfo5 on userinfo5.part_id = department5.id;這種就是變相的right

如果一張表顯示全部,但是另一張表還有多的內容的時候,就會出現空null

inner join 將出現null時一行隱藏

select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
mysql> select * from t1 left join t5 on t1.id=t5.id;
+----+-------+------+------+
| id | name  | id   | name |
+----+-------+------+------+
|  1 | aaa   |    1 | aaa  |
|  2 | aaa   |    2 | aaa  |
|  3 | ccc   |    3 | ccc  |
|  4 | ddd   |    4 | ddd  |
|  5 | eee   |    5 | eee  |
|  6 | hahah | NULL | NULL |
+----+-------+------+------+

隱藏空行

mysql> select * from t1 inner join t5 on t1.id=t5.id;
+----+------+----+------+
| id | name | id | name |
+----+------+----+------+
|  1 | aaa  |  1 | aaa  |
|  2 | aaa  |  2 | aaa  |
|  3 | ccc  |  3 | ccc  |
|  4 | ddd  |  4 | ddd  |
|  5 | eee  |  5 | eee  |
+----+------+----+------+
5 rows in set (0.00 sec)

資料庫的備份

資料庫匯出

  • mysqldump -u使用者名稱 -p密碼 資料庫名稱 >匯出檔案路徑 # 結構+資料(匯入的時候會自動穿件表並把表的內容插入)

  • mysqldump -u使用者名稱 -p密碼 -d 資料庫名稱 >匯出檔案路徑 # 僅僅結構
    匯入現有資料庫資料:

  • mysql -uroot -p密碼 資料庫名稱 < 檔案路徑

注意的是匯入的時候不能用dump