python开发mysql:表关系&单表简单查询

 

 一 一对多,多对一

 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;          删除主键

 

posted @ 2017-09-09 09:28  liqianlong  阅读(865)  评论(0编辑  收藏  举报