1. 程式人生 > >python開發mysql:表關系&單表簡單查詢

python開發mysql:表關系&單表簡單查詢

for 查詢 comment modify tween upd price odi nbsp

一 一對多,多對一

技術分享

 1 1.1 建立多對一 ,一對多的關系需要註意
 2     先建立被關聯的表,被關聯的字段必須保證時唯一的
 3     在創建關聯的表,關聯的字段一定是可以重復的
 4 
 5 1.2 示例;
 6     出版社 多對一,多個老師可能在一家出版社
 7     一夫多妻  一對多
 8     create table dep(.  被關聯的字段必須保證唯一
 9     id int primary key auto_increment,
10     name varchar(20),
11     comment varchar(50)
12     );
13 14 create table emp( 15 id int primary key auto_increment, 16 name varchar(20), 17 dep_id int, 關聯的字段一定保證可以重復的 18 constraint fk_depid_id foreign key(dep_id) references dep(id) 19 foreign key(dep_id) 本表關聯字段 20 references 後面接指定關聯的表,一定是唯一的 21 on update cascade 22 on delete cascade
23 );

二 一對一

技術分享

 1 2.1 示例. 用戶表,管理員表
 2     create table user(
 3     uid int primary key auto_increment,
 4     name varchar(20)
 5     );
 6     insert into user(name) values(egon);
 7 
 8     create table admin(
 9     id int primary key auto_increment,
10     user_id int unique,  唯一
11     password varchar(20),
12 constraint foreign key(user_id) refreences user(uid) 被關聯的字段一定是唯一的 13 on update cascade 14 on delete cascade 15 ); 16 insert into admin(user_id,password) values(3,alex3714); 17 18 2.2 註意關聯字段與被關聯的字段一定都是唯一的 19 20 2.3 示例 學生和客戶,客戶轉化為學生 21 一個學生肯定是一個客戶,但是客戶不一定學生

技術分享

三 多對多,雙向的多對一,就變成多對多

技術分享

 1 3.1 示例. 作者,書
 2     create table book(
 3     id int primary key auto_increment,
 4     name varchar(20)
 5     price varchar(20)
 6     );
 7 
 8     create table book2author(
 9     id int primary key auto_increment,
10     book_id int,
11     author_id int,
12     constraint foreign key(book_id) references book(id),
13     constraint foreign key(author_id) references author(id)
14     on update cascade
15     on delete cascade,
16     unique(book_id,author_id)  聯合唯一
17     );
18 
19     create table author(
20     id int primary key auto_increment,
21     name varchar(20)
22     );

技術分享

四 簡單單表查詢

 1 1 簡單查詢
 2   select * from t1; 先找到表,在找到記錄,測試時候用
 3   select name,id from t1;
 4 
 5 2 where條件 and > < = != between or in is not
 6   select name,id from t1 where id > 3; 先找表,在走條件,然後字段
 7   select name,id from t1 where id > 3 and id <1 0; 多條件
 8   select name,id from t1 where id between 3 and 10; 在..之間 not between
 9   select id from t1 where id=3 or id=4 or id=5;
10   select id from t1 where id in (3,4,5);
11   select id from t1 where id is Null;可以判斷是不是為空‘‘並非空這麽簡單,只有Null才能用is判斷,‘‘用==判斷
12   select name from t1 where name like %n%;
13   select name from t1 where name like e__n; _代表是匹配一個
14 
15 3 group by分組
16   select stu_id,group_concat(name) from t1 group by stu_id; 按照id分組
17   group_concat(name) 看組裏面有哪些人,就需要這個聚合函數
18   select stu_id,count(id) from t1 group by stu_id;  查看每個組裏面多個人
19   select stu_id,max(id) from t1 group by stu_id; 查看每個組裏的最大id
20   max min sum avg平均
21 
22 刪除字段
23   alter table t1 drop age;
24   alter table t1 change id id(int3);        可以用modify替代
25   alter table t1 add primary key(id,age);   將誰設置成主鍵
26   alter table t1 drop primary key;          刪除主鍵

python開發mysql:表關系&單表簡單查詢